blob: 096e1177bcf35ae7b39a9afa7aad9dfb19556981 [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
1821uint32_t
1822widget_get_last_time(struct widget *widget)
1823{
1824 return widget->surface->last_time;
1825}
1826
1827void
1828widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1829{
1830 struct wl_compositor *comp = widget->window->display->compositor;
1831 struct surface *surface = widget->surface;
1832
1833 if (!surface->input_region)
1834 surface->input_region = wl_compositor_create_region(comp);
1835
1836 if (rect) {
1837 wl_region_add(surface->input_region,
1838 rect->x, rect->y, rect->width, rect->height);
1839 }
1840}
1841
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001842void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001843widget_set_resize_handler(struct widget *widget,
1844 widget_resize_handler_t handler)
1845{
1846 widget->resize_handler = handler;
1847}
1848
1849void
1850widget_set_redraw_handler(struct widget *widget,
1851 widget_redraw_handler_t handler)
1852{
1853 widget->redraw_handler = handler;
1854}
1855
1856void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001857widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001858{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001859 widget->enter_handler = handler;
1860}
1861
1862void
1863widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1864{
1865 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001866}
1867
1868void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001869widget_set_motion_handler(struct widget *widget,
1870 widget_motion_handler_t handler)
1871{
1872 widget->motion_handler = handler;
1873}
1874
1875void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001876widget_set_button_handler(struct widget *widget,
1877 widget_button_handler_t handler)
1878{
1879 widget->button_handler = handler;
1880}
1881
1882void
Rusty Lynch041815a2013-08-08 21:20:38 -07001883widget_set_touch_up_handler(struct widget *widget,
1884 widget_touch_up_handler_t handler)
1885{
1886 widget->touch_up_handler = handler;
1887}
1888
1889void
1890widget_set_touch_down_handler(struct widget *widget,
1891 widget_touch_down_handler_t handler)
1892{
1893 widget->touch_down_handler = handler;
1894}
1895
1896void
1897widget_set_touch_motion_handler(struct widget *widget,
1898 widget_touch_motion_handler_t handler)
1899{
1900 widget->touch_motion_handler = handler;
1901}
1902
1903void
1904widget_set_touch_frame_handler(struct widget *widget,
1905 widget_touch_frame_handler_t handler)
1906{
1907 widget->touch_frame_handler = handler;
1908}
1909
1910void
1911widget_set_touch_cancel_handler(struct widget *widget,
1912 widget_touch_cancel_handler_t handler)
1913{
1914 widget->touch_cancel_handler = handler;
1915}
1916
1917void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001918widget_set_axis_handler(struct widget *widget,
1919 widget_axis_handler_t handler)
1920{
1921 widget->axis_handler = handler;
1922}
1923
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001924static void
1925window_schedule_redraw_task(struct window *window);
1926
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001927void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001928widget_schedule_redraw(struct widget *widget)
1929{
Pekka Paalanen71233882013-04-25 13:57:53 +03001930 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001931 widget->surface->redraw_needed = 1;
1932 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001933}
1934
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001935cairo_surface_t *
1936window_get_surface(struct window *window)
1937{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001938 cairo_surface_t *cairo_surface;
1939
1940 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1941
1942 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001943}
1944
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001945struct wl_surface *
1946window_get_wl_surface(struct window *window)
1947{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001948 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001949}
1950
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001951static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001952tooltip_redraw_handler(struct widget *widget, void *data)
1953{
1954 cairo_t *cr;
1955 const int32_t r = 3;
1956 struct tooltip *tooltip = data;
1957 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001958
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001959 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001960 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1961 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1962 cairo_paint(cr);
1963
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001964 width = widget->allocation.width;
1965 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001966 rounded_rect(cr, 0, 0, width, height, r);
1967
1968 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1969 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1970 cairo_fill(cr);
1971
1972 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1973 cairo_move_to(cr, 10, 16);
1974 cairo_show_text(cr, tooltip->entry);
1975 cairo_destroy(cr);
1976}
1977
1978static cairo_text_extents_t
1979get_text_extents(struct tooltip *tooltip)
1980{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001981 cairo_t *cr;
1982 cairo_text_extents_t extents;
1983
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001984 /* Use the dummy_surface because tooltip's surface was not
1985 * created yet, and parent does not have a valid surface
1986 * outside repaint, either.
1987 */
1988 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001989 cairo_text_extents(cr, tooltip->entry, &extents);
1990 cairo_destroy(cr);
1991
1992 return extents;
1993}
1994
1995static int
1996window_create_tooltip(struct tooltip *tooltip)
1997{
1998 struct widget *parent = tooltip->parent;
1999 struct display *display = parent->window->display;
2000 struct window *window;
2001 const int offset_y = 27;
2002 const int margin = 3;
2003 cairo_text_extents_t extents;
2004
2005 if (tooltip->widget)
2006 return 0;
2007
2008 window = window_create_transient(display, parent->window, tooltip->x,
2009 tooltip->y + offset_y,
2010 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2011 if (!window)
2012 return -1;
2013
2014 tooltip->window = window;
2015 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2016
2017 extents = get_text_extents(tooltip);
2018 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2019 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2020
2021 return 0;
2022}
2023
2024void
2025widget_destroy_tooltip(struct widget *parent)
2026{
2027 struct tooltip *tooltip = parent->tooltip;
2028
2029 parent->tooltip_count = 0;
2030 if (!tooltip)
2031 return;
2032
2033 if (tooltip->widget) {
2034 widget_destroy(tooltip->widget);
2035 window_destroy(tooltip->window);
2036 tooltip->widget = NULL;
2037 tooltip->window = NULL;
2038 }
2039
2040 close(tooltip->tooltip_fd);
2041 free(tooltip->entry);
2042 free(tooltip);
2043 parent->tooltip = NULL;
2044}
2045
2046static void
2047tooltip_func(struct task *task, uint32_t events)
2048{
2049 struct tooltip *tooltip =
2050 container_of(task, struct tooltip, tooltip_task);
2051 uint64_t exp;
2052
Martin Olsson8df662a2012-07-08 03:03:47 +02002053 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2054 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002055 window_create_tooltip(tooltip);
2056}
2057
2058#define TOOLTIP_TIMEOUT 500
2059static int
2060tooltip_timer_reset(struct tooltip *tooltip)
2061{
2062 struct itimerspec its;
2063
2064 its.it_interval.tv_sec = 0;
2065 its.it_interval.tv_nsec = 0;
2066 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2067 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2068 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2069 fprintf(stderr, "could not set timerfd\n: %m");
2070 return -1;
2071 }
2072
2073 return 0;
2074}
2075
2076int
2077widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2078{
2079 struct tooltip *tooltip = parent->tooltip;
2080
2081 parent->tooltip_count++;
2082 if (tooltip) {
2083 tooltip->x = x;
2084 tooltip->y = y;
2085 tooltip_timer_reset(tooltip);
2086 return 0;
2087 }
2088
2089 /* the handler might be triggered too fast via input device motion, so
2090 * we need this check here to make sure tooltip is fully initialized */
2091 if (parent->tooltip_count > 1)
2092 return 0;
2093
2094 tooltip = malloc(sizeof *tooltip);
2095 if (!tooltip)
2096 return -1;
2097
2098 parent->tooltip = tooltip;
2099 tooltip->parent = parent;
2100 tooltip->widget = NULL;
2101 tooltip->window = NULL;
2102 tooltip->x = x;
2103 tooltip->y = y;
2104 tooltip->entry = strdup(entry);
2105 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2106 if (tooltip->tooltip_fd < 0) {
2107 fprintf(stderr, "could not create timerfd\n: %m");
2108 return -1;
2109 }
2110
2111 tooltip->tooltip_task.run = tooltip_func;
2112 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2113 EPOLLIN, &tooltip->tooltip_task);
2114 tooltip_timer_reset(tooltip);
2115
2116 return 0;
2117}
2118
2119static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002120workspace_manager_state(void *data,
2121 struct workspace_manager *workspace_manager,
2122 uint32_t current,
2123 uint32_t count)
2124{
2125 struct display *display = data;
2126
2127 display->workspace = current;
2128 display->workspace_count = count;
2129}
2130
2131static const struct workspace_manager_listener workspace_manager_listener = {
2132 workspace_manager_state
2133};
2134
2135static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002136frame_resize_handler(struct widget *widget,
2137 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002138{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002139 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002140 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002141 struct rectangle interior;
2142 struct rectangle input;
2143 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002144
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002145 if (widget->window->type == TYPE_FULLSCREEN) {
2146 interior.x = 0;
2147 interior.y = 0;
2148 interior.width = width;
2149 interior.height = height;
2150 } else {
2151 if (widget->window->type == TYPE_MAXIMIZED) {
2152 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2153 } else {
2154 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2155 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002156
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002157 frame_resize(frame->frame, width, height);
2158 frame_interior(frame->frame, &interior.x, &interior.y,
2159 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002160 }
2161
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002162 widget_set_allocation(child, interior.x, interior.y,
2163 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002164
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002165 if (child->resize_handler) {
2166 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002167 child->user_data);
2168
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002169 if (widget->window->type == TYPE_FULLSCREEN) {
2170 width = child->allocation.width;
2171 height = child->allocation.height;
2172 } else {
2173 frame_resize_inside(frame->frame,
2174 child->allocation.width,
2175 child->allocation.height);
2176 width = frame_width(frame->frame);
2177 height = frame_height(frame->frame);
2178 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002179 }
2180
Scott Moreauf7e498c2012-05-14 11:39:29 -06002181 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002182
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002183 widget->surface->input_region =
2184 wl_compositor_create_region(widget->window->display->compositor);
2185 if (widget->window->type != TYPE_FULLSCREEN) {
2186 frame_input_rect(frame->frame, &input.x, &input.y,
2187 &input.width, &input.height);
2188 wl_region_add(widget->surface->input_region,
2189 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002190 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002191 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002192 }
2193
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002194 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002195
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002196 if (child->opaque) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002197 if (widget->window->type != TYPE_FULLSCREEN) {
2198 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2199 &opaque.width, &opaque.height);
2200
2201 wl_region_add(widget->surface->opaque_region,
2202 opaque.x, opaque.y,
2203 opaque.width, opaque.height);
2204 } else {
2205 wl_region_add(widget->surface->opaque_region,
2206 0, 0, width, height);
2207 }
Martin Minarik1998b152012-05-10 02:04:35 +02002208 }
2209
Martin Minarik1998b152012-05-10 02:04:35 +02002210
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002211 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212}
2213
2214static void
2215frame_redraw_handler(struct widget *widget, void *data)
2216{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002217 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219 struct window *window = widget->window;
2220
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002221 if (window->type == TYPE_FULLSCREEN)
2222 return;
2223
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002224 if (window->focus_count) {
2225 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2226 } else {
2227 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2228 }
2229
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002230 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002231
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002232 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002233
2234 cairo_destroy(cr);
2235}
2236
2237static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002238frame_get_pointer_image_for_location(struct window_frame *frame,
2239 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002240{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002241 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002242
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002243 if (window->type != TYPE_TOPLEVEL)
2244 return CURSOR_LEFT_PTR;
2245
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002246 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002247 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002248 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002249 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002250 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002251 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002252 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002253 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002254 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002255 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002256 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002257 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002258 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002259 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002260 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002261 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002262 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002263 case THEME_LOCATION_EXTERIOR:
2264 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002265 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002266 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002267 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002268}
2269
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002270static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002271frame_menu_func(struct window *window,
2272 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002273{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002274 struct display *display;
2275
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002276 switch (index) {
2277 case 0: /* close */
2278 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002279 window->close_handler(window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002280 else
2281 display_exit(window->display);
2282 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002283 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002284 display = window->display;
2285 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002286 workspace_manager_move_surface(
2287 display->workspace_manager,
2288 window->main_surface->surface,
2289 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002290 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002291 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002292 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002293 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002294 workspace_manager_move_surface(
2295 display->workspace_manager,
2296 window->main_surface->surface,
2297 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002298 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002299 case 3: /* fullscreen */
2300 /* we don't have a way to get out of fullscreen for now */
2301 if (window->fullscreen_handler)
2302 window->fullscreen_handler(window, window->user_data);
2303 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002304 }
2305}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002306
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002307void
2308window_show_frame_menu(struct window *window,
2309 struct input *input, uint32_t time)
2310{
2311 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002312 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002313
2314 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002315 "Close",
2316 "Move to workspace above", "Move to workspace below",
2317 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002318 };
2319
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002320 if (window->fullscreen_handler)
2321 count = ARRAY_LENGTH(entries);
2322 else
2323 count = ARRAY_LENGTH(entries) - 1;
2324
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002325 input_get_position(input, &x, &y);
2326 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002327 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002328}
2329
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002330static int
2331frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002332 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002333{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002334 struct window_frame *frame = data;
2335 enum theme_location location;
2336
2337 location = frame_pointer_enter(frame->frame, input, x, y);
2338 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2339 widget_schedule_redraw(frame->widget);
2340
2341 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002342}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002343
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002344static int
2345frame_motion_handler(struct widget *widget,
2346 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002347 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002348{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002349 struct window_frame *frame = data;
2350 enum theme_location location;
2351
2352 location = frame_pointer_motion(frame->frame, input, x, y);
2353 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2354 widget_schedule_redraw(frame->widget);
2355
2356 return frame_get_pointer_image_for_location(data, location);
2357}
2358
2359static void
2360frame_leave_handler(struct widget *widget,
2361 struct input *input, void *data)
2362{
2363 struct window_frame *frame = data;
2364
2365 frame_pointer_leave(frame->frame, input);
2366 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2367 widget_schedule_redraw(frame->widget);
2368}
2369
2370static void
2371frame_handle_status(struct window_frame *frame, struct input *input,
2372 uint32_t time, enum theme_location location)
2373{
2374 struct window *window = frame->widget->window;
2375 uint32_t status;
2376
2377 status = frame_status(frame->frame);
2378 if (status & FRAME_STATUS_REPAINT)
2379 widget_schedule_redraw(frame->widget);
2380
2381 if (status & FRAME_STATUS_MINIMIZE)
2382 fprintf(stderr,"Minimize stub\n");
2383
2384 if (status & FRAME_STATUS_MENU) {
2385 window_show_frame_menu(window, input, time);
2386 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2387 }
2388
2389 if (status & FRAME_STATUS_MAXIMIZE) {
2390 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2391 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2392 }
2393
2394 if (status & FRAME_STATUS_CLOSE) {
2395 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002396 window->close_handler(window->user_data);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002397 else
2398 display_exit(window->display);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002399 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002400 }
2401
2402 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2403 input_ungrab(input);
2404 wl_shell_surface_move(window->shell_surface,
2405 input_get_seat(input),
2406 window->display->serial);
2407
2408 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2409 }
2410
2411 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2412 input_ungrab(input);
2413
2414 window->resizing = 1;
2415 wl_shell_surface_resize(window->shell_surface,
2416 input_get_seat(input),
2417 window->display->serial,
2418 location);
2419
2420 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2421 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002422}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002423
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002424static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002425frame_button_handler(struct widget *widget,
2426 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002427 uint32_t button, enum wl_pointer_button_state state,
2428 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002429
2430{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002431 struct window_frame *frame = data;
2432 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002433
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002434 location = frame_pointer_button(frame->frame, input, button, state);
2435 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002436}
2437
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002438static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002439frame_touch_down_handler(struct widget *widget, struct input *input,
2440 uint32_t serial, uint32_t time, int32_t id,
2441 float x, float y, void *data)
2442{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002443 struct window_frame *frame = data;
2444
2445 frame_touch_down(frame->frame, input, id, x, y);
2446 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2447}
2448
2449static void
2450frame_touch_up_handler(struct widget *widget,
2451 struct input *input, uint32_t serial, uint32_t time,
2452 int32_t id, void *data)
2453{
2454 struct window_frame *frame = data;
2455
2456 frame_touch_up(frame->frame, input, id);
2457 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002458}
2459
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002460struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002461window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002462{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002463 struct window_frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002464
Peter Huttererf3d62272013-08-08 11:57:05 +10002465 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002466 frame->frame = frame_create(window->display->theme, 0, 0,
2467 FRAME_BUTTON_ALL, window->title);
2468
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002469 frame->widget = window_add_widget(window, frame);
2470 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002471
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002472 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2473 widget_set_resize_handler(frame->widget, frame_resize_handler);
2474 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002475 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002476 widget_set_motion_handler(frame->widget, frame_motion_handler);
2477 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002478 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002479 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002480
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002481 window->frame = frame;
2482
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002483 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002484}
2485
Kristian Høgsberga1627922012-06-20 17:30:03 -04002486void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002487window_frame_set_child_size(struct widget *widget, int child_width,
2488 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002489{
2490 struct display *display = widget->window->display;
2491 struct theme *t = display->theme;
2492 int decoration_width, decoration_height;
2493 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002494 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002495
2496 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002497 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002498 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002499 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002500
2501 width = child_width + decoration_width;
2502 height = child_height + decoration_height;
2503 } else {
2504 width = child_width;
2505 height = child_height;
2506 }
2507
2508 window_schedule_resize(widget->window, width, height);
2509}
2510
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002511static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002512window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002513{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002514 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002515
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002516 /* frame->child must be destroyed by the application */
2517 widget_destroy(frame->widget);
2518 free(frame);
2519}
2520
2521static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002522input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002523 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002524{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002525 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002526 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002527
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002528 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002529 return;
2530
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002531 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002532 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002533 widget = old;
2534 if (input->grab)
2535 widget = input->grab;
2536 if (widget->leave_handler)
2537 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002538 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002539 }
2540
2541 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002542 widget = focus;
2543 if (input->grab)
2544 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002545 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002546 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002547 cursor = widget->enter_handler(focus, input, x, y,
2548 widget->user_data);
2549 else
2550 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002551
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002552 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002553 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002554}
2555
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002556void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002557touch_grab(struct input *input, int32_t touch_id)
2558{
2559 input->touch_grab = 1;
2560 input->touch_grab_id = touch_id;
2561}
2562
2563void
2564touch_ungrab(struct input *input)
2565{
2566 struct touch_point *tp, *tmp;
2567
2568 input->touch_grab = 0;
2569
2570 wl_list_for_each_safe(tp, tmp,
2571 &input->touch_point_list, link) {
2572 if (tp->id != input->touch_grab_id)
2573 continue;
2574 wl_list_remove(&tp->link);
2575 free(tp);
2576
2577 return;
2578 }
2579}
2580
2581void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002582input_grab(struct input *input, struct widget *widget, uint32_t button)
2583{
2584 input->grab = widget;
2585 input->grab_button = button;
2586}
2587
2588void
2589input_ungrab(struct input *input)
2590{
2591 struct widget *widget;
2592
2593 input->grab = NULL;
2594 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002595 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002596 input->sx, input->sy);
2597 input_set_focus_widget(input, widget, input->sx, input->sy);
2598 }
2599}
2600
2601static void
2602input_remove_pointer_focus(struct input *input)
2603{
2604 struct window *window = input->pointer_focus;
2605
2606 if (!window)
2607 return;
2608
2609 input_set_focus_widget(input, NULL, 0, 0);
2610
2611 input->pointer_focus = NULL;
2612 input->current_cursor = CURSOR_UNSET;
2613}
2614
2615static void
2616pointer_handle_enter(void *data, struct wl_pointer *pointer,
2617 uint32_t serial, struct wl_surface *surface,
2618 wl_fixed_t sx_w, wl_fixed_t sy_w)
2619{
2620 struct input *input = data;
2621 struct window *window;
2622 struct widget *widget;
2623 float sx = wl_fixed_to_double(sx_w);
2624 float sy = wl_fixed_to_double(sy_w);
2625
2626 if (!surface) {
2627 /* enter event for a window we've just destroyed */
2628 return;
2629 }
2630
2631 input->display->serial = serial;
2632 input->pointer_enter_serial = serial;
2633 input->pointer_focus = wl_surface_get_user_data(surface);
2634 window = input->pointer_focus;
2635
Pekka Paalanen99436862012-11-19 17:15:59 +02002636 if (window->resizing) {
2637 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002638 /* Schedule a redraw to free the pool */
2639 window_schedule_redraw(window);
2640 }
2641
2642 input->sx = sx;
2643 input->sy = sy;
2644
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002645 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002646 input_set_focus_widget(input, widget, sx, sy);
2647}
2648
2649static void
2650pointer_handle_leave(void *data, struct wl_pointer *pointer,
2651 uint32_t serial, struct wl_surface *surface)
2652{
2653 struct input *input = data;
2654
2655 input->display->serial = serial;
2656 input_remove_pointer_focus(input);
2657}
2658
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002659static void
Daniel Stone37816df2012-05-16 18:45:18 +01002660pointer_handle_motion(void *data, struct wl_pointer *pointer,
2661 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002662{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002663 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002664 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002665 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002666 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002667 float sx = wl_fixed_to_double(sx_w);
2668 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002669
Paul Winwoodb22bf572013-08-29 10:52:54 +01002670 input->sx = sx;
2671 input->sy = sy;
2672
2673 if (!window)
2674 return;
2675
Rob Bradford5f087742013-07-11 19:41:27 +01002676 /* when making the window smaller - e.g. after a unmaximise we might
2677 * still have a pending motion event that the compositor has picked
2678 * based on the old surface dimensions
2679 */
2680 if (sx > window->main_surface->allocation.width ||
2681 sy > window->main_surface->allocation.height)
2682 return;
2683
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002684 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002685 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002686 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002687 }
2688
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002689 if (input->grab)
2690 widget = input->grab;
2691 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002692 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002693 if (widget) {
2694 if (widget->motion_handler)
2695 cursor = widget->motion_handler(input->focus_widget,
2696 input, time, sx, sy,
2697 widget->user_data);
2698 else
2699 cursor = widget->default_cursor;
2700 } else
2701 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002702
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002703 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002704}
2705
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002706static void
Daniel Stone37816df2012-05-16 18:45:18 +01002707pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002708 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002709{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002710 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002711 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002712 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002713
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002714 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002715 if (input->focus_widget && input->grab == NULL &&
2716 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002717 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002718
Neil Roberts6b28aad2012-01-23 19:11:18 +00002719 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002720 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002721 (*widget->button_handler)(widget,
2722 input, time,
2723 button, state,
2724 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002725
Daniel Stone4dbadb12012-05-30 16:31:51 +01002726 if (input->grab && input->grab_button == button &&
2727 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002728 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002729}
2730
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002731static void
Daniel Stone37816df2012-05-16 18:45:18 +01002732pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002733 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002734{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002735 struct input *input = data;
2736 struct widget *widget;
2737
2738 widget = input->focus_widget;
2739 if (input->grab)
2740 widget = input->grab;
2741 if (widget && widget->axis_handler)
2742 (*widget->axis_handler)(widget,
2743 input, time,
2744 axis, value,
2745 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002746}
2747
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002748static const struct wl_pointer_listener pointer_listener = {
2749 pointer_handle_enter,
2750 pointer_handle_leave,
2751 pointer_handle_motion,
2752 pointer_handle_button,
2753 pointer_handle_axis,
2754};
2755
2756static void
2757input_remove_keyboard_focus(struct input *input)
2758{
2759 struct window *window = input->keyboard_focus;
2760 struct itimerspec its;
2761
2762 its.it_interval.tv_sec = 0;
2763 its.it_interval.tv_nsec = 0;
2764 its.it_value.tv_sec = 0;
2765 its.it_value.tv_nsec = 0;
2766 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2767
2768 if (!window)
2769 return;
2770
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002771 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002772 if (window->keyboard_focus_handler)
2773 (*window->keyboard_focus_handler)(window, NULL,
2774 window->user_data);
2775
2776 input->keyboard_focus = NULL;
2777}
2778
2779static void
2780keyboard_repeat_func(struct task *task, uint32_t events)
2781{
2782 struct input *input =
2783 container_of(task, struct input, repeat_task);
2784 struct window *window = input->keyboard_focus;
2785 uint64_t exp;
2786
2787 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2788 /* If we change the timer between the fd becoming
2789 * readable and getting here, there'll be nothing to
2790 * read and we get EAGAIN. */
2791 return;
2792
2793 if (window && window->key_handler) {
2794 (*window->key_handler)(window, input, input->repeat_time,
2795 input->repeat_key, input->repeat_sym,
2796 WL_KEYBOARD_KEY_STATE_PRESSED,
2797 window->user_data);
2798 }
2799}
2800
2801static void
2802keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2803 uint32_t format, int fd, uint32_t size)
2804{
2805 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002806 struct xkb_keymap *keymap;
2807 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002808 char *map_str;
2809
2810 if (!data) {
2811 close(fd);
2812 return;
2813 }
2814
2815 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2816 close(fd);
2817 return;
2818 }
2819
2820 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2821 if (map_str == MAP_FAILED) {
2822 close(fd);
2823 return;
2824 }
2825
Rui Matos3eccb862013-10-10 19:44:22 +02002826 keymap = xkb_map_new_from_string(input->display->xkb_context,
2827 map_str,
2828 XKB_KEYMAP_FORMAT_TEXT_V1,
2829 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002830 munmap(map_str, size);
2831 close(fd);
2832
Rui Matos3eccb862013-10-10 19:44:22 +02002833 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002834 fprintf(stderr, "failed to compile keymap\n");
2835 return;
2836 }
2837
Rui Matos3eccb862013-10-10 19:44:22 +02002838 state = xkb_state_new(keymap);
2839 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002840 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002841 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002842 return;
2843 }
2844
Rui Matos3eccb862013-10-10 19:44:22 +02002845 xkb_keymap_unref(input->xkb.keymap);
2846 xkb_state_unref(input->xkb.state);
2847 input->xkb.keymap = keymap;
2848 input->xkb.state = state;
2849
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002850 input->xkb.control_mask =
2851 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2852 input->xkb.alt_mask =
2853 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2854 input->xkb.shift_mask =
2855 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2856}
2857
2858static void
2859keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2860 uint32_t serial, struct wl_surface *surface,
2861 struct wl_array *keys)
2862{
2863 struct input *input = data;
2864 struct window *window;
2865
2866 input->display->serial = serial;
2867 input->keyboard_focus = wl_surface_get_user_data(surface);
2868
2869 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002870 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002871 if (window->keyboard_focus_handler)
2872 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002873 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002874}
2875
2876static void
2877keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2878 uint32_t serial, struct wl_surface *surface)
2879{
2880 struct input *input = data;
2881
2882 input->display->serial = serial;
2883 input_remove_keyboard_focus(input);
2884}
2885
Scott Moreau210d0792012-03-22 10:47:01 -06002886static void
Daniel Stone37816df2012-05-16 18:45:18 +01002887keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002888 uint32_t serial, uint32_t time, uint32_t key,
2889 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002890{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002891 struct input *input = data;
2892 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002893 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002894 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002895 const xkb_keysym_t *syms;
2896 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002897 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002898
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002899 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002900 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002901 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002902 return;
2903
Daniel Stone97f68542012-05-30 16:32:01 +01002904 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002905
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002906 sym = XKB_KEY_NoSymbol;
2907 if (num_syms == 1)
2908 sym = syms[0];
2909
2910 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002911 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002912 window_set_maximized(window,
2913 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002914 } else if (sym == XKB_KEY_F11 &&
2915 window->fullscreen_handler &&
2916 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2917 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002918 } else if (sym == XKB_KEY_F4 &&
2919 input->modifiers == MOD_ALT_MASK &&
2920 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2921 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002922 window->close_handler(window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002923 else
2924 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002925 } else if (window->key_handler) {
2926 (*window->key_handler)(window, input, time, key,
2927 sym, state, window->user_data);
2928 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002929
2930 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2931 key == input->repeat_key) {
2932 its.it_interval.tv_sec = 0;
2933 its.it_interval.tv_nsec = 0;
2934 its.it_value.tv_sec = 0;
2935 its.it_value.tv_nsec = 0;
2936 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2937 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2938 input->repeat_sym = sym;
2939 input->repeat_key = key;
2940 input->repeat_time = time;
2941 its.it_interval.tv_sec = 0;
2942 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2943 its.it_value.tv_sec = 0;
2944 its.it_value.tv_nsec = 400 * 1000 * 1000;
2945 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2946 }
2947}
2948
2949static void
Daniel Stone351eb612012-05-31 15:27:47 -04002950keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2951 uint32_t serial, uint32_t mods_depressed,
2952 uint32_t mods_latched, uint32_t mods_locked,
2953 uint32_t group)
2954{
2955 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002956 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002957
Matt Ropere61561f2013-06-24 16:52:43 +01002958 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2959 if (!input->xkb.keymap)
2960 return;
2961
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002962 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2963 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002964 mask = xkb_state_serialize_mods(input->xkb.state,
2965 XKB_STATE_DEPRESSED |
2966 XKB_STATE_LATCHED);
2967 input->modifiers = 0;
2968 if (mask & input->xkb.control_mask)
2969 input->modifiers |= MOD_CONTROL_MASK;
2970 if (mask & input->xkb.alt_mask)
2971 input->modifiers |= MOD_ALT_MASK;
2972 if (mask & input->xkb.shift_mask)
2973 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002974}
2975
Daniel Stone37816df2012-05-16 18:45:18 +01002976static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002977 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002978 keyboard_handle_enter,
2979 keyboard_handle_leave,
2980 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002981 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002982};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002983
2984static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002985touch_handle_down(void *data, struct wl_touch *wl_touch,
2986 uint32_t serial, uint32_t time, struct wl_surface *surface,
2987 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2988{
2989 struct input *input = data;
2990 struct widget *widget;
2991 float sx = wl_fixed_to_double(x_w);
2992 float sy = wl_fixed_to_double(y_w);
2993
Rusty Lynch1084da52013-08-15 09:10:08 -07002994 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002995 input->touch_focus = wl_surface_get_user_data(surface);
2996 if (!input->touch_focus) {
2997 DBG("Failed to find to touch focus for surface %p\n", surface);
2998 return;
2999 }
3000
3001 widget = window_find_widget(input->touch_focus,
3002 wl_fixed_to_double(x_w),
3003 wl_fixed_to_double(y_w));
3004 if (widget) {
3005 struct touch_point *tp = xmalloc(sizeof *tp);
3006 if (tp) {
3007 tp->id = id;
3008 tp->widget = widget;
3009 wl_list_insert(&input->touch_point_list, &tp->link);
3010
3011 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003012 (*widget->touch_down_handler)(widget, input,
3013 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003014 sx, sy,
3015 widget->user_data);
3016 }
3017 }
3018}
3019
3020static void
3021touch_handle_up(void *data, struct wl_touch *wl_touch,
3022 uint32_t serial, uint32_t time, int32_t id)
3023{
3024 struct input *input = data;
3025 struct touch_point *tp, *tmp;
3026
Rusty Lynch041815a2013-08-08 21:20:38 -07003027 if (!input->touch_focus) {
3028 DBG("No touch focus found for touch up event!\n");
3029 return;
3030 }
3031
3032 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3033 if (tp->id != id)
3034 continue;
3035
3036 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003037 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003038 time, id,
3039 tp->widget->user_data);
3040
3041 wl_list_remove(&tp->link);
3042 free(tp);
3043
3044 return;
3045 }
3046}
3047
3048static void
3049touch_handle_motion(void *data, struct wl_touch *wl_touch,
3050 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3051{
3052 struct input *input = data;
3053 struct touch_point *tp;
3054 float sx = wl_fixed_to_double(x_w);
3055 float sy = wl_fixed_to_double(y_w);
3056
3057 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3058
3059 if (!input->touch_focus) {
3060 DBG("No touch focus found for touch motion event!\n");
3061 return;
3062 }
3063
3064 wl_list_for_each(tp, &input->touch_point_list, link) {
3065 if (tp->id != id)
3066 continue;
3067
3068 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003069 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003070 id, sx, sy,
3071 tp->widget->user_data);
3072 return;
3073 }
3074}
3075
3076static void
3077touch_handle_frame(void *data, struct wl_touch *wl_touch)
3078{
3079 struct input *input = data;
3080 struct touch_point *tp, *tmp;
3081
3082 DBG("touch_handle_frame\n");
3083
3084 if (!input->touch_focus) {
3085 DBG("No touch focus found for touch frame event!\n");
3086 return;
3087 }
3088
3089 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3090 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003091 (*tp->widget->touch_frame_handler)(tp->widget, input,
3092 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003093
3094 wl_list_remove(&tp->link);
3095 free(tp);
3096 }
3097}
3098
3099static void
3100touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3101{
3102 struct input *input = data;
3103 struct touch_point *tp, *tmp;
3104
3105 DBG("touch_handle_cancel\n");
3106
3107 if (!input->touch_focus) {
3108 DBG("No touch focus found for touch cancel event!\n");
3109 return;
3110 }
3111
3112 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3113 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003114 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3115 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003116
3117 wl_list_remove(&tp->link);
3118 free(tp);
3119 }
3120}
3121
3122static const struct wl_touch_listener touch_listener = {
3123 touch_handle_down,
3124 touch_handle_up,
3125 touch_handle_motion,
3126 touch_handle_frame,
3127 touch_handle_cancel,
3128};
3129
3130static void
Daniel Stone37816df2012-05-16 18:45:18 +01003131seat_handle_capabilities(void *data, struct wl_seat *seat,
3132 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003133{
Daniel Stone37816df2012-05-16 18:45:18 +01003134 struct input *input = data;
3135
3136 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3137 input->pointer = wl_seat_get_pointer(seat);
3138 wl_pointer_set_user_data(input->pointer, input);
3139 wl_pointer_add_listener(input->pointer, &pointer_listener,
3140 input);
3141 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3142 wl_pointer_destroy(input->pointer);
3143 input->pointer = NULL;
3144 }
3145
3146 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3147 input->keyboard = wl_seat_get_keyboard(seat);
3148 wl_keyboard_set_user_data(input->keyboard, input);
3149 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3150 input);
3151 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3152 wl_keyboard_destroy(input->keyboard);
3153 input->keyboard = NULL;
3154 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003155
3156 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3157 input->touch = wl_seat_get_touch(seat);
3158 wl_touch_set_user_data(input->touch, input);
3159 wl_touch_add_listener(input->touch, &touch_listener, input);
3160 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3161 wl_touch_destroy(input->touch);
3162 input->touch = NULL;
3163 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003164}
3165
Rob Bradford08031182013-08-13 20:11:03 +01003166static void
3167seat_handle_name(void *data, struct wl_seat *seat,
3168 const char *name)
3169{
3170
3171}
3172
Daniel Stone37816df2012-05-16 18:45:18 +01003173static const struct wl_seat_listener seat_listener = {
3174 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003175 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003176};
3177
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003178void
3179input_get_position(struct input *input, int32_t *x, int32_t *y)
3180{
3181 *x = input->sx;
3182 *y = input->sy;
3183}
3184
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003185struct display *
3186input_get_display(struct input *input)
3187{
3188 return input->display;
3189}
3190
Daniel Stone37816df2012-05-16 18:45:18 +01003191struct wl_seat *
3192input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003193{
Daniel Stone37816df2012-05-16 18:45:18 +01003194 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003195}
3196
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003197uint32_t
3198input_get_modifiers(struct input *input)
3199{
3200 return input->modifiers;
3201}
3202
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003203struct widget *
3204input_get_focus_widget(struct input *input)
3205{
3206 return input->focus_widget;
3207}
3208
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003209struct data_offer {
3210 struct wl_data_offer *offer;
3211 struct input *input;
3212 struct wl_array types;
3213 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003214
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003215 struct task io_task;
3216 int fd;
3217 data_func_t func;
3218 int32_t x, y;
3219 void *user_data;
3220};
3221
3222static void
3223data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3224{
3225 struct data_offer *offer = data;
3226 char **p;
3227
3228 p = wl_array_add(&offer->types, sizeof *p);
3229 *p = strdup(type);
3230}
3231
3232static const struct wl_data_offer_listener data_offer_listener = {
3233 data_offer_offer,
3234};
3235
3236static void
3237data_offer_destroy(struct data_offer *offer)
3238{
3239 char **p;
3240
3241 offer->refcount--;
3242 if (offer->refcount == 0) {
3243 wl_data_offer_destroy(offer->offer);
3244 for (p = offer->types.data; *p; p++)
3245 free(*p);
3246 wl_array_release(&offer->types);
3247 free(offer);
3248 }
3249}
3250
3251static void
3252data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003253 struct wl_data_device *data_device,
3254 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003255{
3256 struct data_offer *offer;
3257
Brian Lovinbc919262013-08-07 15:34:59 -07003258 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003259
3260 wl_array_init(&offer->types);
3261 offer->refcount = 1;
3262 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003263 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003264 wl_data_offer_add_listener(offer->offer,
3265 &data_offer_listener, offer);
3266}
3267
3268static void
3269data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003270 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003271 wl_fixed_t x_w, wl_fixed_t y_w,
3272 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003273{
3274 struct input *input = data;
3275 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003276 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003277 float x = wl_fixed_to_double(x_w);
3278 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003279 char **p;
3280
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003281 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003282 input->drag_enter_serial = serial;
3283 input->drag_focus = window,
3284 input->drag_x = x;
3285 input->drag_y = y;
3286
3287 if (!input->touch_grab)
3288 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003289
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003290 if (offer) {
3291 input->drag_offer = wl_data_offer_get_user_data(offer);
3292
3293 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3294 *p = NULL;
3295
3296 types_data = input->drag_offer->types.data;
3297 } else {
3298 input->drag_offer = NULL;
3299 types_data = NULL;
3300 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003301
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003302 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003303 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003304 window->user_data);
3305}
3306
3307static void
3308data_device_leave(void *data, struct wl_data_device *data_device)
3309{
3310 struct input *input = data;
3311
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003312 if (input->drag_offer) {
3313 data_offer_destroy(input->drag_offer);
3314 input->drag_offer = NULL;
3315 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003316}
3317
3318static void
3319data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003320 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003321{
3322 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003323 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003324 float x = wl_fixed_to_double(x_w);
3325 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003326 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003327
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003328 input->drag_x = x;
3329 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003330
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003331 if (input->drag_offer)
3332 types_data = input->drag_offer->types.data;
3333 else
3334 types_data = NULL;
3335
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003336 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003337 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003338 window->user_data);
3339}
3340
3341static void
3342data_device_drop(void *data, struct wl_data_device *data_device)
3343{
3344 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003345 struct window *window = input->drag_focus;
3346 float x, y;
3347
3348 x = input->drag_x;
3349 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003350
3351 if (window->drop_handler)
3352 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003353 x, y, window->user_data);
3354
3355 if (input->touch_grab)
3356 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003357}
3358
3359static void
3360data_device_selection(void *data,
3361 struct wl_data_device *wl_data_device,
3362 struct wl_data_offer *offer)
3363{
3364 struct input *input = data;
3365 char **p;
3366
3367 if (input->selection_offer)
3368 data_offer_destroy(input->selection_offer);
3369
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003370 if (offer) {
3371 input->selection_offer = wl_data_offer_get_user_data(offer);
3372 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3373 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003374 } else {
3375 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003376 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003377}
3378
3379static const struct wl_data_device_listener data_device_listener = {
3380 data_device_data_offer,
3381 data_device_enter,
3382 data_device_leave,
3383 data_device_motion,
3384 data_device_drop,
3385 data_device_selection
3386};
3387
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003388static void
3389input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003390{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003391 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003392 struct wl_cursor *cursor;
3393 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003394
Daniel Stone80972742012-11-07 17:51:39 +11003395 if (!input->pointer)
3396 return;
3397
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003398 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003399 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003400 return;
3401
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003402 if (index >= (int) cursor->image_count) {
3403 fprintf(stderr, "cursor index out of range\n");
3404 return;
3405 }
3406
3407 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003408 buffer = wl_cursor_image_get_buffer(image);
3409 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003410 return;
3411
Kristian Høgsbergae277372012-08-01 09:41:08 -04003412 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003413 input->pointer_surface,
3414 image->hotspot_x, image->hotspot_y);
3415 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3416 wl_surface_damage(input->pointer_surface, 0, 0,
3417 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003418 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003419}
3420
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003421static const struct wl_callback_listener pointer_surface_listener;
3422
3423static void
3424pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3425 uint32_t time)
3426{
3427 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003428 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003429 int i;
3430
3431 if (callback) {
3432 assert(callback == input->cursor_frame_cb);
3433 wl_callback_destroy(callback);
3434 input->cursor_frame_cb = NULL;
3435 }
3436
Daniel Stone80972742012-11-07 17:51:39 +11003437 if (!input->pointer)
3438 return;
3439
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003440 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003441 wl_pointer_set_cursor(input->pointer,
3442 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003443 NULL, 0, 0);
3444 return;
3445 }
3446
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003447 if (input->current_cursor == CURSOR_UNSET)
3448 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003449 cursor = input->display->cursors[input->current_cursor];
3450 if (!cursor)
3451 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003452
3453 /* FIXME We don't have the current time on the first call so we set
3454 * the animation start to the time of the first frame callback. */
3455 if (time == 0)
3456 input->cursor_anim_start = 0;
3457 else if (input->cursor_anim_start == 0)
3458 input->cursor_anim_start = time;
3459
3460 if (time == 0 || input->cursor_anim_start == 0)
3461 i = 0;
3462 else
3463 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3464
Pekka Paalanenbc106382012-10-10 12:49:31 +03003465 if (cursor->image_count > 1) {
3466 input->cursor_frame_cb =
3467 wl_surface_frame(input->pointer_surface);
3468 wl_callback_add_listener(input->cursor_frame_cb,
3469 &pointer_surface_listener, input);
3470 }
3471
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003472 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003473}
3474
3475static const struct wl_callback_listener pointer_surface_listener = {
3476 pointer_surface_frame_callback
3477};
3478
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003479void
3480input_set_pointer_image(struct input *input, int pointer)
3481{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003482 int force = 0;
3483
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003484 if (!input->pointer)
3485 return;
3486
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003487 if (input->pointer_enter_serial > input->cursor_serial)
3488 force = 1;
3489
3490 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003491 return;
3492
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003493 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003494 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003495 if (!input->cursor_frame_cb)
3496 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003497 else if (force) {
3498 /* The current frame callback may be stuck if, for instance,
3499 * the set cursor request was processed by the server after
3500 * this client lost the focus. In this case the cursor surface
3501 * might not be mapped and the frame callback wouldn't ever
3502 * complete. Send a set_cursor and attach to try to map the
3503 * cursor surface again so that the callback will finish */
3504 input_set_pointer_image_index(input, 0);
3505 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003506}
3507
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003508struct wl_data_device *
3509input_get_data_device(struct input *input)
3510{
3511 return input->data_device;
3512}
3513
3514void
3515input_set_selection(struct input *input,
3516 struct wl_data_source *source, uint32_t time)
3517{
3518 wl_data_device_set_selection(input->data_device, source, time);
3519}
3520
3521void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003522input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003523{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003524 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003525 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003526}
3527
3528static void
3529offer_io_func(struct task *task, uint32_t events)
3530{
3531 struct data_offer *offer =
3532 container_of(task, struct data_offer, io_task);
3533 unsigned int len;
3534 char buffer[4096];
3535
3536 len = read(offer->fd, buffer, sizeof buffer);
3537 offer->func(buffer, len,
3538 offer->x, offer->y, offer->user_data);
3539
3540 if (len == 0) {
3541 close(offer->fd);
3542 data_offer_destroy(offer);
3543 }
3544}
3545
3546static void
3547data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3548 data_func_t func, void *user_data)
3549{
3550 int p[2];
3551
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003552 if (pipe2(p, O_CLOEXEC) == -1)
3553 return;
3554
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003555 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3556 close(p[1]);
3557
3558 offer->io_task.run = offer_io_func;
3559 offer->fd = p[0];
3560 offer->func = func;
3561 offer->refcount++;
3562 offer->user_data = user_data;
3563
3564 display_watch_fd(offer->input->display,
3565 offer->fd, EPOLLIN, &offer->io_task);
3566}
3567
3568void
3569input_receive_drag_data(struct input *input, const char *mime_type,
3570 data_func_t func, void *data)
3571{
3572 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003573 input->drag_offer->x = input->drag_x;
3574 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003575}
3576
3577int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003578input_receive_drag_data_to_fd(struct input *input,
3579 const char *mime_type, int fd)
3580{
3581 if (input->drag_offer)
3582 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3583
3584 return 0;
3585}
3586
3587int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003588input_receive_selection_data(struct input *input, const char *mime_type,
3589 data_func_t func, void *data)
3590{
3591 char **p;
3592
3593 if (input->selection_offer == NULL)
3594 return -1;
3595
3596 for (p = input->selection_offer->types.data; *p; p++)
3597 if (strcmp(mime_type, *p) == 0)
3598 break;
3599
3600 if (*p == NULL)
3601 return -1;
3602
3603 data_offer_receive_data(input->selection_offer,
3604 mime_type, func, data);
3605 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003606}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003607
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003608int
3609input_receive_selection_data_to_fd(struct input *input,
3610 const char *mime_type, int fd)
3611{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003612 if (input->selection_offer)
3613 wl_data_offer_receive(input->selection_offer->offer,
3614 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003615
3616 return 0;
3617}
3618
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003619void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003620window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003621{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003622 if (!window->shell_surface)
3623 return;
3624
Daniel Stone37816df2012-05-16 18:45:18 +01003625 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003626}
3627
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003628static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003629surface_set_synchronized(struct surface *surface)
3630{
3631 if (!surface->subsurface)
3632 return;
3633
3634 if (surface->synchronized)
3635 return;
3636
3637 wl_subsurface_set_sync(surface->subsurface);
3638 surface->synchronized = 1;
3639}
3640
3641static void
3642surface_set_synchronized_default(struct surface *surface)
3643{
3644 if (!surface->subsurface)
3645 return;
3646
3647 if (surface->synchronized == surface->synchronized_default)
3648 return;
3649
3650 if (surface->synchronized_default)
3651 wl_subsurface_set_sync(surface->subsurface);
3652 else
3653 wl_subsurface_set_desync(surface->subsurface);
3654
3655 surface->synchronized = surface->synchronized_default;
3656}
3657
3658static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003659surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003660{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003661 struct widget *widget = surface->widget;
3662 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003663
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003664 if (surface->input_region) {
3665 wl_region_destroy(surface->input_region);
3666 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003667 }
3668
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003669 if (surface->opaque_region)
3670 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003671
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003672 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003673
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003674 if (widget->resize_handler)
3675 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003676 widget->allocation.width,
3677 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003678 widget->user_data);
3679
Pekka Paalanen35e82632013-04-25 13:57:48 +03003680 if (surface->subsurface &&
3681 (surface->allocation.x != widget->allocation.x ||
3682 surface->allocation.y != widget->allocation.y)) {
3683 wl_subsurface_set_position(surface->subsurface,
3684 widget->allocation.x,
3685 widget->allocation.y);
3686 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003687 if (surface->allocation.width != widget->allocation.width ||
3688 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003689 window_schedule_redraw(widget->window);
3690 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003691 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003692
3693 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003694 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003695 widget->allocation.width,
3696 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003697}
3698
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003699static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003700hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3701{
3702 /*
3703 * This hack should be removed, when EGL respects
3704 * eglSwapInterval(0).
3705 *
3706 * If this window has sub-surfaces, especially a free-running
3707 * EGL-widget, we need to post the parent surface once with
3708 * all the old state to guarantee, that the EGL-widget will
3709 * receive its frame callback soon. Otherwise, a forced call
3710 * to eglSwapBuffers may end up blocking, waiting for a frame
3711 * event that will never come, because we will commit the parent
3712 * surface with all new state only after eglSwapBuffers returns.
3713 *
3714 * This assumes, that:
3715 * 1. When the EGL widget's resize hook is called, it pauses.
3716 * 2. When the EGL widget's redraw hook is called, it forces a
3717 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3718 * In a single threaded application condition 1 is a no-op.
3719 *
3720 * XXX: This should actually be after the surface_resize() calls,
3721 * but cannot, because then it would commit the incomplete state
3722 * accumulated from the widget resize hooks.
3723 */
3724 if (window->subsurface_list.next != &window->main_surface->link ||
3725 window->subsurface_list.prev != &window->main_surface->link)
3726 wl_surface_commit(window->main_surface->surface);
3727}
3728
3729static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003730window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003731{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003732 struct surface *surface;
3733
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003734 widget_set_allocation(window->main_surface->widget,
3735 window->pending_allocation.x,
3736 window->pending_allocation.y,
3737 window->pending_allocation.width,
3738 window->pending_allocation.height);
3739
3740 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003741
3742 /* The main surface is in the list, too. Main surface's
3743 * resize_handler is responsible for calling widget_set_allocation()
3744 * on all sub-surface root widgets, so they will be resized
3745 * properly.
3746 */
3747 wl_list_for_each(surface, &window->subsurface_list, link) {
3748 if (surface == window->main_surface)
3749 continue;
3750
3751 surface_set_synchronized(surface);
3752 surface_resize(surface);
3753 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003754}
3755
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003756static void
3757idle_resize(struct window *window)
3758{
3759 window->resize_needed = 0;
3760 window->redraw_needed = 1;
3761
3762 DBG("from %dx%d to %dx%d\n",
3763 window->main_surface->server_allocation.width,
3764 window->main_surface->server_allocation.height,
3765 window->pending_allocation.width,
3766 window->pending_allocation.height);
3767
3768 hack_prevent_EGL_sub_surface_deadlock(window);
3769
3770 window_do_resize(window);
3771}
3772
3773static void
3774undo_resize(struct window *window)
3775{
3776 window->pending_allocation.width =
3777 window->main_surface->server_allocation.width;
3778 window->pending_allocation.height =
3779 window->main_surface->server_allocation.height;
3780
3781 DBG("back to %dx%d\n",
3782 window->main_surface->server_allocation.width,
3783 window->main_surface->server_allocation.height);
3784
3785 window_do_resize(window);
3786
3787 if (window->pending_allocation.width == 0 &&
3788 window->pending_allocation.height == 0) {
3789 fprintf(stderr, "Error: Could not draw a surface, "
3790 "most likely due to insufficient disk space in "
3791 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3792 exit(EXIT_FAILURE);
3793 }
3794}
3795
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003796void
3797window_schedule_resize(struct window *window, int width, int height)
3798{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003799 /* We should probably get these numbers from the theme. */
3800 const int min_width = 200, min_height = 200;
3801
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003802 window->pending_allocation.x = 0;
3803 window->pending_allocation.y = 0;
3804 window->pending_allocation.width = width;
3805 window->pending_allocation.height = height;
3806
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003807 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003808 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003809 window->min_allocation.width = min_width;
3810 else
3811 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003812 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003813 window->min_allocation.height = min_height;
3814 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003815 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003816 }
3817
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003818 if (window->pending_allocation.width < window->min_allocation.width)
3819 window->pending_allocation.width = window->min_allocation.width;
3820 if (window->pending_allocation.height < window->min_allocation.height)
3821 window->pending_allocation.height = window->min_allocation.height;
3822
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003823 window->resize_needed = 1;
3824 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003825}
3826
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003827void
3828widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3829{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003830 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003831}
3832
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003833static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003834handle_ping(void *data, struct wl_shell_surface *shell_surface,
3835 uint32_t serial)
3836{
3837 wl_shell_surface_pong(shell_surface, serial);
3838}
3839
3840static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003841handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003842 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003843{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003844 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003845
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003846 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003847 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003848}
3849
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003850static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003851menu_destroy(struct menu *menu)
3852{
3853 widget_destroy(menu->widget);
3854 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003855 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003856 free(menu);
3857}
3858
3859static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003860handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3861{
3862 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003863 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003864
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003865 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003866 * device. Or just use wl_callback. And this really needs to
3867 * be a window vfunc that the menu can set. And we need the
3868 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003869
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003870 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003871 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003872}
3873
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003874static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003875 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003876 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003877 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003878};
3879
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003880void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003881window_get_allocation(struct window *window,
3882 struct rectangle *allocation)
3883{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003884 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003885}
3886
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003887static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003888widget_redraw(struct widget *widget)
3889{
3890 struct widget *child;
3891
3892 if (widget->redraw_handler)
3893 widget->redraw_handler(widget, widget->user_data);
3894 wl_list_for_each(child, &widget->child_list, link)
3895 widget_redraw(child);
3896}
3897
3898static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003899frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3900{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003901 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003902
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003903 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003904 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003905 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003906 surface->frame_cb = NULL;
3907
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003908 surface->last_time = time;
3909
Pekka Paalanen71233882013-04-25 13:57:53 +03003910 if (surface->redraw_needed || surface->window->redraw_needed) {
3911 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003912 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003913 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003914}
3915
3916static const struct wl_callback_listener listener = {
3917 frame_callback
3918};
3919
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003920static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003921surface_redraw(struct surface *surface)
3922{
Pekka Paalanen71233882013-04-25 13:57:53 +03003923 DBG_OBJ(surface->surface, "begin\n");
3924
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003925 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003926 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003927
3928 /* Whole-window redraw forces a redraw even if the previous has
3929 * not yet hit the screen.
3930 */
3931 if (surface->frame_cb) {
3932 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003933 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003934
Pekka Paalanen71233882013-04-25 13:57:53 +03003935 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003936 wl_callback_destroy(surface->frame_cb);
3937 }
3938
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003939 if (!widget_get_cairo_surface(surface->widget)) {
3940 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3941 return -1;
3942 }
3943
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003944 surface->frame_cb = wl_surface_frame(surface->surface);
3945 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003946 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003947
3948 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003949 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003950 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003951 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003952 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003953}
3954
3955static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003956idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003957{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003958 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003959 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003960 int failed = 0;
3961 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003962
Pekka Paalanen71233882013-04-25 13:57:53 +03003963 DBG(" --------- \n");
3964
Pekka Paalaneneebff542013-04-25 13:57:52 +03003965 wl_list_init(&window->redraw_task.link);
3966 window->redraw_task_scheduled = 0;
3967
3968 if (window->resize_needed) {
3969 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003970 if (window->main_surface->frame_cb) {
3971 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003972 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003973 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003974
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003975 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003976 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03003977 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003978
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003979 if (surface_redraw(window->main_surface) < 0) {
3980 /*
3981 * Only main_surface failure will cause us to undo the resize.
3982 * If sub-surfaces fail, they will just be broken with old
3983 * content.
3984 */
3985 failed = 1;
3986 } else {
3987 wl_list_for_each(surface, &window->subsurface_list, link) {
3988 if (surface == window->main_surface)
3989 continue;
3990
3991 surface_redraw(surface);
3992 }
3993 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003994
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003995 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003996 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003997
3998 wl_list_for_each(surface, &window->subsurface_list, link)
3999 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004000
4001 if (resized && failed) {
4002 /* Restore widget tree to correspond to what is on screen. */
4003 undo_resize(window);
4004 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004005}
4006
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004007static void
4008window_schedule_redraw_task(struct window *window)
4009{
4010 if (window->configure_requests)
4011 return;
4012 if (!window->redraw_task_scheduled) {
4013 window->redraw_task.run = idle_redraw;
4014 display_defer(window->display, &window->redraw_task);
4015 window->redraw_task_scheduled = 1;
4016 }
4017}
4018
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004019void
4020window_schedule_redraw(struct window *window)
4021{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004022 struct surface *surface;
4023
Pekka Paalanen71233882013-04-25 13:57:53 +03004024 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4025
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004026 wl_list_for_each(surface, &window->subsurface_list, link)
4027 surface->redraw_needed = 1;
4028
4029 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004030}
4031
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004032int
4033window_is_fullscreen(struct window *window)
4034{
4035 return window->type == TYPE_FULLSCREEN;
4036}
4037
Philip Withnallc971d2a2013-11-25 18:01:33 +00004038int
4039window_is_transient(struct window *window)
4040{
4041 return window->type == TYPE_TRANSIENT;
4042}
4043
MoD063a8822013-03-02 23:43:57 +00004044static void
4045configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4046{
4047 struct window *window = data;
4048
4049 wl_callback_destroy(callback);
4050 window->configure_requests--;
4051
4052 if (!window->configure_requests)
4053 window_schedule_redraw(window);
4054}
4055
4056static struct wl_callback_listener configure_request_listener = {
4057 configure_request_completed,
4058};
4059
4060static void
4061window_defer_redraw_until_configure(struct window* window)
4062{
4063 struct wl_callback *callback;
4064
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004065 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00004066 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004067 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00004068 }
4069
4070 callback = wl_display_sync(window->display->display);
4071 wl_callback_add_listener(callback, &configure_request_listener, window);
4072 window->configure_requests++;
4073}
4074
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004075void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004076window_set_fullscreen(struct window *window, int fullscreen)
4077{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004078 if (!window->display->shell)
4079 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004080
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04004081 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004082 return;
4083
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004084 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004085 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01004086 if (window->type == TYPE_TOPLEVEL) {
4087 window->saved_allocation = window->main_surface->allocation;
4088 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004089 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004090 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004091 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004092 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004093 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004094 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004095 if (window->saved_type == TYPE_MAXIMIZED) {
4096 window_set_maximized(window, 1);
4097 } else {
4098 window->type = TYPE_TOPLEVEL;
4099 wl_shell_surface_set_toplevel(window->shell_surface);
4100 window_schedule_resize(window,
4101 window->saved_allocation.width,
4102 window->saved_allocation.height);
4103 }
4104
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004105 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004106}
4107
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004108void
4109window_set_fullscreen_method(struct window *window,
4110 enum wl_shell_surface_fullscreen_method method)
4111{
4112 window->fullscreen_method = method;
4113}
4114
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004115int
4116window_is_maximized(struct window *window)
4117{
4118 return window->type == TYPE_MAXIMIZED;
4119}
4120
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004121void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004122window_set_maximized(struct window *window, int maximized)
4123{
4124 if (!window->display->shell)
4125 return;
4126
4127 if ((window->type == TYPE_MAXIMIZED) == maximized)
4128 return;
4129
4130 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004131 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004132 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4133 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004134 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004135 } else if (window->type == TYPE_FULLSCREEN) {
4136 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4137 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004138 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004139 } else {
4140 wl_shell_surface_set_toplevel(window->shell_surface);
4141 window->type = TYPE_TOPLEVEL;
4142 window_schedule_resize(window,
4143 window->saved_allocation.width,
4144 window->saved_allocation.height);
4145 }
4146}
4147
4148void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004149window_set_user_data(struct window *window, void *data)
4150{
4151 window->user_data = data;
4152}
4153
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004154void *
4155window_get_user_data(struct window *window)
4156{
4157 return window->user_data;
4158}
4159
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004160void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004161window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004162 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004163{
4164 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004165}
4166
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004167void
4168window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004169 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004170{
4171 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004172}
4173
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004174void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004175window_set_data_handler(struct window *window, window_data_handler_t handler)
4176{
4177 window->data_handler = handler;
4178}
4179
4180void
4181window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4182{
4183 window->drop_handler = handler;
4184}
4185
4186void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004187window_set_close_handler(struct window *window,
4188 window_close_handler_t handler)
4189{
4190 window->close_handler = handler;
4191}
4192
4193void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004194window_set_fullscreen_handler(struct window *window,
4195 window_fullscreen_handler_t handler)
4196{
4197 window->fullscreen_handler = handler;
4198}
4199
4200void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004201window_set_output_handler(struct window *window,
4202 window_output_handler_t handler)
4203{
4204 window->output_handler = handler;
4205}
4206
4207void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004208window_set_title(struct window *window, const char *title)
4209{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004210 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004211 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004212 if (window->frame) {
4213 frame_set_title(window->frame->frame, title);
4214 widget_schedule_redraw(window->frame->widget);
4215 }
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004216 if (window->shell_surface)
4217 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004218}
4219
4220const char *
4221window_get_title(struct window *window)
4222{
4223 return window->title;
4224}
4225
4226void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004227window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4228{
4229 struct text_cursor_position *text_cursor_position =
4230 window->display->text_cursor_position;
4231
Scott Moreau9295ce02012-06-01 12:46:10 -06004232 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004233 return;
4234
4235 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004236 window->main_surface->surface,
4237 wl_fixed_from_int(x),
4238 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004239}
4240
4241void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004242window_damage(struct window *window, int32_t x, int32_t y,
4243 int32_t width, int32_t height)
4244{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004245 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004246}
4247
Casey Dahlin9074db52012-04-19 22:50:09 -04004248static void
4249surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004250 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004251{
Rob Bradford7507b572012-05-15 17:55:34 +01004252 struct window *window = data;
4253 struct output *output;
4254 struct output *output_found = NULL;
4255 struct window_output *window_output;
4256
4257 wl_list_for_each(output, &window->display->output_list, link) {
4258 if (output->output == wl_output) {
4259 output_found = output;
4260 break;
4261 }
4262 }
4263
4264 if (!output_found)
4265 return;
4266
Brian Lovinbc919262013-08-07 15:34:59 -07004267 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004268 window_output->output = output_found;
4269
4270 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004271
4272 if (window->output_handler)
4273 window->output_handler(window, output_found, 1,
4274 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004275}
4276
4277static void
4278surface_leave(void *data,
4279 struct wl_surface *wl_surface, struct wl_output *output)
4280{
Rob Bradford7507b572012-05-15 17:55:34 +01004281 struct window *window = data;
4282 struct window_output *window_output;
4283 struct window_output *window_output_found = NULL;
4284
4285 wl_list_for_each(window_output, &window->window_output_list, link) {
4286 if (window_output->output->output == output) {
4287 window_output_found = window_output;
4288 break;
4289 }
4290 }
4291
4292 if (window_output_found) {
4293 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004294
4295 if (window->output_handler)
4296 window->output_handler(window, window_output->output,
4297 0, window->user_data);
4298
Rob Bradford7507b572012-05-15 17:55:34 +01004299 free(window_output_found);
4300 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004301}
4302
4303static const struct wl_surface_listener surface_listener = {
4304 surface_enter,
4305 surface_leave
4306};
4307
Pekka Paalanen4e373742013-02-13 16:17:13 +02004308static struct surface *
4309surface_create(struct window *window)
4310{
4311 struct display *display = window->display;
4312 struct surface *surface;
4313
Brian Lovinbc919262013-08-07 15:34:59 -07004314 surface = xmalloc(sizeof *surface);
4315 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004316 if (!surface)
4317 return NULL;
4318
4319 surface->window = window;
4320 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004321 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004322 wl_surface_add_listener(surface->surface, &surface_listener, window);
4323
Pekka Paalanen35e82632013-04-25 13:57:48 +03004324 wl_list_insert(&window->subsurface_list, &surface->link);
4325
Pekka Paalanen4e373742013-02-13 16:17:13 +02004326 return surface;
4327}
4328
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004329static struct window *
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004330window_create_internal(struct display *display, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004331{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004332 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004333 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004334
Peter Huttererf3d62272013-08-08 11:57:05 +10004335 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004336 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004337 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004338
4339 surface = surface_create(window);
4340 window->main_surface = surface;
4341
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004342 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004343 window->shell_surface =
4344 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004345 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004346 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004347 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004348
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004349 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004350 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004351 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004352 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004353
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004354 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004355#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004356 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004357#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004358 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004359#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004360 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004361 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004362
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004363 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004364 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004365 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004366
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004367 if (window->shell_surface) {
4368 wl_shell_surface_set_user_data(window->shell_surface, window);
4369 wl_shell_surface_add_listener(window->shell_surface,
4370 &shell_surface_listener, window);
4371 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004372
Rob Bradford7507b572012-05-15 17:55:34 +01004373 wl_list_init (&window->window_output_list);
4374
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004375 return window;
4376}
4377
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004378struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004379window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004380{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004381 return window_create_internal(display, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004382}
4383
4384struct window *
4385window_create_custom(struct display *display)
4386{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004387 return window_create_internal(display, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004388}
4389
4390struct window *
4391window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004392 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004393{
4394 struct window *window;
4395
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004396 window = window_create_internal(parent->display, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004397
4398 window->x = x;
4399 window->y = y;
4400
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004401 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004402 wl_shell_surface_set_transient(
4403 window->shell_surface,
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004404 parent->main_surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004405 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004406
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004407 return window;
4408}
4409
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004410static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004411menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004412{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004413 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004414 int next;
4415
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004416 frame_interior(menu->frame, &x, &y, &width, &height);
4417 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004418 if (menu->current != next) {
4419 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004420 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004421 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004422}
4423
4424static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004425menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004426 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004427 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004428{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004429 struct menu *menu = data;
4430
4431 if (widget == menu->widget)
4432 menu_set_item(data, y);
4433
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004434 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004435}
4436
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004437static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004438menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004439 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004440{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004441 struct menu *menu = data;
4442
4443 if (widget == menu->widget)
4444 menu_set_item(data, y);
4445
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004446 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004447}
4448
4449static void
4450menu_leave_handler(struct widget *widget, struct input *input, void *data)
4451{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004452 struct menu *menu = data;
4453
4454 if (widget == menu->widget)
4455 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004456}
4457
4458static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004459menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004460 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004461 uint32_t button, enum wl_pointer_button_state state,
4462 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004463
4464{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004465 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004466
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004467 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4468 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004469 /* Either relase after press-drag-release or
4470 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004471 menu->func(menu->parent, input,
4472 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004473 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004474 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004475 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004476 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004477 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004478}
4479
4480static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004481menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004482{
4483 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004484 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004485 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004486
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004487 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004488
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004489 frame_repaint(menu->frame, cr);
4490 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004491
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004492 theme_set_background_source(menu->window->display->theme,
4493 cr, THEME_FRAME_ACTIVE);
4494 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004495 cairo_fill(cr);
4496
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004497 cairo_select_font_face(cr, "sans",
4498 CAIRO_FONT_SLANT_NORMAL,
4499 CAIRO_FONT_WEIGHT_NORMAL);
4500 cairo_set_font_size(cr, 12);
4501
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004502 for (i = 0; i < menu->count; i++) {
4503 if (i == menu->current) {
4504 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004505 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004506 cairo_fill(cr);
4507 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004508 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004509 cairo_show_text(cr, menu->entries[i]);
4510 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004511 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4512 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004513 cairo_show_text(cr, menu->entries[i]);
4514 }
4515 }
4516
4517 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004518}
4519
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004520void
4521window_show_menu(struct display *display,
4522 struct input *input, uint32_t time, struct window *parent,
4523 int32_t x, int32_t y,
4524 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004525{
4526 struct window *window;
4527 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004528 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004529
4530 menu = malloc(sizeof *menu);
4531 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004532 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004533
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004534 window = window_create_internal(parent->display, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004535 if (!window) {
4536 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004537 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004538 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004539
4540 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004541 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004542 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004543 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4544 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004545 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004546 FRAME_BUTTON_NONE, NULL);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004547 menu->entries = entries;
4548 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004549 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004550 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004551 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004552 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004553 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004554 window->type = TYPE_MENU;
4555 window->x = x;
4556 window->y = y;
4557
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004558 input_ungrab(input);
4559
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004560 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004561 widget_set_enter_handler(menu->widget, menu_enter_handler);
4562 widget_set_leave_handler(menu->widget, menu_leave_handler);
4563 widget_set_motion_handler(menu->widget, menu_motion_handler);
4564 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004565
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004566 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004567 frame_resize_inside(menu->frame, 200, count * 20);
4568 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4569 window_schedule_resize(window, frame_width(menu->frame),
4570 frame_height(menu->frame));
4571
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004572 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4573 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4574 display_get_serial(window->display),
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004575 parent->main_surface->surface,
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004576 window->x - ix, window->y - iy, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004577}
4578
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004579void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004580window_set_buffer_type(struct window *window, enum window_buffer_type type)
4581{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004582 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004583}
4584
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004585void
4586window_set_preferred_format(struct window *window,
4587 enum preferred_format format)
4588{
4589 window->preferred_format = format;
4590}
4591
Pekka Paalanen35e82632013-04-25 13:57:48 +03004592struct widget *
4593window_add_subsurface(struct window *window, void *data,
4594 enum subsurface_mode default_mode)
4595{
4596 struct widget *widget;
4597 struct surface *surface;
4598 struct wl_surface *parent;
4599 struct wl_subcompositor *subcompo = window->display->subcompositor;
4600
Pekka Paalanen35e82632013-04-25 13:57:48 +03004601 surface = surface_create(window);
4602 widget = widget_create(window, surface, data);
4603 wl_list_init(&widget->link);
4604 surface->widget = widget;
4605
4606 parent = window->main_surface->surface;
4607 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4608 surface->surface,
4609 parent);
4610 surface->synchronized = 1;
4611
4612 switch (default_mode) {
4613 case SUBSURFACE_SYNCHRONIZED:
4614 surface->synchronized_default = 1;
4615 break;
4616 case SUBSURFACE_DESYNCHRONIZED:
4617 surface->synchronized_default = 0;
4618 break;
4619 default:
4620 assert(!"bad enum subsurface_mode");
4621 }
4622
4623 return widget;
4624}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004625
4626static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004627display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004628 struct wl_output *wl_output,
4629 int x, int y,
4630 int physical_width,
4631 int physical_height,
4632 int subpixel,
4633 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004634 const char *model,
4635 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004636{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004637 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004638
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004639 output->allocation.x = x;
4640 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004641 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004642}
4643
4644static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004645display_handle_done(void *data,
4646 struct wl_output *wl_output)
4647{
4648}
4649
4650static void
4651display_handle_scale(void *data,
4652 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004653 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004654{
4655 struct output *output = data;
4656
4657 output->scale = scale;
4658}
4659
4660static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004661display_handle_mode(void *data,
4662 struct wl_output *wl_output,
4663 uint32_t flags,
4664 int width,
4665 int height,
4666 int refresh)
4667{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004668 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004669 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004670
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004671 if (flags & WL_OUTPUT_MODE_CURRENT) {
4672 output->allocation.width = width;
4673 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004674 if (display->output_configure_handler)
4675 (*display->output_configure_handler)(
4676 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004677 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004678}
4679
4680static const struct wl_output_listener output_listener = {
4681 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004682 display_handle_mode,
4683 display_handle_done,
4684 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004685};
4686
4687static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004688display_add_output(struct display *d, uint32_t id)
4689{
4690 struct output *output;
4691
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004692 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004693 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004694 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004695 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004696 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004697 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004698 wl_list_insert(d->output_list.prev, &output->link);
4699
4700 wl_output_add_listener(output->output, &output_listener, output);
4701}
4702
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004703static void
4704output_destroy(struct output *output)
4705{
4706 if (output->destroy_handler)
4707 (*output->destroy_handler)(output, output->user_data);
4708
4709 wl_output_destroy(output->output);
4710 wl_list_remove(&output->link);
4711 free(output);
4712}
4713
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004714static void
4715display_destroy_output(struct display *d, uint32_t id)
4716{
4717 struct output *output;
4718
4719 wl_list_for_each(output, &d->output_list, link) {
4720 if (output->server_output_id == id) {
4721 output_destroy(output);
4722 break;
4723 }
4724 }
4725}
4726
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004727void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004728display_set_global_handler(struct display *display,
4729 display_global_handler_t handler)
4730{
4731 struct global *global;
4732
4733 display->global_handler = handler;
4734 if (!handler)
4735 return;
4736
4737 wl_list_for_each(global, &display->global_list, link)
4738 display->global_handler(display,
4739 global->name, global->interface,
4740 global->version, display->user_data);
4741}
4742
4743void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004744display_set_global_handler_remove(struct display *display,
4745 display_global_handler_t remove_handler)
4746{
4747 display->global_handler_remove = remove_handler;
4748 if (!remove_handler)
4749 return;
4750}
4751
4752void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004753display_set_output_configure_handler(struct display *display,
4754 display_output_handler_t handler)
4755{
4756 struct output *output;
4757
4758 display->output_configure_handler = handler;
4759 if (!handler)
4760 return;
4761
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004762 wl_list_for_each(output, &display->output_list, link) {
4763 if (output->allocation.width == 0 &&
4764 output->allocation.height == 0)
4765 continue;
4766
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004767 (*display->output_configure_handler)(output,
4768 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004769 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004770}
4771
4772void
4773output_set_user_data(struct output *output, void *data)
4774{
4775 output->user_data = data;
4776}
4777
4778void *
4779output_get_user_data(struct output *output)
4780{
4781 return output->user_data;
4782}
4783
4784void
4785output_set_destroy_handler(struct output *output,
4786 display_output_handler_t handler)
4787{
4788 output->destroy_handler = handler;
4789 /* FIXME: implement this, once we have way to remove outputs */
4790}
4791
4792void
Scott Moreau4e072362012-09-29 02:03:11 -06004793output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004794{
Scott Moreau4e072362012-09-29 02:03:11 -06004795 struct rectangle allocation = output->allocation;
4796
4797 switch (output->transform) {
4798 case WL_OUTPUT_TRANSFORM_90:
4799 case WL_OUTPUT_TRANSFORM_270:
4800 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4801 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4802 /* Swap width and height */
4803 allocation.width = output->allocation.height;
4804 allocation.height = output->allocation.width;
4805 break;
4806 }
4807
4808 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004809}
4810
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004811struct wl_output *
4812output_get_wl_output(struct output *output)
4813{
4814 return output->output;
4815}
4816
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004817enum wl_output_transform
4818output_get_transform(struct output *output)
4819{
4820 return output->transform;
4821}
4822
Alexander Larssonafd319a2013-05-22 14:41:27 +02004823uint32_t
4824output_get_scale(struct output *output)
4825{
4826 return output->scale;
4827}
4828
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004829static void
Daniel Stone97f68542012-05-30 16:32:01 +01004830fini_xkb(struct input *input)
4831{
4832 xkb_state_unref(input->xkb.state);
4833 xkb_map_unref(input->xkb.keymap);
4834}
4835
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004836#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004837
Daniel Stone97f68542012-05-30 16:32:01 +01004838static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004839display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004840{
4841 struct input *input;
4842
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004843 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004844 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004845 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004846 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004847 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004848 input->pointer_focus = NULL;
4849 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004850 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004851 wl_list_insert(d->input_list.prev, &input->link);
4852
Daniel Stone37816df2012-05-16 18:45:18 +01004853 wl_seat_add_listener(input->seat, &seat_listener, input);
4854 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004855
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004856 input->data_device =
4857 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004858 input->seat);
4859 wl_data_device_add_listener(input->data_device, &data_device_listener,
4860 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004861
4862 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004863
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004864 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4865 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004866 input->repeat_task.run = keyboard_repeat_func;
4867 display_watch_fd(d, input->repeat_timer_fd,
4868 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004869}
4870
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004871static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004872input_destroy(struct input *input)
4873{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004874 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004875 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004876
4877 if (input->drag_offer)
4878 data_offer_destroy(input->drag_offer);
4879
4880 if (input->selection_offer)
4881 data_offer_destroy(input->selection_offer);
4882
4883 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004884
4885 if (input->display->seat_version >= 3) {
4886 if (input->pointer)
4887 wl_pointer_release(input->pointer);
4888 if (input->keyboard)
4889 wl_keyboard_release(input->keyboard);
4890 }
4891
Daniel Stone97f68542012-05-30 16:32:01 +01004892 fini_xkb(input);
4893
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004894 wl_surface_destroy(input->pointer_surface);
4895
Pekka Paalanene1207c72011-12-16 12:02:09 +02004896 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004897 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004898 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004899 free(input);
4900}
4901
4902static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004903init_workspace_manager(struct display *d, uint32_t id)
4904{
4905 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004906 wl_registry_bind(d->registry, id,
4907 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004908 if (d->workspace_manager != NULL)
4909 workspace_manager_add_listener(d->workspace_manager,
4910 &workspace_manager_listener,
4911 d);
4912}
4913
4914static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004915shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4916{
4917 struct display *d = data;
4918
4919 if (format == WL_SHM_FORMAT_RGB565)
4920 d->has_rgb565 = 1;
4921}
4922
4923struct wl_shm_listener shm_listener = {
4924 shm_format
4925};
4926
4927static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004928registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4929 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004930{
4931 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004932 struct global *global;
4933
Brian Lovinbc919262013-08-07 15:34:59 -07004934 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004935 global->name = id;
4936 global->interface = strdup(interface);
4937 global->version = version;
4938 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004939
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004940 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004941 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05004942 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004943 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004944 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004945 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01004946 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004947 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004948 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004949 d->shell = wl_registry_bind(registry,
4950 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004951 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004952 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004953 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004954 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4955 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004956 wl_registry_bind(registry, id,
4957 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004958 } else if (strcmp(interface, "text_cursor_position") == 0) {
4959 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004960 wl_registry_bind(registry, id,
4961 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004962 } else if (strcmp(interface, "workspace_manager") == 0) {
4963 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004964 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4965 d->subcompositor =
4966 wl_registry_bind(registry, id,
4967 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004968 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004969
4970 if (d->global_handler)
4971 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004972}
4973
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004974static void
4975registry_handle_global_remove(void *data, struct wl_registry *registry,
4976 uint32_t name)
4977{
4978 struct display *d = data;
4979 struct global *global;
4980 struct global *tmp;
4981
4982 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4983 if (global->name != name)
4984 continue;
4985
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004986 if (strcmp(global->interface, "wl_output") == 0)
4987 display_destroy_output(d, name);
4988
4989 /* XXX: Should destroy remaining bound globals */
4990
4991 if (d->global_handler_remove)
4992 d->global_handler_remove(d, name, global->interface,
4993 global->version, d->user_data);
4994
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004995 wl_list_remove(&global->link);
4996 free(global->interface);
4997 free(global);
4998 }
4999}
5000
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005001void *
5002display_bind(struct display *display, uint32_t name,
5003 const struct wl_interface *interface, uint32_t version)
5004{
5005 return wl_registry_bind(display->registry, name, interface, version);
5006}
5007
5008static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005009 registry_handle_global,
5010 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005011};
5012
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005013#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005014static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005015init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005016{
5017 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005018 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005019
Rob Clark6396ed32012-03-11 19:48:41 -05005020#ifdef USE_CAIRO_GLESV2
5021# define GL_BIT EGL_OPENGL_ES2_BIT
5022#else
5023# define GL_BIT EGL_OPENGL_BIT
5024#endif
5025
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005026 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005027 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005028 EGL_RED_SIZE, 1,
5029 EGL_GREEN_SIZE, 1,
5030 EGL_BLUE_SIZE, 1,
5031 EGL_ALPHA_SIZE, 1,
5032 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005033 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005034 EGL_NONE
5035 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005036
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005037#ifdef USE_CAIRO_GLESV2
5038 static const EGLint context_attribs[] = {
5039 EGL_CONTEXT_CLIENT_VERSION, 2,
5040 EGL_NONE
5041 };
5042 EGLint api = EGL_OPENGL_ES_API;
5043#else
5044 EGLint *context_attribs = NULL;
5045 EGLint api = EGL_OPENGL_API;
5046#endif
5047
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005048 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005049 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005050 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005051 return -1;
5052 }
5053
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005054 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005055 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005056 return -1;
5057 }
5058
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005059 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5060 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005061 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005062 return -1;
5063 }
5064
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005065 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005066 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005067 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005068 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005069 return -1;
5070 }
5071
Benjamin Franzke0c991632011-09-27 21:57:31 +02005072 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5073 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005074 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005075 return -1;
5076 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005077
5078 return 0;
5079}
5080
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005081static void
5082fini_egl(struct display *display)
5083{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005084 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005085
5086 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5087 EGL_NO_CONTEXT);
5088
5089 eglTerminate(display->dpy);
5090 eglReleaseThread();
5091}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005092#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005093
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005094static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005095init_dummy_surface(struct display *display)
5096{
5097 int len;
5098 void *data;
5099
5100 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5101 data = malloc(len);
5102 display->dummy_surface =
5103 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5104 1, 1, len);
5105 display->dummy_surface_data = data;
5106}
5107
5108static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005109handle_display_data(struct task *task, uint32_t events)
5110{
5111 struct display *display =
5112 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005113 struct epoll_event ep;
5114 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005115
5116 display->display_fd_events = events;
5117
5118 if (events & EPOLLERR || events & EPOLLHUP) {
5119 display_exit(display);
5120 return;
5121 }
5122
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005123 if (events & EPOLLIN) {
5124 ret = wl_display_dispatch(display->display);
5125 if (ret == -1) {
5126 display_exit(display);
5127 return;
5128 }
5129 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005130
5131 if (events & EPOLLOUT) {
5132 ret = wl_display_flush(display->display);
5133 if (ret == 0) {
5134 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5135 ep.data.ptr = &display->display_task;
5136 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5137 display->display_fd, &ep);
5138 } else if (ret == -1 && errno != EAGAIN) {
5139 display_exit(display);
5140 return;
5141 }
5142 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005143}
5144
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005145static void
5146log_handler(const char *format, va_list args)
5147{
5148 vfprintf(stderr, format, args);
5149}
5150
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005151struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005152display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005153{
5154 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005155
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005156 wl_log_set_handler_client(log_handler);
5157
Peter Huttererf3d62272013-08-08 11:57:05 +10005158 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005159 if (d == NULL)
5160 return NULL;
5161
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005162 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005163 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005164 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005165 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005166 return NULL;
5167 }
5168
Rob Bradford5ab9c752013-07-26 16:29:43 +01005169 d->xkb_context = xkb_context_new(0);
5170 if (d->xkb_context == NULL) {
5171 fprintf(stderr, "Failed to create XKB context\n");
5172 free(d);
5173 return NULL;
5174 }
5175
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005176 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005177 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005178 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005179 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5180 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005181
5182 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005183 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005184 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005185 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005186
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005187 d->workspace = 0;
5188 d->workspace_count = 1;
5189
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005190 d->registry = wl_display_get_registry(d->display);
5191 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005192
5193 if (wl_display_dispatch(d->display) < 0) {
5194 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5195 return NULL;
5196 }
5197
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005198#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005199 if (init_egl(d) < 0)
5200 fprintf(stderr, "EGL does not seem to work, "
5201 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005202#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005203
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005204 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005205
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005206 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005207
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005208 wl_list_init(&d->window_list);
5209
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005210 init_dummy_surface(d);
5211
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005212 return d;
5213}
5214
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005215static void
5216display_destroy_outputs(struct display *display)
5217{
5218 struct output *tmp;
5219 struct output *output;
5220
5221 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5222 output_destroy(output);
5223}
5224
Pekka Paalanene1207c72011-12-16 12:02:09 +02005225static void
5226display_destroy_inputs(struct display *display)
5227{
5228 struct input *tmp;
5229 struct input *input;
5230
5231 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5232 input_destroy(input);
5233}
5234
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005235void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005236display_destroy(struct display *display)
5237{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005238 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005239 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5240 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005241
5242 if (!wl_list_empty(&display->deferred_list))
5243 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5244
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005245 cairo_surface_destroy(display->dummy_surface);
5246 free(display->dummy_surface_data);
5247
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005248 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005249 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005250
Daniel Stone97f68542012-05-30 16:32:01 +01005251 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005252
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005253 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005254 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005255
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005256#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005257 if (display->argb_device)
5258 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005259#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005260
Pekka Paalanen35e82632013-04-25 13:57:48 +03005261 if (display->subcompositor)
5262 wl_subcompositor_destroy(display->subcompositor);
5263
Pekka Paalanenc2052982011-12-16 11:41:32 +02005264 if (display->shell)
5265 wl_shell_destroy(display->shell);
5266
5267 if (display->shm)
5268 wl_shm_destroy(display->shm);
5269
5270 if (display->data_device_manager)
5271 wl_data_device_manager_destroy(display->data_device_manager);
5272
5273 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005274 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005275
5276 close(display->epoll_fd);
5277
U. Artie Eoff44874d92012-10-02 21:12:35 -07005278 if (!(display->display_fd_events & EPOLLERR) &&
5279 !(display->display_fd_events & EPOLLHUP))
5280 wl_display_flush(display->display);
5281
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005282 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005283 free(display);
5284}
5285
5286void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005287display_set_user_data(struct display *display, void *data)
5288{
5289 display->user_data = data;
5290}
5291
5292void *
5293display_get_user_data(struct display *display)
5294{
5295 return display->user_data;
5296}
5297
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005298struct wl_display *
5299display_get_display(struct display *display)
5300{
5301 return display->display;
5302}
5303
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005304int
5305display_has_subcompositor(struct display *display)
5306{
5307 if (display->subcompositor)
5308 return 1;
5309
5310 wl_display_roundtrip(display->display);
5311
5312 return display->subcompositor != NULL;
5313}
5314
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005315cairo_device_t *
5316display_get_cairo_device(struct display *display)
5317{
5318 return display->argb_device;
5319}
5320
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005321struct output *
5322display_get_output(struct display *display)
5323{
5324 return container_of(display->output_list.next, struct output, link);
5325}
5326
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005327struct wl_compositor *
5328display_get_compositor(struct display *display)
5329{
5330 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005331}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005332
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005333uint32_t
5334display_get_serial(struct display *display)
5335{
5336 return display->serial;
5337}
5338
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005339EGLDisplay
5340display_get_egl_display(struct display *d)
5341{
5342 return d->dpy;
5343}
5344
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005345struct wl_data_source *
5346display_create_data_source(struct display *display)
5347{
5348 return wl_data_device_manager_create_data_source(display->data_device_manager);
5349}
5350
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005351EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005352display_get_argb_egl_config(struct display *d)
5353{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005354 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005355}
5356
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005357int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005358display_acquire_window_surface(struct display *display,
5359 struct window *window,
5360 EGLContext ctx)
5361{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005362 struct surface *surface = window->main_surface;
5363
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005364 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005365 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005366
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005367 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005368 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005369}
5370
5371void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005372display_release_window_surface(struct display *display,
5373 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005374{
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 Franzke0c991632011-09-27 21:57:31 +02005378 return;
5379
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005380 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005381}
5382
5383void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005384display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005385{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005386 wl_list_insert(&display->deferred_list, &task->link);
5387}
5388
5389void
5390display_watch_fd(struct display *display,
5391 int fd, uint32_t events, struct task *task)
5392{
5393 struct epoll_event ep;
5394
5395 ep.events = events;
5396 ep.data.ptr = task;
5397 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5398}
5399
5400void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005401display_unwatch_fd(struct display *display, int fd)
5402{
5403 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5404}
5405
5406void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005407display_run(struct display *display)
5408{
5409 struct task *task;
5410 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005411 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005412
Pekka Paalanen826d7952011-12-15 10:14:07 +02005413 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005414 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005415 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005416 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005417 struct task, link);
5418 wl_list_remove(&task->link);
5419 task->run(task, 0);
5420 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005421
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005422 wl_display_dispatch_pending(display->display);
5423
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005424 if (!display->running)
5425 break;
5426
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005427 ret = wl_display_flush(display->display);
5428 if (ret < 0 && errno == EAGAIN) {
5429 ep[0].events =
5430 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5431 ep[0].data.ptr = &display->display_task;
5432
5433 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5434 display->display_fd, &ep[0]);
5435 } else if (ret < 0) {
5436 break;
5437 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005438
5439 count = epoll_wait(display->epoll_fd,
5440 ep, ARRAY_LENGTH(ep), -1);
5441 for (i = 0; i < count; i++) {
5442 task = ep[i].data.ptr;
5443 task->run(task, ep[i].events);
5444 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005445 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005446}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005447
5448void
5449display_exit(struct display *display)
5450{
5451 display->running = 0;
5452}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005453
5454void
5455keysym_modifiers_add(struct wl_array *modifiers_map,
5456 const char *name)
5457{
5458 size_t len = strlen(name) + 1;
5459 char *p;
5460
5461 p = wl_array_add(modifiers_map, len);
5462
5463 if (p == NULL)
5464 return;
5465
5466 strncpy(p, name, len);
5467}
5468
5469static xkb_mod_index_t
5470keysym_modifiers_get_index(struct wl_array *modifiers_map,
5471 const char *name)
5472{
5473 xkb_mod_index_t index = 0;
5474 char *p = modifiers_map->data;
5475
5476 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5477 if (strcmp(p, name) == 0)
5478 return index;
5479
5480 index++;
5481 p += strlen(p) + 1;
5482 }
5483
5484 return XKB_MOD_INVALID;
5485}
5486
5487xkb_mod_mask_t
5488keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5489 const char *name)
5490{
5491 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5492
5493 if (index == XKB_MOD_INVALID)
5494 return XKB_MOD_INVALID;
5495
5496 return 1 << index;
5497}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005498
5499void *
5500fail_on_null(void *p)
5501{
5502 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005503 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005504 exit(EXIT_FAILURE);
5505 }
5506
5507 return p;
5508}
5509
5510void *
5511xmalloc(size_t s)
5512{
5513 return fail_on_null(malloc(s));
5514}
5515
Peter Huttererf3d62272013-08-08 11:57:05 +10005516void *
5517xzalloc(size_t s)
5518{
5519 return fail_on_null(zalloc(s));
5520}
5521
Kristian Høgsbergce278412013-07-25 15:20:20 -07005522char *
5523xstrdup(const char *s)
5524{
5525 return fail_on_null(strdup(s));
5526}