blob: 83af7ed5f04950f0fbca47ae05dd06efd29baeab [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;
Daniel Stone97f68542012-05-30 16:32:01 +0100323
324 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100325 struct xkb_keymap *keymap;
326 struct xkb_state *state;
327 xkb_mod_mask_t control_mask;
328 xkb_mod_mask_t alt_mask;
329 xkb_mod_mask_t shift_mask;
330 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400331
332 struct task repeat_task;
333 int repeat_timer_fd;
334 uint32_t repeat_sym;
335 uint32_t repeat_key;
336 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400337};
338
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500339struct output {
340 struct display *display;
341 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800342 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500343 struct rectangle allocation;
344 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600345 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200346 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200347
348 display_output_handler_t destroy_handler;
349 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500350};
351
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500352struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500353 struct widget *widget;
354 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500355 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500356};
357
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500358struct menu {
359 struct window *window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -0500360 struct window *parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500361 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500362 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700363 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500364 const char **entries;
365 uint32_t time;
366 int current;
367 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400368 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500369 menu_func_t func;
370};
371
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300372struct tooltip {
373 struct widget *parent;
374 struct window *window;
375 struct widget *widget;
376 char *entry;
377 struct task tooltip_task;
378 int tooltip_fd;
379 float x, y;
380};
381
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700382struct shm_pool {
383 struct wl_shm_pool *pool;
384 size_t size;
385 size_t used;
386 void *data;
387};
388
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400389enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300390 CURSOR_DEFAULT = 100,
391 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400392};
393
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500394enum window_location {
395 WINDOW_INTERIOR = 0,
396 WINDOW_RESIZING_TOP = 1,
397 WINDOW_RESIZING_BOTTOM = 2,
398 WINDOW_RESIZING_LEFT = 4,
399 WINDOW_RESIZING_TOP_LEFT = 5,
400 WINDOW_RESIZING_BOTTOM_LEFT = 6,
401 WINDOW_RESIZING_RIGHT = 8,
402 WINDOW_RESIZING_TOP_RIGHT = 9,
403 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
404 WINDOW_RESIZING_MASK = 15,
405 WINDOW_EXTERIOR = 16,
406 WINDOW_TITLEBAR = 17,
407 WINDOW_CLIENT_AREA = 18,
408};
409
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200410static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411
Pekka Paalanen97777442013-05-22 10:20:05 +0300412/* #define DEBUG */
413
414#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300415
416static void
417debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
418__attribute__ ((format (printf, 4, 5)));
419
420static void
421debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
422{
423 va_list ap;
424 struct timeval tv;
425
426 gettimeofday(&tv, NULL);
427 fprintf(stderr, "%8ld.%03ld ",
428 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
429
430 if (proxy)
431 fprintf(stderr, "%s@%d ",
432 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
433
434 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
435 fprintf(stderr, "%s ", func);
436
437 va_start(ap, fmt);
438 vfprintf(stderr, fmt, ap);
439 va_end(ap);
440}
441
442#define DBG(fmt, ...) \
443 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
444
445#define DBG_OBJ(obj, fmt, ...) \
446 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
447
448#else
449
450#define DBG(...) do {} while (0)
451#define DBG_OBJ(...) do {} while (0)
452
453#endif
454
Alexander Larsson1818e312013-05-22 14:41:31 +0200455static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200456surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200457{
458 int32_t tmp;
459
460 switch (buffer_transform) {
461 case WL_OUTPUT_TRANSFORM_90:
462 case WL_OUTPUT_TRANSFORM_270:
463 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
464 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
465 tmp = *width;
466 *width = *height;
467 *height = tmp;
468 break;
469 default:
470 break;
471 }
472
473 *width *= buffer_scale;
474 *height *= buffer_scale;
475}
476
477static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200478buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200479{
480 int32_t tmp;
481
482 switch (buffer_transform) {
483 case WL_OUTPUT_TRANSFORM_90:
484 case WL_OUTPUT_TRANSFORM_270:
485 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
486 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
487 tmp = *width;
488 *width = *height;
489 *height = tmp;
490 break;
491 default:
492 break;
493 }
494
495 *width /= buffer_scale;
496 *height /= buffer_scale;
497}
498
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500499#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200501struct egl_window_surface {
502 struct toysurface base;
503 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100504 struct display *display;
505 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200506 struct wl_egl_window *egl_window;
507 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100508};
509
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200510static struct egl_window_surface *
511to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100512{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200513 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100514}
515
516static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200518 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200519 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200521 struct egl_window_surface *surface = to_egl_window_surface(base);
522
Alexander Larsson1818e312013-05-22 14:41:31 +0200523 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
524
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200525 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
526 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
527
528 return cairo_surface_reference(surface->cairo_surface);
529}
530
531static void
532egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200533 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534 struct rectangle *server_allocation)
535{
536 struct egl_window_surface *surface = to_egl_window_surface(base);
537
538 cairo_gl_surface_swapbuffers(surface->cairo_surface);
539 wl_egl_window_get_attached_size(surface->egl_window,
540 &server_allocation->width,
541 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200542
543 buffer_to_surface_size (buffer_transform, buffer_scale,
544 &server_allocation->width,
545 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200546}
547
548static int
549egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
550{
551 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200552 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100553
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200554 device = cairo_surface_get_device(surface->cairo_surface);
555 if (!device)
556 return -1;
557
558 if (!ctx) {
559 if (device == surface->display->argb_device)
560 ctx = surface->display->argb_ctx;
561 else
562 assert(0);
563 }
564
565 cairo_device_flush(device);
566 cairo_device_acquire(device);
567 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
568 surface->egl_surface, ctx))
569 fprintf(stderr, "failed to make surface current\n");
570
571 return 0;
572}
573
574static void
575egl_window_surface_release(struct toysurface *base)
576{
577 struct egl_window_surface *surface = to_egl_window_surface(base);
578 cairo_device_t *device;
579
580 device = cairo_surface_get_device(surface->cairo_surface);
581 if (!device)
582 return;
583
584 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
585 surface->display->argb_ctx))
586 fprintf(stderr, "failed to make context current\n");
587
588 cairo_device_release(device);
589}
590
591static void
592egl_window_surface_destroy(struct toysurface *base)
593{
594 struct egl_window_surface *surface = to_egl_window_surface(base);
595 struct display *d = surface->display;
596
597 cairo_surface_destroy(surface->cairo_surface);
598 eglDestroySurface(d->dpy, surface->egl_surface);
599 wl_egl_window_destroy(surface->egl_window);
600 surface->surface = NULL;
601
602 free(surface);
603}
604
605static struct toysurface *
606egl_window_surface_create(struct display *display,
607 struct wl_surface *wl_surface,
608 uint32_t flags,
609 struct rectangle *rectangle)
610{
611 struct egl_window_surface *surface;
612
Pekka Paalanenb3627362012-11-19 17:16:00 +0200613 if (display->dpy == EGL_NO_DISPLAY)
614 return NULL;
615
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200616 surface = calloc(1, sizeof *surface);
617 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100618 return NULL;
619
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200620 surface->base.prepare = egl_window_surface_prepare;
621 surface->base.swap = egl_window_surface_swap;
622 surface->base.acquire = egl_window_surface_acquire;
623 surface->base.release = egl_window_surface_release;
624 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100625
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200626 surface->display = display;
627 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400628
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200629 surface->egl_window = wl_egl_window_create(surface->surface,
630 rectangle->width,
631 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100632
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200633 surface->egl_surface = eglCreateWindowSurface(display->dpy,
634 display->argb_config,
635 surface->egl_window,
636 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100637
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200638 surface->cairo_surface =
639 cairo_gl_surface_create_for_egl(display->argb_device,
640 surface->egl_surface,
641 rectangle->width,
642 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200644 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100645}
646
Pekka Paalanenb3627362012-11-19 17:16:00 +0200647#else
648
649static struct toysurface *
650egl_window_surface_create(struct display *display,
651 struct wl_surface *wl_surface,
652 uint32_t flags,
653 struct rectangle *rectangle)
654{
655 return NULL;
656}
657
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658#endif
659
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200660struct shm_surface_data {
661 struct wl_buffer *buffer;
662 struct shm_pool *pool;
663};
664
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400665struct wl_buffer *
666display_get_buffer_for_surface(struct display *display,
667 cairo_surface_t *surface)
668{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200669 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200671 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400672
673 return data->buffer;
674}
675
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500676static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700677shm_pool_destroy(struct shm_pool *pool);
678
679static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400680shm_surface_data_destroy(void *p)
681{
682 struct shm_surface_data *data = p;
683
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200684 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700685 if (data->pool)
686 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300687
688 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689}
690
Kristian Høgsberg16626282012-04-03 11:21:27 -0400691static struct wl_shm_pool *
692make_shm_pool(struct display *display, int size, void **data)
693{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400694 struct wl_shm_pool *pool;
695 int fd;
696
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300697 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400698 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300699 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
700 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400701 return NULL;
702 }
703
704 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400705 if (*data == MAP_FAILED) {
706 fprintf(stderr, "mmap failed: %m\n");
707 close(fd);
708 return NULL;
709 }
710
711 pool = wl_shm_create_pool(display->shm, fd, size);
712
713 close(fd);
714
715 return pool;
716}
717
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700718static struct shm_pool *
719shm_pool_create(struct display *display, size_t size)
720{
721 struct shm_pool *pool = malloc(sizeof *pool);
722
723 if (!pool)
724 return NULL;
725
726 pool->pool = make_shm_pool(display, size, &pool->data);
727 if (!pool->pool) {
728 free(pool);
729 return NULL;
730 }
731
732 pool->size = size;
733 pool->used = 0;
734
735 return pool;
736}
737
738static void *
739shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
740{
741 if (pool->used + size > pool->size)
742 return NULL;
743
744 *offset = pool->used;
745 pool->used += size;
746
747 return (char *) pool->data + *offset;
748}
749
750/* destroy the pool. this does not unmap the memory though */
751static void
752shm_pool_destroy(struct shm_pool *pool)
753{
754 munmap(pool->data, pool->size);
755 wl_shm_pool_destroy(pool->pool);
756 free(pool);
757}
758
759/* Start allocating from the beginning of the pool again */
760static void
761shm_pool_reset(struct shm_pool *pool)
762{
763 pool->used = 0;
764}
765
766static int
767data_length_for_shm_surface(struct rectangle *rect)
768{
769 int stride;
770
771 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
772 rect->width);
773 return stride * rect->height;
774}
775
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500776static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700777display_create_shm_surface_from_pool(struct display *display,
778 struct rectangle *rectangle,
779 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400780{
781 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400782 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400783 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200784 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700785 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400786 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400787
788 data = malloc(sizeof *data);
789 if (data == NULL)
790 return NULL;
791
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200792 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
793 cairo_format = CAIRO_FORMAT_RGB16_565;
794 else
795 cairo_format = CAIRO_FORMAT_ARGB32;
796
797 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700798 length = stride * rectangle->height;
799 data->pool = NULL;
800 map = shm_pool_allocate(pool, length, &offset);
801
802 if (!map) {
803 free(data);
804 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400805 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400806
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400807 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200808 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400809 rectangle->width,
810 rectangle->height,
811 stride);
812
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200813 cairo_surface_set_user_data(surface, &shm_surface_data_key,
814 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400815
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200816 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
817 format = WL_SHM_FORMAT_RGB565;
818 else {
819 if (flags & SURFACE_OPAQUE)
820 format = WL_SHM_FORMAT_XRGB8888;
821 else
822 format = WL_SHM_FORMAT_ARGB8888;
823 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400824
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200825 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
826 rectangle->width,
827 rectangle->height,
828 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400829
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700830 return surface;
831}
832
833static cairo_surface_t *
834display_create_shm_surface(struct display *display,
835 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200836 struct shm_pool *alternate_pool,
837 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700838{
839 struct shm_surface_data *data;
840 struct shm_pool *pool;
841 cairo_surface_t *surface;
842
Pekka Paalanen99436862012-11-19 17:15:59 +0200843 if (alternate_pool) {
844 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700845 surface = display_create_shm_surface_from_pool(display,
846 rectangle,
847 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200848 alternate_pool);
849 if (surface) {
850 data = cairo_surface_get_user_data(surface,
851 &shm_surface_data_key);
852 goto out;
853 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700854 }
855
856 pool = shm_pool_create(display,
857 data_length_for_shm_surface(rectangle));
858 if (!pool)
859 return NULL;
860
861 surface =
862 display_create_shm_surface_from_pool(display, rectangle,
863 flags, pool);
864
865 if (!surface) {
866 shm_pool_destroy(pool);
867 return NULL;
868 }
869
870 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200871 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700872 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400873
Pekka Paalanen99436862012-11-19 17:15:59 +0200874out:
875 if (data_ret)
876 *data_ret = data;
877
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400878 return surface;
879}
880
nobled7b87cb02011-02-01 18:51:47 +0000881static int
882check_size(struct rectangle *rect)
883{
884 if (rect->width && rect->height)
885 return 0;
886
887 fprintf(stderr, "tried to create surface of "
888 "width: %d, height: %d\n", rect->width, rect->height);
889 return -1;
890}
891
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400892cairo_surface_t *
893display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100894 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400895 struct rectangle *rectangle,
896 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400897{
nobled7b87cb02011-02-01 18:51:47 +0000898 if (check_size(rectangle) < 0)
899 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200900
901 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200902 return display_create_shm_surface(display, rectangle, flags,
903 NULL, NULL);
904}
905
Pekka Paalanena4eda732012-11-19 17:16:02 +0200906struct shm_surface_leaf {
907 cairo_surface_t *cairo_surface;
908 /* 'data' is automatically destroyed, when 'cairo_surface' is */
909 struct shm_surface_data *data;
910
911 struct shm_pool *resize_pool;
912 int busy;
913};
914
915static void
916shm_surface_leaf_release(struct shm_surface_leaf *leaf)
917{
918 if (leaf->cairo_surface)
919 cairo_surface_destroy(leaf->cairo_surface);
920 /* leaf->data already destroyed via cairo private */
921
922 if (leaf->resize_pool)
923 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200924
925 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200926}
927
Pekka Paalanenaef02542013-04-25 13:57:47 +0300928#define MAX_LEAVES 3
929
Pekka Paalanen99436862012-11-19 17:15:59 +0200930struct shm_surface {
931 struct toysurface base;
932 struct display *display;
933 struct wl_surface *surface;
934 uint32_t flags;
935 int dx, dy;
936
Pekka Paalanenaef02542013-04-25 13:57:47 +0300937 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200939};
940
941static struct shm_surface *
942to_shm_surface(struct toysurface *base)
943{
944 return container_of(base, struct shm_surface, base);
945}
946
Pekka Paalanena4eda732012-11-19 17:16:02 +0200947static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300948shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
949{
950#ifdef DEBUG
951 struct shm_surface_leaf *leaf;
952 char bufs[MAX_LEAVES + 1];
953 int i;
954
955 for (i = 0; i < MAX_LEAVES; i++) {
956 leaf = &surface->leaf[i];
957
958 if (leaf->busy)
959 bufs[i] = 'b';
960 else if (leaf->cairo_surface)
961 bufs[i] = 'a';
962 else
963 bufs[i] = ' ';
964 }
965
966 bufs[MAX_LEAVES] = '\0';
967 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
968#endif
969}
970
971static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200972shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
973{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200974 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300975 struct shm_surface_leaf *leaf;
976 int i;
977 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300978
979 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200980
Pekka Paalanenaef02542013-04-25 13:57:47 +0300981 for (i = 0; i < MAX_LEAVES; i++) {
982 leaf = &surface->leaf[i];
983 if (leaf->data && leaf->data->buffer == buffer) {
984 leaf->busy = 0;
985 break;
986 }
987 }
988 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200989
Pekka Paalanenaef02542013-04-25 13:57:47 +0300990 /* Leave one free leaf with storage, release others */
991 free_found = 0;
992 for (i = 0; i < MAX_LEAVES; i++) {
993 leaf = &surface->leaf[i];
994
995 if (!leaf->cairo_surface || leaf->busy)
996 continue;
997
998 if (!free_found)
999 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001000 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001001 shm_surface_leaf_release(leaf);
1002 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001003
Pekka Paalanen97777442013-05-22 10:20:05 +03001004 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001005}
1006
1007static const struct wl_buffer_listener shm_surface_buffer_listener = {
1008 shm_surface_buffer_release
1009};
1010
Pekka Paalanen99436862012-11-19 17:15:59 +02001011static cairo_surface_t *
1012shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001013 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001014 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001015{
Pekka Paalanenec076692012-11-30 13:37:27 +02001016 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001017 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001018 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001019 struct shm_surface_leaf *leaf = NULL;
1020 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001021
1022 surface->dx = dx;
1023 surface->dy = dy;
1024
Pekka Paalanenaef02542013-04-25 13:57:47 +03001025 /* pick a free buffer, preferrably one that already has storage */
1026 for (i = 0; i < MAX_LEAVES; i++) {
1027 if (surface->leaf[i].busy)
1028 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001029
Pekka Paalanenaef02542013-04-25 13:57:47 +03001030 if (!leaf || surface->leaf[i].cairo_surface)
1031 leaf = &surface->leaf[i];
1032 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001033 DBG_OBJ(surface->surface, "pick leaf %d\n",
1034 (int)(leaf - &surface->leaf[0]));
1035
Pekka Paalanenaef02542013-04-25 13:57:47 +03001036 if (!leaf) {
1037 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001038 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001039 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001040 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001041 }
1042
Pekka Paalanena4eda732012-11-19 17:16:02 +02001043 if (!resize_hint && leaf->resize_pool) {
1044 cairo_surface_destroy(leaf->cairo_surface);
1045 leaf->cairo_surface = NULL;
1046 shm_pool_destroy(leaf->resize_pool);
1047 leaf->resize_pool = NULL;
1048 }
1049
Alexander Larsson1818e312013-05-22 14:41:31 +02001050 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1051
Pekka Paalanena4eda732012-11-19 17:16:02 +02001052 if (leaf->cairo_surface &&
1053 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1054 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001055 goto out;
1056
Pekka Paalanena4eda732012-11-19 17:16:02 +02001057 if (leaf->cairo_surface)
1058 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001059
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001060#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001061 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001062 /* Create a big pool to allocate from, while continuously
1063 * resizing. Mmapping a new pool in the server
1064 * is relatively expensive, so reusing a pool performs
1065 * better, but may temporarily reserve unneeded memory.
1066 */
1067 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001068 leaf->resize_pool = shm_pool_create(surface->display,
1069 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001070 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001071#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001072
Alexander Larsson1818e312013-05-22 14:41:31 +02001073 rect.width = width;
1074 rect.height = height;
1075
Pekka Paalanena4eda732012-11-19 17:16:02 +02001076 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001077 display_create_shm_surface(surface->display, &rect,
1078 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 leaf->resize_pool,
1080 &leaf->data);
1081 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001082 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001083
1084out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001085 surface->current = leaf;
1086
1087 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001088}
1089
1090static void
1091shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001092 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001093 struct rectangle *server_allocation)
1094{
1095 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001097
1098 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001099 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001100 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001101 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001102
Alexander Larsson1818e312013-05-22 14:41:31 +02001103 buffer_to_surface_size (buffer_transform, buffer_scale,
1104 &server_allocation->width,
1105 &server_allocation->height);
1106
Pekka Paalanena4eda732012-11-19 17:16:02 +02001107 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001108 surface->dx, surface->dy);
1109 wl_surface_damage(surface->surface, 0, 0,
1110 server_allocation->width, server_allocation->height);
1111 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001112
Pekka Paalanen71233882013-04-25 13:57:53 +03001113 DBG_OBJ(surface->surface, "leaf %d busy\n",
1114 (int)(leaf - &surface->leaf[0]));
1115
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 leaf->busy = 1;
1117 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001118}
1119
1120static int
1121shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1122{
1123 return -1;
1124}
1125
1126static void
1127shm_surface_release(struct toysurface *base)
1128{
1129}
1130
1131static void
1132shm_surface_destroy(struct toysurface *base)
1133{
1134 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001135 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001136
Pekka Paalanenaef02542013-04-25 13:57:47 +03001137 for (i = 0; i < MAX_LEAVES; i++)
1138 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001139
1140 free(surface);
1141}
1142
1143static struct toysurface *
1144shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1145 uint32_t flags, struct rectangle *rectangle)
1146{
1147 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001148 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001149
Brian Lovinbc919262013-08-07 15:34:59 -07001150 surface = xmalloc(sizeof *surface);
1151 memset(surface, 0, sizeof *surface);
1152
Pekka Paalanen99436862012-11-19 17:15:59 +02001153 if (!surface)
1154 return NULL;
1155
1156 surface->base.prepare = shm_surface_prepare;
1157 surface->base.swap = shm_surface_swap;
1158 surface->base.acquire = shm_surface_acquire;
1159 surface->base.release = shm_surface_release;
1160 surface->base.destroy = shm_surface_destroy;
1161
1162 surface->display = display;
1163 surface->surface = wl_surface;
1164 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001165
1166 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001167}
1168
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001169/*
1170 * The following correspondences between file names and cursors was copied
1171 * from: https://bugs.kde.org/attachment.cgi?id=67313
1172 */
1173
1174static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001175 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001176 "sw-resize",
1177 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001178};
1179
1180static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001181 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001182 "se-resize",
1183 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001184};
1185
1186static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001187 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001188 "s-resize",
1189 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001190};
1191
1192static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001193 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001194 "closedhand",
1195 "208530c400c041818281048008011002"
1196};
1197
1198static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001199 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001200 "default",
1201 "top_left_arrow",
1202 "left-arrow"
1203};
1204
1205static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001206 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001207 "w-resize",
1208 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001209};
1210
1211static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001212 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001213 "e-resize",
1214 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215};
1216
1217static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001218 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001219 "nw-resize",
1220 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001221};
1222
1223static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001224 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001225 "ne-resize",
1226 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001227};
1228
1229static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001230 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001231 "n-resize",
1232 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001233};
1234
1235static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001236 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001237 "ibeam",
1238 "text"
1239};
1240
1241static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001242 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001243 "pointer",
1244 "pointing_hand",
1245 "e29285e634086352946a0e7090d73106"
1246};
1247
1248static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001249 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001250 "wait",
1251 "0426c94ea35c87780ff01dc239897213"
1252};
1253
1254struct cursor_alternatives {
1255 const char **names;
1256 size_t count;
1257};
1258
1259static const struct cursor_alternatives cursors[] = {
1260 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1261 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1262 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1263 {grabbings, ARRAY_LENGTH(grabbings)},
1264 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1265 {left_sides, ARRAY_LENGTH(left_sides)},
1266 {right_sides, ARRAY_LENGTH(right_sides)},
1267 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1268 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1269 {top_sides, ARRAY_LENGTH(top_sides)},
1270 {xterms, ARRAY_LENGTH(xterms)},
1271 {hand1s, ARRAY_LENGTH(hand1s)},
1272 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001273};
1274
1275static void
1276create_cursors(struct display *display)
1277{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001278 struct weston_config *config;
1279 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001280 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001281 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001282 unsigned int i, j;
1283 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001284
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001285 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001286 s = weston_config_get_section(config, "shell", NULL, NULL);
1287 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1288 weston_config_section_get_int(s, "cursor-size", &size, 32);
1289 weston_config_destroy(config);
1290
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001291 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001292 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001293 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001294 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001295
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001296 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001297 cursor = NULL;
1298 for (j = 0; !cursor && j < cursors[i].count; ++j)
1299 cursor = wl_cursor_theme_get_cursor(
1300 display->cursor_theme, cursors[i].names[j]);
1301
1302 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001303 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001304 cursors[i].names[0]);
1305
1306 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001307 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001308}
1309
1310static void
1311destroy_cursors(struct display *display)
1312{
1313 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001314 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001315}
1316
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001317struct wl_cursor_image *
1318display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001319{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001320 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001321
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001322 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001323}
1324
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001325static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001326surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001327{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001328 if (!surface->cairo_surface)
1329 return;
1330
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001331 if (surface->opaque_region) {
1332 wl_surface_set_opaque_region(surface->surface,
1333 surface->opaque_region);
1334 wl_region_destroy(surface->opaque_region);
1335 surface->opaque_region = NULL;
1336 }
1337
1338 if (surface->input_region) {
1339 wl_surface_set_input_region(surface->surface,
1340 surface->input_region);
1341 wl_region_destroy(surface->input_region);
1342 surface->input_region = NULL;
1343 }
1344
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001345 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001346 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001347 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001348
Pekka Paalanen89dee002013-02-13 16:17:20 +02001349 cairo_surface_destroy(surface->cairo_surface);
1350 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001351}
1352
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001353int
1354window_has_focus(struct window *window)
1355{
1356 return window->focus_count > 0;
1357}
1358
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001359static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001360window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001361{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001362 struct surface *surface;
1363
Pekka Paalanen89dee002013-02-13 16:17:20 +02001364 if (window->type == TYPE_NONE) {
1365 window->type = TYPE_TOPLEVEL;
1366 if (window->shell_surface)
1367 wl_shell_surface_set_toplevel(window->shell_surface);
1368 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001369
Pekka Paalanen35e82632013-04-25 13:57:48 +03001370 wl_list_for_each(surface, &window->subsurface_list, link) {
1371 if (surface == window->main_surface)
1372 continue;
1373
1374 surface_flush(surface);
1375 }
1376
Pekka Paalanen89dee002013-02-13 16:17:20 +02001377 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001378}
1379
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001380struct display *
1381window_get_display(struct window *window)
1382{
1383 return window->display;
1384}
1385
Pekka Paalanen89dee002013-02-13 16:17:20 +02001386static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001387surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001388{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001389 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001390 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001391
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001392 if (!surface->toysurface && display->dpy &&
1393 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001394 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001395 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001396 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001397 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001398 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001399 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001400
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001401 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001402 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001403 surface->surface,
1404 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001405
Pekka Paalanen89dee002013-02-13 16:17:20 +02001406 surface->cairo_surface = surface->toysurface->prepare(
1407 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001408 allocation.width, allocation.height, flags,
1409 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001410}
1411
1412static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001413window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001414{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001415 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001416 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001417 int dx = 0;
1418 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001419
Pekka Paalanenec076692012-11-30 13:37:27 +02001420 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001421 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001422
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001423 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1424 flags |= SURFACE_HINT_RGB565;
1425
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001426 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1427 dx = surface->server_allocation.width -
1428 surface->allocation.width;
1429
1430 if (window->resize_edges & WINDOW_RESIZING_TOP)
1431 dy = surface->server_allocation.height -
1432 surface->allocation.height;
1433
1434 window->resize_edges = 0;
1435
Pekka Paalanen89dee002013-02-13 16:17:20 +02001436 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001437}
1438
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001439int
1440window_get_buffer_transform(struct window *window)
1441{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001442 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001443}
1444
1445void
1446window_set_buffer_transform(struct window *window,
1447 enum wl_output_transform transform)
1448{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001449 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001450 wl_surface_set_buffer_transform(window->main_surface->surface,
1451 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001452}
1453
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001454void
1455window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001456 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001457{
1458 window->main_surface->buffer_scale = scale;
1459 wl_surface_set_buffer_scale(window->main_surface->surface,
1460 scale);
1461}
1462
1463uint32_t
1464window_get_buffer_scale(struct window *window)
1465{
1466 return window->main_surface->buffer_scale;
1467}
1468
Alexander Larssond68f5232013-05-22 14:41:33 +02001469uint32_t
1470window_get_output_scale(struct window *window)
1471{
1472 struct window_output *window_output;
1473 struct window_output *window_output_tmp;
1474 int scale = 1;
1475
1476 wl_list_for_each_safe(window_output, window_output_tmp,
1477 &window->window_output_list, link) {
1478 if (window_output->output->scale > scale)
1479 scale = window_output->output->scale;
1480 }
1481
1482 return scale;
1483}
1484
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001485static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001486
Pekka Paalanen4e373742013-02-13 16:17:13 +02001487static void
1488surface_destroy(struct surface *surface)
1489{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001490 if (surface->frame_cb)
1491 wl_callback_destroy(surface->frame_cb);
1492
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001493 if (surface->input_region)
1494 wl_region_destroy(surface->input_region);
1495
1496 if (surface->opaque_region)
1497 wl_region_destroy(surface->opaque_region);
1498
Pekka Paalanen35e82632013-04-25 13:57:48 +03001499 if (surface->subsurface)
1500 wl_subsurface_destroy(surface->subsurface);
1501
Pekka Paalanen4e373742013-02-13 16:17:13 +02001502 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001503
1504 if (surface->toysurface)
1505 surface->toysurface->destroy(surface->toysurface);
1506
Pekka Paalanen35e82632013-04-25 13:57:48 +03001507 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001508 free(surface);
1509}
1510
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001511void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001512window_destroy(struct window *window)
1513{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001514 struct display *display = window->display;
1515 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001516 struct window_output *window_output;
1517 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001518
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001519 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001520
Rusty Lynch1084da52013-08-15 09:10:08 -07001521 wl_list_for_each(input, &display->input_list, link) {
1522 if (input->touch_focus == window)
1523 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001524 if (input->pointer_focus == window)
1525 input->pointer_focus = NULL;
1526 if (input->keyboard_focus == window)
1527 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001528 if (input->focus_widget &&
1529 input->focus_widget->window == window)
1530 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001531 }
1532
Rob Bradford7507b572012-05-15 17:55:34 +01001533 wl_list_for_each_safe(window_output, window_output_tmp,
1534 &window->window_output_list, link) {
1535 free (window_output);
1536 }
1537
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001538 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001539 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001540
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001541 if (window->shell_surface)
1542 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001543
1544 surface_destroy(window->main_surface);
1545
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001546 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001547
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001548 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001549 free(window);
1550}
1551
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001552static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001553widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001554{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001555 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001556
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001557 wl_list_for_each(child, &widget->child_list, link) {
1558 target = widget_find_widget(child, x, y);
1559 if (target)
1560 return target;
1561 }
1562
1563 if (widget->allocation.x <= x &&
1564 x < widget->allocation.x + widget->allocation.width &&
1565 widget->allocation.y <= y &&
1566 y < widget->allocation.y + widget->allocation.height) {
1567 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001568 }
1569
1570 return NULL;
1571}
1572
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001573static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001574window_find_widget(struct window *window, int32_t x, int32_t y)
1575{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001576 struct surface *surface;
1577 struct widget *widget;
1578
1579 wl_list_for_each(surface, &window->subsurface_list, link) {
1580 widget = widget_find_widget(surface->widget, x, y);
1581 if (widget)
1582 return widget;
1583 }
1584
1585 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001586}
1587
1588static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001589widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001590{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001591 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001592
Peter Huttererf3d62272013-08-08 11:57:05 +10001593 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001594 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001595 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001596 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001597 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001598 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001599 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001600 widget->tooltip = NULL;
1601 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001602 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001603
1604 return widget;
1605}
1606
1607struct widget *
1608window_add_widget(struct window *window, void *data)
1609{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001610 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001611
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001612 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001613 wl_list_init(&widget->link);
1614 window->main_surface->widget = widget;
1615
1616 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001617}
1618
1619struct widget *
1620widget_add_widget(struct widget *parent, void *data)
1621{
1622 struct widget *widget;
1623
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001624 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001625 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001626
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001627 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001628}
1629
1630void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001631widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001632{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001633 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001634 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001635 struct input *input;
1636
Pekka Paalanen35e82632013-04-25 13:57:48 +03001637 /* Destroy the sub-surface along with the root widget */
1638 if (surface->widget == widget && surface->subsurface)
1639 surface_destroy(widget->surface);
1640
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001641 if (widget->tooltip) {
1642 free(widget->tooltip);
1643 widget->tooltip = NULL;
1644 }
1645
Pekka Paalanene156fb62012-01-19 13:51:38 +02001646 wl_list_for_each(input, &display->input_list, link) {
1647 if (input->focus_widget == widget)
1648 input->focus_widget = NULL;
1649 }
1650
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001651 wl_list_remove(&widget->link);
1652 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001653}
1654
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001655void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001656widget_set_default_cursor(struct widget *widget, int cursor)
1657{
1658 widget->default_cursor = cursor;
1659}
1660
1661void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001662widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001664 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001665}
1666
1667void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001668widget_set_size(struct widget *widget, int32_t width, int32_t height)
1669{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001670 widget->allocation.width = width;
1671 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001672}
1673
1674void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001675widget_set_allocation(struct widget *widget,
1676 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001677{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001678 widget->allocation.x = x;
1679 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001680 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001681}
1682
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001683void
1684widget_set_transparent(struct widget *widget, int transparent)
1685{
1686 widget->opaque = !transparent;
1687}
1688
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001689void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001690widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001691{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001692 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001693}
1694
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001695static cairo_surface_t *
1696widget_get_cairo_surface(struct widget *widget)
1697{
1698 struct surface *surface = widget->surface;
1699 struct window *window = widget->window;
1700
1701 if (!surface->cairo_surface) {
1702 if (surface == window->main_surface)
1703 window_create_main_surface(window);
1704 else
1705 surface_create_surface(surface, 0, 0, 0);
1706 }
1707
1708 return surface->cairo_surface;
1709}
1710
Alexander Larsson15901f02013-05-22 14:41:25 +02001711static void
1712widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1713{
1714 struct surface *surface = widget->surface;
1715 double angle;
1716 cairo_matrix_t m;
1717 enum wl_output_transform transform;
1718 int surface_width, surface_height;
1719 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001720 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001721
1722 surface_width = surface->allocation.width;
1723 surface_height = surface->allocation.height;
1724
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001725 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001726 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001727
Alexander Larsson15901f02013-05-22 14:41:25 +02001728 switch (transform) {
1729 case WL_OUTPUT_TRANSFORM_FLIPPED:
1730 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1731 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1732 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1733 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1734 break;
1735 default:
1736 cairo_matrix_init_identity(&m);
1737 break;
1738 }
1739
1740 switch (transform) {
1741 case WL_OUTPUT_TRANSFORM_NORMAL:
1742 default:
1743 angle = 0;
1744 translate_x = 0;
1745 translate_y = 0;
1746 break;
1747 case WL_OUTPUT_TRANSFORM_FLIPPED:
1748 angle = 0;
1749 translate_x = surface_width;
1750 translate_y = 0;
1751 break;
1752 case WL_OUTPUT_TRANSFORM_90:
1753 angle = M_PI_2;
1754 translate_x = surface_height;
1755 translate_y = 0;
1756 break;
1757 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1758 angle = M_PI_2;
1759 translate_x = surface_height;
1760 translate_y = surface_width;
1761 break;
1762 case WL_OUTPUT_TRANSFORM_180:
1763 angle = M_PI;
1764 translate_x = surface_width;
1765 translate_y = surface_height;
1766 break;
1767 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1768 angle = M_PI;
1769 translate_x = 0;
1770 translate_y = surface_height;
1771 break;
1772 case WL_OUTPUT_TRANSFORM_270:
1773 angle = M_PI + M_PI_2;
1774 translate_x = 0;
1775 translate_y = surface_width;
1776 break;
1777 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1778 angle = M_PI + M_PI_2;
1779 translate_x = 0;
1780 translate_y = 0;
1781 break;
1782 }
1783
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001784 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001785 cairo_translate(cr, translate_x, translate_y);
1786 cairo_rotate(cr, angle);
1787 cairo_transform(cr, &m);
1788}
1789
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001790cairo_t *
1791widget_cairo_create(struct widget *widget)
1792{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001793 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001794 cairo_surface_t *cairo_surface;
1795 cairo_t *cr;
1796
1797 cairo_surface = widget_get_cairo_surface(widget);
1798 cr = cairo_create(cairo_surface);
1799
Alexander Larsson15901f02013-05-22 14:41:25 +02001800 widget_cairo_update_transform(widget, cr);
1801
Pekka Paalanen35e82632013-04-25 13:57:48 +03001802 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1803
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001804 return cr;
1805}
1806
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001807struct wl_surface *
1808widget_get_wl_surface(struct widget *widget)
1809{
1810 return widget->surface->surface;
1811}
1812
1813uint32_t
1814widget_get_last_time(struct widget *widget)
1815{
1816 return widget->surface->last_time;
1817}
1818
1819void
1820widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1821{
1822 struct wl_compositor *comp = widget->window->display->compositor;
1823 struct surface *surface = widget->surface;
1824
1825 if (!surface->input_region)
1826 surface->input_region = wl_compositor_create_region(comp);
1827
1828 if (rect) {
1829 wl_region_add(surface->input_region,
1830 rect->x, rect->y, rect->width, rect->height);
1831 }
1832}
1833
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001834void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001835widget_set_resize_handler(struct widget *widget,
1836 widget_resize_handler_t handler)
1837{
1838 widget->resize_handler = handler;
1839}
1840
1841void
1842widget_set_redraw_handler(struct widget *widget,
1843 widget_redraw_handler_t handler)
1844{
1845 widget->redraw_handler = handler;
1846}
1847
1848void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001849widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001850{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001851 widget->enter_handler = handler;
1852}
1853
1854void
1855widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1856{
1857 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001858}
1859
1860void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001861widget_set_motion_handler(struct widget *widget,
1862 widget_motion_handler_t handler)
1863{
1864 widget->motion_handler = handler;
1865}
1866
1867void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001868widget_set_button_handler(struct widget *widget,
1869 widget_button_handler_t handler)
1870{
1871 widget->button_handler = handler;
1872}
1873
1874void
Rusty Lynch041815a2013-08-08 21:20:38 -07001875widget_set_touch_up_handler(struct widget *widget,
1876 widget_touch_up_handler_t handler)
1877{
1878 widget->touch_up_handler = handler;
1879}
1880
1881void
1882widget_set_touch_down_handler(struct widget *widget,
1883 widget_touch_down_handler_t handler)
1884{
1885 widget->touch_down_handler = handler;
1886}
1887
1888void
1889widget_set_touch_motion_handler(struct widget *widget,
1890 widget_touch_motion_handler_t handler)
1891{
1892 widget->touch_motion_handler = handler;
1893}
1894
1895void
1896widget_set_touch_frame_handler(struct widget *widget,
1897 widget_touch_frame_handler_t handler)
1898{
1899 widget->touch_frame_handler = handler;
1900}
1901
1902void
1903widget_set_touch_cancel_handler(struct widget *widget,
1904 widget_touch_cancel_handler_t handler)
1905{
1906 widget->touch_cancel_handler = handler;
1907}
1908
1909void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001910widget_set_axis_handler(struct widget *widget,
1911 widget_axis_handler_t handler)
1912{
1913 widget->axis_handler = handler;
1914}
1915
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001916static void
1917window_schedule_redraw_task(struct window *window);
1918
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001919void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001920widget_schedule_redraw(struct widget *widget)
1921{
Pekka Paalanen71233882013-04-25 13:57:53 +03001922 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001923 widget->surface->redraw_needed = 1;
1924 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001925}
1926
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001927cairo_surface_t *
1928window_get_surface(struct window *window)
1929{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001930 cairo_surface_t *cairo_surface;
1931
1932 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1933
1934 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001935}
1936
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001937struct wl_surface *
1938window_get_wl_surface(struct window *window)
1939{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001940 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001941}
1942
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001943static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001944tooltip_redraw_handler(struct widget *widget, void *data)
1945{
1946 cairo_t *cr;
1947 const int32_t r = 3;
1948 struct tooltip *tooltip = data;
1949 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001950
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001951 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001952 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1953 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1954 cairo_paint(cr);
1955
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001956 width = widget->allocation.width;
1957 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001958 rounded_rect(cr, 0, 0, width, height, r);
1959
1960 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1961 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1962 cairo_fill(cr);
1963
1964 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1965 cairo_move_to(cr, 10, 16);
1966 cairo_show_text(cr, tooltip->entry);
1967 cairo_destroy(cr);
1968}
1969
1970static cairo_text_extents_t
1971get_text_extents(struct tooltip *tooltip)
1972{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001973 cairo_t *cr;
1974 cairo_text_extents_t extents;
1975
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001976 /* Use the dummy_surface because tooltip's surface was not
1977 * created yet, and parent does not have a valid surface
1978 * outside repaint, either.
1979 */
1980 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001981 cairo_text_extents(cr, tooltip->entry, &extents);
1982 cairo_destroy(cr);
1983
1984 return extents;
1985}
1986
1987static int
1988window_create_tooltip(struct tooltip *tooltip)
1989{
1990 struct widget *parent = tooltip->parent;
1991 struct display *display = parent->window->display;
1992 struct window *window;
1993 const int offset_y = 27;
1994 const int margin = 3;
1995 cairo_text_extents_t extents;
1996
1997 if (tooltip->widget)
1998 return 0;
1999
2000 window = window_create_transient(display, parent->window, tooltip->x,
2001 tooltip->y + offset_y,
2002 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2003 if (!window)
2004 return -1;
2005
2006 tooltip->window = window;
2007 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2008
2009 extents = get_text_extents(tooltip);
2010 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2011 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2012
2013 return 0;
2014}
2015
2016void
2017widget_destroy_tooltip(struct widget *parent)
2018{
2019 struct tooltip *tooltip = parent->tooltip;
2020
2021 parent->tooltip_count = 0;
2022 if (!tooltip)
2023 return;
2024
2025 if (tooltip->widget) {
2026 widget_destroy(tooltip->widget);
2027 window_destroy(tooltip->window);
2028 tooltip->widget = NULL;
2029 tooltip->window = NULL;
2030 }
2031
2032 close(tooltip->tooltip_fd);
2033 free(tooltip->entry);
2034 free(tooltip);
2035 parent->tooltip = NULL;
2036}
2037
2038static void
2039tooltip_func(struct task *task, uint32_t events)
2040{
2041 struct tooltip *tooltip =
2042 container_of(task, struct tooltip, tooltip_task);
2043 uint64_t exp;
2044
Martin Olsson8df662a2012-07-08 03:03:47 +02002045 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2046 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002047 window_create_tooltip(tooltip);
2048}
2049
2050#define TOOLTIP_TIMEOUT 500
2051static int
2052tooltip_timer_reset(struct tooltip *tooltip)
2053{
2054 struct itimerspec its;
2055
2056 its.it_interval.tv_sec = 0;
2057 its.it_interval.tv_nsec = 0;
2058 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2059 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2060 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2061 fprintf(stderr, "could not set timerfd\n: %m");
2062 return -1;
2063 }
2064
2065 return 0;
2066}
2067
2068int
2069widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2070{
2071 struct tooltip *tooltip = parent->tooltip;
2072
2073 parent->tooltip_count++;
2074 if (tooltip) {
2075 tooltip->x = x;
2076 tooltip->y = y;
2077 tooltip_timer_reset(tooltip);
2078 return 0;
2079 }
2080
2081 /* the handler might be triggered too fast via input device motion, so
2082 * we need this check here to make sure tooltip is fully initialized */
2083 if (parent->tooltip_count > 1)
2084 return 0;
2085
2086 tooltip = malloc(sizeof *tooltip);
2087 if (!tooltip)
2088 return -1;
2089
2090 parent->tooltip = tooltip;
2091 tooltip->parent = parent;
2092 tooltip->widget = NULL;
2093 tooltip->window = NULL;
2094 tooltip->x = x;
2095 tooltip->y = y;
2096 tooltip->entry = strdup(entry);
2097 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2098 if (tooltip->tooltip_fd < 0) {
2099 fprintf(stderr, "could not create timerfd\n: %m");
2100 return -1;
2101 }
2102
2103 tooltip->tooltip_task.run = tooltip_func;
2104 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2105 EPOLLIN, &tooltip->tooltip_task);
2106 tooltip_timer_reset(tooltip);
2107
2108 return 0;
2109}
2110
2111static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002112workspace_manager_state(void *data,
2113 struct workspace_manager *workspace_manager,
2114 uint32_t current,
2115 uint32_t count)
2116{
2117 struct display *display = data;
2118
2119 display->workspace = current;
2120 display->workspace_count = count;
2121}
2122
2123static const struct workspace_manager_listener workspace_manager_listener = {
2124 workspace_manager_state
2125};
2126
2127static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002128frame_resize_handler(struct widget *widget,
2129 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002130{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002131 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002132 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002133 struct rectangle interior;
2134 struct rectangle input;
2135 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002136
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002137 if (widget->window->type == TYPE_FULLSCREEN) {
2138 interior.x = 0;
2139 interior.y = 0;
2140 interior.width = width;
2141 interior.height = height;
2142 } else {
2143 if (widget->window->type == TYPE_MAXIMIZED) {
2144 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2145 } else {
2146 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2147 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002148
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002149 frame_resize(frame->frame, width, height);
2150 frame_interior(frame->frame, &interior.x, &interior.y,
2151 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002152 }
2153
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002154 widget_set_allocation(child, interior.x, interior.y,
2155 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002156
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002157 if (child->resize_handler) {
2158 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002159 child->user_data);
2160
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002161 if (widget->window->type == TYPE_FULLSCREEN) {
2162 width = child->allocation.width;
2163 height = child->allocation.height;
2164 } else {
2165 frame_resize_inside(frame->frame,
2166 child->allocation.width,
2167 child->allocation.height);
2168 width = frame_width(frame->frame);
2169 height = frame_height(frame->frame);
2170 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002171 }
2172
Scott Moreauf7e498c2012-05-14 11:39:29 -06002173 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002174
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002175 widget->surface->input_region =
2176 wl_compositor_create_region(widget->window->display->compositor);
2177 if (widget->window->type != TYPE_FULLSCREEN) {
2178 frame_input_rect(frame->frame, &input.x, &input.y,
2179 &input.width, &input.height);
2180 wl_region_add(widget->surface->input_region,
2181 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002182 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002183 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002184 }
2185
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002186 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002187
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002188 if (child->opaque) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002189 if (widget->window->type != TYPE_FULLSCREEN) {
2190 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2191 &opaque.width, &opaque.height);
2192
2193 wl_region_add(widget->surface->opaque_region,
2194 opaque.x, opaque.y,
2195 opaque.width, opaque.height);
2196 } else {
2197 wl_region_add(widget->surface->opaque_region,
2198 0, 0, width, height);
2199 }
Martin Minarik1998b152012-05-10 02:04:35 +02002200 }
2201
Martin Minarik1998b152012-05-10 02:04:35 +02002202
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002203 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002204}
2205
2206static void
2207frame_redraw_handler(struct widget *widget, void *data)
2208{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002209 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002210 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211 struct window *window = widget->window;
2212
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002213 if (window->type == TYPE_FULLSCREEN)
2214 return;
2215
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002216 if (window->focus_count) {
2217 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2218 } else {
2219 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2220 }
2221
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002222 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002223
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002224 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002225
2226 cairo_destroy(cr);
2227}
2228
2229static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002230frame_get_pointer_image_for_location(struct window_frame *frame,
2231 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002232{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002233 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002234
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002235 if (window->type != TYPE_TOPLEVEL)
2236 return CURSOR_LEFT_PTR;
2237
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002238 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002239 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002240 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002241 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002242 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002243 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002244 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002245 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002246 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002247 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002248 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002249 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002250 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002251 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002252 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002253 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002254 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002255 case THEME_LOCATION_EXTERIOR:
2256 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002257 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002258 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002259 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002260}
2261
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002262static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002263frame_menu_func(struct window *window,
2264 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002265{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002266 struct display *display;
2267
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002268 switch (index) {
2269 case 0: /* close */
2270 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002271 window->close_handler(window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002272 else
2273 display_exit(window->display);
2274 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002275 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002276 display = window->display;
2277 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002278 workspace_manager_move_surface(
2279 display->workspace_manager,
2280 window->main_surface->surface,
2281 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002282 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002283 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002284 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002285 if (display->workspace < display->workspace_count - 1)
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);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002290 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002291 case 3: /* fullscreen */
2292 /* we don't have a way to get out of fullscreen for now */
2293 if (window->fullscreen_handler)
2294 window->fullscreen_handler(window, window->user_data);
2295 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002296 }
2297}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002298
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002299void
2300window_show_frame_menu(struct window *window,
2301 struct input *input, uint32_t time)
2302{
2303 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002304 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002305
2306 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002307 "Close",
2308 "Move to workspace above", "Move to workspace below",
2309 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002310 };
2311
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002312 if (window->fullscreen_handler)
2313 count = ARRAY_LENGTH(entries);
2314 else
2315 count = ARRAY_LENGTH(entries) - 1;
2316
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002317 input_get_position(input, &x, &y);
2318 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002319 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002320}
2321
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002322static int
2323frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002324 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002325{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002326 struct window_frame *frame = data;
2327 enum theme_location location;
2328
2329 location = frame_pointer_enter(frame->frame, input, x, y);
2330 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2331 widget_schedule_redraw(frame->widget);
2332
2333 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002334}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002335
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002336static int
2337frame_motion_handler(struct widget *widget,
2338 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002339 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002340{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002341 struct window_frame *frame = data;
2342 enum theme_location location;
2343
2344 location = frame_pointer_motion(frame->frame, input, x, y);
2345 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2346 widget_schedule_redraw(frame->widget);
2347
2348 return frame_get_pointer_image_for_location(data, location);
2349}
2350
2351static void
2352frame_leave_handler(struct widget *widget,
2353 struct input *input, void *data)
2354{
2355 struct window_frame *frame = data;
2356
2357 frame_pointer_leave(frame->frame, input);
2358 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2359 widget_schedule_redraw(frame->widget);
2360}
2361
2362static void
2363frame_handle_status(struct window_frame *frame, struct input *input,
2364 uint32_t time, enum theme_location location)
2365{
2366 struct window *window = frame->widget->window;
2367 uint32_t status;
2368
2369 status = frame_status(frame->frame);
2370 if (status & FRAME_STATUS_REPAINT)
2371 widget_schedule_redraw(frame->widget);
2372
2373 if (status & FRAME_STATUS_MINIMIZE)
2374 fprintf(stderr,"Minimize stub\n");
2375
2376 if (status & FRAME_STATUS_MENU) {
2377 window_show_frame_menu(window, input, time);
2378 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2379 }
2380
2381 if (status & FRAME_STATUS_MAXIMIZE) {
2382 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2383 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2384 }
2385
2386 if (status & FRAME_STATUS_CLOSE) {
2387 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002388 window->close_handler(window->user_data);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002389 else
2390 display_exit(window->display);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002391 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002392 }
2393
2394 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2395 input_ungrab(input);
2396 wl_shell_surface_move(window->shell_surface,
2397 input_get_seat(input),
2398 window->display->serial);
2399
2400 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2401 }
2402
2403 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2404 input_ungrab(input);
2405
2406 window->resizing = 1;
2407 wl_shell_surface_resize(window->shell_surface,
2408 input_get_seat(input),
2409 window->display->serial,
2410 location);
2411
2412 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2413 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002414}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002415
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002416static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002417frame_button_handler(struct widget *widget,
2418 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002419 uint32_t button, enum wl_pointer_button_state state,
2420 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002421
2422{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002423 struct window_frame *frame = data;
2424 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002425
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002426 location = frame_pointer_button(frame->frame, input, button, state);
2427 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002428}
2429
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002430static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002431frame_touch_down_handler(struct widget *widget, struct input *input,
2432 uint32_t serial, uint32_t time, int32_t id,
2433 float x, float y, void *data)
2434{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002435 struct window_frame *frame = data;
2436
2437 frame_touch_down(frame->frame, input, id, x, y);
2438 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2439}
2440
2441static void
2442frame_touch_up_handler(struct widget *widget,
2443 struct input *input, uint32_t serial, uint32_t time,
2444 int32_t id, void *data)
2445{
2446 struct window_frame *frame = data;
2447
2448 frame_touch_up(frame->frame, input, id);
2449 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002450}
2451
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002452struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002453window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002454{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002455 struct window_frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002456
Peter Huttererf3d62272013-08-08 11:57:05 +10002457 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002458 frame->frame = frame_create(window->display->theme, 0, 0,
2459 FRAME_BUTTON_ALL, window->title);
2460
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002461 frame->widget = window_add_widget(window, frame);
2462 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002463
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002464 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2465 widget_set_resize_handler(frame->widget, frame_resize_handler);
2466 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002467 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002468 widget_set_motion_handler(frame->widget, frame_motion_handler);
2469 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002470 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002471 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002472
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002473 window->frame = frame;
2474
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002475 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002476}
2477
Kristian Høgsberga1627922012-06-20 17:30:03 -04002478void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002479window_frame_set_child_size(struct widget *widget, int child_width,
2480 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002481{
2482 struct display *display = widget->window->display;
2483 struct theme *t = display->theme;
2484 int decoration_width, decoration_height;
2485 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002486 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002487
2488 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002489 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002490 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002491 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002492
2493 width = child_width + decoration_width;
2494 height = child_height + decoration_height;
2495 } else {
2496 width = child_width;
2497 height = child_height;
2498 }
2499
2500 window_schedule_resize(widget->window, width, height);
2501}
2502
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002503static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002504window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002505{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002506 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002507
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002508 /* frame->child must be destroyed by the application */
2509 widget_destroy(frame->widget);
2510 free(frame);
2511}
2512
2513static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002514input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002515 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002516{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002517 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002518 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002519
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002520 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002521 return;
2522
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002523 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002524 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002525 widget = old;
2526 if (input->grab)
2527 widget = input->grab;
2528 if (widget->leave_handler)
2529 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002530 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002531 }
2532
2533 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002534 widget = focus;
2535 if (input->grab)
2536 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002537 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002538 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002539 cursor = widget->enter_handler(focus, input, x, y,
2540 widget->user_data);
2541 else
2542 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002543
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002544 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002545 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002546}
2547
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002548void
2549input_grab(struct input *input, struct widget *widget, uint32_t button)
2550{
2551 input->grab = widget;
2552 input->grab_button = button;
2553}
2554
2555void
2556input_ungrab(struct input *input)
2557{
2558 struct widget *widget;
2559
2560 input->grab = NULL;
2561 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002562 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002563 input->sx, input->sy);
2564 input_set_focus_widget(input, widget, input->sx, input->sy);
2565 }
2566}
2567
2568static void
2569input_remove_pointer_focus(struct input *input)
2570{
2571 struct window *window = input->pointer_focus;
2572
2573 if (!window)
2574 return;
2575
2576 input_set_focus_widget(input, NULL, 0, 0);
2577
2578 input->pointer_focus = NULL;
2579 input->current_cursor = CURSOR_UNSET;
2580}
2581
2582static void
2583pointer_handle_enter(void *data, struct wl_pointer *pointer,
2584 uint32_t serial, struct wl_surface *surface,
2585 wl_fixed_t sx_w, wl_fixed_t sy_w)
2586{
2587 struct input *input = data;
2588 struct window *window;
2589 struct widget *widget;
2590 float sx = wl_fixed_to_double(sx_w);
2591 float sy = wl_fixed_to_double(sy_w);
2592
2593 if (!surface) {
2594 /* enter event for a window we've just destroyed */
2595 return;
2596 }
2597
2598 input->display->serial = serial;
2599 input->pointer_enter_serial = serial;
2600 input->pointer_focus = wl_surface_get_user_data(surface);
2601 window = input->pointer_focus;
2602
Pekka Paalanen99436862012-11-19 17:15:59 +02002603 if (window->resizing) {
2604 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002605 /* Schedule a redraw to free the pool */
2606 window_schedule_redraw(window);
2607 }
2608
2609 input->sx = sx;
2610 input->sy = sy;
2611
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002612 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002613 input_set_focus_widget(input, widget, sx, sy);
2614}
2615
2616static void
2617pointer_handle_leave(void *data, struct wl_pointer *pointer,
2618 uint32_t serial, struct wl_surface *surface)
2619{
2620 struct input *input = data;
2621
2622 input->display->serial = serial;
2623 input_remove_pointer_focus(input);
2624}
2625
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002626static void
Daniel Stone37816df2012-05-16 18:45:18 +01002627pointer_handle_motion(void *data, struct wl_pointer *pointer,
2628 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002629{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002630 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002631 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002632 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002633 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002634 float sx = wl_fixed_to_double(sx_w);
2635 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002636
Paul Winwoodb22bf572013-08-29 10:52:54 +01002637 input->sx = sx;
2638 input->sy = sy;
2639
2640 if (!window)
2641 return;
2642
Rob Bradford5f087742013-07-11 19:41:27 +01002643 /* when making the window smaller - e.g. after a unmaximise we might
2644 * still have a pending motion event that the compositor has picked
2645 * based on the old surface dimensions
2646 */
2647 if (sx > window->main_surface->allocation.width ||
2648 sy > window->main_surface->allocation.height)
2649 return;
2650
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002651 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002652 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002653 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002654 }
2655
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002656 if (input->grab)
2657 widget = input->grab;
2658 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002659 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002660 if (widget) {
2661 if (widget->motion_handler)
2662 cursor = widget->motion_handler(input->focus_widget,
2663 input, time, sx, sy,
2664 widget->user_data);
2665 else
2666 cursor = widget->default_cursor;
2667 } else
2668 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002669
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002670 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002671}
2672
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002673static void
Daniel Stone37816df2012-05-16 18:45:18 +01002674pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002675 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002676{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002677 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002678 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002679 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002680
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002681 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002682 if (input->focus_widget && input->grab == NULL &&
2683 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002684 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002685
Neil Roberts6b28aad2012-01-23 19:11:18 +00002686 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002687 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002688 (*widget->button_handler)(widget,
2689 input, time,
2690 button, state,
2691 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002692
Daniel Stone4dbadb12012-05-30 16:31:51 +01002693 if (input->grab && input->grab_button == button &&
2694 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002695 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002696}
2697
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002698static void
Daniel Stone37816df2012-05-16 18:45:18 +01002699pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002700 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002701{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002702 struct input *input = data;
2703 struct widget *widget;
2704
2705 widget = input->focus_widget;
2706 if (input->grab)
2707 widget = input->grab;
2708 if (widget && widget->axis_handler)
2709 (*widget->axis_handler)(widget,
2710 input, time,
2711 axis, value,
2712 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002713}
2714
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002715static const struct wl_pointer_listener pointer_listener = {
2716 pointer_handle_enter,
2717 pointer_handle_leave,
2718 pointer_handle_motion,
2719 pointer_handle_button,
2720 pointer_handle_axis,
2721};
2722
2723static void
2724input_remove_keyboard_focus(struct input *input)
2725{
2726 struct window *window = input->keyboard_focus;
2727 struct itimerspec its;
2728
2729 its.it_interval.tv_sec = 0;
2730 its.it_interval.tv_nsec = 0;
2731 its.it_value.tv_sec = 0;
2732 its.it_value.tv_nsec = 0;
2733 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2734
2735 if (!window)
2736 return;
2737
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002738 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002739 if (window->keyboard_focus_handler)
2740 (*window->keyboard_focus_handler)(window, NULL,
2741 window->user_data);
2742
2743 input->keyboard_focus = NULL;
2744}
2745
2746static void
2747keyboard_repeat_func(struct task *task, uint32_t events)
2748{
2749 struct input *input =
2750 container_of(task, struct input, repeat_task);
2751 struct window *window = input->keyboard_focus;
2752 uint64_t exp;
2753
2754 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2755 /* If we change the timer between the fd becoming
2756 * readable and getting here, there'll be nothing to
2757 * read and we get EAGAIN. */
2758 return;
2759
2760 if (window && window->key_handler) {
2761 (*window->key_handler)(window, input, input->repeat_time,
2762 input->repeat_key, input->repeat_sym,
2763 WL_KEYBOARD_KEY_STATE_PRESSED,
2764 window->user_data);
2765 }
2766}
2767
2768static void
2769keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2770 uint32_t format, int fd, uint32_t size)
2771{
2772 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002773 struct xkb_keymap *keymap;
2774 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002775 char *map_str;
2776
2777 if (!data) {
2778 close(fd);
2779 return;
2780 }
2781
2782 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2783 close(fd);
2784 return;
2785 }
2786
2787 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2788 if (map_str == MAP_FAILED) {
2789 close(fd);
2790 return;
2791 }
2792
Rui Matos3eccb862013-10-10 19:44:22 +02002793 keymap = xkb_map_new_from_string(input->display->xkb_context,
2794 map_str,
2795 XKB_KEYMAP_FORMAT_TEXT_V1,
2796 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002797 munmap(map_str, size);
2798 close(fd);
2799
Rui Matos3eccb862013-10-10 19:44:22 +02002800 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002801 fprintf(stderr, "failed to compile keymap\n");
2802 return;
2803 }
2804
Rui Matos3eccb862013-10-10 19:44:22 +02002805 state = xkb_state_new(keymap);
2806 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002807 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002808 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002809 return;
2810 }
2811
Rui Matos3eccb862013-10-10 19:44:22 +02002812 xkb_keymap_unref(input->xkb.keymap);
2813 xkb_state_unref(input->xkb.state);
2814 input->xkb.keymap = keymap;
2815 input->xkb.state = state;
2816
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002817 input->xkb.control_mask =
2818 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2819 input->xkb.alt_mask =
2820 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2821 input->xkb.shift_mask =
2822 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2823}
2824
2825static void
2826keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2827 uint32_t serial, struct wl_surface *surface,
2828 struct wl_array *keys)
2829{
2830 struct input *input = data;
2831 struct window *window;
2832
2833 input->display->serial = serial;
2834 input->keyboard_focus = wl_surface_get_user_data(surface);
2835
2836 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002837 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002838 if (window->keyboard_focus_handler)
2839 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002840 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002841}
2842
2843static void
2844keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2845 uint32_t serial, struct wl_surface *surface)
2846{
2847 struct input *input = data;
2848
2849 input->display->serial = serial;
2850 input_remove_keyboard_focus(input);
2851}
2852
Scott Moreau210d0792012-03-22 10:47:01 -06002853static void
Daniel Stone37816df2012-05-16 18:45:18 +01002854keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002855 uint32_t serial, uint32_t time, uint32_t key,
2856 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002857{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002858 struct input *input = data;
2859 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002860 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002861 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002862 const xkb_keysym_t *syms;
2863 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002864 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002865
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002866 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002867 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002868 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002869 return;
2870
Daniel Stone97f68542012-05-30 16:32:01 +01002871 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002872
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002873 sym = XKB_KEY_NoSymbol;
2874 if (num_syms == 1)
2875 sym = syms[0];
2876
2877 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002878 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002879 window_set_maximized(window,
2880 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002881 } else if (sym == XKB_KEY_F11 &&
2882 window->fullscreen_handler &&
2883 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2884 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002885 } else if (sym == XKB_KEY_F4 &&
2886 input->modifiers == MOD_ALT_MASK &&
2887 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2888 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002889 window->close_handler(window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002890 else
2891 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002892 } else if (window->key_handler) {
2893 (*window->key_handler)(window, input, time, key,
2894 sym, state, window->user_data);
2895 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002896
2897 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2898 key == input->repeat_key) {
2899 its.it_interval.tv_sec = 0;
2900 its.it_interval.tv_nsec = 0;
2901 its.it_value.tv_sec = 0;
2902 its.it_value.tv_nsec = 0;
2903 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2904 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2905 input->repeat_sym = sym;
2906 input->repeat_key = key;
2907 input->repeat_time = time;
2908 its.it_interval.tv_sec = 0;
2909 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2910 its.it_value.tv_sec = 0;
2911 its.it_value.tv_nsec = 400 * 1000 * 1000;
2912 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2913 }
2914}
2915
2916static void
Daniel Stone351eb612012-05-31 15:27:47 -04002917keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2918 uint32_t serial, uint32_t mods_depressed,
2919 uint32_t mods_latched, uint32_t mods_locked,
2920 uint32_t group)
2921{
2922 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002923 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002924
Matt Ropere61561f2013-06-24 16:52:43 +01002925 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2926 if (!input->xkb.keymap)
2927 return;
2928
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002929 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2930 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002931 mask = xkb_state_serialize_mods(input->xkb.state,
2932 XKB_STATE_DEPRESSED |
2933 XKB_STATE_LATCHED);
2934 input->modifiers = 0;
2935 if (mask & input->xkb.control_mask)
2936 input->modifiers |= MOD_CONTROL_MASK;
2937 if (mask & input->xkb.alt_mask)
2938 input->modifiers |= MOD_ALT_MASK;
2939 if (mask & input->xkb.shift_mask)
2940 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002941}
2942
Daniel Stone37816df2012-05-16 18:45:18 +01002943static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002944 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002945 keyboard_handle_enter,
2946 keyboard_handle_leave,
2947 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002948 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002949};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002950
2951static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002952touch_handle_down(void *data, struct wl_touch *wl_touch,
2953 uint32_t serial, uint32_t time, struct wl_surface *surface,
2954 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2955{
2956 struct input *input = data;
2957 struct widget *widget;
2958 float sx = wl_fixed_to_double(x_w);
2959 float sy = wl_fixed_to_double(y_w);
2960
Rusty Lynch1084da52013-08-15 09:10:08 -07002961 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002962 input->touch_focus = wl_surface_get_user_data(surface);
2963 if (!input->touch_focus) {
2964 DBG("Failed to find to touch focus for surface %p\n", surface);
2965 return;
2966 }
2967
2968 widget = window_find_widget(input->touch_focus,
2969 wl_fixed_to_double(x_w),
2970 wl_fixed_to_double(y_w));
2971 if (widget) {
2972 struct touch_point *tp = xmalloc(sizeof *tp);
2973 if (tp) {
2974 tp->id = id;
2975 tp->widget = widget;
2976 wl_list_insert(&input->touch_point_list, &tp->link);
2977
2978 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07002979 (*widget->touch_down_handler)(widget, input,
2980 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07002981 sx, sy,
2982 widget->user_data);
2983 }
2984 }
2985}
2986
2987static void
2988touch_handle_up(void *data, struct wl_touch *wl_touch,
2989 uint32_t serial, uint32_t time, int32_t id)
2990{
2991 struct input *input = data;
2992 struct touch_point *tp, *tmp;
2993
Rusty Lynch041815a2013-08-08 21:20:38 -07002994 if (!input->touch_focus) {
2995 DBG("No touch focus found for touch up event!\n");
2996 return;
2997 }
2998
2999 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3000 if (tp->id != id)
3001 continue;
3002
3003 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003004 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003005 time, id,
3006 tp->widget->user_data);
3007
3008 wl_list_remove(&tp->link);
3009 free(tp);
3010
3011 return;
3012 }
3013}
3014
3015static void
3016touch_handle_motion(void *data, struct wl_touch *wl_touch,
3017 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3018{
3019 struct input *input = data;
3020 struct touch_point *tp;
3021 float sx = wl_fixed_to_double(x_w);
3022 float sy = wl_fixed_to_double(y_w);
3023
3024 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3025
3026 if (!input->touch_focus) {
3027 DBG("No touch focus found for touch motion event!\n");
3028 return;
3029 }
3030
3031 wl_list_for_each(tp, &input->touch_point_list, link) {
3032 if (tp->id != id)
3033 continue;
3034
3035 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003036 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003037 id, sx, sy,
3038 tp->widget->user_data);
3039 return;
3040 }
3041}
3042
3043static void
3044touch_handle_frame(void *data, struct wl_touch *wl_touch)
3045{
3046 struct input *input = data;
3047 struct touch_point *tp, *tmp;
3048
3049 DBG("touch_handle_frame\n");
3050
3051 if (!input->touch_focus) {
3052 DBG("No touch focus found for touch frame event!\n");
3053 return;
3054 }
3055
3056 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3057 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003058 (*tp->widget->touch_frame_handler)(tp->widget, input,
3059 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003060
3061 wl_list_remove(&tp->link);
3062 free(tp);
3063 }
3064}
3065
3066static void
3067touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3068{
3069 struct input *input = data;
3070 struct touch_point *tp, *tmp;
3071
3072 DBG("touch_handle_cancel\n");
3073
3074 if (!input->touch_focus) {
3075 DBG("No touch focus found for touch cancel event!\n");
3076 return;
3077 }
3078
3079 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3080 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003081 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3082 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003083
3084 wl_list_remove(&tp->link);
3085 free(tp);
3086 }
3087}
3088
3089static const struct wl_touch_listener touch_listener = {
3090 touch_handle_down,
3091 touch_handle_up,
3092 touch_handle_motion,
3093 touch_handle_frame,
3094 touch_handle_cancel,
3095};
3096
3097static void
Daniel Stone37816df2012-05-16 18:45:18 +01003098seat_handle_capabilities(void *data, struct wl_seat *seat,
3099 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003100{
Daniel Stone37816df2012-05-16 18:45:18 +01003101 struct input *input = data;
3102
3103 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3104 input->pointer = wl_seat_get_pointer(seat);
3105 wl_pointer_set_user_data(input->pointer, input);
3106 wl_pointer_add_listener(input->pointer, &pointer_listener,
3107 input);
3108 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3109 wl_pointer_destroy(input->pointer);
3110 input->pointer = NULL;
3111 }
3112
3113 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3114 input->keyboard = wl_seat_get_keyboard(seat);
3115 wl_keyboard_set_user_data(input->keyboard, input);
3116 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3117 input);
3118 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3119 wl_keyboard_destroy(input->keyboard);
3120 input->keyboard = NULL;
3121 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003122
3123 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3124 input->touch = wl_seat_get_touch(seat);
3125 wl_touch_set_user_data(input->touch, input);
3126 wl_touch_add_listener(input->touch, &touch_listener, input);
3127 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3128 wl_touch_destroy(input->touch);
3129 input->touch = NULL;
3130 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003131}
3132
Rob Bradford08031182013-08-13 20:11:03 +01003133static void
3134seat_handle_name(void *data, struct wl_seat *seat,
3135 const char *name)
3136{
3137
3138}
3139
Daniel Stone37816df2012-05-16 18:45:18 +01003140static const struct wl_seat_listener seat_listener = {
3141 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003142 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003143};
3144
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003145void
3146input_get_position(struct input *input, int32_t *x, int32_t *y)
3147{
3148 *x = input->sx;
3149 *y = input->sy;
3150}
3151
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003152struct display *
3153input_get_display(struct input *input)
3154{
3155 return input->display;
3156}
3157
Daniel Stone37816df2012-05-16 18:45:18 +01003158struct wl_seat *
3159input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003160{
Daniel Stone37816df2012-05-16 18:45:18 +01003161 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003162}
3163
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003164uint32_t
3165input_get_modifiers(struct input *input)
3166{
3167 return input->modifiers;
3168}
3169
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003170struct widget *
3171input_get_focus_widget(struct input *input)
3172{
3173 return input->focus_widget;
3174}
3175
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003176struct data_offer {
3177 struct wl_data_offer *offer;
3178 struct input *input;
3179 struct wl_array types;
3180 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003181
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003182 struct task io_task;
3183 int fd;
3184 data_func_t func;
3185 int32_t x, y;
3186 void *user_data;
3187};
3188
3189static void
3190data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3191{
3192 struct data_offer *offer = data;
3193 char **p;
3194
3195 p = wl_array_add(&offer->types, sizeof *p);
3196 *p = strdup(type);
3197}
3198
3199static const struct wl_data_offer_listener data_offer_listener = {
3200 data_offer_offer,
3201};
3202
3203static void
3204data_offer_destroy(struct data_offer *offer)
3205{
3206 char **p;
3207
3208 offer->refcount--;
3209 if (offer->refcount == 0) {
3210 wl_data_offer_destroy(offer->offer);
3211 for (p = offer->types.data; *p; p++)
3212 free(*p);
3213 wl_array_release(&offer->types);
3214 free(offer);
3215 }
3216}
3217
3218static void
3219data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003220 struct wl_data_device *data_device,
3221 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003222{
3223 struct data_offer *offer;
3224
Brian Lovinbc919262013-08-07 15:34:59 -07003225 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003226
3227 wl_array_init(&offer->types);
3228 offer->refcount = 1;
3229 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003230 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003231 wl_data_offer_add_listener(offer->offer,
3232 &data_offer_listener, offer);
3233}
3234
3235static void
3236data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003237 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003238 wl_fixed_t x_w, wl_fixed_t y_w,
3239 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003240{
3241 struct input *input = data;
3242 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003243 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003244 float x = wl_fixed_to_double(x_w);
3245 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003246 char **p;
3247
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003248 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003249 window = wl_surface_get_user_data(surface);
3250 input->pointer_focus = window;
Xiong Zhang853a7792013-11-25 18:42:50 +08003251 input->sx = x;
3252 input->sy = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003253
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003254 if (offer) {
3255 input->drag_offer = wl_data_offer_get_user_data(offer);
3256
3257 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3258 *p = NULL;
3259
3260 types_data = input->drag_offer->types.data;
3261 } else {
3262 input->drag_offer = NULL;
3263 types_data = NULL;
3264 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003265
3266 window = input->pointer_focus;
3267 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003268 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003269 window->user_data);
3270}
3271
3272static void
3273data_device_leave(void *data, struct wl_data_device *data_device)
3274{
3275 struct input *input = data;
3276
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003277 if (input->drag_offer) {
3278 data_offer_destroy(input->drag_offer);
3279 input->drag_offer = NULL;
3280 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003281}
3282
3283static void
3284data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003285 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003286{
3287 struct input *input = data;
3288 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003289 float x = wl_fixed_to_double(x_w);
3290 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003291 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003292
3293 input->sx = x;
3294 input->sy = y;
3295
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003296 if (input->drag_offer)
3297 types_data = input->drag_offer->types.data;
3298 else
3299 types_data = NULL;
3300
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003301 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003302 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003303 window->user_data);
3304}
3305
3306static void
3307data_device_drop(void *data, struct wl_data_device *data_device)
3308{
3309 struct input *input = data;
3310 struct window *window = input->pointer_focus;
3311
3312 if (window->drop_handler)
3313 window->drop_handler(window, input,
3314 input->sx, input->sy, window->user_data);
3315}
3316
3317static void
3318data_device_selection(void *data,
3319 struct wl_data_device *wl_data_device,
3320 struct wl_data_offer *offer)
3321{
3322 struct input *input = data;
3323 char **p;
3324
3325 if (input->selection_offer)
3326 data_offer_destroy(input->selection_offer);
3327
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003328 if (offer) {
3329 input->selection_offer = wl_data_offer_get_user_data(offer);
3330 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3331 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003332 } else {
3333 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003334 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003335}
3336
3337static const struct wl_data_device_listener data_device_listener = {
3338 data_device_data_offer,
3339 data_device_enter,
3340 data_device_leave,
3341 data_device_motion,
3342 data_device_drop,
3343 data_device_selection
3344};
3345
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003346static void
3347input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003348{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003349 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003350 struct wl_cursor *cursor;
3351 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003352
Daniel Stone80972742012-11-07 17:51:39 +11003353 if (!input->pointer)
3354 return;
3355
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003356 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003357 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003358 return;
3359
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003360 if (index >= (int) cursor->image_count) {
3361 fprintf(stderr, "cursor index out of range\n");
3362 return;
3363 }
3364
3365 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003366 buffer = wl_cursor_image_get_buffer(image);
3367 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003368 return;
3369
Kristian Høgsbergae277372012-08-01 09:41:08 -04003370 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003371 input->pointer_surface,
3372 image->hotspot_x, image->hotspot_y);
3373 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3374 wl_surface_damage(input->pointer_surface, 0, 0,
3375 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003376 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003377}
3378
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003379static const struct wl_callback_listener pointer_surface_listener;
3380
3381static void
3382pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3383 uint32_t time)
3384{
3385 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003386 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003387 int i;
3388
3389 if (callback) {
3390 assert(callback == input->cursor_frame_cb);
3391 wl_callback_destroy(callback);
3392 input->cursor_frame_cb = NULL;
3393 }
3394
Daniel Stone80972742012-11-07 17:51:39 +11003395 if (!input->pointer)
3396 return;
3397
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003398 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003399 wl_pointer_set_cursor(input->pointer,
3400 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003401 NULL, 0, 0);
3402 return;
3403 }
3404
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003405 if (input->current_cursor == CURSOR_UNSET)
3406 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003407 cursor = input->display->cursors[input->current_cursor];
3408 if (!cursor)
3409 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003410
3411 /* FIXME We don't have the current time on the first call so we set
3412 * the animation start to the time of the first frame callback. */
3413 if (time == 0)
3414 input->cursor_anim_start = 0;
3415 else if (input->cursor_anim_start == 0)
3416 input->cursor_anim_start = time;
3417
3418 if (time == 0 || input->cursor_anim_start == 0)
3419 i = 0;
3420 else
3421 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3422
Pekka Paalanenbc106382012-10-10 12:49:31 +03003423 if (cursor->image_count > 1) {
3424 input->cursor_frame_cb =
3425 wl_surface_frame(input->pointer_surface);
3426 wl_callback_add_listener(input->cursor_frame_cb,
3427 &pointer_surface_listener, input);
3428 }
3429
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003430 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003431}
3432
3433static const struct wl_callback_listener pointer_surface_listener = {
3434 pointer_surface_frame_callback
3435};
3436
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003437void
3438input_set_pointer_image(struct input *input, int pointer)
3439{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003440 int force = 0;
3441
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003442 if (!input->pointer)
3443 return;
3444
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003445 if (input->pointer_enter_serial > input->cursor_serial)
3446 force = 1;
3447
3448 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003449 return;
3450
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003451 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003452 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003453 if (!input->cursor_frame_cb)
3454 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003455 else if (force) {
3456 /* The current frame callback may be stuck if, for instance,
3457 * the set cursor request was processed by the server after
3458 * this client lost the focus. In this case the cursor surface
3459 * might not be mapped and the frame callback wouldn't ever
3460 * complete. Send a set_cursor and attach to try to map the
3461 * cursor surface again so that the callback will finish */
3462 input_set_pointer_image_index(input, 0);
3463 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003464}
3465
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003466struct wl_data_device *
3467input_get_data_device(struct input *input)
3468{
3469 return input->data_device;
3470}
3471
3472void
3473input_set_selection(struct input *input,
3474 struct wl_data_source *source, uint32_t time)
3475{
3476 wl_data_device_set_selection(input->data_device, source, time);
3477}
3478
3479void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003480input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003481{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003482 wl_data_offer_accept(input->drag_offer->offer,
3483 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003484}
3485
3486static void
3487offer_io_func(struct task *task, uint32_t events)
3488{
3489 struct data_offer *offer =
3490 container_of(task, struct data_offer, io_task);
3491 unsigned int len;
3492 char buffer[4096];
3493
3494 len = read(offer->fd, buffer, sizeof buffer);
3495 offer->func(buffer, len,
3496 offer->x, offer->y, offer->user_data);
3497
3498 if (len == 0) {
3499 close(offer->fd);
3500 data_offer_destroy(offer);
3501 }
3502}
3503
3504static void
3505data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3506 data_func_t func, void *user_data)
3507{
3508 int p[2];
3509
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003510 if (pipe2(p, O_CLOEXEC) == -1)
3511 return;
3512
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003513 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3514 close(p[1]);
3515
3516 offer->io_task.run = offer_io_func;
3517 offer->fd = p[0];
3518 offer->func = func;
3519 offer->refcount++;
3520 offer->user_data = user_data;
3521
3522 display_watch_fd(offer->input->display,
3523 offer->fd, EPOLLIN, &offer->io_task);
3524}
3525
3526void
3527input_receive_drag_data(struct input *input, const char *mime_type,
3528 data_func_t func, void *data)
3529{
3530 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3531 input->drag_offer->x = input->sx;
3532 input->drag_offer->y = input->sy;
3533}
3534
3535int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003536input_receive_drag_data_to_fd(struct input *input,
3537 const char *mime_type, int fd)
3538{
3539 if (input->drag_offer)
3540 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3541
3542 return 0;
3543}
3544
3545int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003546input_receive_selection_data(struct input *input, const char *mime_type,
3547 data_func_t func, void *data)
3548{
3549 char **p;
3550
3551 if (input->selection_offer == NULL)
3552 return -1;
3553
3554 for (p = input->selection_offer->types.data; *p; p++)
3555 if (strcmp(mime_type, *p) == 0)
3556 break;
3557
3558 if (*p == NULL)
3559 return -1;
3560
3561 data_offer_receive_data(input->selection_offer,
3562 mime_type, func, data);
3563 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003564}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003565
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003566int
3567input_receive_selection_data_to_fd(struct input *input,
3568 const char *mime_type, int fd)
3569{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003570 if (input->selection_offer)
3571 wl_data_offer_receive(input->selection_offer->offer,
3572 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003573
3574 return 0;
3575}
3576
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003577void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003578window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003579{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003580 if (!window->shell_surface)
3581 return;
3582
Daniel Stone37816df2012-05-16 18:45:18 +01003583 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003584}
3585
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003586static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003587surface_set_synchronized(struct surface *surface)
3588{
3589 if (!surface->subsurface)
3590 return;
3591
3592 if (surface->synchronized)
3593 return;
3594
3595 wl_subsurface_set_sync(surface->subsurface);
3596 surface->synchronized = 1;
3597}
3598
3599static void
3600surface_set_synchronized_default(struct surface *surface)
3601{
3602 if (!surface->subsurface)
3603 return;
3604
3605 if (surface->synchronized == surface->synchronized_default)
3606 return;
3607
3608 if (surface->synchronized_default)
3609 wl_subsurface_set_sync(surface->subsurface);
3610 else
3611 wl_subsurface_set_desync(surface->subsurface);
3612
3613 surface->synchronized = surface->synchronized_default;
3614}
3615
3616static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003617surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003618{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003619 struct widget *widget = surface->widget;
3620 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003621
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003622 if (surface->input_region) {
3623 wl_region_destroy(surface->input_region);
3624 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003625 }
3626
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003627 if (surface->opaque_region)
3628 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003629
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003630 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003631
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003632 if (widget->resize_handler)
3633 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003634 widget->allocation.width,
3635 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003636 widget->user_data);
3637
Pekka Paalanen35e82632013-04-25 13:57:48 +03003638 if (surface->subsurface &&
3639 (surface->allocation.x != widget->allocation.x ||
3640 surface->allocation.y != widget->allocation.y)) {
3641 wl_subsurface_set_position(surface->subsurface,
3642 widget->allocation.x,
3643 widget->allocation.y);
3644 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003645 if (surface->allocation.width != widget->allocation.width ||
3646 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003647 window_schedule_redraw(widget->window);
3648 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003649 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003650
3651 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003652 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003653 widget->allocation.width,
3654 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003655}
3656
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003657static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003658hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3659{
3660 /*
3661 * This hack should be removed, when EGL respects
3662 * eglSwapInterval(0).
3663 *
3664 * If this window has sub-surfaces, especially a free-running
3665 * EGL-widget, we need to post the parent surface once with
3666 * all the old state to guarantee, that the EGL-widget will
3667 * receive its frame callback soon. Otherwise, a forced call
3668 * to eglSwapBuffers may end up blocking, waiting for a frame
3669 * event that will never come, because we will commit the parent
3670 * surface with all new state only after eglSwapBuffers returns.
3671 *
3672 * This assumes, that:
3673 * 1. When the EGL widget's resize hook is called, it pauses.
3674 * 2. When the EGL widget's redraw hook is called, it forces a
3675 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3676 * In a single threaded application condition 1 is a no-op.
3677 *
3678 * XXX: This should actually be after the surface_resize() calls,
3679 * but cannot, because then it would commit the incomplete state
3680 * accumulated from the widget resize hooks.
3681 */
3682 if (window->subsurface_list.next != &window->main_surface->link ||
3683 window->subsurface_list.prev != &window->main_surface->link)
3684 wl_surface_commit(window->main_surface->surface);
3685}
3686
3687static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003688idle_resize(struct window *window)
3689{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003690 struct surface *surface;
3691
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003692 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003693 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003694
Pekka Paalanen71233882013-04-25 13:57:53 +03003695 DBG("from %dx%d to %dx%d\n",
3696 window->main_surface->server_allocation.width,
3697 window->main_surface->server_allocation.height,
3698 window->pending_allocation.width,
3699 window->pending_allocation.height);
3700
Pekka Paalanene9297f82013-04-25 13:57:51 +03003701 hack_prevent_EGL_sub_surface_deadlock(window);
3702
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003703 widget_set_allocation(window->main_surface->widget,
3704 window->pending_allocation.x,
3705 window->pending_allocation.y,
3706 window->pending_allocation.width,
3707 window->pending_allocation.height);
3708
3709 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003710
3711 /* The main surface is in the list, too. Main surface's
3712 * resize_handler is responsible for calling widget_set_allocation()
3713 * on all sub-surface root widgets, so they will be resized
3714 * properly.
3715 */
3716 wl_list_for_each(surface, &window->subsurface_list, link) {
3717 if (surface == window->main_surface)
3718 continue;
3719
3720 surface_set_synchronized(surface);
3721 surface_resize(surface);
3722 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003723}
3724
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003725void
3726window_schedule_resize(struct window *window, int width, int height)
3727{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003728 /* We should probably get these numbers from the theme. */
3729 const int min_width = 200, min_height = 200;
3730
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003731 window->pending_allocation.x = 0;
3732 window->pending_allocation.y = 0;
3733 window->pending_allocation.width = width;
3734 window->pending_allocation.height = height;
3735
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003736 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003737 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003738 window->min_allocation.width = min_width;
3739 else
3740 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003741 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003742 window->min_allocation.height = min_height;
3743 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003744 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003745 }
3746
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003747 if (window->pending_allocation.width < window->min_allocation.width)
3748 window->pending_allocation.width = window->min_allocation.width;
3749 if (window->pending_allocation.height < window->min_allocation.height)
3750 window->pending_allocation.height = window->min_allocation.height;
3751
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003752 window->resize_needed = 1;
3753 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003754}
3755
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003756void
3757widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3758{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003759 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003760}
3761
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003762static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003763handle_ping(void *data, struct wl_shell_surface *shell_surface,
3764 uint32_t serial)
3765{
3766 wl_shell_surface_pong(shell_surface, serial);
3767}
3768
3769static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003770handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003771 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003772{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003773 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003774
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003775 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003776 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003777}
3778
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003779static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003780menu_destroy(struct menu *menu)
3781{
3782 widget_destroy(menu->widget);
3783 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003784 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003785 free(menu);
3786}
3787
3788static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003789handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3790{
3791 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003792 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003793
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003794 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003795 * device. Or just use wl_callback. And this really needs to
3796 * be a window vfunc that the menu can set. And we need the
3797 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003798
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003799 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003800 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003801}
3802
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003803static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003804 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003805 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003806 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003807};
3808
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003809void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003810window_get_allocation(struct window *window,
3811 struct rectangle *allocation)
3812{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003813 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003814}
3815
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003816static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003817widget_redraw(struct widget *widget)
3818{
3819 struct widget *child;
3820
3821 if (widget->redraw_handler)
3822 widget->redraw_handler(widget, widget->user_data);
3823 wl_list_for_each(child, &widget->child_list, link)
3824 widget_redraw(child);
3825}
3826
3827static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003828frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3829{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003830 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003831
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003832 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003833 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003834 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003835 surface->frame_cb = NULL;
3836
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003837 surface->last_time = time;
3838
Pekka Paalanen71233882013-04-25 13:57:53 +03003839 if (surface->redraw_needed || surface->window->redraw_needed) {
3840 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003841 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003842 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003843}
3844
3845static const struct wl_callback_listener listener = {
3846 frame_callback
3847};
3848
3849static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003850surface_redraw(struct surface *surface)
3851{
Pekka Paalanen71233882013-04-25 13:57:53 +03003852 DBG_OBJ(surface->surface, "begin\n");
3853
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003854 if (!surface->window->redraw_needed && !surface->redraw_needed)
3855 return;
3856
3857 /* Whole-window redraw forces a redraw even if the previous has
3858 * not yet hit the screen.
3859 */
3860 if (surface->frame_cb) {
3861 if (!surface->window->redraw_needed)
3862 return;
3863
Pekka Paalanen71233882013-04-25 13:57:53 +03003864 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003865 wl_callback_destroy(surface->frame_cb);
3866 }
3867
3868 surface->frame_cb = wl_surface_frame(surface->surface);
3869 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003870 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003871
3872 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003873 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003874 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003875 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003876}
3877
3878static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003879idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003880{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003881 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003882 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003883
Pekka Paalanen71233882013-04-25 13:57:53 +03003884 DBG(" --------- \n");
3885
Pekka Paalaneneebff542013-04-25 13:57:52 +03003886 wl_list_init(&window->redraw_task.link);
3887 window->redraw_task_scheduled = 0;
3888
3889 if (window->resize_needed) {
3890 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003891 if (window->main_surface->frame_cb) {
3892 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003893 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003894 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003895
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003896 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003897 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003898
Pekka Paalanen35e82632013-04-25 13:57:48 +03003899 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003900 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003901
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003902 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003903 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003904
3905 wl_list_for_each(surface, &window->subsurface_list, link)
3906 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003907}
3908
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003909static void
3910window_schedule_redraw_task(struct window *window)
3911{
3912 if (window->configure_requests)
3913 return;
3914 if (!window->redraw_task_scheduled) {
3915 window->redraw_task.run = idle_redraw;
3916 display_defer(window->display, &window->redraw_task);
3917 window->redraw_task_scheduled = 1;
3918 }
3919}
3920
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003921void
3922window_schedule_redraw(struct window *window)
3923{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003924 struct surface *surface;
3925
Pekka Paalanen71233882013-04-25 13:57:53 +03003926 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3927
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003928 wl_list_for_each(surface, &window->subsurface_list, link)
3929 surface->redraw_needed = 1;
3930
3931 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003932}
3933
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003934int
3935window_is_fullscreen(struct window *window)
3936{
3937 return window->type == TYPE_FULLSCREEN;
3938}
3939
MoD063a8822013-03-02 23:43:57 +00003940static void
3941configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3942{
3943 struct window *window = data;
3944
3945 wl_callback_destroy(callback);
3946 window->configure_requests--;
3947
3948 if (!window->configure_requests)
3949 window_schedule_redraw(window);
3950}
3951
3952static struct wl_callback_listener configure_request_listener = {
3953 configure_request_completed,
3954};
3955
3956static void
3957window_defer_redraw_until_configure(struct window* window)
3958{
3959 struct wl_callback *callback;
3960
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003961 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003962 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003963 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003964 }
3965
3966 callback = wl_display_sync(window->display->display);
3967 wl_callback_add_listener(callback, &configure_request_listener, window);
3968 window->configure_requests++;
3969}
3970
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003971void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003972window_set_fullscreen(struct window *window, int fullscreen)
3973{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003974 if (!window->display->shell)
3975 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003976
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003977 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003978 return;
3979
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003980 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003981 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003982 if (window->type == TYPE_TOPLEVEL) {
3983 window->saved_allocation = window->main_surface->allocation;
3984 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003985 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003986 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003987 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003988 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003989 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003990 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003991 if (window->saved_type == TYPE_MAXIMIZED) {
3992 window_set_maximized(window, 1);
3993 } else {
3994 window->type = TYPE_TOPLEVEL;
3995 wl_shell_surface_set_toplevel(window->shell_surface);
3996 window_schedule_resize(window,
3997 window->saved_allocation.width,
3998 window->saved_allocation.height);
3999 }
4000
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004001 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004002}
4003
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004004void
4005window_set_fullscreen_method(struct window *window,
4006 enum wl_shell_surface_fullscreen_method method)
4007{
4008 window->fullscreen_method = method;
4009}
4010
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004011int
4012window_is_maximized(struct window *window)
4013{
4014 return window->type == TYPE_MAXIMIZED;
4015}
4016
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004017void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004018window_set_maximized(struct window *window, int maximized)
4019{
4020 if (!window->display->shell)
4021 return;
4022
4023 if ((window->type == TYPE_MAXIMIZED) == maximized)
4024 return;
4025
4026 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004027 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004028 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4029 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004030 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004031 } else if (window->type == TYPE_FULLSCREEN) {
4032 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4033 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004034 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004035 } else {
4036 wl_shell_surface_set_toplevel(window->shell_surface);
4037 window->type = TYPE_TOPLEVEL;
4038 window_schedule_resize(window,
4039 window->saved_allocation.width,
4040 window->saved_allocation.height);
4041 }
4042}
4043
4044void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004045window_set_user_data(struct window *window, void *data)
4046{
4047 window->user_data = data;
4048}
4049
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004050void *
4051window_get_user_data(struct window *window)
4052{
4053 return window->user_data;
4054}
4055
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004056void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004057window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004058 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004059{
4060 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004061}
4062
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004063void
4064window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004065 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004066{
4067 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004068}
4069
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004070void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004071window_set_data_handler(struct window *window, window_data_handler_t handler)
4072{
4073 window->data_handler = handler;
4074}
4075
4076void
4077window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4078{
4079 window->drop_handler = handler;
4080}
4081
4082void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004083window_set_close_handler(struct window *window,
4084 window_close_handler_t handler)
4085{
4086 window->close_handler = handler;
4087}
4088
4089void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004090window_set_fullscreen_handler(struct window *window,
4091 window_fullscreen_handler_t handler)
4092{
4093 window->fullscreen_handler = handler;
4094}
4095
4096void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004097window_set_output_handler(struct window *window,
4098 window_output_handler_t handler)
4099{
4100 window->output_handler = handler;
4101}
4102
4103void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004104window_set_title(struct window *window, const char *title)
4105{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004106 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004107 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004108 if (window->frame) {
4109 frame_set_title(window->frame->frame, title);
4110 widget_schedule_redraw(window->frame->widget);
4111 }
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004112 if (window->shell_surface)
4113 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004114}
4115
4116const char *
4117window_get_title(struct window *window)
4118{
4119 return window->title;
4120}
4121
4122void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004123window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4124{
4125 struct text_cursor_position *text_cursor_position =
4126 window->display->text_cursor_position;
4127
Scott Moreau9295ce02012-06-01 12:46:10 -06004128 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004129 return;
4130
4131 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004132 window->main_surface->surface,
4133 wl_fixed_from_int(x),
4134 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004135}
4136
4137void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004138window_damage(struct window *window, int32_t x, int32_t y,
4139 int32_t width, int32_t height)
4140{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004141 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004142}
4143
Casey Dahlin9074db52012-04-19 22:50:09 -04004144static void
4145surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004146 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004147{
Rob Bradford7507b572012-05-15 17:55:34 +01004148 struct window *window = data;
4149 struct output *output;
4150 struct output *output_found = NULL;
4151 struct window_output *window_output;
4152
4153 wl_list_for_each(output, &window->display->output_list, link) {
4154 if (output->output == wl_output) {
4155 output_found = output;
4156 break;
4157 }
4158 }
4159
4160 if (!output_found)
4161 return;
4162
Brian Lovinbc919262013-08-07 15:34:59 -07004163 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004164 window_output->output = output_found;
4165
4166 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004167
4168 if (window->output_handler)
4169 window->output_handler(window, output_found, 1,
4170 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004171}
4172
4173static void
4174surface_leave(void *data,
4175 struct wl_surface *wl_surface, struct wl_output *output)
4176{
Rob Bradford7507b572012-05-15 17:55:34 +01004177 struct window *window = data;
4178 struct window_output *window_output;
4179 struct window_output *window_output_found = NULL;
4180
4181 wl_list_for_each(window_output, &window->window_output_list, link) {
4182 if (window_output->output->output == output) {
4183 window_output_found = window_output;
4184 break;
4185 }
4186 }
4187
4188 if (window_output_found) {
4189 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004190
4191 if (window->output_handler)
4192 window->output_handler(window, window_output->output,
4193 0, window->user_data);
4194
Rob Bradford7507b572012-05-15 17:55:34 +01004195 free(window_output_found);
4196 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004197}
4198
4199static const struct wl_surface_listener surface_listener = {
4200 surface_enter,
4201 surface_leave
4202};
4203
Pekka Paalanen4e373742013-02-13 16:17:13 +02004204static struct surface *
4205surface_create(struct window *window)
4206{
4207 struct display *display = window->display;
4208 struct surface *surface;
4209
Brian Lovinbc919262013-08-07 15:34:59 -07004210 surface = xmalloc(sizeof *surface);
4211 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004212 if (!surface)
4213 return NULL;
4214
4215 surface->window = window;
4216 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004217 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004218 wl_surface_add_listener(surface->surface, &surface_listener, window);
4219
Pekka Paalanen35e82632013-04-25 13:57:48 +03004220 wl_list_insert(&window->subsurface_list, &surface->link);
4221
Pekka Paalanen4e373742013-02-13 16:17:13 +02004222 return surface;
4223}
4224
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004225static struct window *
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004226window_create_internal(struct display *display, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004227{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004228 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004229 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004230
Peter Huttererf3d62272013-08-08 11:57:05 +10004231 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004232 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004233 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004234
4235 surface = surface_create(window);
4236 window->main_surface = surface;
4237
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004238 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004239 window->shell_surface =
4240 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004241 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004242 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004243 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004244
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004245 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004246 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004247 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004248 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004249
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004250 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004251#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004252 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004253#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004254 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004255#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004256 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004257 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004258
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004259 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004260 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004261 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004262
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004263 if (window->shell_surface) {
4264 wl_shell_surface_set_user_data(window->shell_surface, window);
4265 wl_shell_surface_add_listener(window->shell_surface,
4266 &shell_surface_listener, window);
4267 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004268
Rob Bradford7507b572012-05-15 17:55:34 +01004269 wl_list_init (&window->window_output_list);
4270
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004271 return window;
4272}
4273
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004274struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004275window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004276{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004277 return window_create_internal(display, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004278}
4279
4280struct window *
4281window_create_custom(struct display *display)
4282{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004283 return window_create_internal(display, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004284}
4285
4286struct window *
4287window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004288 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004289{
4290 struct window *window;
4291
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004292 window = window_create_internal(parent->display, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004293
4294 window->x = x;
4295 window->y = y;
4296
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004297 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004298 wl_shell_surface_set_transient(
4299 window->shell_surface,
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004300 parent->main_surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004301 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004302
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004303 return window;
4304}
4305
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004306static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004307menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004308{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004309 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004310 int next;
4311
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004312 frame_interior(menu->frame, &x, &y, &width, &height);
4313 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004314 if (menu->current != next) {
4315 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004316 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004317 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004318}
4319
4320static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004321menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004322 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004323 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004324{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004325 struct menu *menu = data;
4326
4327 if (widget == menu->widget)
4328 menu_set_item(data, y);
4329
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004330 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004331}
4332
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004333static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004334menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004335 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004336{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004337 struct menu *menu = data;
4338
4339 if (widget == menu->widget)
4340 menu_set_item(data, y);
4341
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004342 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004343}
4344
4345static void
4346menu_leave_handler(struct widget *widget, struct input *input, void *data)
4347{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004348 struct menu *menu = data;
4349
4350 if (widget == menu->widget)
4351 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004352}
4353
4354static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004355menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004356 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004357 uint32_t button, enum wl_pointer_button_state state,
4358 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004359
4360{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004361 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004362
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004363 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4364 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004365 /* Either relase after press-drag-release or
4366 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004367 menu->func(menu->parent, input,
4368 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004369 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004370 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004371 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004372 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004373 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004374}
4375
4376static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004377menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004378{
4379 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004380 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004381 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004382
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004383 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004384
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004385 frame_repaint(menu->frame, cr);
4386 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004387
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004388 theme_set_background_source(menu->window->display->theme,
4389 cr, THEME_FRAME_ACTIVE);
4390 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004391 cairo_fill(cr);
4392
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004393 cairo_select_font_face(cr, "sans",
4394 CAIRO_FONT_SLANT_NORMAL,
4395 CAIRO_FONT_WEIGHT_NORMAL);
4396 cairo_set_font_size(cr, 12);
4397
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004398 for (i = 0; i < menu->count; i++) {
4399 if (i == menu->current) {
4400 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004401 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004402 cairo_fill(cr);
4403 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004404 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004405 cairo_show_text(cr, menu->entries[i]);
4406 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004407 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4408 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004409 cairo_show_text(cr, menu->entries[i]);
4410 }
4411 }
4412
4413 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004414}
4415
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004416void
4417window_show_menu(struct display *display,
4418 struct input *input, uint32_t time, struct window *parent,
4419 int32_t x, int32_t y,
4420 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004421{
4422 struct window *window;
4423 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004424 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004425
4426 menu = malloc(sizeof *menu);
4427 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004428 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004429
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004430 window = window_create_internal(parent->display, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004431 if (!window) {
4432 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004433 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004434 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004435
4436 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004437 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004438 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004439 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4440 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004441 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004442 FRAME_BUTTON_NONE, NULL);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004443 menu->entries = entries;
4444 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004445 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004446 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004447 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004448 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004449 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004450 window->type = TYPE_MENU;
4451 window->x = x;
4452 window->y = y;
4453
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004454 input_ungrab(input);
4455
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004456 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004457 widget_set_enter_handler(menu->widget, menu_enter_handler);
4458 widget_set_leave_handler(menu->widget, menu_leave_handler);
4459 widget_set_motion_handler(menu->widget, menu_motion_handler);
4460 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004461
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004462 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004463 frame_resize_inside(menu->frame, 200, count * 20);
4464 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4465 window_schedule_resize(window, frame_width(menu->frame),
4466 frame_height(menu->frame));
4467
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004468 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4469 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4470 display_get_serial(window->display),
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004471 parent->main_surface->surface,
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004472 window->x - ix, window->y - iy, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004473}
4474
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004475void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004476window_set_buffer_type(struct window *window, enum window_buffer_type type)
4477{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004478 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004479}
4480
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004481void
4482window_set_preferred_format(struct window *window,
4483 enum preferred_format format)
4484{
4485 window->preferred_format = format;
4486}
4487
Pekka Paalanen35e82632013-04-25 13:57:48 +03004488struct widget *
4489window_add_subsurface(struct window *window, void *data,
4490 enum subsurface_mode default_mode)
4491{
4492 struct widget *widget;
4493 struct surface *surface;
4494 struct wl_surface *parent;
4495 struct wl_subcompositor *subcompo = window->display->subcompositor;
4496
Pekka Paalanen35e82632013-04-25 13:57:48 +03004497 surface = surface_create(window);
4498 widget = widget_create(window, surface, data);
4499 wl_list_init(&widget->link);
4500 surface->widget = widget;
4501
4502 parent = window->main_surface->surface;
4503 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4504 surface->surface,
4505 parent);
4506 surface->synchronized = 1;
4507
4508 switch (default_mode) {
4509 case SUBSURFACE_SYNCHRONIZED:
4510 surface->synchronized_default = 1;
4511 break;
4512 case SUBSURFACE_DESYNCHRONIZED:
4513 surface->synchronized_default = 0;
4514 break;
4515 default:
4516 assert(!"bad enum subsurface_mode");
4517 }
4518
4519 return widget;
4520}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004521
4522static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004523display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004524 struct wl_output *wl_output,
4525 int x, int y,
4526 int physical_width,
4527 int physical_height,
4528 int subpixel,
4529 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004530 const char *model,
4531 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004532{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004533 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004534
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004535 output->allocation.x = x;
4536 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004537 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004538}
4539
4540static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004541display_handle_done(void *data,
4542 struct wl_output *wl_output)
4543{
4544}
4545
4546static void
4547display_handle_scale(void *data,
4548 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004549 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004550{
4551 struct output *output = data;
4552
4553 output->scale = scale;
4554}
4555
4556static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004557display_handle_mode(void *data,
4558 struct wl_output *wl_output,
4559 uint32_t flags,
4560 int width,
4561 int height,
4562 int refresh)
4563{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004564 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004565 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004566
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004567 if (flags & WL_OUTPUT_MODE_CURRENT) {
4568 output->allocation.width = width;
4569 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004570 if (display->output_configure_handler)
4571 (*display->output_configure_handler)(
4572 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004573 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004574}
4575
4576static const struct wl_output_listener output_listener = {
4577 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004578 display_handle_mode,
4579 display_handle_done,
4580 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004581};
4582
4583static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004584display_add_output(struct display *d, uint32_t id)
4585{
4586 struct output *output;
4587
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004588 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004589 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004590 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004591 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004592 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004593 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004594 wl_list_insert(d->output_list.prev, &output->link);
4595
4596 wl_output_add_listener(output->output, &output_listener, output);
4597}
4598
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004599static void
4600output_destroy(struct output *output)
4601{
4602 if (output->destroy_handler)
4603 (*output->destroy_handler)(output, output->user_data);
4604
4605 wl_output_destroy(output->output);
4606 wl_list_remove(&output->link);
4607 free(output);
4608}
4609
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004610static void
4611display_destroy_output(struct display *d, uint32_t id)
4612{
4613 struct output *output;
4614
4615 wl_list_for_each(output, &d->output_list, link) {
4616 if (output->server_output_id == id) {
4617 output_destroy(output);
4618 break;
4619 }
4620 }
4621}
4622
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004623void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004624display_set_global_handler(struct display *display,
4625 display_global_handler_t handler)
4626{
4627 struct global *global;
4628
4629 display->global_handler = handler;
4630 if (!handler)
4631 return;
4632
4633 wl_list_for_each(global, &display->global_list, link)
4634 display->global_handler(display,
4635 global->name, global->interface,
4636 global->version, display->user_data);
4637}
4638
4639void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004640display_set_global_handler_remove(struct display *display,
4641 display_global_handler_t remove_handler)
4642{
4643 display->global_handler_remove = remove_handler;
4644 if (!remove_handler)
4645 return;
4646}
4647
4648void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004649display_set_output_configure_handler(struct display *display,
4650 display_output_handler_t handler)
4651{
4652 struct output *output;
4653
4654 display->output_configure_handler = handler;
4655 if (!handler)
4656 return;
4657
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004658 wl_list_for_each(output, &display->output_list, link) {
4659 if (output->allocation.width == 0 &&
4660 output->allocation.height == 0)
4661 continue;
4662
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004663 (*display->output_configure_handler)(output,
4664 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004665 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004666}
4667
4668void
4669output_set_user_data(struct output *output, void *data)
4670{
4671 output->user_data = data;
4672}
4673
4674void *
4675output_get_user_data(struct output *output)
4676{
4677 return output->user_data;
4678}
4679
4680void
4681output_set_destroy_handler(struct output *output,
4682 display_output_handler_t handler)
4683{
4684 output->destroy_handler = handler;
4685 /* FIXME: implement this, once we have way to remove outputs */
4686}
4687
4688void
Scott Moreau4e072362012-09-29 02:03:11 -06004689output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004690{
Scott Moreau4e072362012-09-29 02:03:11 -06004691 struct rectangle allocation = output->allocation;
4692
4693 switch (output->transform) {
4694 case WL_OUTPUT_TRANSFORM_90:
4695 case WL_OUTPUT_TRANSFORM_270:
4696 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4697 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4698 /* Swap width and height */
4699 allocation.width = output->allocation.height;
4700 allocation.height = output->allocation.width;
4701 break;
4702 }
4703
4704 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004705}
4706
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004707struct wl_output *
4708output_get_wl_output(struct output *output)
4709{
4710 return output->output;
4711}
4712
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004713enum wl_output_transform
4714output_get_transform(struct output *output)
4715{
4716 return output->transform;
4717}
4718
Alexander Larssonafd319a2013-05-22 14:41:27 +02004719uint32_t
4720output_get_scale(struct output *output)
4721{
4722 return output->scale;
4723}
4724
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004725static void
Daniel Stone97f68542012-05-30 16:32:01 +01004726fini_xkb(struct input *input)
4727{
4728 xkb_state_unref(input->xkb.state);
4729 xkb_map_unref(input->xkb.keymap);
4730}
4731
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004732#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004733
Daniel Stone97f68542012-05-30 16:32:01 +01004734static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004735display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004736{
4737 struct input *input;
4738
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004739 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004740 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004741 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004742 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004743 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004744 input->pointer_focus = NULL;
4745 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004746 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004747 wl_list_insert(d->input_list.prev, &input->link);
4748
Daniel Stone37816df2012-05-16 18:45:18 +01004749 wl_seat_add_listener(input->seat, &seat_listener, input);
4750 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004751
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004752 input->data_device =
4753 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004754 input->seat);
4755 wl_data_device_add_listener(input->data_device, &data_device_listener,
4756 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004757
4758 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004759
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004760 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4761 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004762 input->repeat_task.run = keyboard_repeat_func;
4763 display_watch_fd(d, input->repeat_timer_fd,
4764 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004765}
4766
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004767static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004768input_destroy(struct input *input)
4769{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004770 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004771 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004772
4773 if (input->drag_offer)
4774 data_offer_destroy(input->drag_offer);
4775
4776 if (input->selection_offer)
4777 data_offer_destroy(input->selection_offer);
4778
4779 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004780
4781 if (input->display->seat_version >= 3) {
4782 if (input->pointer)
4783 wl_pointer_release(input->pointer);
4784 if (input->keyboard)
4785 wl_keyboard_release(input->keyboard);
4786 }
4787
Daniel Stone97f68542012-05-30 16:32:01 +01004788 fini_xkb(input);
4789
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004790 wl_surface_destroy(input->pointer_surface);
4791
Pekka Paalanene1207c72011-12-16 12:02:09 +02004792 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004793 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004794 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004795 free(input);
4796}
4797
4798static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004799init_workspace_manager(struct display *d, uint32_t id)
4800{
4801 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004802 wl_registry_bind(d->registry, id,
4803 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004804 if (d->workspace_manager != NULL)
4805 workspace_manager_add_listener(d->workspace_manager,
4806 &workspace_manager_listener,
4807 d);
4808}
4809
4810static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004811shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4812{
4813 struct display *d = data;
4814
4815 if (format == WL_SHM_FORMAT_RGB565)
4816 d->has_rgb565 = 1;
4817}
4818
4819struct wl_shm_listener shm_listener = {
4820 shm_format
4821};
4822
4823static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004824registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4825 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004826{
4827 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004828 struct global *global;
4829
Brian Lovinbc919262013-08-07 15:34:59 -07004830 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004831 global->name = id;
4832 global->interface = strdup(interface);
4833 global->version = version;
4834 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004835
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004836 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004837 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05004838 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004839 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004840 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004841 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01004842 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004843 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004844 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004845 d->shell = wl_registry_bind(registry,
4846 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004847 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004848 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004849 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004850 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4851 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004852 wl_registry_bind(registry, id,
4853 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004854 } else if (strcmp(interface, "text_cursor_position") == 0) {
4855 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004856 wl_registry_bind(registry, id,
4857 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004858 } else if (strcmp(interface, "workspace_manager") == 0) {
4859 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004860 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4861 d->subcompositor =
4862 wl_registry_bind(registry, id,
4863 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004864 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004865
4866 if (d->global_handler)
4867 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004868}
4869
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004870static void
4871registry_handle_global_remove(void *data, struct wl_registry *registry,
4872 uint32_t name)
4873{
4874 struct display *d = data;
4875 struct global *global;
4876 struct global *tmp;
4877
4878 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4879 if (global->name != name)
4880 continue;
4881
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004882 if (strcmp(global->interface, "wl_output") == 0)
4883 display_destroy_output(d, name);
4884
4885 /* XXX: Should destroy remaining bound globals */
4886
4887 if (d->global_handler_remove)
4888 d->global_handler_remove(d, name, global->interface,
4889 global->version, d->user_data);
4890
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004891 wl_list_remove(&global->link);
4892 free(global->interface);
4893 free(global);
4894 }
4895}
4896
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004897void *
4898display_bind(struct display *display, uint32_t name,
4899 const struct wl_interface *interface, uint32_t version)
4900{
4901 return wl_registry_bind(display->registry, name, interface, version);
4902}
4903
4904static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004905 registry_handle_global,
4906 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004907};
4908
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004909#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004910static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004911init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004912{
4913 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004914 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004915
Rob Clark6396ed32012-03-11 19:48:41 -05004916#ifdef USE_CAIRO_GLESV2
4917# define GL_BIT EGL_OPENGL_ES2_BIT
4918#else
4919# define GL_BIT EGL_OPENGL_BIT
4920#endif
4921
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004922 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004923 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004924 EGL_RED_SIZE, 1,
4925 EGL_GREEN_SIZE, 1,
4926 EGL_BLUE_SIZE, 1,
4927 EGL_ALPHA_SIZE, 1,
4928 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004929 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004930 EGL_NONE
4931 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004932
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004933#ifdef USE_CAIRO_GLESV2
4934 static const EGLint context_attribs[] = {
4935 EGL_CONTEXT_CLIENT_VERSION, 2,
4936 EGL_NONE
4937 };
4938 EGLint api = EGL_OPENGL_ES_API;
4939#else
4940 EGLint *context_attribs = NULL;
4941 EGLint api = EGL_OPENGL_API;
4942#endif
4943
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004944 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004945 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004946 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004947 return -1;
4948 }
4949
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004950 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004951 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004952 return -1;
4953 }
4954
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004955 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4956 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004957 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004958 return -1;
4959 }
4960
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004961 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004962 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004963 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004964 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004965 return -1;
4966 }
4967
Benjamin Franzke0c991632011-09-27 21:57:31 +02004968 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4969 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004970 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004971 return -1;
4972 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004973
4974 return 0;
4975}
4976
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004977static void
4978fini_egl(struct display *display)
4979{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004980 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004981
4982 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4983 EGL_NO_CONTEXT);
4984
4985 eglTerminate(display->dpy);
4986 eglReleaseThread();
4987}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004988#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004989
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004990static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004991init_dummy_surface(struct display *display)
4992{
4993 int len;
4994 void *data;
4995
4996 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4997 data = malloc(len);
4998 display->dummy_surface =
4999 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5000 1, 1, len);
5001 display->dummy_surface_data = data;
5002}
5003
5004static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005005handle_display_data(struct task *task, uint32_t events)
5006{
5007 struct display *display =
5008 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005009 struct epoll_event ep;
5010 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005011
5012 display->display_fd_events = events;
5013
5014 if (events & EPOLLERR || events & EPOLLHUP) {
5015 display_exit(display);
5016 return;
5017 }
5018
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005019 if (events & EPOLLIN) {
5020 ret = wl_display_dispatch(display->display);
5021 if (ret == -1) {
5022 display_exit(display);
5023 return;
5024 }
5025 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005026
5027 if (events & EPOLLOUT) {
5028 ret = wl_display_flush(display->display);
5029 if (ret == 0) {
5030 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5031 ep.data.ptr = &display->display_task;
5032 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5033 display->display_fd, &ep);
5034 } else if (ret == -1 && errno != EAGAIN) {
5035 display_exit(display);
5036 return;
5037 }
5038 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005039}
5040
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005041static void
5042log_handler(const char *format, va_list args)
5043{
5044 vfprintf(stderr, format, args);
5045}
5046
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005047struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005048display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005049{
5050 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005051
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005052 wl_log_set_handler_client(log_handler);
5053
Peter Huttererf3d62272013-08-08 11:57:05 +10005054 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005055 if (d == NULL)
5056 return NULL;
5057
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005058 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005059 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005060 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005061 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005062 return NULL;
5063 }
5064
Rob Bradford5ab9c752013-07-26 16:29:43 +01005065 d->xkb_context = xkb_context_new(0);
5066 if (d->xkb_context == NULL) {
5067 fprintf(stderr, "Failed to create XKB context\n");
5068 free(d);
5069 return NULL;
5070 }
5071
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005072 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005073 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005074 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005075 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5076 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005077
5078 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005079 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005080 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005081 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005082
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005083 d->workspace = 0;
5084 d->workspace_count = 1;
5085
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005086 d->registry = wl_display_get_registry(d->display);
5087 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005088
5089 if (wl_display_dispatch(d->display) < 0) {
5090 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5091 return NULL;
5092 }
5093
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005094#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005095 if (init_egl(d) < 0)
5096 fprintf(stderr, "EGL does not seem to work, "
5097 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005098#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005099
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005100 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005101
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005102 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005103
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005104 wl_list_init(&d->window_list);
5105
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005106 init_dummy_surface(d);
5107
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005108 return d;
5109}
5110
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005111static void
5112display_destroy_outputs(struct display *display)
5113{
5114 struct output *tmp;
5115 struct output *output;
5116
5117 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5118 output_destroy(output);
5119}
5120
Pekka Paalanene1207c72011-12-16 12:02:09 +02005121static void
5122display_destroy_inputs(struct display *display)
5123{
5124 struct input *tmp;
5125 struct input *input;
5126
5127 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5128 input_destroy(input);
5129}
5130
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005131void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005132display_destroy(struct display *display)
5133{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005134 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005135 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5136 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005137
5138 if (!wl_list_empty(&display->deferred_list))
5139 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5140
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005141 cairo_surface_destroy(display->dummy_surface);
5142 free(display->dummy_surface_data);
5143
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005144 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005145 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005146
Daniel Stone97f68542012-05-30 16:32:01 +01005147 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005148
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005149 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005150 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005151
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005152#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005153 if (display->argb_device)
5154 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005155#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005156
Pekka Paalanen35e82632013-04-25 13:57:48 +03005157 if (display->subcompositor)
5158 wl_subcompositor_destroy(display->subcompositor);
5159
Pekka Paalanenc2052982011-12-16 11:41:32 +02005160 if (display->shell)
5161 wl_shell_destroy(display->shell);
5162
5163 if (display->shm)
5164 wl_shm_destroy(display->shm);
5165
5166 if (display->data_device_manager)
5167 wl_data_device_manager_destroy(display->data_device_manager);
5168
5169 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005170 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005171
5172 close(display->epoll_fd);
5173
U. Artie Eoff44874d92012-10-02 21:12:35 -07005174 if (!(display->display_fd_events & EPOLLERR) &&
5175 !(display->display_fd_events & EPOLLHUP))
5176 wl_display_flush(display->display);
5177
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005178 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005179 free(display);
5180}
5181
5182void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005183display_set_user_data(struct display *display, void *data)
5184{
5185 display->user_data = data;
5186}
5187
5188void *
5189display_get_user_data(struct display *display)
5190{
5191 return display->user_data;
5192}
5193
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005194struct wl_display *
5195display_get_display(struct display *display)
5196{
5197 return display->display;
5198}
5199
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005200int
5201display_has_subcompositor(struct display *display)
5202{
5203 if (display->subcompositor)
5204 return 1;
5205
5206 wl_display_roundtrip(display->display);
5207
5208 return display->subcompositor != NULL;
5209}
5210
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005211cairo_device_t *
5212display_get_cairo_device(struct display *display)
5213{
5214 return display->argb_device;
5215}
5216
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005217struct output *
5218display_get_output(struct display *display)
5219{
5220 return container_of(display->output_list.next, struct output, link);
5221}
5222
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005223struct wl_compositor *
5224display_get_compositor(struct display *display)
5225{
5226 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005227}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005228
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005229uint32_t
5230display_get_serial(struct display *display)
5231{
5232 return display->serial;
5233}
5234
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005235EGLDisplay
5236display_get_egl_display(struct display *d)
5237{
5238 return d->dpy;
5239}
5240
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005241struct wl_data_source *
5242display_create_data_source(struct display *display)
5243{
5244 return wl_data_device_manager_create_data_source(display->data_device_manager);
5245}
5246
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005247EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005248display_get_argb_egl_config(struct display *d)
5249{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005250 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005251}
5252
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005253int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005254display_acquire_window_surface(struct display *display,
5255 struct window *window,
5256 EGLContext ctx)
5257{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005258 struct surface *surface = window->main_surface;
5259
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005260 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005261 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005262
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005263 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005264 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005265}
5266
5267void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005268display_release_window_surface(struct display *display,
5269 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005270{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005271 struct surface *surface = window->main_surface;
5272
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005273 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005274 return;
5275
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005276 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005277}
5278
5279void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005280display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005281{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005282 wl_list_insert(&display->deferred_list, &task->link);
5283}
5284
5285void
5286display_watch_fd(struct display *display,
5287 int fd, uint32_t events, struct task *task)
5288{
5289 struct epoll_event ep;
5290
5291 ep.events = events;
5292 ep.data.ptr = task;
5293 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5294}
5295
5296void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005297display_unwatch_fd(struct display *display, int fd)
5298{
5299 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5300}
5301
5302void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005303display_run(struct display *display)
5304{
5305 struct task *task;
5306 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005307 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005308
Pekka Paalanen826d7952011-12-15 10:14:07 +02005309 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005310 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005311 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005312 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005313 struct task, link);
5314 wl_list_remove(&task->link);
5315 task->run(task, 0);
5316 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005317
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005318 wl_display_dispatch_pending(display->display);
5319
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005320 if (!display->running)
5321 break;
5322
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005323 ret = wl_display_flush(display->display);
5324 if (ret < 0 && errno == EAGAIN) {
5325 ep[0].events =
5326 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5327 ep[0].data.ptr = &display->display_task;
5328
5329 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5330 display->display_fd, &ep[0]);
5331 } else if (ret < 0) {
5332 break;
5333 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005334
5335 count = epoll_wait(display->epoll_fd,
5336 ep, ARRAY_LENGTH(ep), -1);
5337 for (i = 0; i < count; i++) {
5338 task = ep[i].data.ptr;
5339 task->run(task, ep[i].events);
5340 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005341 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005342}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005343
5344void
5345display_exit(struct display *display)
5346{
5347 display->running = 0;
5348}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005349
5350void
5351keysym_modifiers_add(struct wl_array *modifiers_map,
5352 const char *name)
5353{
5354 size_t len = strlen(name) + 1;
5355 char *p;
5356
5357 p = wl_array_add(modifiers_map, len);
5358
5359 if (p == NULL)
5360 return;
5361
5362 strncpy(p, name, len);
5363}
5364
5365static xkb_mod_index_t
5366keysym_modifiers_get_index(struct wl_array *modifiers_map,
5367 const char *name)
5368{
5369 xkb_mod_index_t index = 0;
5370 char *p = modifiers_map->data;
5371
5372 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5373 if (strcmp(p, name) == 0)
5374 return index;
5375
5376 index++;
5377 p += strlen(p) + 1;
5378 }
5379
5380 return XKB_MOD_INVALID;
5381}
5382
5383xkb_mod_mask_t
5384keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5385 const char *name)
5386{
5387 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5388
5389 if (index == XKB_MOD_INVALID)
5390 return XKB_MOD_INVALID;
5391
5392 return 1 << index;
5393}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005394
5395void *
5396fail_on_null(void *p)
5397{
5398 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005399 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005400 exit(EXIT_FAILURE);
5401 }
5402
5403 return p;
5404}
5405
5406void *
5407xmalloc(size_t s)
5408{
5409 return fail_on_null(malloc(s));
5410}
5411
Peter Huttererf3d62272013-08-08 11:57:05 +10005412void *
5413xzalloc(size_t s)
5414{
5415 return fail_on_null(zalloc(s));
5416}
5417
Kristian Høgsbergce278412013-07-25 15:20:20 -07005418char *
5419xstrdup(const char *s)
5420{
5421 return fail_on_null(strdup(s));
5422}