blob: a201ebb4cb9298c7a5353583bd92bbe1f243a03e [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025
Kristian Høgsberg61017b12008-11-02 18:51:48 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030029#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050030#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030042#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030055#else /* HAVE_CAIRO_EGL */
56typedef void *EGLDisplay;
57typedef void *EGLConfig;
58typedef void *EGLContext;
59#define EGL_NO_DISPLAY ((EGLDisplay)0)
60#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061
Daniel Stone9d4f0302012-02-15 16:33:21 +000062#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030063#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040064
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020066#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040067#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060068#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020069#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030070#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050071
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050072#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050073
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070074struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030075
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040076struct global {
77 uint32_t name;
78 char *interface;
79 uint32_t version;
80 struct wl_list link;
81};
82
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050083struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050084 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040085 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050086 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030087 struct wl_subcompositor *subcompositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040088 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040093 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050094 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020095 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040097 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040098
99 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700100 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400101 struct task display_task;
102
103 int epoll_fd;
104 struct wl_list deferred_list;
105
Pekka Paalanen826d7952011-12-15 10:14:07 +0200106 int running;
107
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400108 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400109 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400110 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500111 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400112
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400113 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400114
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300115 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300116 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400117
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200118 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400119 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800120 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200121
122 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100123
124 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200125
126 uint32_t workspace;
127 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200128
129 /* A hack to get text extents for tooltips */
130 cairo_surface_t *dummy_surface;
131 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200132
133 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100134 int seat_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500135};
136
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400138 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400139 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400140 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500141 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500143 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400144 TYPE_CUSTOM
145};
Rob Bradford7507b572012-05-15 17:55:34 +0100146
147struct window_output {
148 struct output *output;
149 struct wl_list link;
150};
151
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200152struct toysurface {
153 /*
154 * Prepare the surface for drawing. Makes sure there is a surface
155 * of the right size available for rendering, and returns it.
156 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200157 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200158 * If flags has SURFACE_HINT_RESIZE set, the user is
159 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200160 * Returns the Cairo surface to draw to.
161 */
162 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200163 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200164 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200165
166 /*
167 * Post the surface to the server, returning the server allocation
168 * rectangle. The Cairo surface from prepare() must be destroyed
169 * after calling this.
170 */
171 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200172 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200173 struct rectangle *server_allocation);
174
175 /*
176 * Make the toysurface current with the given EGL context.
177 * Returns 0 on success, and negative of failure.
178 */
179 int (*acquire)(struct toysurface *base, EGLContext ctx);
180
181 /*
182 * Release the toysurface from the EGL context, returning control
183 * to Cairo.
184 */
185 void (*release)(struct toysurface *base);
186
187 /*
188 * Destroy the toysurface, including the Cairo surface, any
189 * backing storage, and the Wayland protocol objects.
190 */
191 void (*destroy)(struct toysurface *base);
192};
193
Pekka Paalanen4e373742013-02-13 16:17:13 +0200194struct surface {
195 struct window *window;
196
197 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300198 struct wl_subsurface *subsurface;
199 int synchronized;
200 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200201 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200202 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300203 int redraw_needed;
204 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300205 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200206
207 struct rectangle allocation;
208 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200209
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200210 struct wl_region *input_region;
211 struct wl_region *opaque_region;
212
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200213 enum window_buffer_type buffer_type;
214 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200215 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200216
217 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300218
219 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200220};
221
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500222struct window {
223 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100224 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500225 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200226 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400227 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500228 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500229 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400230 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400231 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300232 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400233 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400234 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100235 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400236 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400237 int focus_count;
238
Pekka Paalanen99436862012-11-19 17:15:59 +0200239 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200240 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000241 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400242
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200243 enum preferred_format preferred_format;
244
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500245 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500246 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400247 window_data_handler_t data_handler;
248 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500249 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400250 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200251 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400252
Pekka Paalanen4e373742013-02-13 16:17:13 +0200253 struct surface *main_surface;
254 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300255
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500256 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500257
Pekka Paalanen35e82632013-04-25 13:57:48 +0300258 /* struct surface::link, contains also main_surface */
259 struct wl_list subsurface_list;
260
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500261 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400262 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500263};
264
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500265struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500266 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200267 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300268 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500269 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400270 struct wl_list link;
271 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500272 widget_resize_handler_t resize_handler;
273 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500274 widget_enter_handler_t enter_handler;
275 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500276 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500277 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700278 widget_touch_down_handler_t touch_down_handler;
279 widget_touch_up_handler_t touch_up_handler;
280 widget_touch_motion_handler_t touch_motion_handler;
281 widget_touch_frame_handler_t touch_frame_handler;
282 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200283 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400284 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500285 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300286 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500287 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400288};
289
Rusty Lynch041815a2013-08-08 21:20:38 -0700290struct touch_point {
291 int32_t id;
292 struct widget *widget;
293 struct wl_list link;
294};
295
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400296struct input {
297 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400303 struct window *pointer_focus;
304 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300306 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300309 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400310 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400311 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400312 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400313 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400315
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500316 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500317 struct widget *grab;
318 uint32_t grab_button;
319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800323 uint32_t touch_grab;
324 int32_t touch_grab_id;
325 float drag_x, drag_y;
326 struct window *drag_focus;
327 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100328
329 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100330 struct xkb_keymap *keymap;
331 struct xkb_state *state;
332 xkb_mod_mask_t control_mask;
333 xkb_mod_mask_t alt_mask;
334 xkb_mod_mask_t shift_mask;
335 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400336
337 struct task repeat_task;
338 int repeat_timer_fd;
339 uint32_t repeat_sym;
340 uint32_t repeat_key;
341 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400342};
343
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500344struct output {
345 struct display *display;
346 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800347 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500348 struct rectangle allocation;
349 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600350 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200351 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200352
353 display_output_handler_t destroy_handler;
354 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500355};
356
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500357struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500358 struct widget *widget;
359 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500360 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500361};
362
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500363struct menu {
364 struct window *window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -0500365 struct window *parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500366 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500367 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700368 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500369 const char **entries;
370 uint32_t time;
371 int current;
372 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400373 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500374 menu_func_t func;
375};
376
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300377struct tooltip {
378 struct widget *parent;
379 struct window *window;
380 struct widget *widget;
381 char *entry;
382 struct task tooltip_task;
383 int tooltip_fd;
384 float x, y;
385};
386
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700387struct shm_pool {
388 struct wl_shm_pool *pool;
389 size_t size;
390 size_t used;
391 void *data;
392};
393
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400394enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300395 CURSOR_DEFAULT = 100,
396 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400397};
398
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500399enum window_location {
400 WINDOW_INTERIOR = 0,
401 WINDOW_RESIZING_TOP = 1,
402 WINDOW_RESIZING_BOTTOM = 2,
403 WINDOW_RESIZING_LEFT = 4,
404 WINDOW_RESIZING_TOP_LEFT = 5,
405 WINDOW_RESIZING_BOTTOM_LEFT = 6,
406 WINDOW_RESIZING_RIGHT = 8,
407 WINDOW_RESIZING_TOP_RIGHT = 9,
408 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
409 WINDOW_RESIZING_MASK = 15,
410 WINDOW_EXTERIOR = 16,
411 WINDOW_TITLEBAR = 17,
412 WINDOW_CLIENT_AREA = 18,
413};
414
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200415static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400416
Pekka Paalanen97777442013-05-22 10:20:05 +0300417/* #define DEBUG */
418
419#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300420
421static void
422debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
423__attribute__ ((format (printf, 4, 5)));
424
425static void
426debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
427{
428 va_list ap;
429 struct timeval tv;
430
431 gettimeofday(&tv, NULL);
432 fprintf(stderr, "%8ld.%03ld ",
433 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
434
435 if (proxy)
436 fprintf(stderr, "%s@%d ",
437 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
438
439 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
440 fprintf(stderr, "%s ", func);
441
442 va_start(ap, fmt);
443 vfprintf(stderr, fmt, ap);
444 va_end(ap);
445}
446
447#define DBG(fmt, ...) \
448 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
449
450#define DBG_OBJ(obj, fmt, ...) \
451 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
452
453#else
454
455#define DBG(...) do {} while (0)
456#define DBG_OBJ(...) do {} while (0)
457
458#endif
459
Alexander Larsson1818e312013-05-22 14:41:31 +0200460static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200461surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200462{
463 int32_t tmp;
464
465 switch (buffer_transform) {
466 case WL_OUTPUT_TRANSFORM_90:
467 case WL_OUTPUT_TRANSFORM_270:
468 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
469 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
470 tmp = *width;
471 *width = *height;
472 *height = tmp;
473 break;
474 default:
475 break;
476 }
477
478 *width *= buffer_scale;
479 *height *= buffer_scale;
480}
481
482static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200483buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200484{
485 int32_t tmp;
486
487 switch (buffer_transform) {
488 case WL_OUTPUT_TRANSFORM_90:
489 case WL_OUTPUT_TRANSFORM_270:
490 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
491 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
492 tmp = *width;
493 *width = *height;
494 *height = tmp;
495 break;
496 default:
497 break;
498 }
499
500 *width /= buffer_scale;
501 *height /= buffer_scale;
502}
503
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500504#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200506struct egl_window_surface {
507 struct toysurface base;
508 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100509 struct display *display;
510 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200511 struct wl_egl_window *egl_window;
512 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100513};
514
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200515static struct egl_window_surface *
516to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100517{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100519}
520
521static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200523 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200524 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200526 struct egl_window_surface *surface = to_egl_window_surface(base);
527
Alexander Larsson1818e312013-05-22 14:41:31 +0200528 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
529
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
531 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
532
533 return cairo_surface_reference(surface->cairo_surface);
534}
535
536static void
537egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200538 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200539 struct rectangle *server_allocation)
540{
541 struct egl_window_surface *surface = to_egl_window_surface(base);
542
543 cairo_gl_surface_swapbuffers(surface->cairo_surface);
544 wl_egl_window_get_attached_size(surface->egl_window,
545 &server_allocation->width,
546 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200547
548 buffer_to_surface_size (buffer_transform, buffer_scale,
549 &server_allocation->width,
550 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200551}
552
553static int
554egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
555{
556 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200557 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200559 device = cairo_surface_get_device(surface->cairo_surface);
560 if (!device)
561 return -1;
562
563 if (!ctx) {
564 if (device == surface->display->argb_device)
565 ctx = surface->display->argb_ctx;
566 else
567 assert(0);
568 }
569
570 cairo_device_flush(device);
571 cairo_device_acquire(device);
572 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
573 surface->egl_surface, ctx))
574 fprintf(stderr, "failed to make surface current\n");
575
576 return 0;
577}
578
579static void
580egl_window_surface_release(struct toysurface *base)
581{
582 struct egl_window_surface *surface = to_egl_window_surface(base);
583 cairo_device_t *device;
584
585 device = cairo_surface_get_device(surface->cairo_surface);
586 if (!device)
587 return;
588
589 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
590 surface->display->argb_ctx))
591 fprintf(stderr, "failed to make context current\n");
592
593 cairo_device_release(device);
594}
595
596static void
597egl_window_surface_destroy(struct toysurface *base)
598{
599 struct egl_window_surface *surface = to_egl_window_surface(base);
600 struct display *d = surface->display;
601
602 cairo_surface_destroy(surface->cairo_surface);
603 eglDestroySurface(d->dpy, surface->egl_surface);
604 wl_egl_window_destroy(surface->egl_window);
605 surface->surface = NULL;
606
607 free(surface);
608}
609
610static struct toysurface *
611egl_window_surface_create(struct display *display,
612 struct wl_surface *wl_surface,
613 uint32_t flags,
614 struct rectangle *rectangle)
615{
616 struct egl_window_surface *surface;
617
Pekka Paalanenb3627362012-11-19 17:16:00 +0200618 if (display->dpy == EGL_NO_DISPLAY)
619 return NULL;
620
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200621 surface = calloc(1, sizeof *surface);
622 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100623 return NULL;
624
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200625 surface->base.prepare = egl_window_surface_prepare;
626 surface->base.swap = egl_window_surface_swap;
627 surface->base.acquire = egl_window_surface_acquire;
628 surface->base.release = egl_window_surface_release;
629 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100630
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200631 surface->display = display;
632 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400633
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200634 surface->egl_window = wl_egl_window_create(surface->surface,
635 rectangle->width,
636 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100637
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200638 surface->egl_surface = eglCreateWindowSurface(display->dpy,
639 display->argb_config,
640 surface->egl_window,
641 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200643 surface->cairo_surface =
644 cairo_gl_surface_create_for_egl(display->argb_device,
645 surface->egl_surface,
646 rectangle->width,
647 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100648
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200649 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650}
651
Pekka Paalanenb3627362012-11-19 17:16:00 +0200652#else
653
654static struct toysurface *
655egl_window_surface_create(struct display *display,
656 struct wl_surface *wl_surface,
657 uint32_t flags,
658 struct rectangle *rectangle)
659{
660 return NULL;
661}
662
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400663#endif
664
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200665struct shm_surface_data {
666 struct wl_buffer *buffer;
667 struct shm_pool *pool;
668};
669
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670struct wl_buffer *
671display_get_buffer_for_surface(struct display *display,
672 cairo_surface_t *surface)
673{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200674 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200676 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400677
678 return data->buffer;
679}
680
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500681static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700682shm_pool_destroy(struct shm_pool *pool);
683
684static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400685shm_surface_data_destroy(void *p)
686{
687 struct shm_surface_data *data = p;
688
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200689 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700690 if (data->pool)
691 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300692
693 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400694}
695
Kristian Høgsberg16626282012-04-03 11:21:27 -0400696static struct wl_shm_pool *
697make_shm_pool(struct display *display, int size, void **data)
698{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400699 struct wl_shm_pool *pool;
700 int fd;
701
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300702 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400703 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300704 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
705 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400706 return NULL;
707 }
708
709 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400710 if (*data == MAP_FAILED) {
711 fprintf(stderr, "mmap failed: %m\n");
712 close(fd);
713 return NULL;
714 }
715
716 pool = wl_shm_create_pool(display->shm, fd, size);
717
718 close(fd);
719
720 return pool;
721}
722
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723static struct shm_pool *
724shm_pool_create(struct display *display, size_t size)
725{
726 struct shm_pool *pool = malloc(sizeof *pool);
727
728 if (!pool)
729 return NULL;
730
731 pool->pool = make_shm_pool(display, size, &pool->data);
732 if (!pool->pool) {
733 free(pool);
734 return NULL;
735 }
736
737 pool->size = size;
738 pool->used = 0;
739
740 return pool;
741}
742
743static void *
744shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
745{
746 if (pool->used + size > pool->size)
747 return NULL;
748
749 *offset = pool->used;
750 pool->used += size;
751
752 return (char *) pool->data + *offset;
753}
754
755/* destroy the pool. this does not unmap the memory though */
756static void
757shm_pool_destroy(struct shm_pool *pool)
758{
759 munmap(pool->data, pool->size);
760 wl_shm_pool_destroy(pool->pool);
761 free(pool);
762}
763
764/* Start allocating from the beginning of the pool again */
765static void
766shm_pool_reset(struct shm_pool *pool)
767{
768 pool->used = 0;
769}
770
771static int
772data_length_for_shm_surface(struct rectangle *rect)
773{
774 int stride;
775
776 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
777 rect->width);
778 return stride * rect->height;
779}
780
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500781static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700782display_create_shm_surface_from_pool(struct display *display,
783 struct rectangle *rectangle,
784 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400785{
786 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400787 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400788 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200789 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700790 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400791 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400792
793 data = malloc(sizeof *data);
794 if (data == NULL)
795 return NULL;
796
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200797 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
798 cairo_format = CAIRO_FORMAT_RGB16_565;
799 else
800 cairo_format = CAIRO_FORMAT_ARGB32;
801
802 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700803 length = stride * rectangle->height;
804 data->pool = NULL;
805 map = shm_pool_allocate(pool, length, &offset);
806
807 if (!map) {
808 free(data);
809 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400810 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400811
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400812 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200813 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400814 rectangle->width,
815 rectangle->height,
816 stride);
817
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200818 cairo_surface_set_user_data(surface, &shm_surface_data_key,
819 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400820
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200821 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
822 format = WL_SHM_FORMAT_RGB565;
823 else {
824 if (flags & SURFACE_OPAQUE)
825 format = WL_SHM_FORMAT_XRGB8888;
826 else
827 format = WL_SHM_FORMAT_ARGB8888;
828 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400829
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200830 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
831 rectangle->width,
832 rectangle->height,
833 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700835 return surface;
836}
837
838static cairo_surface_t *
839display_create_shm_surface(struct display *display,
840 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200841 struct shm_pool *alternate_pool,
842 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700843{
844 struct shm_surface_data *data;
845 struct shm_pool *pool;
846 cairo_surface_t *surface;
847
Pekka Paalanen99436862012-11-19 17:15:59 +0200848 if (alternate_pool) {
849 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700850 surface = display_create_shm_surface_from_pool(display,
851 rectangle,
852 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200853 alternate_pool);
854 if (surface) {
855 data = cairo_surface_get_user_data(surface,
856 &shm_surface_data_key);
857 goto out;
858 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700859 }
860
861 pool = shm_pool_create(display,
862 data_length_for_shm_surface(rectangle));
863 if (!pool)
864 return NULL;
865
866 surface =
867 display_create_shm_surface_from_pool(display, rectangle,
868 flags, pool);
869
870 if (!surface) {
871 shm_pool_destroy(pool);
872 return NULL;
873 }
874
875 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200876 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700877 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400878
Pekka Paalanen99436862012-11-19 17:15:59 +0200879out:
880 if (data_ret)
881 *data_ret = data;
882
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400883 return surface;
884}
885
nobled7b87cb02011-02-01 18:51:47 +0000886static int
887check_size(struct rectangle *rect)
888{
889 if (rect->width && rect->height)
890 return 0;
891
892 fprintf(stderr, "tried to create surface of "
893 "width: %d, height: %d\n", rect->width, rect->height);
894 return -1;
895}
896
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400897cairo_surface_t *
898display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100899 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400900 struct rectangle *rectangle,
901 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400902{
nobled7b87cb02011-02-01 18:51:47 +0000903 if (check_size(rectangle) < 0)
904 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200905
906 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200907 return display_create_shm_surface(display, rectangle, flags,
908 NULL, NULL);
909}
910
Pekka Paalanena4eda732012-11-19 17:16:02 +0200911struct shm_surface_leaf {
912 cairo_surface_t *cairo_surface;
913 /* 'data' is automatically destroyed, when 'cairo_surface' is */
914 struct shm_surface_data *data;
915
916 struct shm_pool *resize_pool;
917 int busy;
918};
919
920static void
921shm_surface_leaf_release(struct shm_surface_leaf *leaf)
922{
923 if (leaf->cairo_surface)
924 cairo_surface_destroy(leaf->cairo_surface);
925 /* leaf->data already destroyed via cairo private */
926
927 if (leaf->resize_pool)
928 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200929
930 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200931}
932
Pekka Paalanenaef02542013-04-25 13:57:47 +0300933#define MAX_LEAVES 3
934
Pekka Paalanen99436862012-11-19 17:15:59 +0200935struct shm_surface {
936 struct toysurface base;
937 struct display *display;
938 struct wl_surface *surface;
939 uint32_t flags;
940 int dx, dy;
941
Pekka Paalanenaef02542013-04-25 13:57:47 +0300942 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200943 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200944};
945
946static struct shm_surface *
947to_shm_surface(struct toysurface *base)
948{
949 return container_of(base, struct shm_surface, base);
950}
951
Pekka Paalanena4eda732012-11-19 17:16:02 +0200952static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300953shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
954{
955#ifdef DEBUG
956 struct shm_surface_leaf *leaf;
957 char bufs[MAX_LEAVES + 1];
958 int i;
959
960 for (i = 0; i < MAX_LEAVES; i++) {
961 leaf = &surface->leaf[i];
962
963 if (leaf->busy)
964 bufs[i] = 'b';
965 else if (leaf->cairo_surface)
966 bufs[i] = 'a';
967 else
968 bufs[i] = ' ';
969 }
970
971 bufs[MAX_LEAVES] = '\0';
972 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
973#endif
974}
975
976static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200977shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
978{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200979 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300980 struct shm_surface_leaf *leaf;
981 int i;
982 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300983
984 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200985
Pekka Paalanenaef02542013-04-25 13:57:47 +0300986 for (i = 0; i < MAX_LEAVES; i++) {
987 leaf = &surface->leaf[i];
988 if (leaf->data && leaf->data->buffer == buffer) {
989 leaf->busy = 0;
990 break;
991 }
992 }
993 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200994
Pekka Paalanenaef02542013-04-25 13:57:47 +0300995 /* Leave one free leaf with storage, release others */
996 free_found = 0;
997 for (i = 0; i < MAX_LEAVES; i++) {
998 leaf = &surface->leaf[i];
999
1000 if (!leaf->cairo_surface || leaf->busy)
1001 continue;
1002
1003 if (!free_found)
1004 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001005 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001006 shm_surface_leaf_release(leaf);
1007 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001008
Pekka Paalanen97777442013-05-22 10:20:05 +03001009 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001010}
1011
1012static const struct wl_buffer_listener shm_surface_buffer_listener = {
1013 shm_surface_buffer_release
1014};
1015
Pekka Paalanen99436862012-11-19 17:15:59 +02001016static cairo_surface_t *
1017shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001018 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001019 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001020{
Pekka Paalanenec076692012-11-30 13:37:27 +02001021 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001022 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001023 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001024 struct shm_surface_leaf *leaf = NULL;
1025 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001026
1027 surface->dx = dx;
1028 surface->dy = dy;
1029
Pekka Paalanenaef02542013-04-25 13:57:47 +03001030 /* pick a free buffer, preferrably one that already has storage */
1031 for (i = 0; i < MAX_LEAVES; i++) {
1032 if (surface->leaf[i].busy)
1033 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001034
Pekka Paalanenaef02542013-04-25 13:57:47 +03001035 if (!leaf || surface->leaf[i].cairo_surface)
1036 leaf = &surface->leaf[i];
1037 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001038 DBG_OBJ(surface->surface, "pick leaf %d\n",
1039 (int)(leaf - &surface->leaf[0]));
1040
Pekka Paalanenaef02542013-04-25 13:57:47 +03001041 if (!leaf) {
1042 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001043 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001044 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001045 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001046 }
1047
Pekka Paalanena4eda732012-11-19 17:16:02 +02001048 if (!resize_hint && leaf->resize_pool) {
1049 cairo_surface_destroy(leaf->cairo_surface);
1050 leaf->cairo_surface = NULL;
1051 shm_pool_destroy(leaf->resize_pool);
1052 leaf->resize_pool = NULL;
1053 }
1054
Alexander Larsson1818e312013-05-22 14:41:31 +02001055 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1056
Pekka Paalanena4eda732012-11-19 17:16:02 +02001057 if (leaf->cairo_surface &&
1058 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1059 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001060 goto out;
1061
Pekka Paalanena4eda732012-11-19 17:16:02 +02001062 if (leaf->cairo_surface)
1063 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001064
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001065#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001066 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001067 /* Create a big pool to allocate from, while continuously
1068 * resizing. Mmapping a new pool in the server
1069 * is relatively expensive, so reusing a pool performs
1070 * better, but may temporarily reserve unneeded memory.
1071 */
1072 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001073 leaf->resize_pool = shm_pool_create(surface->display,
1074 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001075 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001076#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001077
Alexander Larsson1818e312013-05-22 14:41:31 +02001078 rect.width = width;
1079 rect.height = height;
1080
Pekka Paalanena4eda732012-11-19 17:16:02 +02001081 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001082 display_create_shm_surface(surface->display, &rect,
1083 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001084 leaf->resize_pool,
1085 &leaf->data);
1086 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001087 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001088
1089out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001090 surface->current = leaf;
1091
1092 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001093}
1094
1095static void
1096shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001097 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001098 struct rectangle *server_allocation)
1099{
1100 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001101 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001102
1103 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001105 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001106 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001107
Alexander Larsson1818e312013-05-22 14:41:31 +02001108 buffer_to_surface_size (buffer_transform, buffer_scale,
1109 &server_allocation->width,
1110 &server_allocation->height);
1111
Pekka Paalanena4eda732012-11-19 17:16:02 +02001112 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001113 surface->dx, surface->dy);
1114 wl_surface_damage(surface->surface, 0, 0,
1115 server_allocation->width, server_allocation->height);
1116 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001117
Pekka Paalanen71233882013-04-25 13:57:53 +03001118 DBG_OBJ(surface->surface, "leaf %d busy\n",
1119 (int)(leaf - &surface->leaf[0]));
1120
Pekka Paalanena4eda732012-11-19 17:16:02 +02001121 leaf->busy = 1;
1122 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001123}
1124
1125static int
1126shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1127{
1128 return -1;
1129}
1130
1131static void
1132shm_surface_release(struct toysurface *base)
1133{
1134}
1135
1136static void
1137shm_surface_destroy(struct toysurface *base)
1138{
1139 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001140 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001141
Pekka Paalanenaef02542013-04-25 13:57:47 +03001142 for (i = 0; i < MAX_LEAVES; i++)
1143 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001144
1145 free(surface);
1146}
1147
1148static struct toysurface *
1149shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1150 uint32_t flags, struct rectangle *rectangle)
1151{
1152 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001153 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001154
Brian Lovinbc919262013-08-07 15:34:59 -07001155 surface = xmalloc(sizeof *surface);
1156 memset(surface, 0, sizeof *surface);
1157
Pekka Paalanen99436862012-11-19 17:15:59 +02001158 if (!surface)
1159 return NULL;
1160
1161 surface->base.prepare = shm_surface_prepare;
1162 surface->base.swap = shm_surface_swap;
1163 surface->base.acquire = shm_surface_acquire;
1164 surface->base.release = shm_surface_release;
1165 surface->base.destroy = shm_surface_destroy;
1166
1167 surface->display = display;
1168 surface->surface = wl_surface;
1169 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001170
1171 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001172}
1173
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001174/*
1175 * The following correspondences between file names and cursors was copied
1176 * from: https://bugs.kde.org/attachment.cgi?id=67313
1177 */
1178
1179static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001180 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001181 "sw-resize",
1182 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001183};
1184
1185static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001186 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001187 "se-resize",
1188 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001189};
1190
1191static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001192 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001193 "s-resize",
1194 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001195};
1196
1197static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001198 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001199 "closedhand",
1200 "208530c400c041818281048008011002"
1201};
1202
1203static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001204 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001205 "default",
1206 "top_left_arrow",
1207 "left-arrow"
1208};
1209
1210static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001211 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001212 "w-resize",
1213 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001214};
1215
1216static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001217 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001218 "e-resize",
1219 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001220};
1221
1222static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001223 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001224 "nw-resize",
1225 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001226};
1227
1228static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001229 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001230 "ne-resize",
1231 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001232};
1233
1234static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001235 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001236 "n-resize",
1237 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001238};
1239
1240static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001241 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242 "ibeam",
1243 "text"
1244};
1245
1246static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001247 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248 "pointer",
1249 "pointing_hand",
1250 "e29285e634086352946a0e7090d73106"
1251};
1252
1253static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001254 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001255 "wait",
1256 "0426c94ea35c87780ff01dc239897213"
1257};
1258
1259struct cursor_alternatives {
1260 const char **names;
1261 size_t count;
1262};
1263
1264static const struct cursor_alternatives cursors[] = {
1265 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1266 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1267 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1268 {grabbings, ARRAY_LENGTH(grabbings)},
1269 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1270 {left_sides, ARRAY_LENGTH(left_sides)},
1271 {right_sides, ARRAY_LENGTH(right_sides)},
1272 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1273 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1274 {top_sides, ARRAY_LENGTH(top_sides)},
1275 {xterms, ARRAY_LENGTH(xterms)},
1276 {hand1s, ARRAY_LENGTH(hand1s)},
1277 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001278};
1279
1280static void
1281create_cursors(struct display *display)
1282{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001283 struct weston_config *config;
1284 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001285 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001286 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001287 unsigned int i, j;
1288 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001289
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001290 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001291 s = weston_config_get_section(config, "shell", NULL, NULL);
1292 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1293 weston_config_section_get_int(s, "cursor-size", &size, 32);
1294 weston_config_destroy(config);
1295
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001296 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001297 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001298 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001299 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001300
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001301 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001302 cursor = NULL;
1303 for (j = 0; !cursor && j < cursors[i].count; ++j)
1304 cursor = wl_cursor_theme_get_cursor(
1305 display->cursor_theme, cursors[i].names[j]);
1306
1307 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001308 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001309 cursors[i].names[0]);
1310
1311 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001312 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001313}
1314
1315static void
1316destroy_cursors(struct display *display)
1317{
1318 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001319 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001320}
1321
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001322struct wl_cursor_image *
1323display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001324{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001325 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001326
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001327 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001328}
1329
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001330static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001331surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001332{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001333 if (!surface->cairo_surface)
1334 return;
1335
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001336 if (surface->opaque_region) {
1337 wl_surface_set_opaque_region(surface->surface,
1338 surface->opaque_region);
1339 wl_region_destroy(surface->opaque_region);
1340 surface->opaque_region = NULL;
1341 }
1342
1343 if (surface->input_region) {
1344 wl_surface_set_input_region(surface->surface,
1345 surface->input_region);
1346 wl_region_destroy(surface->input_region);
1347 surface->input_region = NULL;
1348 }
1349
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001350 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001351 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001352 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001353
Pekka Paalanen89dee002013-02-13 16:17:20 +02001354 cairo_surface_destroy(surface->cairo_surface);
1355 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001356}
1357
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001358int
1359window_has_focus(struct window *window)
1360{
1361 return window->focus_count > 0;
1362}
1363
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001364static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001365window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001366{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001367 struct surface *surface;
1368
Pekka Paalanen89dee002013-02-13 16:17:20 +02001369 if (window->type == TYPE_NONE) {
1370 window->type = TYPE_TOPLEVEL;
1371 if (window->shell_surface)
1372 wl_shell_surface_set_toplevel(window->shell_surface);
1373 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001374
Pekka Paalanen35e82632013-04-25 13:57:48 +03001375 wl_list_for_each(surface, &window->subsurface_list, link) {
1376 if (surface == window->main_surface)
1377 continue;
1378
1379 surface_flush(surface);
1380 }
1381
Pekka Paalanen89dee002013-02-13 16:17:20 +02001382 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001383}
1384
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001385struct display *
1386window_get_display(struct window *window)
1387{
1388 return window->display;
1389}
1390
Pekka Paalanen89dee002013-02-13 16:17:20 +02001391static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001392surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001393{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001394 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001395 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001396
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001397 if (!surface->toysurface && display->dpy &&
1398 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001399 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001400 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001401 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001402 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001403 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001404 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001405
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001406 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001408 surface->surface,
1409 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001410
Pekka Paalanen89dee002013-02-13 16:17:20 +02001411 surface->cairo_surface = surface->toysurface->prepare(
1412 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001413 allocation.width, allocation.height, flags,
1414 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001415}
1416
1417static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001418window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001419{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001420 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001421 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001422 int dx = 0;
1423 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001424
Pekka Paalanenec076692012-11-30 13:37:27 +02001425 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001426 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001427
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001428 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1429 flags |= SURFACE_HINT_RGB565;
1430
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001431 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1432 dx = surface->server_allocation.width -
1433 surface->allocation.width;
1434
1435 if (window->resize_edges & WINDOW_RESIZING_TOP)
1436 dy = surface->server_allocation.height -
1437 surface->allocation.height;
1438
1439 window->resize_edges = 0;
1440
Pekka Paalanen89dee002013-02-13 16:17:20 +02001441 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001442}
1443
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001444int
1445window_get_buffer_transform(struct window *window)
1446{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001447 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001448}
1449
1450void
1451window_set_buffer_transform(struct window *window,
1452 enum wl_output_transform transform)
1453{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001454 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001455 wl_surface_set_buffer_transform(window->main_surface->surface,
1456 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001457}
1458
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001459void
1460window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001461 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001462{
1463 window->main_surface->buffer_scale = scale;
1464 wl_surface_set_buffer_scale(window->main_surface->surface,
1465 scale);
1466}
1467
1468uint32_t
1469window_get_buffer_scale(struct window *window)
1470{
1471 return window->main_surface->buffer_scale;
1472}
1473
Alexander Larssond68f5232013-05-22 14:41:33 +02001474uint32_t
1475window_get_output_scale(struct window *window)
1476{
1477 struct window_output *window_output;
1478 struct window_output *window_output_tmp;
1479 int scale = 1;
1480
1481 wl_list_for_each_safe(window_output, window_output_tmp,
1482 &window->window_output_list, link) {
1483 if (window_output->output->scale > scale)
1484 scale = window_output->output->scale;
1485 }
1486
1487 return scale;
1488}
1489
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001490static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001491
Pekka Paalanen4e373742013-02-13 16:17:13 +02001492static void
1493surface_destroy(struct surface *surface)
1494{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001495 if (surface->frame_cb)
1496 wl_callback_destroy(surface->frame_cb);
1497
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001498 if (surface->input_region)
1499 wl_region_destroy(surface->input_region);
1500
1501 if (surface->opaque_region)
1502 wl_region_destroy(surface->opaque_region);
1503
Pekka Paalanen35e82632013-04-25 13:57:48 +03001504 if (surface->subsurface)
1505 wl_subsurface_destroy(surface->subsurface);
1506
Pekka Paalanen4e373742013-02-13 16:17:13 +02001507 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001508
1509 if (surface->toysurface)
1510 surface->toysurface->destroy(surface->toysurface);
1511
Pekka Paalanen35e82632013-04-25 13:57:48 +03001512 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001513 free(surface);
1514}
1515
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001516void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001517window_destroy(struct window *window)
1518{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001519 struct display *display = window->display;
1520 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001521 struct window_output *window_output;
1522 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001523
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001524 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001525
Rusty Lynch1084da52013-08-15 09:10:08 -07001526 wl_list_for_each(input, &display->input_list, link) {
1527 if (input->touch_focus == window)
1528 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001529 if (input->pointer_focus == window)
1530 input->pointer_focus = NULL;
1531 if (input->keyboard_focus == window)
1532 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001533 if (input->focus_widget &&
1534 input->focus_widget->window == window)
1535 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001536 }
1537
Rob Bradford7507b572012-05-15 17:55:34 +01001538 wl_list_for_each_safe(window_output, window_output_tmp,
1539 &window->window_output_list, link) {
1540 free (window_output);
1541 }
1542
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001543 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001544 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001545
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001546 if (window->shell_surface)
1547 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001548
1549 surface_destroy(window->main_surface);
1550
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001551 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001552
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001553 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001554 free(window);
1555}
1556
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001557static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001558widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001559{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001560 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001561
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001562 wl_list_for_each(child, &widget->child_list, link) {
1563 target = widget_find_widget(child, x, y);
1564 if (target)
1565 return target;
1566 }
1567
1568 if (widget->allocation.x <= x &&
1569 x < widget->allocation.x + widget->allocation.width &&
1570 widget->allocation.y <= y &&
1571 y < widget->allocation.y + widget->allocation.height) {
1572 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001573 }
1574
1575 return NULL;
1576}
1577
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001578static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001579window_find_widget(struct window *window, int32_t x, int32_t y)
1580{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001581 struct surface *surface;
1582 struct widget *widget;
1583
1584 wl_list_for_each(surface, &window->subsurface_list, link) {
1585 widget = widget_find_widget(surface->widget, x, y);
1586 if (widget)
1587 return widget;
1588 }
1589
1590 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001591}
1592
1593static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001594widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001595{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001596 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001597
Peter Huttererf3d62272013-08-08 11:57:05 +10001598 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001599 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001600 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001601 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001602 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001603 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001604 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001605 widget->tooltip = NULL;
1606 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001607 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001608
1609 return widget;
1610}
1611
1612struct widget *
1613window_add_widget(struct window *window, void *data)
1614{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001615 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001616
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001617 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001618 wl_list_init(&widget->link);
1619 window->main_surface->widget = widget;
1620
1621 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001622}
1623
1624struct widget *
1625widget_add_widget(struct widget *parent, void *data)
1626{
1627 struct widget *widget;
1628
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001629 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001630 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001631
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001632 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001633}
1634
1635void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001636widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001637{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001638 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001639 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001640 struct input *input;
1641
Pekka Paalanen35e82632013-04-25 13:57:48 +03001642 /* Destroy the sub-surface along with the root widget */
1643 if (surface->widget == widget && surface->subsurface)
1644 surface_destroy(widget->surface);
1645
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001646 if (widget->tooltip) {
1647 free(widget->tooltip);
1648 widget->tooltip = NULL;
1649 }
1650
Pekka Paalanene156fb62012-01-19 13:51:38 +02001651 wl_list_for_each(input, &display->input_list, link) {
1652 if (input->focus_widget == widget)
1653 input->focus_widget = NULL;
1654 }
1655
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001656 wl_list_remove(&widget->link);
1657 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001658}
1659
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001660void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001661widget_set_default_cursor(struct widget *widget, int cursor)
1662{
1663 widget->default_cursor = cursor;
1664}
1665
1666void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001667widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001668{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001669 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001670}
1671
1672void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001673widget_set_size(struct widget *widget, int32_t width, int32_t height)
1674{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001675 widget->allocation.width = width;
1676 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001677}
1678
1679void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001680widget_set_allocation(struct widget *widget,
1681 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001682{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001683 widget->allocation.x = x;
1684 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001685 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686}
1687
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001688void
1689widget_set_transparent(struct widget *widget, int transparent)
1690{
1691 widget->opaque = !transparent;
1692}
1693
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001694void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001695widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001696{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001697 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001698}
1699
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001700static cairo_surface_t *
1701widget_get_cairo_surface(struct widget *widget)
1702{
1703 struct surface *surface = widget->surface;
1704 struct window *window = widget->window;
1705
1706 if (!surface->cairo_surface) {
1707 if (surface == window->main_surface)
1708 window_create_main_surface(window);
1709 else
1710 surface_create_surface(surface, 0, 0, 0);
1711 }
1712
1713 return surface->cairo_surface;
1714}
1715
Alexander Larsson15901f02013-05-22 14:41:25 +02001716static void
1717widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1718{
1719 struct surface *surface = widget->surface;
1720 double angle;
1721 cairo_matrix_t m;
1722 enum wl_output_transform transform;
1723 int surface_width, surface_height;
1724 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001725 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001726
1727 surface_width = surface->allocation.width;
1728 surface_height = surface->allocation.height;
1729
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001730 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001731 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001732
Alexander Larsson15901f02013-05-22 14:41:25 +02001733 switch (transform) {
1734 case WL_OUTPUT_TRANSFORM_FLIPPED:
1735 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1736 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1737 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1738 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1739 break;
1740 default:
1741 cairo_matrix_init_identity(&m);
1742 break;
1743 }
1744
1745 switch (transform) {
1746 case WL_OUTPUT_TRANSFORM_NORMAL:
1747 default:
1748 angle = 0;
1749 translate_x = 0;
1750 translate_y = 0;
1751 break;
1752 case WL_OUTPUT_TRANSFORM_FLIPPED:
1753 angle = 0;
1754 translate_x = surface_width;
1755 translate_y = 0;
1756 break;
1757 case WL_OUTPUT_TRANSFORM_90:
1758 angle = M_PI_2;
1759 translate_x = surface_height;
1760 translate_y = 0;
1761 break;
1762 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1763 angle = M_PI_2;
1764 translate_x = surface_height;
1765 translate_y = surface_width;
1766 break;
1767 case WL_OUTPUT_TRANSFORM_180:
1768 angle = M_PI;
1769 translate_x = surface_width;
1770 translate_y = surface_height;
1771 break;
1772 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1773 angle = M_PI;
1774 translate_x = 0;
1775 translate_y = surface_height;
1776 break;
1777 case WL_OUTPUT_TRANSFORM_270:
1778 angle = M_PI + M_PI_2;
1779 translate_x = 0;
1780 translate_y = surface_width;
1781 break;
1782 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1783 angle = M_PI + M_PI_2;
1784 translate_x = 0;
1785 translate_y = 0;
1786 break;
1787 }
1788
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001789 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001790 cairo_translate(cr, translate_x, translate_y);
1791 cairo_rotate(cr, angle);
1792 cairo_transform(cr, &m);
1793}
1794
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001795cairo_t *
1796widget_cairo_create(struct widget *widget)
1797{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001798 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001799 cairo_surface_t *cairo_surface;
1800 cairo_t *cr;
1801
1802 cairo_surface = widget_get_cairo_surface(widget);
1803 cr = cairo_create(cairo_surface);
1804
Alexander Larsson15901f02013-05-22 14:41:25 +02001805 widget_cairo_update_transform(widget, cr);
1806
Pekka Paalanen35e82632013-04-25 13:57:48 +03001807 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1808
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001809 return cr;
1810}
1811
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001812struct wl_surface *
1813widget_get_wl_surface(struct widget *widget)
1814{
1815 return widget->surface->surface;
1816}
1817
1818uint32_t
1819widget_get_last_time(struct widget *widget)
1820{
1821 return widget->surface->last_time;
1822}
1823
1824void
1825widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1826{
1827 struct wl_compositor *comp = widget->window->display->compositor;
1828 struct surface *surface = widget->surface;
1829
1830 if (!surface->input_region)
1831 surface->input_region = wl_compositor_create_region(comp);
1832
1833 if (rect) {
1834 wl_region_add(surface->input_region,
1835 rect->x, rect->y, rect->width, rect->height);
1836 }
1837}
1838
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001839void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001840widget_set_resize_handler(struct widget *widget,
1841 widget_resize_handler_t handler)
1842{
1843 widget->resize_handler = handler;
1844}
1845
1846void
1847widget_set_redraw_handler(struct widget *widget,
1848 widget_redraw_handler_t handler)
1849{
1850 widget->redraw_handler = handler;
1851}
1852
1853void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001854widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001855{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001856 widget->enter_handler = handler;
1857}
1858
1859void
1860widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1861{
1862 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001863}
1864
1865void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001866widget_set_motion_handler(struct widget *widget,
1867 widget_motion_handler_t handler)
1868{
1869 widget->motion_handler = handler;
1870}
1871
1872void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001873widget_set_button_handler(struct widget *widget,
1874 widget_button_handler_t handler)
1875{
1876 widget->button_handler = handler;
1877}
1878
1879void
Rusty Lynch041815a2013-08-08 21:20:38 -07001880widget_set_touch_up_handler(struct widget *widget,
1881 widget_touch_up_handler_t handler)
1882{
1883 widget->touch_up_handler = handler;
1884}
1885
1886void
1887widget_set_touch_down_handler(struct widget *widget,
1888 widget_touch_down_handler_t handler)
1889{
1890 widget->touch_down_handler = handler;
1891}
1892
1893void
1894widget_set_touch_motion_handler(struct widget *widget,
1895 widget_touch_motion_handler_t handler)
1896{
1897 widget->touch_motion_handler = handler;
1898}
1899
1900void
1901widget_set_touch_frame_handler(struct widget *widget,
1902 widget_touch_frame_handler_t handler)
1903{
1904 widget->touch_frame_handler = handler;
1905}
1906
1907void
1908widget_set_touch_cancel_handler(struct widget *widget,
1909 widget_touch_cancel_handler_t handler)
1910{
1911 widget->touch_cancel_handler = handler;
1912}
1913
1914void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001915widget_set_axis_handler(struct widget *widget,
1916 widget_axis_handler_t handler)
1917{
1918 widget->axis_handler = handler;
1919}
1920
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001921static void
1922window_schedule_redraw_task(struct window *window);
1923
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001924void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001925widget_schedule_redraw(struct widget *widget)
1926{
Pekka Paalanen71233882013-04-25 13:57:53 +03001927 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001928 widget->surface->redraw_needed = 1;
1929 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001930}
1931
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001932cairo_surface_t *
1933window_get_surface(struct window *window)
1934{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001935 cairo_surface_t *cairo_surface;
1936
1937 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1938
1939 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001940}
1941
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001942struct wl_surface *
1943window_get_wl_surface(struct window *window)
1944{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001945 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001946}
1947
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001948static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001949tooltip_redraw_handler(struct widget *widget, void *data)
1950{
1951 cairo_t *cr;
1952 const int32_t r = 3;
1953 struct tooltip *tooltip = data;
1954 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001955
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001956 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001957 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1958 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1959 cairo_paint(cr);
1960
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001961 width = widget->allocation.width;
1962 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001963 rounded_rect(cr, 0, 0, width, height, r);
1964
1965 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1966 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1967 cairo_fill(cr);
1968
1969 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1970 cairo_move_to(cr, 10, 16);
1971 cairo_show_text(cr, tooltip->entry);
1972 cairo_destroy(cr);
1973}
1974
1975static cairo_text_extents_t
1976get_text_extents(struct tooltip *tooltip)
1977{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001978 cairo_t *cr;
1979 cairo_text_extents_t extents;
1980
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001981 /* Use the dummy_surface because tooltip's surface was not
1982 * created yet, and parent does not have a valid surface
1983 * outside repaint, either.
1984 */
1985 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001986 cairo_text_extents(cr, tooltip->entry, &extents);
1987 cairo_destroy(cr);
1988
1989 return extents;
1990}
1991
1992static int
1993window_create_tooltip(struct tooltip *tooltip)
1994{
1995 struct widget *parent = tooltip->parent;
1996 struct display *display = parent->window->display;
1997 struct window *window;
1998 const int offset_y = 27;
1999 const int margin = 3;
2000 cairo_text_extents_t extents;
2001
2002 if (tooltip->widget)
2003 return 0;
2004
2005 window = window_create_transient(display, parent->window, tooltip->x,
2006 tooltip->y + offset_y,
2007 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2008 if (!window)
2009 return -1;
2010
2011 tooltip->window = window;
2012 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2013
2014 extents = get_text_extents(tooltip);
2015 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2016 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2017
2018 return 0;
2019}
2020
2021void
2022widget_destroy_tooltip(struct widget *parent)
2023{
2024 struct tooltip *tooltip = parent->tooltip;
2025
2026 parent->tooltip_count = 0;
2027 if (!tooltip)
2028 return;
2029
2030 if (tooltip->widget) {
2031 widget_destroy(tooltip->widget);
2032 window_destroy(tooltip->window);
2033 tooltip->widget = NULL;
2034 tooltip->window = NULL;
2035 }
2036
2037 close(tooltip->tooltip_fd);
2038 free(tooltip->entry);
2039 free(tooltip);
2040 parent->tooltip = NULL;
2041}
2042
2043static void
2044tooltip_func(struct task *task, uint32_t events)
2045{
2046 struct tooltip *tooltip =
2047 container_of(task, struct tooltip, tooltip_task);
2048 uint64_t exp;
2049
Martin Olsson8df662a2012-07-08 03:03:47 +02002050 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2051 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002052 window_create_tooltip(tooltip);
2053}
2054
2055#define TOOLTIP_TIMEOUT 500
2056static int
2057tooltip_timer_reset(struct tooltip *tooltip)
2058{
2059 struct itimerspec its;
2060
2061 its.it_interval.tv_sec = 0;
2062 its.it_interval.tv_nsec = 0;
2063 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2064 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2065 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2066 fprintf(stderr, "could not set timerfd\n: %m");
2067 return -1;
2068 }
2069
2070 return 0;
2071}
2072
2073int
2074widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2075{
2076 struct tooltip *tooltip = parent->tooltip;
2077
2078 parent->tooltip_count++;
2079 if (tooltip) {
2080 tooltip->x = x;
2081 tooltip->y = y;
2082 tooltip_timer_reset(tooltip);
2083 return 0;
2084 }
2085
2086 /* the handler might be triggered too fast via input device motion, so
2087 * we need this check here to make sure tooltip is fully initialized */
2088 if (parent->tooltip_count > 1)
2089 return 0;
2090
2091 tooltip = malloc(sizeof *tooltip);
2092 if (!tooltip)
2093 return -1;
2094
2095 parent->tooltip = tooltip;
2096 tooltip->parent = parent;
2097 tooltip->widget = NULL;
2098 tooltip->window = NULL;
2099 tooltip->x = x;
2100 tooltip->y = y;
2101 tooltip->entry = strdup(entry);
2102 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2103 if (tooltip->tooltip_fd < 0) {
2104 fprintf(stderr, "could not create timerfd\n: %m");
2105 return -1;
2106 }
2107
2108 tooltip->tooltip_task.run = tooltip_func;
2109 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2110 EPOLLIN, &tooltip->tooltip_task);
2111 tooltip_timer_reset(tooltip);
2112
2113 return 0;
2114}
2115
2116static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002117workspace_manager_state(void *data,
2118 struct workspace_manager *workspace_manager,
2119 uint32_t current,
2120 uint32_t count)
2121{
2122 struct display *display = data;
2123
2124 display->workspace = current;
2125 display->workspace_count = count;
2126}
2127
2128static const struct workspace_manager_listener workspace_manager_listener = {
2129 workspace_manager_state
2130};
2131
2132static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002133frame_resize_handler(struct widget *widget,
2134 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002135{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002136 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002137 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002138 struct rectangle interior;
2139 struct rectangle input;
2140 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002141
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002142 if (widget->window->type == TYPE_FULLSCREEN) {
2143 interior.x = 0;
2144 interior.y = 0;
2145 interior.width = width;
2146 interior.height = height;
2147 } else {
2148 if (widget->window->type == TYPE_MAXIMIZED) {
2149 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2150 } else {
2151 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2152 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002153
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002154 frame_resize(frame->frame, width, height);
2155 frame_interior(frame->frame, &interior.x, &interior.y,
2156 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002157 }
2158
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002159 widget_set_allocation(child, interior.x, interior.y,
2160 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002161
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002162 if (child->resize_handler) {
2163 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002164 child->user_data);
2165
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002166 if (widget->window->type == TYPE_FULLSCREEN) {
2167 width = child->allocation.width;
2168 height = child->allocation.height;
2169 } else {
2170 frame_resize_inside(frame->frame,
2171 child->allocation.width,
2172 child->allocation.height);
2173 width = frame_width(frame->frame);
2174 height = frame_height(frame->frame);
2175 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002176 }
2177
Scott Moreauf7e498c2012-05-14 11:39:29 -06002178 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002179
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002180 widget->surface->input_region =
2181 wl_compositor_create_region(widget->window->display->compositor);
2182 if (widget->window->type != TYPE_FULLSCREEN) {
2183 frame_input_rect(frame->frame, &input.x, &input.y,
2184 &input.width, &input.height);
2185 wl_region_add(widget->surface->input_region,
2186 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002187 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002188 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002189 }
2190
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002191 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002192
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 if (child->opaque) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002194 if (widget->window->type != TYPE_FULLSCREEN) {
2195 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2196 &opaque.width, &opaque.height);
2197
2198 wl_region_add(widget->surface->opaque_region,
2199 opaque.x, opaque.y,
2200 opaque.width, opaque.height);
2201 } else {
2202 wl_region_add(widget->surface->opaque_region,
2203 0, 0, width, height);
2204 }
Martin Minarik1998b152012-05-10 02:04:35 +02002205 }
2206
Martin Minarik1998b152012-05-10 02:04:35 +02002207
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002208 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002209}
2210
2211static void
2212frame_redraw_handler(struct widget *widget, void *data)
2213{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002214 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002215 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002216 struct window *window = widget->window;
2217
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002218 if (window->type == TYPE_FULLSCREEN)
2219 return;
2220
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002221 if (window->focus_count) {
2222 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2223 } else {
2224 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2225 }
2226
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002227 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002228
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002229 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002230
2231 cairo_destroy(cr);
2232}
2233
2234static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002235frame_get_pointer_image_for_location(struct window_frame *frame,
2236 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002237{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002238 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002239
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002240 if (window->type != TYPE_TOPLEVEL)
2241 return CURSOR_LEFT_PTR;
2242
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002243 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002244 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002245 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002246 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002247 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002248 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002249 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002250 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002251 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002252 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002253 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002254 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002255 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002256 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002257 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002258 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002259 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002260 case THEME_LOCATION_EXTERIOR:
2261 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002262 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002263 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002264 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002265}
2266
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002267static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002268frame_menu_func(struct window *window,
2269 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002270{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002271 struct display *display;
2272
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002273 switch (index) {
2274 case 0: /* close */
2275 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002276 window->close_handler(window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002277 else
2278 display_exit(window->display);
2279 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002280 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002281 display = window->display;
2282 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002283 workspace_manager_move_surface(
2284 display->workspace_manager,
2285 window->main_surface->surface,
2286 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002287 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002288 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002289 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002290 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002291 workspace_manager_move_surface(
2292 display->workspace_manager,
2293 window->main_surface->surface,
2294 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002295 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002296 case 3: /* fullscreen */
2297 /* we don't have a way to get out of fullscreen for now */
2298 if (window->fullscreen_handler)
2299 window->fullscreen_handler(window, window->user_data);
2300 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002301 }
2302}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002303
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002304void
2305window_show_frame_menu(struct window *window,
2306 struct input *input, uint32_t time)
2307{
2308 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002309 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002310
2311 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002312 "Close",
2313 "Move to workspace above", "Move to workspace below",
2314 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002315 };
2316
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002317 if (window->fullscreen_handler)
2318 count = ARRAY_LENGTH(entries);
2319 else
2320 count = ARRAY_LENGTH(entries) - 1;
2321
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002322 input_get_position(input, &x, &y);
2323 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002324 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002325}
2326
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002327static int
2328frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002329 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002330{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002331 struct window_frame *frame = data;
2332 enum theme_location location;
2333
2334 location = frame_pointer_enter(frame->frame, input, x, y);
2335 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2336 widget_schedule_redraw(frame->widget);
2337
2338 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002339}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002340
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002341static int
2342frame_motion_handler(struct widget *widget,
2343 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002344 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002345{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002346 struct window_frame *frame = data;
2347 enum theme_location location;
2348
2349 location = frame_pointer_motion(frame->frame, input, x, y);
2350 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2351 widget_schedule_redraw(frame->widget);
2352
2353 return frame_get_pointer_image_for_location(data, location);
2354}
2355
2356static void
2357frame_leave_handler(struct widget *widget,
2358 struct input *input, void *data)
2359{
2360 struct window_frame *frame = data;
2361
2362 frame_pointer_leave(frame->frame, input);
2363 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2364 widget_schedule_redraw(frame->widget);
2365}
2366
2367static void
2368frame_handle_status(struct window_frame *frame, struct input *input,
2369 uint32_t time, enum theme_location location)
2370{
2371 struct window *window = frame->widget->window;
2372 uint32_t status;
2373
2374 status = frame_status(frame->frame);
2375 if (status & FRAME_STATUS_REPAINT)
2376 widget_schedule_redraw(frame->widget);
2377
2378 if (status & FRAME_STATUS_MINIMIZE)
2379 fprintf(stderr,"Minimize stub\n");
2380
2381 if (status & FRAME_STATUS_MENU) {
2382 window_show_frame_menu(window, input, time);
2383 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2384 }
2385
2386 if (status & FRAME_STATUS_MAXIMIZE) {
2387 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2388 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2389 }
2390
2391 if (status & FRAME_STATUS_CLOSE) {
2392 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002393 window->close_handler(window->user_data);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002394 else
2395 display_exit(window->display);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002396 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002397 }
2398
2399 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2400 input_ungrab(input);
2401 wl_shell_surface_move(window->shell_surface,
2402 input_get_seat(input),
2403 window->display->serial);
2404
2405 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2406 }
2407
2408 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2409 input_ungrab(input);
2410
2411 window->resizing = 1;
2412 wl_shell_surface_resize(window->shell_surface,
2413 input_get_seat(input),
2414 window->display->serial,
2415 location);
2416
2417 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2418 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002419}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002420
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002421static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002422frame_button_handler(struct widget *widget,
2423 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002424 uint32_t button, enum wl_pointer_button_state state,
2425 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002426
2427{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002428 struct window_frame *frame = data;
2429 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002430
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002431 location = frame_pointer_button(frame->frame, input, button, state);
2432 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002433}
2434
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002435static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002436frame_touch_down_handler(struct widget *widget, struct input *input,
2437 uint32_t serial, uint32_t time, int32_t id,
2438 float x, float y, void *data)
2439{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002440 struct window_frame *frame = data;
2441
2442 frame_touch_down(frame->frame, input, id, x, y);
2443 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2444}
2445
2446static void
2447frame_touch_up_handler(struct widget *widget,
2448 struct input *input, uint32_t serial, uint32_t time,
2449 int32_t id, void *data)
2450{
2451 struct window_frame *frame = data;
2452
2453 frame_touch_up(frame->frame, input, id);
2454 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002455}
2456
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002457struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002458window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002459{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002460 struct window_frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002461
Peter Huttererf3d62272013-08-08 11:57:05 +10002462 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002463 frame->frame = frame_create(window->display->theme, 0, 0,
2464 FRAME_BUTTON_ALL, window->title);
2465
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002466 frame->widget = window_add_widget(window, frame);
2467 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002468
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002469 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2470 widget_set_resize_handler(frame->widget, frame_resize_handler);
2471 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002472 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002473 widget_set_motion_handler(frame->widget, frame_motion_handler);
2474 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002475 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002476 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002477
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002478 window->frame = frame;
2479
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002480 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002481}
2482
Kristian Høgsberga1627922012-06-20 17:30:03 -04002483void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002484window_frame_set_child_size(struct widget *widget, int child_width,
2485 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002486{
2487 struct display *display = widget->window->display;
2488 struct theme *t = display->theme;
2489 int decoration_width, decoration_height;
2490 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002491 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002492
2493 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002494 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002495 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002496 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002497
2498 width = child_width + decoration_width;
2499 height = child_height + decoration_height;
2500 } else {
2501 width = child_width;
2502 height = child_height;
2503 }
2504
2505 window_schedule_resize(widget->window, width, height);
2506}
2507
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002508static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002509window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002510{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002511 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002512
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002513 /* frame->child must be destroyed by the application */
2514 widget_destroy(frame->widget);
2515 free(frame);
2516}
2517
2518static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002519input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002520 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002521{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002522 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002523 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002524
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002525 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002526 return;
2527
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002528 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002529 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002530 widget = old;
2531 if (input->grab)
2532 widget = input->grab;
2533 if (widget->leave_handler)
2534 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002535 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002536 }
2537
2538 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002539 widget = focus;
2540 if (input->grab)
2541 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002542 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002543 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002544 cursor = widget->enter_handler(focus, input, x, y,
2545 widget->user_data);
2546 else
2547 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002548
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002549 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002550 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002551}
2552
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002553void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002554touch_grab(struct input *input, int32_t touch_id)
2555{
2556 input->touch_grab = 1;
2557 input->touch_grab_id = touch_id;
2558}
2559
2560void
2561touch_ungrab(struct input *input)
2562{
2563 struct touch_point *tp, *tmp;
2564
2565 input->touch_grab = 0;
2566
2567 wl_list_for_each_safe(tp, tmp,
2568 &input->touch_point_list, link) {
2569 if (tp->id != input->touch_grab_id)
2570 continue;
2571 wl_list_remove(&tp->link);
2572 free(tp);
2573
2574 return;
2575 }
2576}
2577
2578void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002579input_grab(struct input *input, struct widget *widget, uint32_t button)
2580{
2581 input->grab = widget;
2582 input->grab_button = button;
2583}
2584
2585void
2586input_ungrab(struct input *input)
2587{
2588 struct widget *widget;
2589
2590 input->grab = NULL;
2591 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002592 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002593 input->sx, input->sy);
2594 input_set_focus_widget(input, widget, input->sx, input->sy);
2595 }
2596}
2597
2598static void
2599input_remove_pointer_focus(struct input *input)
2600{
2601 struct window *window = input->pointer_focus;
2602
2603 if (!window)
2604 return;
2605
2606 input_set_focus_widget(input, NULL, 0, 0);
2607
2608 input->pointer_focus = NULL;
2609 input->current_cursor = CURSOR_UNSET;
2610}
2611
2612static void
2613pointer_handle_enter(void *data, struct wl_pointer *pointer,
2614 uint32_t serial, struct wl_surface *surface,
2615 wl_fixed_t sx_w, wl_fixed_t sy_w)
2616{
2617 struct input *input = data;
2618 struct window *window;
2619 struct widget *widget;
2620 float sx = wl_fixed_to_double(sx_w);
2621 float sy = wl_fixed_to_double(sy_w);
2622
2623 if (!surface) {
2624 /* enter event for a window we've just destroyed */
2625 return;
2626 }
2627
2628 input->display->serial = serial;
2629 input->pointer_enter_serial = serial;
2630 input->pointer_focus = wl_surface_get_user_data(surface);
2631 window = input->pointer_focus;
2632
Pekka Paalanen99436862012-11-19 17:15:59 +02002633 if (window->resizing) {
2634 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002635 /* Schedule a redraw to free the pool */
2636 window_schedule_redraw(window);
2637 }
2638
2639 input->sx = sx;
2640 input->sy = sy;
2641
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002642 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002643 input_set_focus_widget(input, widget, sx, sy);
2644}
2645
2646static void
2647pointer_handle_leave(void *data, struct wl_pointer *pointer,
2648 uint32_t serial, struct wl_surface *surface)
2649{
2650 struct input *input = data;
2651
2652 input->display->serial = serial;
2653 input_remove_pointer_focus(input);
2654}
2655
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002656static void
Daniel Stone37816df2012-05-16 18:45:18 +01002657pointer_handle_motion(void *data, struct wl_pointer *pointer,
2658 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002659{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002660 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002661 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002662 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002663 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002664 float sx = wl_fixed_to_double(sx_w);
2665 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002666
Paul Winwoodb22bf572013-08-29 10:52:54 +01002667 input->sx = sx;
2668 input->sy = sy;
2669
2670 if (!window)
2671 return;
2672
Rob Bradford5f087742013-07-11 19:41:27 +01002673 /* when making the window smaller - e.g. after a unmaximise we might
2674 * still have a pending motion event that the compositor has picked
2675 * based on the old surface dimensions
2676 */
2677 if (sx > window->main_surface->allocation.width ||
2678 sy > window->main_surface->allocation.height)
2679 return;
2680
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002681 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002682 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002683 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002684 }
2685
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002686 if (input->grab)
2687 widget = input->grab;
2688 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002689 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002690 if (widget) {
2691 if (widget->motion_handler)
2692 cursor = widget->motion_handler(input->focus_widget,
2693 input, time, sx, sy,
2694 widget->user_data);
2695 else
2696 cursor = widget->default_cursor;
2697 } else
2698 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002699
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002700 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002701}
2702
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002703static void
Daniel Stone37816df2012-05-16 18:45:18 +01002704pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002705 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002706{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002707 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002708 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002709 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002710
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002711 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002712 if (input->focus_widget && input->grab == NULL &&
2713 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002714 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002715
Neil Roberts6b28aad2012-01-23 19:11:18 +00002716 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002717 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002718 (*widget->button_handler)(widget,
2719 input, time,
2720 button, state,
2721 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002722
Daniel Stone4dbadb12012-05-30 16:31:51 +01002723 if (input->grab && input->grab_button == button &&
2724 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002725 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002726}
2727
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002728static void
Daniel Stone37816df2012-05-16 18:45:18 +01002729pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002730 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002731{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002732 struct input *input = data;
2733 struct widget *widget;
2734
2735 widget = input->focus_widget;
2736 if (input->grab)
2737 widget = input->grab;
2738 if (widget && widget->axis_handler)
2739 (*widget->axis_handler)(widget,
2740 input, time,
2741 axis, value,
2742 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002743}
2744
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002745static const struct wl_pointer_listener pointer_listener = {
2746 pointer_handle_enter,
2747 pointer_handle_leave,
2748 pointer_handle_motion,
2749 pointer_handle_button,
2750 pointer_handle_axis,
2751};
2752
2753static void
2754input_remove_keyboard_focus(struct input *input)
2755{
2756 struct window *window = input->keyboard_focus;
2757 struct itimerspec its;
2758
2759 its.it_interval.tv_sec = 0;
2760 its.it_interval.tv_nsec = 0;
2761 its.it_value.tv_sec = 0;
2762 its.it_value.tv_nsec = 0;
2763 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2764
2765 if (!window)
2766 return;
2767
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002768 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002769 if (window->keyboard_focus_handler)
2770 (*window->keyboard_focus_handler)(window, NULL,
2771 window->user_data);
2772
2773 input->keyboard_focus = NULL;
2774}
2775
2776static void
2777keyboard_repeat_func(struct task *task, uint32_t events)
2778{
2779 struct input *input =
2780 container_of(task, struct input, repeat_task);
2781 struct window *window = input->keyboard_focus;
2782 uint64_t exp;
2783
2784 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2785 /* If we change the timer between the fd becoming
2786 * readable and getting here, there'll be nothing to
2787 * read and we get EAGAIN. */
2788 return;
2789
2790 if (window && window->key_handler) {
2791 (*window->key_handler)(window, input, input->repeat_time,
2792 input->repeat_key, input->repeat_sym,
2793 WL_KEYBOARD_KEY_STATE_PRESSED,
2794 window->user_data);
2795 }
2796}
2797
2798static void
2799keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2800 uint32_t format, int fd, uint32_t size)
2801{
2802 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002803 struct xkb_keymap *keymap;
2804 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002805 char *map_str;
2806
2807 if (!data) {
2808 close(fd);
2809 return;
2810 }
2811
2812 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2813 close(fd);
2814 return;
2815 }
2816
2817 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2818 if (map_str == MAP_FAILED) {
2819 close(fd);
2820 return;
2821 }
2822
Rui Matos3eccb862013-10-10 19:44:22 +02002823 keymap = xkb_map_new_from_string(input->display->xkb_context,
2824 map_str,
2825 XKB_KEYMAP_FORMAT_TEXT_V1,
2826 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002827 munmap(map_str, size);
2828 close(fd);
2829
Rui Matos3eccb862013-10-10 19:44:22 +02002830 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002831 fprintf(stderr, "failed to compile keymap\n");
2832 return;
2833 }
2834
Rui Matos3eccb862013-10-10 19:44:22 +02002835 state = xkb_state_new(keymap);
2836 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002837 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002838 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002839 return;
2840 }
2841
Rui Matos3eccb862013-10-10 19:44:22 +02002842 xkb_keymap_unref(input->xkb.keymap);
2843 xkb_state_unref(input->xkb.state);
2844 input->xkb.keymap = keymap;
2845 input->xkb.state = state;
2846
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002847 input->xkb.control_mask =
2848 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2849 input->xkb.alt_mask =
2850 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2851 input->xkb.shift_mask =
2852 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2853}
2854
2855static void
2856keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2857 uint32_t serial, struct wl_surface *surface,
2858 struct wl_array *keys)
2859{
2860 struct input *input = data;
2861 struct window *window;
2862
2863 input->display->serial = serial;
2864 input->keyboard_focus = wl_surface_get_user_data(surface);
2865
2866 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002867 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002868 if (window->keyboard_focus_handler)
2869 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002870 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002871}
2872
2873static void
2874keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2875 uint32_t serial, struct wl_surface *surface)
2876{
2877 struct input *input = data;
2878
2879 input->display->serial = serial;
2880 input_remove_keyboard_focus(input);
2881}
2882
Scott Moreau210d0792012-03-22 10:47:01 -06002883static void
Daniel Stone37816df2012-05-16 18:45:18 +01002884keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002885 uint32_t serial, uint32_t time, uint32_t key,
2886 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002887{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002888 struct input *input = data;
2889 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002890 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002891 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002892 const xkb_keysym_t *syms;
2893 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002894 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002895
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002896 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002897 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002898 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002899 return;
2900
Daniel Stone97f68542012-05-30 16:32:01 +01002901 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002902
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002903 sym = XKB_KEY_NoSymbol;
2904 if (num_syms == 1)
2905 sym = syms[0];
2906
2907 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002908 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002909 window_set_maximized(window,
2910 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002911 } else if (sym == XKB_KEY_F11 &&
2912 window->fullscreen_handler &&
2913 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2914 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002915 } else if (sym == XKB_KEY_F4 &&
2916 input->modifiers == MOD_ALT_MASK &&
2917 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2918 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002919 window->close_handler(window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002920 else
2921 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002922 } else if (window->key_handler) {
2923 (*window->key_handler)(window, input, time, key,
2924 sym, state, window->user_data);
2925 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002926
2927 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2928 key == input->repeat_key) {
2929 its.it_interval.tv_sec = 0;
2930 its.it_interval.tv_nsec = 0;
2931 its.it_value.tv_sec = 0;
2932 its.it_value.tv_nsec = 0;
2933 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2934 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2935 input->repeat_sym = sym;
2936 input->repeat_key = key;
2937 input->repeat_time = time;
2938 its.it_interval.tv_sec = 0;
2939 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2940 its.it_value.tv_sec = 0;
2941 its.it_value.tv_nsec = 400 * 1000 * 1000;
2942 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2943 }
2944}
2945
2946static void
Daniel Stone351eb612012-05-31 15:27:47 -04002947keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2948 uint32_t serial, uint32_t mods_depressed,
2949 uint32_t mods_latched, uint32_t mods_locked,
2950 uint32_t group)
2951{
2952 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002953 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002954
Matt Ropere61561f2013-06-24 16:52:43 +01002955 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2956 if (!input->xkb.keymap)
2957 return;
2958
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002959 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2960 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002961 mask = xkb_state_serialize_mods(input->xkb.state,
2962 XKB_STATE_DEPRESSED |
2963 XKB_STATE_LATCHED);
2964 input->modifiers = 0;
2965 if (mask & input->xkb.control_mask)
2966 input->modifiers |= MOD_CONTROL_MASK;
2967 if (mask & input->xkb.alt_mask)
2968 input->modifiers |= MOD_ALT_MASK;
2969 if (mask & input->xkb.shift_mask)
2970 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002971}
2972
Daniel Stone37816df2012-05-16 18:45:18 +01002973static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002974 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002975 keyboard_handle_enter,
2976 keyboard_handle_leave,
2977 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002978 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002979};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002980
2981static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002982touch_handle_down(void *data, struct wl_touch *wl_touch,
2983 uint32_t serial, uint32_t time, struct wl_surface *surface,
2984 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2985{
2986 struct input *input = data;
2987 struct widget *widget;
2988 float sx = wl_fixed_to_double(x_w);
2989 float sy = wl_fixed_to_double(y_w);
2990
Rusty Lynch1084da52013-08-15 09:10:08 -07002991 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002992 input->touch_focus = wl_surface_get_user_data(surface);
2993 if (!input->touch_focus) {
2994 DBG("Failed to find to touch focus for surface %p\n", surface);
2995 return;
2996 }
2997
2998 widget = window_find_widget(input->touch_focus,
2999 wl_fixed_to_double(x_w),
3000 wl_fixed_to_double(y_w));
3001 if (widget) {
3002 struct touch_point *tp = xmalloc(sizeof *tp);
3003 if (tp) {
3004 tp->id = id;
3005 tp->widget = widget;
3006 wl_list_insert(&input->touch_point_list, &tp->link);
3007
3008 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003009 (*widget->touch_down_handler)(widget, input,
3010 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003011 sx, sy,
3012 widget->user_data);
3013 }
3014 }
3015}
3016
3017static void
3018touch_handle_up(void *data, struct wl_touch *wl_touch,
3019 uint32_t serial, uint32_t time, int32_t id)
3020{
3021 struct input *input = data;
3022 struct touch_point *tp, *tmp;
3023
Rusty Lynch041815a2013-08-08 21:20:38 -07003024 if (!input->touch_focus) {
3025 DBG("No touch focus found for touch up event!\n");
3026 return;
3027 }
3028
3029 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3030 if (tp->id != id)
3031 continue;
3032
3033 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003034 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003035 time, id,
3036 tp->widget->user_data);
3037
3038 wl_list_remove(&tp->link);
3039 free(tp);
3040
3041 return;
3042 }
3043}
3044
3045static void
3046touch_handle_motion(void *data, struct wl_touch *wl_touch,
3047 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3048{
3049 struct input *input = data;
3050 struct touch_point *tp;
3051 float sx = wl_fixed_to_double(x_w);
3052 float sy = wl_fixed_to_double(y_w);
3053
3054 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3055
3056 if (!input->touch_focus) {
3057 DBG("No touch focus found for touch motion event!\n");
3058 return;
3059 }
3060
3061 wl_list_for_each(tp, &input->touch_point_list, link) {
3062 if (tp->id != id)
3063 continue;
3064
3065 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003066 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003067 id, sx, sy,
3068 tp->widget->user_data);
3069 return;
3070 }
3071}
3072
3073static void
3074touch_handle_frame(void *data, struct wl_touch *wl_touch)
3075{
3076 struct input *input = data;
3077 struct touch_point *tp, *tmp;
3078
3079 DBG("touch_handle_frame\n");
3080
3081 if (!input->touch_focus) {
3082 DBG("No touch focus found for touch frame event!\n");
3083 return;
3084 }
3085
3086 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3087 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003088 (*tp->widget->touch_frame_handler)(tp->widget, input,
3089 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003090
3091 wl_list_remove(&tp->link);
3092 free(tp);
3093 }
3094}
3095
3096static void
3097touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3098{
3099 struct input *input = data;
3100 struct touch_point *tp, *tmp;
3101
3102 DBG("touch_handle_cancel\n");
3103
3104 if (!input->touch_focus) {
3105 DBG("No touch focus found for touch cancel event!\n");
3106 return;
3107 }
3108
3109 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3110 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003111 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3112 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003113
3114 wl_list_remove(&tp->link);
3115 free(tp);
3116 }
3117}
3118
3119static const struct wl_touch_listener touch_listener = {
3120 touch_handle_down,
3121 touch_handle_up,
3122 touch_handle_motion,
3123 touch_handle_frame,
3124 touch_handle_cancel,
3125};
3126
3127static void
Daniel Stone37816df2012-05-16 18:45:18 +01003128seat_handle_capabilities(void *data, struct wl_seat *seat,
3129 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003130{
Daniel Stone37816df2012-05-16 18:45:18 +01003131 struct input *input = data;
3132
3133 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3134 input->pointer = wl_seat_get_pointer(seat);
3135 wl_pointer_set_user_data(input->pointer, input);
3136 wl_pointer_add_listener(input->pointer, &pointer_listener,
3137 input);
3138 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3139 wl_pointer_destroy(input->pointer);
3140 input->pointer = NULL;
3141 }
3142
3143 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3144 input->keyboard = wl_seat_get_keyboard(seat);
3145 wl_keyboard_set_user_data(input->keyboard, input);
3146 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3147 input);
3148 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3149 wl_keyboard_destroy(input->keyboard);
3150 input->keyboard = NULL;
3151 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003152
3153 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3154 input->touch = wl_seat_get_touch(seat);
3155 wl_touch_set_user_data(input->touch, input);
3156 wl_touch_add_listener(input->touch, &touch_listener, input);
3157 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3158 wl_touch_destroy(input->touch);
3159 input->touch = NULL;
3160 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003161}
3162
Rob Bradford08031182013-08-13 20:11:03 +01003163static void
3164seat_handle_name(void *data, struct wl_seat *seat,
3165 const char *name)
3166{
3167
3168}
3169
Daniel Stone37816df2012-05-16 18:45:18 +01003170static const struct wl_seat_listener seat_listener = {
3171 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003172 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003173};
3174
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003175void
3176input_get_position(struct input *input, int32_t *x, int32_t *y)
3177{
3178 *x = input->sx;
3179 *y = input->sy;
3180}
3181
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003182struct display *
3183input_get_display(struct input *input)
3184{
3185 return input->display;
3186}
3187
Daniel Stone37816df2012-05-16 18:45:18 +01003188struct wl_seat *
3189input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003190{
Daniel Stone37816df2012-05-16 18:45:18 +01003191 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003192}
3193
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003194uint32_t
3195input_get_modifiers(struct input *input)
3196{
3197 return input->modifiers;
3198}
3199
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003200struct widget *
3201input_get_focus_widget(struct input *input)
3202{
3203 return input->focus_widget;
3204}
3205
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003206struct data_offer {
3207 struct wl_data_offer *offer;
3208 struct input *input;
3209 struct wl_array types;
3210 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003211
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003212 struct task io_task;
3213 int fd;
3214 data_func_t func;
3215 int32_t x, y;
3216 void *user_data;
3217};
3218
3219static void
3220data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3221{
3222 struct data_offer *offer = data;
3223 char **p;
3224
3225 p = wl_array_add(&offer->types, sizeof *p);
3226 *p = strdup(type);
3227}
3228
3229static const struct wl_data_offer_listener data_offer_listener = {
3230 data_offer_offer,
3231};
3232
3233static void
3234data_offer_destroy(struct data_offer *offer)
3235{
3236 char **p;
3237
3238 offer->refcount--;
3239 if (offer->refcount == 0) {
3240 wl_data_offer_destroy(offer->offer);
3241 for (p = offer->types.data; *p; p++)
3242 free(*p);
3243 wl_array_release(&offer->types);
3244 free(offer);
3245 }
3246}
3247
3248static void
3249data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003250 struct wl_data_device *data_device,
3251 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003252{
3253 struct data_offer *offer;
3254
Brian Lovinbc919262013-08-07 15:34:59 -07003255 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003256
3257 wl_array_init(&offer->types);
3258 offer->refcount = 1;
3259 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003260 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003261 wl_data_offer_add_listener(offer->offer,
3262 &data_offer_listener, offer);
3263}
3264
3265static void
3266data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003267 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003268 wl_fixed_t x_w, wl_fixed_t y_w,
3269 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003270{
3271 struct input *input = data;
3272 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003273 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003274 float x = wl_fixed_to_double(x_w);
3275 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003276 char **p;
3277
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003278 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003279 input->drag_enter_serial = serial;
3280 input->drag_focus = window,
3281 input->drag_x = x;
3282 input->drag_y = y;
3283
3284 if (!input->touch_grab)
3285 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003286
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003287 if (offer) {
3288 input->drag_offer = wl_data_offer_get_user_data(offer);
3289
3290 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3291 *p = NULL;
3292
3293 types_data = input->drag_offer->types.data;
3294 } else {
3295 input->drag_offer = NULL;
3296 types_data = NULL;
3297 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003298
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003299 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003300 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003301 window->user_data);
3302}
3303
3304static void
3305data_device_leave(void *data, struct wl_data_device *data_device)
3306{
3307 struct input *input = data;
3308
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003309 if (input->drag_offer) {
3310 data_offer_destroy(input->drag_offer);
3311 input->drag_offer = NULL;
3312 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003313}
3314
3315static void
3316data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003317 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003318{
3319 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003320 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003321 float x = wl_fixed_to_double(x_w);
3322 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003323 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003324
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003325 input->drag_x = x;
3326 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003327
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003328 if (input->drag_offer)
3329 types_data = input->drag_offer->types.data;
3330 else
3331 types_data = NULL;
3332
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003333 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003334 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003335 window->user_data);
3336}
3337
3338static void
3339data_device_drop(void *data, struct wl_data_device *data_device)
3340{
3341 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003342 struct window *window = input->drag_focus;
3343 float x, y;
3344
3345 x = input->drag_x;
3346 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003347
3348 if (window->drop_handler)
3349 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003350 x, y, window->user_data);
3351
3352 if (input->touch_grab)
3353 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003354}
3355
3356static void
3357data_device_selection(void *data,
3358 struct wl_data_device *wl_data_device,
3359 struct wl_data_offer *offer)
3360{
3361 struct input *input = data;
3362 char **p;
3363
3364 if (input->selection_offer)
3365 data_offer_destroy(input->selection_offer);
3366
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003367 if (offer) {
3368 input->selection_offer = wl_data_offer_get_user_data(offer);
3369 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3370 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003371 } else {
3372 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003373 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003374}
3375
3376static const struct wl_data_device_listener data_device_listener = {
3377 data_device_data_offer,
3378 data_device_enter,
3379 data_device_leave,
3380 data_device_motion,
3381 data_device_drop,
3382 data_device_selection
3383};
3384
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003385static void
3386input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003387{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003388 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003389 struct wl_cursor *cursor;
3390 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003391
Daniel Stone80972742012-11-07 17:51:39 +11003392 if (!input->pointer)
3393 return;
3394
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003395 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003396 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003397 return;
3398
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003399 if (index >= (int) cursor->image_count) {
3400 fprintf(stderr, "cursor index out of range\n");
3401 return;
3402 }
3403
3404 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003405 buffer = wl_cursor_image_get_buffer(image);
3406 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003407 return;
3408
Kristian Høgsbergae277372012-08-01 09:41:08 -04003409 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003410 input->pointer_surface,
3411 image->hotspot_x, image->hotspot_y);
3412 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3413 wl_surface_damage(input->pointer_surface, 0, 0,
3414 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003415 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003416}
3417
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003418static const struct wl_callback_listener pointer_surface_listener;
3419
3420static void
3421pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3422 uint32_t time)
3423{
3424 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003425 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003426 int i;
3427
3428 if (callback) {
3429 assert(callback == input->cursor_frame_cb);
3430 wl_callback_destroy(callback);
3431 input->cursor_frame_cb = NULL;
3432 }
3433
Daniel Stone80972742012-11-07 17:51:39 +11003434 if (!input->pointer)
3435 return;
3436
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003437 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003438 wl_pointer_set_cursor(input->pointer,
3439 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003440 NULL, 0, 0);
3441 return;
3442 }
3443
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003444 if (input->current_cursor == CURSOR_UNSET)
3445 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003446 cursor = input->display->cursors[input->current_cursor];
3447 if (!cursor)
3448 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003449
3450 /* FIXME We don't have the current time on the first call so we set
3451 * the animation start to the time of the first frame callback. */
3452 if (time == 0)
3453 input->cursor_anim_start = 0;
3454 else if (input->cursor_anim_start == 0)
3455 input->cursor_anim_start = time;
3456
3457 if (time == 0 || input->cursor_anim_start == 0)
3458 i = 0;
3459 else
3460 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3461
Pekka Paalanenbc106382012-10-10 12:49:31 +03003462 if (cursor->image_count > 1) {
3463 input->cursor_frame_cb =
3464 wl_surface_frame(input->pointer_surface);
3465 wl_callback_add_listener(input->cursor_frame_cb,
3466 &pointer_surface_listener, input);
3467 }
3468
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003469 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003470}
3471
3472static const struct wl_callback_listener pointer_surface_listener = {
3473 pointer_surface_frame_callback
3474};
3475
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003476void
3477input_set_pointer_image(struct input *input, int pointer)
3478{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003479 int force = 0;
3480
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003481 if (!input->pointer)
3482 return;
3483
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003484 if (input->pointer_enter_serial > input->cursor_serial)
3485 force = 1;
3486
3487 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003488 return;
3489
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003490 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003491 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003492 if (!input->cursor_frame_cb)
3493 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003494 else if (force) {
3495 /* The current frame callback may be stuck if, for instance,
3496 * the set cursor request was processed by the server after
3497 * this client lost the focus. In this case the cursor surface
3498 * might not be mapped and the frame callback wouldn't ever
3499 * complete. Send a set_cursor and attach to try to map the
3500 * cursor surface again so that the callback will finish */
3501 input_set_pointer_image_index(input, 0);
3502 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003503}
3504
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003505struct wl_data_device *
3506input_get_data_device(struct input *input)
3507{
3508 return input->data_device;
3509}
3510
3511void
3512input_set_selection(struct input *input,
3513 struct wl_data_source *source, uint32_t time)
3514{
3515 wl_data_device_set_selection(input->data_device, source, time);
3516}
3517
3518void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003519input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003520{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003521 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003522 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003523}
3524
3525static void
3526offer_io_func(struct task *task, uint32_t events)
3527{
3528 struct data_offer *offer =
3529 container_of(task, struct data_offer, io_task);
3530 unsigned int len;
3531 char buffer[4096];
3532
3533 len = read(offer->fd, buffer, sizeof buffer);
3534 offer->func(buffer, len,
3535 offer->x, offer->y, offer->user_data);
3536
3537 if (len == 0) {
3538 close(offer->fd);
3539 data_offer_destroy(offer);
3540 }
3541}
3542
3543static void
3544data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3545 data_func_t func, void *user_data)
3546{
3547 int p[2];
3548
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003549 if (pipe2(p, O_CLOEXEC) == -1)
3550 return;
3551
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003552 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3553 close(p[1]);
3554
3555 offer->io_task.run = offer_io_func;
3556 offer->fd = p[0];
3557 offer->func = func;
3558 offer->refcount++;
3559 offer->user_data = user_data;
3560
3561 display_watch_fd(offer->input->display,
3562 offer->fd, EPOLLIN, &offer->io_task);
3563}
3564
3565void
3566input_receive_drag_data(struct input *input, const char *mime_type,
3567 data_func_t func, void *data)
3568{
3569 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003570 input->drag_offer->x = input->drag_x;
3571 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003572}
3573
3574int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003575input_receive_drag_data_to_fd(struct input *input,
3576 const char *mime_type, int fd)
3577{
3578 if (input->drag_offer)
3579 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3580
3581 return 0;
3582}
3583
3584int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003585input_receive_selection_data(struct input *input, const char *mime_type,
3586 data_func_t func, void *data)
3587{
3588 char **p;
3589
3590 if (input->selection_offer == NULL)
3591 return -1;
3592
3593 for (p = input->selection_offer->types.data; *p; p++)
3594 if (strcmp(mime_type, *p) == 0)
3595 break;
3596
3597 if (*p == NULL)
3598 return -1;
3599
3600 data_offer_receive_data(input->selection_offer,
3601 mime_type, func, data);
3602 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003603}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003604
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003605int
3606input_receive_selection_data_to_fd(struct input *input,
3607 const char *mime_type, int fd)
3608{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003609 if (input->selection_offer)
3610 wl_data_offer_receive(input->selection_offer->offer,
3611 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003612
3613 return 0;
3614}
3615
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003616void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003617window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003618{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003619 if (!window->shell_surface)
3620 return;
3621
Daniel Stone37816df2012-05-16 18:45:18 +01003622 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003623}
3624
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003625static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003626surface_set_synchronized(struct surface *surface)
3627{
3628 if (!surface->subsurface)
3629 return;
3630
3631 if (surface->synchronized)
3632 return;
3633
3634 wl_subsurface_set_sync(surface->subsurface);
3635 surface->synchronized = 1;
3636}
3637
3638static void
3639surface_set_synchronized_default(struct surface *surface)
3640{
3641 if (!surface->subsurface)
3642 return;
3643
3644 if (surface->synchronized == surface->synchronized_default)
3645 return;
3646
3647 if (surface->synchronized_default)
3648 wl_subsurface_set_sync(surface->subsurface);
3649 else
3650 wl_subsurface_set_desync(surface->subsurface);
3651
3652 surface->synchronized = surface->synchronized_default;
3653}
3654
3655static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003656surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003657{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003658 struct widget *widget = surface->widget;
3659 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003660
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003661 if (surface->input_region) {
3662 wl_region_destroy(surface->input_region);
3663 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003664 }
3665
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003666 if (surface->opaque_region)
3667 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003668
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003669 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003670
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003671 if (widget->resize_handler)
3672 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003673 widget->allocation.width,
3674 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003675 widget->user_data);
3676
Pekka Paalanen35e82632013-04-25 13:57:48 +03003677 if (surface->subsurface &&
3678 (surface->allocation.x != widget->allocation.x ||
3679 surface->allocation.y != widget->allocation.y)) {
3680 wl_subsurface_set_position(surface->subsurface,
3681 widget->allocation.x,
3682 widget->allocation.y);
3683 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003684 if (surface->allocation.width != widget->allocation.width ||
3685 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003686 window_schedule_redraw(widget->window);
3687 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003688 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003689
3690 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003691 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003692 widget->allocation.width,
3693 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003694}
3695
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003696static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003697hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3698{
3699 /*
3700 * This hack should be removed, when EGL respects
3701 * eglSwapInterval(0).
3702 *
3703 * If this window has sub-surfaces, especially a free-running
3704 * EGL-widget, we need to post the parent surface once with
3705 * all the old state to guarantee, that the EGL-widget will
3706 * receive its frame callback soon. Otherwise, a forced call
3707 * to eglSwapBuffers may end up blocking, waiting for a frame
3708 * event that will never come, because we will commit the parent
3709 * surface with all new state only after eglSwapBuffers returns.
3710 *
3711 * This assumes, that:
3712 * 1. When the EGL widget's resize hook is called, it pauses.
3713 * 2. When the EGL widget's redraw hook is called, it forces a
3714 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3715 * In a single threaded application condition 1 is a no-op.
3716 *
3717 * XXX: This should actually be after the surface_resize() calls,
3718 * but cannot, because then it would commit the incomplete state
3719 * accumulated from the widget resize hooks.
3720 */
3721 if (window->subsurface_list.next != &window->main_surface->link ||
3722 window->subsurface_list.prev != &window->main_surface->link)
3723 wl_surface_commit(window->main_surface->surface);
3724}
3725
3726static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003727idle_resize(struct window *window)
3728{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003729 struct surface *surface;
3730
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003731 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003732 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003733
Pekka Paalanen71233882013-04-25 13:57:53 +03003734 DBG("from %dx%d to %dx%d\n",
3735 window->main_surface->server_allocation.width,
3736 window->main_surface->server_allocation.height,
3737 window->pending_allocation.width,
3738 window->pending_allocation.height);
3739
Pekka Paalanene9297f82013-04-25 13:57:51 +03003740 hack_prevent_EGL_sub_surface_deadlock(window);
3741
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003742 widget_set_allocation(window->main_surface->widget,
3743 window->pending_allocation.x,
3744 window->pending_allocation.y,
3745 window->pending_allocation.width,
3746 window->pending_allocation.height);
3747
3748 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003749
3750 /* The main surface is in the list, too. Main surface's
3751 * resize_handler is responsible for calling widget_set_allocation()
3752 * on all sub-surface root widgets, so they will be resized
3753 * properly.
3754 */
3755 wl_list_for_each(surface, &window->subsurface_list, link) {
3756 if (surface == window->main_surface)
3757 continue;
3758
3759 surface_set_synchronized(surface);
3760 surface_resize(surface);
3761 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003762}
3763
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003764void
3765window_schedule_resize(struct window *window, int width, int height)
3766{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003767 /* We should probably get these numbers from the theme. */
3768 const int min_width = 200, min_height = 200;
3769
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003770 window->pending_allocation.x = 0;
3771 window->pending_allocation.y = 0;
3772 window->pending_allocation.width = width;
3773 window->pending_allocation.height = height;
3774
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003775 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003776 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003777 window->min_allocation.width = min_width;
3778 else
3779 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003780 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003781 window->min_allocation.height = min_height;
3782 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003783 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003784 }
3785
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003786 if (window->pending_allocation.width < window->min_allocation.width)
3787 window->pending_allocation.width = window->min_allocation.width;
3788 if (window->pending_allocation.height < window->min_allocation.height)
3789 window->pending_allocation.height = window->min_allocation.height;
3790
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003791 window->resize_needed = 1;
3792 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003793}
3794
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003795void
3796widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3797{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003798 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003799}
3800
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003801static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003802handle_ping(void *data, struct wl_shell_surface *shell_surface,
3803 uint32_t serial)
3804{
3805 wl_shell_surface_pong(shell_surface, serial);
3806}
3807
3808static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003809handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003810 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003811{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003812 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003813
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003814 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003815 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003816}
3817
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003818static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003819menu_destroy(struct menu *menu)
3820{
3821 widget_destroy(menu->widget);
3822 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003823 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003824 free(menu);
3825}
3826
3827static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003828handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3829{
3830 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003831 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003832
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003833 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003834 * device. Or just use wl_callback. And this really needs to
3835 * be a window vfunc that the menu can set. And we need the
3836 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003837
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003838 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003839 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003840}
3841
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003842static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003843 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003844 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003845 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003846};
3847
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003848void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003849window_get_allocation(struct window *window,
3850 struct rectangle *allocation)
3851{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003852 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003853}
3854
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003855static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003856widget_redraw(struct widget *widget)
3857{
3858 struct widget *child;
3859
3860 if (widget->redraw_handler)
3861 widget->redraw_handler(widget, widget->user_data);
3862 wl_list_for_each(child, &widget->child_list, link)
3863 widget_redraw(child);
3864}
3865
3866static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003867frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3868{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003869 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003870
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003871 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003872 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003873 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003874 surface->frame_cb = NULL;
3875
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003876 surface->last_time = time;
3877
Pekka Paalanen71233882013-04-25 13:57:53 +03003878 if (surface->redraw_needed || surface->window->redraw_needed) {
3879 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003880 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003881 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003882}
3883
3884static const struct wl_callback_listener listener = {
3885 frame_callback
3886};
3887
3888static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003889surface_redraw(struct surface *surface)
3890{
Pekka Paalanen71233882013-04-25 13:57:53 +03003891 DBG_OBJ(surface->surface, "begin\n");
3892
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003893 if (!surface->window->redraw_needed && !surface->redraw_needed)
3894 return;
3895
3896 /* Whole-window redraw forces a redraw even if the previous has
3897 * not yet hit the screen.
3898 */
3899 if (surface->frame_cb) {
3900 if (!surface->window->redraw_needed)
3901 return;
3902
Pekka Paalanen71233882013-04-25 13:57:53 +03003903 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003904 wl_callback_destroy(surface->frame_cb);
3905 }
3906
3907 surface->frame_cb = wl_surface_frame(surface->surface);
3908 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003909 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003910
3911 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003912 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003913 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003914 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003915}
3916
3917static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003918idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003919{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003920 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003921 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003922
Pekka Paalanen71233882013-04-25 13:57:53 +03003923 DBG(" --------- \n");
3924
Pekka Paalaneneebff542013-04-25 13:57:52 +03003925 wl_list_init(&window->redraw_task.link);
3926 window->redraw_task_scheduled = 0;
3927
3928 if (window->resize_needed) {
3929 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003930 if (window->main_surface->frame_cb) {
3931 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003932 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003933 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003934
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003935 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003936 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003937
Pekka Paalanen35e82632013-04-25 13:57:48 +03003938 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003939 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003940
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003941 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003942 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003943
3944 wl_list_for_each(surface, &window->subsurface_list, link)
3945 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003946}
3947
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003948static void
3949window_schedule_redraw_task(struct window *window)
3950{
3951 if (window->configure_requests)
3952 return;
3953 if (!window->redraw_task_scheduled) {
3954 window->redraw_task.run = idle_redraw;
3955 display_defer(window->display, &window->redraw_task);
3956 window->redraw_task_scheduled = 1;
3957 }
3958}
3959
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003960void
3961window_schedule_redraw(struct window *window)
3962{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003963 struct surface *surface;
3964
Pekka Paalanen71233882013-04-25 13:57:53 +03003965 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3966
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003967 wl_list_for_each(surface, &window->subsurface_list, link)
3968 surface->redraw_needed = 1;
3969
3970 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003971}
3972
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003973int
3974window_is_fullscreen(struct window *window)
3975{
3976 return window->type == TYPE_FULLSCREEN;
3977}
3978
MoD063a8822013-03-02 23:43:57 +00003979static void
3980configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3981{
3982 struct window *window = data;
3983
3984 wl_callback_destroy(callback);
3985 window->configure_requests--;
3986
3987 if (!window->configure_requests)
3988 window_schedule_redraw(window);
3989}
3990
3991static struct wl_callback_listener configure_request_listener = {
3992 configure_request_completed,
3993};
3994
3995static void
3996window_defer_redraw_until_configure(struct window* window)
3997{
3998 struct wl_callback *callback;
3999
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004000 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00004001 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004002 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00004003 }
4004
4005 callback = wl_display_sync(window->display->display);
4006 wl_callback_add_listener(callback, &configure_request_listener, window);
4007 window->configure_requests++;
4008}
4009
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004010void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004011window_set_fullscreen(struct window *window, int fullscreen)
4012{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004013 if (!window->display->shell)
4014 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004015
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04004016 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004017 return;
4018
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004019 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004020 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01004021 if (window->type == TYPE_TOPLEVEL) {
4022 window->saved_allocation = window->main_surface->allocation;
4023 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004024 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004025 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004026 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004027 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004028 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004029 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004030 if (window->saved_type == TYPE_MAXIMIZED) {
4031 window_set_maximized(window, 1);
4032 } else {
4033 window->type = TYPE_TOPLEVEL;
4034 wl_shell_surface_set_toplevel(window->shell_surface);
4035 window_schedule_resize(window,
4036 window->saved_allocation.width,
4037 window->saved_allocation.height);
4038 }
4039
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004040 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004041}
4042
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004043void
4044window_set_fullscreen_method(struct window *window,
4045 enum wl_shell_surface_fullscreen_method method)
4046{
4047 window->fullscreen_method = method;
4048}
4049
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004050int
4051window_is_maximized(struct window *window)
4052{
4053 return window->type == TYPE_MAXIMIZED;
4054}
4055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004056void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004057window_set_maximized(struct window *window, int maximized)
4058{
4059 if (!window->display->shell)
4060 return;
4061
4062 if ((window->type == TYPE_MAXIMIZED) == maximized)
4063 return;
4064
4065 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004066 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004067 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4068 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004069 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004070 } else if (window->type == TYPE_FULLSCREEN) {
4071 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4072 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004073 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004074 } else {
4075 wl_shell_surface_set_toplevel(window->shell_surface);
4076 window->type = TYPE_TOPLEVEL;
4077 window_schedule_resize(window,
4078 window->saved_allocation.width,
4079 window->saved_allocation.height);
4080 }
4081}
4082
4083void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004084window_set_user_data(struct window *window, void *data)
4085{
4086 window->user_data = data;
4087}
4088
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004089void *
4090window_get_user_data(struct window *window)
4091{
4092 return window->user_data;
4093}
4094
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004095void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004096window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004097 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004098{
4099 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004100}
4101
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004102void
4103window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004104 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004105{
4106 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004107}
4108
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004109void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004110window_set_data_handler(struct window *window, window_data_handler_t handler)
4111{
4112 window->data_handler = handler;
4113}
4114
4115void
4116window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4117{
4118 window->drop_handler = handler;
4119}
4120
4121void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004122window_set_close_handler(struct window *window,
4123 window_close_handler_t handler)
4124{
4125 window->close_handler = handler;
4126}
4127
4128void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004129window_set_fullscreen_handler(struct window *window,
4130 window_fullscreen_handler_t handler)
4131{
4132 window->fullscreen_handler = handler;
4133}
4134
4135void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004136window_set_output_handler(struct window *window,
4137 window_output_handler_t handler)
4138{
4139 window->output_handler = handler;
4140}
4141
4142void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004143window_set_title(struct window *window, const char *title)
4144{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004145 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004146 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004147 if (window->frame) {
4148 frame_set_title(window->frame->frame, title);
4149 widget_schedule_redraw(window->frame->widget);
4150 }
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004151 if (window->shell_surface)
4152 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004153}
4154
4155const char *
4156window_get_title(struct window *window)
4157{
4158 return window->title;
4159}
4160
4161void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004162window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4163{
4164 struct text_cursor_position *text_cursor_position =
4165 window->display->text_cursor_position;
4166
Scott Moreau9295ce02012-06-01 12:46:10 -06004167 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004168 return;
4169
4170 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004171 window->main_surface->surface,
4172 wl_fixed_from_int(x),
4173 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004174}
4175
4176void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004177window_damage(struct window *window, int32_t x, int32_t y,
4178 int32_t width, int32_t height)
4179{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004180 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004181}
4182
Casey Dahlin9074db52012-04-19 22:50:09 -04004183static void
4184surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004185 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004186{
Rob Bradford7507b572012-05-15 17:55:34 +01004187 struct window *window = data;
4188 struct output *output;
4189 struct output *output_found = NULL;
4190 struct window_output *window_output;
4191
4192 wl_list_for_each(output, &window->display->output_list, link) {
4193 if (output->output == wl_output) {
4194 output_found = output;
4195 break;
4196 }
4197 }
4198
4199 if (!output_found)
4200 return;
4201
Brian Lovinbc919262013-08-07 15:34:59 -07004202 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004203 window_output->output = output_found;
4204
4205 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004206
4207 if (window->output_handler)
4208 window->output_handler(window, output_found, 1,
4209 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004210}
4211
4212static void
4213surface_leave(void *data,
4214 struct wl_surface *wl_surface, struct wl_output *output)
4215{
Rob Bradford7507b572012-05-15 17:55:34 +01004216 struct window *window = data;
4217 struct window_output *window_output;
4218 struct window_output *window_output_found = NULL;
4219
4220 wl_list_for_each(window_output, &window->window_output_list, link) {
4221 if (window_output->output->output == output) {
4222 window_output_found = window_output;
4223 break;
4224 }
4225 }
4226
4227 if (window_output_found) {
4228 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004229
4230 if (window->output_handler)
4231 window->output_handler(window, window_output->output,
4232 0, window->user_data);
4233
Rob Bradford7507b572012-05-15 17:55:34 +01004234 free(window_output_found);
4235 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004236}
4237
4238static const struct wl_surface_listener surface_listener = {
4239 surface_enter,
4240 surface_leave
4241};
4242
Pekka Paalanen4e373742013-02-13 16:17:13 +02004243static struct surface *
4244surface_create(struct window *window)
4245{
4246 struct display *display = window->display;
4247 struct surface *surface;
4248
Brian Lovinbc919262013-08-07 15:34:59 -07004249 surface = xmalloc(sizeof *surface);
4250 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004251 if (!surface)
4252 return NULL;
4253
4254 surface->window = window;
4255 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004256 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004257 wl_surface_add_listener(surface->surface, &surface_listener, window);
4258
Pekka Paalanen35e82632013-04-25 13:57:48 +03004259 wl_list_insert(&window->subsurface_list, &surface->link);
4260
Pekka Paalanen4e373742013-02-13 16:17:13 +02004261 return surface;
4262}
4263
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004264static struct window *
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004265window_create_internal(struct display *display, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004266{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004267 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004268 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004269
Peter Huttererf3d62272013-08-08 11:57:05 +10004270 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004271 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004272 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004273
4274 surface = surface_create(window);
4275 window->main_surface = surface;
4276
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004277 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004278 window->shell_surface =
4279 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004280 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004281 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004282 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004283
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004284 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004285 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004286 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004287 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004288
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004289 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004290#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004291 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004292#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004293 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004294#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004295 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004296 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004297
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004298 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004299 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004300 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004301
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004302 if (window->shell_surface) {
4303 wl_shell_surface_set_user_data(window->shell_surface, window);
4304 wl_shell_surface_add_listener(window->shell_surface,
4305 &shell_surface_listener, window);
4306 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004307
Rob Bradford7507b572012-05-15 17:55:34 +01004308 wl_list_init (&window->window_output_list);
4309
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004310 return window;
4311}
4312
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004313struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004314window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004315{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004316 return window_create_internal(display, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004317}
4318
4319struct window *
4320window_create_custom(struct display *display)
4321{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004322 return window_create_internal(display, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004323}
4324
4325struct window *
4326window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004327 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004328{
4329 struct window *window;
4330
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004331 window = window_create_internal(parent->display, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004332
4333 window->x = x;
4334 window->y = y;
4335
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004336 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004337 wl_shell_surface_set_transient(
4338 window->shell_surface,
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004339 parent->main_surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004340 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004341
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004342 return window;
4343}
4344
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004345static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004346menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004347{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004348 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004349 int next;
4350
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004351 frame_interior(menu->frame, &x, &y, &width, &height);
4352 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004353 if (menu->current != next) {
4354 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004355 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004356 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004357}
4358
4359static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004360menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004361 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004362 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004363{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004364 struct menu *menu = data;
4365
4366 if (widget == menu->widget)
4367 menu_set_item(data, y);
4368
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004369 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004370}
4371
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004372static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004373menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004374 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004375{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004376 struct menu *menu = data;
4377
4378 if (widget == menu->widget)
4379 menu_set_item(data, y);
4380
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004381 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004382}
4383
4384static void
4385menu_leave_handler(struct widget *widget, struct input *input, void *data)
4386{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004387 struct menu *menu = data;
4388
4389 if (widget == menu->widget)
4390 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004391}
4392
4393static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004394menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004395 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004396 uint32_t button, enum wl_pointer_button_state state,
4397 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004398
4399{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004400 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004401
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004402 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4403 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004404 /* Either relase after press-drag-release or
4405 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004406 menu->func(menu->parent, input,
4407 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004408 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004409 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004410 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004411 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004412 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004413}
4414
4415static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004416menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004417{
4418 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004419 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004420 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004421
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004422 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004423
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004424 frame_repaint(menu->frame, cr);
4425 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004426
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004427 theme_set_background_source(menu->window->display->theme,
4428 cr, THEME_FRAME_ACTIVE);
4429 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004430 cairo_fill(cr);
4431
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004432 cairo_select_font_face(cr, "sans",
4433 CAIRO_FONT_SLANT_NORMAL,
4434 CAIRO_FONT_WEIGHT_NORMAL);
4435 cairo_set_font_size(cr, 12);
4436
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004437 for (i = 0; i < menu->count; i++) {
4438 if (i == menu->current) {
4439 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004440 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004441 cairo_fill(cr);
4442 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004443 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004444 cairo_show_text(cr, menu->entries[i]);
4445 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004446 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4447 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004448 cairo_show_text(cr, menu->entries[i]);
4449 }
4450 }
4451
4452 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004453}
4454
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004455void
4456window_show_menu(struct display *display,
4457 struct input *input, uint32_t time, struct window *parent,
4458 int32_t x, int32_t y,
4459 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004460{
4461 struct window *window;
4462 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004463 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004464
4465 menu = malloc(sizeof *menu);
4466 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004467 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004468
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004469 window = window_create_internal(parent->display, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004470 if (!window) {
4471 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004472 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004473 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004474
4475 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004476 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004477 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004478 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4479 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004480 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004481 FRAME_BUTTON_NONE, NULL);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004482 menu->entries = entries;
4483 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004484 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004485 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004486 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004487 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004488 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004489 window->type = TYPE_MENU;
4490 window->x = x;
4491 window->y = y;
4492
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004493 input_ungrab(input);
4494
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004495 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004496 widget_set_enter_handler(menu->widget, menu_enter_handler);
4497 widget_set_leave_handler(menu->widget, menu_leave_handler);
4498 widget_set_motion_handler(menu->widget, menu_motion_handler);
4499 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004500
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004501 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004502 frame_resize_inside(menu->frame, 200, count * 20);
4503 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4504 window_schedule_resize(window, frame_width(menu->frame),
4505 frame_height(menu->frame));
4506
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004507 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4508 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4509 display_get_serial(window->display),
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004510 parent->main_surface->surface,
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004511 window->x - ix, window->y - iy, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004512}
4513
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004514void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004515window_set_buffer_type(struct window *window, enum window_buffer_type type)
4516{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004517 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004518}
4519
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004520void
4521window_set_preferred_format(struct window *window,
4522 enum preferred_format format)
4523{
4524 window->preferred_format = format;
4525}
4526
Pekka Paalanen35e82632013-04-25 13:57:48 +03004527struct widget *
4528window_add_subsurface(struct window *window, void *data,
4529 enum subsurface_mode default_mode)
4530{
4531 struct widget *widget;
4532 struct surface *surface;
4533 struct wl_surface *parent;
4534 struct wl_subcompositor *subcompo = window->display->subcompositor;
4535
Pekka Paalanen35e82632013-04-25 13:57:48 +03004536 surface = surface_create(window);
4537 widget = widget_create(window, surface, data);
4538 wl_list_init(&widget->link);
4539 surface->widget = widget;
4540
4541 parent = window->main_surface->surface;
4542 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4543 surface->surface,
4544 parent);
4545 surface->synchronized = 1;
4546
4547 switch (default_mode) {
4548 case SUBSURFACE_SYNCHRONIZED:
4549 surface->synchronized_default = 1;
4550 break;
4551 case SUBSURFACE_DESYNCHRONIZED:
4552 surface->synchronized_default = 0;
4553 break;
4554 default:
4555 assert(!"bad enum subsurface_mode");
4556 }
4557
4558 return widget;
4559}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004560
4561static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004562display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004563 struct wl_output *wl_output,
4564 int x, int y,
4565 int physical_width,
4566 int physical_height,
4567 int subpixel,
4568 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004569 const char *model,
4570 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004571{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004572 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004573
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004574 output->allocation.x = x;
4575 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004576 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004577}
4578
4579static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004580display_handle_done(void *data,
4581 struct wl_output *wl_output)
4582{
4583}
4584
4585static void
4586display_handle_scale(void *data,
4587 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004588 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004589{
4590 struct output *output = data;
4591
4592 output->scale = scale;
4593}
4594
4595static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004596display_handle_mode(void *data,
4597 struct wl_output *wl_output,
4598 uint32_t flags,
4599 int width,
4600 int height,
4601 int refresh)
4602{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004603 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004604 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004605
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004606 if (flags & WL_OUTPUT_MODE_CURRENT) {
4607 output->allocation.width = width;
4608 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004609 if (display->output_configure_handler)
4610 (*display->output_configure_handler)(
4611 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004612 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004613}
4614
4615static const struct wl_output_listener output_listener = {
4616 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004617 display_handle_mode,
4618 display_handle_done,
4619 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004620};
4621
4622static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004623display_add_output(struct display *d, uint32_t id)
4624{
4625 struct output *output;
4626
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004627 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004628 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004629 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004630 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004631 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004632 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004633 wl_list_insert(d->output_list.prev, &output->link);
4634
4635 wl_output_add_listener(output->output, &output_listener, output);
4636}
4637
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004638static void
4639output_destroy(struct output *output)
4640{
4641 if (output->destroy_handler)
4642 (*output->destroy_handler)(output, output->user_data);
4643
4644 wl_output_destroy(output->output);
4645 wl_list_remove(&output->link);
4646 free(output);
4647}
4648
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004649static void
4650display_destroy_output(struct display *d, uint32_t id)
4651{
4652 struct output *output;
4653
4654 wl_list_for_each(output, &d->output_list, link) {
4655 if (output->server_output_id == id) {
4656 output_destroy(output);
4657 break;
4658 }
4659 }
4660}
4661
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004662void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004663display_set_global_handler(struct display *display,
4664 display_global_handler_t handler)
4665{
4666 struct global *global;
4667
4668 display->global_handler = handler;
4669 if (!handler)
4670 return;
4671
4672 wl_list_for_each(global, &display->global_list, link)
4673 display->global_handler(display,
4674 global->name, global->interface,
4675 global->version, display->user_data);
4676}
4677
4678void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004679display_set_global_handler_remove(struct display *display,
4680 display_global_handler_t remove_handler)
4681{
4682 display->global_handler_remove = remove_handler;
4683 if (!remove_handler)
4684 return;
4685}
4686
4687void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004688display_set_output_configure_handler(struct display *display,
4689 display_output_handler_t handler)
4690{
4691 struct output *output;
4692
4693 display->output_configure_handler = handler;
4694 if (!handler)
4695 return;
4696
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004697 wl_list_for_each(output, &display->output_list, link) {
4698 if (output->allocation.width == 0 &&
4699 output->allocation.height == 0)
4700 continue;
4701
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004702 (*display->output_configure_handler)(output,
4703 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004704 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004705}
4706
4707void
4708output_set_user_data(struct output *output, void *data)
4709{
4710 output->user_data = data;
4711}
4712
4713void *
4714output_get_user_data(struct output *output)
4715{
4716 return output->user_data;
4717}
4718
4719void
4720output_set_destroy_handler(struct output *output,
4721 display_output_handler_t handler)
4722{
4723 output->destroy_handler = handler;
4724 /* FIXME: implement this, once we have way to remove outputs */
4725}
4726
4727void
Scott Moreau4e072362012-09-29 02:03:11 -06004728output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004729{
Scott Moreau4e072362012-09-29 02:03:11 -06004730 struct rectangle allocation = output->allocation;
4731
4732 switch (output->transform) {
4733 case WL_OUTPUT_TRANSFORM_90:
4734 case WL_OUTPUT_TRANSFORM_270:
4735 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4736 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4737 /* Swap width and height */
4738 allocation.width = output->allocation.height;
4739 allocation.height = output->allocation.width;
4740 break;
4741 }
4742
4743 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004744}
4745
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004746struct wl_output *
4747output_get_wl_output(struct output *output)
4748{
4749 return output->output;
4750}
4751
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004752enum wl_output_transform
4753output_get_transform(struct output *output)
4754{
4755 return output->transform;
4756}
4757
Alexander Larssonafd319a2013-05-22 14:41:27 +02004758uint32_t
4759output_get_scale(struct output *output)
4760{
4761 return output->scale;
4762}
4763
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004764static void
Daniel Stone97f68542012-05-30 16:32:01 +01004765fini_xkb(struct input *input)
4766{
4767 xkb_state_unref(input->xkb.state);
4768 xkb_map_unref(input->xkb.keymap);
4769}
4770
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004771#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004772
Daniel Stone97f68542012-05-30 16:32:01 +01004773static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004774display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004775{
4776 struct input *input;
4777
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004778 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004779 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004780 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004781 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004782 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004783 input->pointer_focus = NULL;
4784 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004785 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004786 wl_list_insert(d->input_list.prev, &input->link);
4787
Daniel Stone37816df2012-05-16 18:45:18 +01004788 wl_seat_add_listener(input->seat, &seat_listener, input);
4789 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004790
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004791 input->data_device =
4792 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004793 input->seat);
4794 wl_data_device_add_listener(input->data_device, &data_device_listener,
4795 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004796
4797 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004798
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004799 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4800 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004801 input->repeat_task.run = keyboard_repeat_func;
4802 display_watch_fd(d, input->repeat_timer_fd,
4803 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004804}
4805
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004806static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004807input_destroy(struct input *input)
4808{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004809 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004810 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004811
4812 if (input->drag_offer)
4813 data_offer_destroy(input->drag_offer);
4814
4815 if (input->selection_offer)
4816 data_offer_destroy(input->selection_offer);
4817
4818 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004819
4820 if (input->display->seat_version >= 3) {
4821 if (input->pointer)
4822 wl_pointer_release(input->pointer);
4823 if (input->keyboard)
4824 wl_keyboard_release(input->keyboard);
4825 }
4826
Daniel Stone97f68542012-05-30 16:32:01 +01004827 fini_xkb(input);
4828
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004829 wl_surface_destroy(input->pointer_surface);
4830
Pekka Paalanene1207c72011-12-16 12:02:09 +02004831 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004832 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004833 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004834 free(input);
4835}
4836
4837static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004838init_workspace_manager(struct display *d, uint32_t id)
4839{
4840 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004841 wl_registry_bind(d->registry, id,
4842 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004843 if (d->workspace_manager != NULL)
4844 workspace_manager_add_listener(d->workspace_manager,
4845 &workspace_manager_listener,
4846 d);
4847}
4848
4849static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004850shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4851{
4852 struct display *d = data;
4853
4854 if (format == WL_SHM_FORMAT_RGB565)
4855 d->has_rgb565 = 1;
4856}
4857
4858struct wl_shm_listener shm_listener = {
4859 shm_format
4860};
4861
4862static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004863registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4864 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004865{
4866 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004867 struct global *global;
4868
Brian Lovinbc919262013-08-07 15:34:59 -07004869 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004870 global->name = id;
4871 global->interface = strdup(interface);
4872 global->version = version;
4873 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004874
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004875 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004876 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05004877 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004878 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004879 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004880 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01004881 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004882 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004883 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004884 d->shell = wl_registry_bind(registry,
4885 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004886 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004887 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004888 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004889 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4890 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004891 wl_registry_bind(registry, id,
4892 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004893 } else if (strcmp(interface, "text_cursor_position") == 0) {
4894 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004895 wl_registry_bind(registry, id,
4896 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004897 } else if (strcmp(interface, "workspace_manager") == 0) {
4898 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004899 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4900 d->subcompositor =
4901 wl_registry_bind(registry, id,
4902 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004903 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004904
4905 if (d->global_handler)
4906 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004907}
4908
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004909static void
4910registry_handle_global_remove(void *data, struct wl_registry *registry,
4911 uint32_t name)
4912{
4913 struct display *d = data;
4914 struct global *global;
4915 struct global *tmp;
4916
4917 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4918 if (global->name != name)
4919 continue;
4920
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004921 if (strcmp(global->interface, "wl_output") == 0)
4922 display_destroy_output(d, name);
4923
4924 /* XXX: Should destroy remaining bound globals */
4925
4926 if (d->global_handler_remove)
4927 d->global_handler_remove(d, name, global->interface,
4928 global->version, d->user_data);
4929
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004930 wl_list_remove(&global->link);
4931 free(global->interface);
4932 free(global);
4933 }
4934}
4935
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004936void *
4937display_bind(struct display *display, uint32_t name,
4938 const struct wl_interface *interface, uint32_t version)
4939{
4940 return wl_registry_bind(display->registry, name, interface, version);
4941}
4942
4943static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004944 registry_handle_global,
4945 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004946};
4947
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004948#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004949static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004950init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004951{
4952 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004953 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004954
Rob Clark6396ed32012-03-11 19:48:41 -05004955#ifdef USE_CAIRO_GLESV2
4956# define GL_BIT EGL_OPENGL_ES2_BIT
4957#else
4958# define GL_BIT EGL_OPENGL_BIT
4959#endif
4960
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004961 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004962 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004963 EGL_RED_SIZE, 1,
4964 EGL_GREEN_SIZE, 1,
4965 EGL_BLUE_SIZE, 1,
4966 EGL_ALPHA_SIZE, 1,
4967 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004968 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004969 EGL_NONE
4970 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004971
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004972#ifdef USE_CAIRO_GLESV2
4973 static const EGLint context_attribs[] = {
4974 EGL_CONTEXT_CLIENT_VERSION, 2,
4975 EGL_NONE
4976 };
4977 EGLint api = EGL_OPENGL_ES_API;
4978#else
4979 EGLint *context_attribs = NULL;
4980 EGLint api = EGL_OPENGL_API;
4981#endif
4982
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004983 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004984 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004985 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004986 return -1;
4987 }
4988
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004989 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004990 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004991 return -1;
4992 }
4993
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004994 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4995 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004996 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004997 return -1;
4998 }
4999
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005000 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005001 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005002 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005003 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005004 return -1;
5005 }
5006
Benjamin Franzke0c991632011-09-27 21:57:31 +02005007 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5008 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005009 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005010 return -1;
5011 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005012
5013 return 0;
5014}
5015
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005016static void
5017fini_egl(struct display *display)
5018{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005019 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005020
5021 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5022 EGL_NO_CONTEXT);
5023
5024 eglTerminate(display->dpy);
5025 eglReleaseThread();
5026}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005027#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005028
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005029static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005030init_dummy_surface(struct display *display)
5031{
5032 int len;
5033 void *data;
5034
5035 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5036 data = malloc(len);
5037 display->dummy_surface =
5038 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5039 1, 1, len);
5040 display->dummy_surface_data = data;
5041}
5042
5043static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005044handle_display_data(struct task *task, uint32_t events)
5045{
5046 struct display *display =
5047 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005048 struct epoll_event ep;
5049 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005050
5051 display->display_fd_events = events;
5052
5053 if (events & EPOLLERR || events & EPOLLHUP) {
5054 display_exit(display);
5055 return;
5056 }
5057
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005058 if (events & EPOLLIN) {
5059 ret = wl_display_dispatch(display->display);
5060 if (ret == -1) {
5061 display_exit(display);
5062 return;
5063 }
5064 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005065
5066 if (events & EPOLLOUT) {
5067 ret = wl_display_flush(display->display);
5068 if (ret == 0) {
5069 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5070 ep.data.ptr = &display->display_task;
5071 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5072 display->display_fd, &ep);
5073 } else if (ret == -1 && errno != EAGAIN) {
5074 display_exit(display);
5075 return;
5076 }
5077 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005078}
5079
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005080static void
5081log_handler(const char *format, va_list args)
5082{
5083 vfprintf(stderr, format, args);
5084}
5085
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005086struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005087display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005088{
5089 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005090
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005091 wl_log_set_handler_client(log_handler);
5092
Peter Huttererf3d62272013-08-08 11:57:05 +10005093 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005094 if (d == NULL)
5095 return NULL;
5096
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005097 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005098 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005099 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005100 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005101 return NULL;
5102 }
5103
Rob Bradford5ab9c752013-07-26 16:29:43 +01005104 d->xkb_context = xkb_context_new(0);
5105 if (d->xkb_context == NULL) {
5106 fprintf(stderr, "Failed to create XKB context\n");
5107 free(d);
5108 return NULL;
5109 }
5110
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005111 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005112 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005113 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005114 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5115 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005116
5117 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005118 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005119 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005120 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005121
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005122 d->workspace = 0;
5123 d->workspace_count = 1;
5124
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005125 d->registry = wl_display_get_registry(d->display);
5126 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005127
5128 if (wl_display_dispatch(d->display) < 0) {
5129 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5130 return NULL;
5131 }
5132
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005133#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005134 if (init_egl(d) < 0)
5135 fprintf(stderr, "EGL does not seem to work, "
5136 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005137#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005138
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005139 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005140
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005141 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005142
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005143 wl_list_init(&d->window_list);
5144
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005145 init_dummy_surface(d);
5146
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005147 return d;
5148}
5149
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005150static void
5151display_destroy_outputs(struct display *display)
5152{
5153 struct output *tmp;
5154 struct output *output;
5155
5156 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5157 output_destroy(output);
5158}
5159
Pekka Paalanene1207c72011-12-16 12:02:09 +02005160static void
5161display_destroy_inputs(struct display *display)
5162{
5163 struct input *tmp;
5164 struct input *input;
5165
5166 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5167 input_destroy(input);
5168}
5169
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005170void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005171display_destroy(struct display *display)
5172{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005173 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005174 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5175 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005176
5177 if (!wl_list_empty(&display->deferred_list))
5178 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5179
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005180 cairo_surface_destroy(display->dummy_surface);
5181 free(display->dummy_surface_data);
5182
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005183 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005184 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005185
Daniel Stone97f68542012-05-30 16:32:01 +01005186 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005187
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005188 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005189 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005190
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005191#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005192 if (display->argb_device)
5193 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005194#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005195
Pekka Paalanen35e82632013-04-25 13:57:48 +03005196 if (display->subcompositor)
5197 wl_subcompositor_destroy(display->subcompositor);
5198
Pekka Paalanenc2052982011-12-16 11:41:32 +02005199 if (display->shell)
5200 wl_shell_destroy(display->shell);
5201
5202 if (display->shm)
5203 wl_shm_destroy(display->shm);
5204
5205 if (display->data_device_manager)
5206 wl_data_device_manager_destroy(display->data_device_manager);
5207
5208 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005209 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005210
5211 close(display->epoll_fd);
5212
U. Artie Eoff44874d92012-10-02 21:12:35 -07005213 if (!(display->display_fd_events & EPOLLERR) &&
5214 !(display->display_fd_events & EPOLLHUP))
5215 wl_display_flush(display->display);
5216
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005217 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005218 free(display);
5219}
5220
5221void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005222display_set_user_data(struct display *display, void *data)
5223{
5224 display->user_data = data;
5225}
5226
5227void *
5228display_get_user_data(struct display *display)
5229{
5230 return display->user_data;
5231}
5232
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005233struct wl_display *
5234display_get_display(struct display *display)
5235{
5236 return display->display;
5237}
5238
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005239int
5240display_has_subcompositor(struct display *display)
5241{
5242 if (display->subcompositor)
5243 return 1;
5244
5245 wl_display_roundtrip(display->display);
5246
5247 return display->subcompositor != NULL;
5248}
5249
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005250cairo_device_t *
5251display_get_cairo_device(struct display *display)
5252{
5253 return display->argb_device;
5254}
5255
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005256struct output *
5257display_get_output(struct display *display)
5258{
5259 return container_of(display->output_list.next, struct output, link);
5260}
5261
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005262struct wl_compositor *
5263display_get_compositor(struct display *display)
5264{
5265 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005266}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005267
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005268uint32_t
5269display_get_serial(struct display *display)
5270{
5271 return display->serial;
5272}
5273
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005274EGLDisplay
5275display_get_egl_display(struct display *d)
5276{
5277 return d->dpy;
5278}
5279
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005280struct wl_data_source *
5281display_create_data_source(struct display *display)
5282{
5283 return wl_data_device_manager_create_data_source(display->data_device_manager);
5284}
5285
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005286EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005287display_get_argb_egl_config(struct display *d)
5288{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005289 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005290}
5291
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005292int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005293display_acquire_window_surface(struct display *display,
5294 struct window *window,
5295 EGLContext ctx)
5296{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005297 struct surface *surface = window->main_surface;
5298
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005299 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005300 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005301
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005302 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005303 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005304}
5305
5306void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005307display_release_window_surface(struct display *display,
5308 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005309{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005310 struct surface *surface = window->main_surface;
5311
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005312 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005313 return;
5314
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005315 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005316}
5317
5318void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005319display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005320{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005321 wl_list_insert(&display->deferred_list, &task->link);
5322}
5323
5324void
5325display_watch_fd(struct display *display,
5326 int fd, uint32_t events, struct task *task)
5327{
5328 struct epoll_event ep;
5329
5330 ep.events = events;
5331 ep.data.ptr = task;
5332 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5333}
5334
5335void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005336display_unwatch_fd(struct display *display, int fd)
5337{
5338 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5339}
5340
5341void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005342display_run(struct display *display)
5343{
5344 struct task *task;
5345 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005346 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005347
Pekka Paalanen826d7952011-12-15 10:14:07 +02005348 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005349 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005350 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005351 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005352 struct task, link);
5353 wl_list_remove(&task->link);
5354 task->run(task, 0);
5355 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005356
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005357 wl_display_dispatch_pending(display->display);
5358
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005359 if (!display->running)
5360 break;
5361
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005362 ret = wl_display_flush(display->display);
5363 if (ret < 0 && errno == EAGAIN) {
5364 ep[0].events =
5365 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5366 ep[0].data.ptr = &display->display_task;
5367
5368 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5369 display->display_fd, &ep[0]);
5370 } else if (ret < 0) {
5371 break;
5372 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005373
5374 count = epoll_wait(display->epoll_fd,
5375 ep, ARRAY_LENGTH(ep), -1);
5376 for (i = 0; i < count; i++) {
5377 task = ep[i].data.ptr;
5378 task->run(task, ep[i].events);
5379 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005380 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005381}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005382
5383void
5384display_exit(struct display *display)
5385{
5386 display->running = 0;
5387}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005388
5389void
5390keysym_modifiers_add(struct wl_array *modifiers_map,
5391 const char *name)
5392{
5393 size_t len = strlen(name) + 1;
5394 char *p;
5395
5396 p = wl_array_add(modifiers_map, len);
5397
5398 if (p == NULL)
5399 return;
5400
5401 strncpy(p, name, len);
5402}
5403
5404static xkb_mod_index_t
5405keysym_modifiers_get_index(struct wl_array *modifiers_map,
5406 const char *name)
5407{
5408 xkb_mod_index_t index = 0;
5409 char *p = modifiers_map->data;
5410
5411 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5412 if (strcmp(p, name) == 0)
5413 return index;
5414
5415 index++;
5416 p += strlen(p) + 1;
5417 }
5418
5419 return XKB_MOD_INVALID;
5420}
5421
5422xkb_mod_mask_t
5423keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5424 const char *name)
5425{
5426 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5427
5428 if (index == XKB_MOD_INVALID)
5429 return XKB_MOD_INVALID;
5430
5431 return 1 << index;
5432}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005433
5434void *
5435fail_on_null(void *p)
5436{
5437 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005438 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005439 exit(EXIT_FAILURE);
5440 }
5441
5442 return p;
5443}
5444
5445void *
5446xmalloc(size_t s)
5447{
5448 return fail_on_null(malloc(s));
5449}
5450
Peter Huttererf3d62272013-08-08 11:57:05 +10005451void *
5452xzalloc(size_t s)
5453{
5454 return fail_on_null(zalloc(s));
5455}
5456
Kristian Høgsbergce278412013-07-25 15:20:20 -07005457char *
5458xstrdup(const char *s)
5459{
5460 return fail_on_null(strdup(s));
5461}