blob: be89b641255f0195f40e2273d250eb246dd2d8d6 [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;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800292 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700293 struct widget *widget;
294 struct wl_list link;
295};
296
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400297struct input {
298 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100299 struct wl_seat *seat;
300 struct wl_pointer *pointer;
301 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700302 struct wl_touch *touch;
303 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400304 struct window *pointer_focus;
305 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700306 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300307 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300308 uint32_t cursor_anim_start;
309 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300310 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400311 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400312 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400313 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400314 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400315 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400316
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500317 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500318 struct widget *grab;
319 uint32_t grab_button;
320
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400321 struct wl_data_device *data_device;
322 struct data_offer *drag_offer;
323 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800324 uint32_t touch_grab;
325 int32_t touch_grab_id;
326 float drag_x, drag_y;
327 struct window *drag_focus;
328 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100329
330 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100331 struct xkb_keymap *keymap;
332 struct xkb_state *state;
333 xkb_mod_mask_t control_mask;
334 xkb_mod_mask_t alt_mask;
335 xkb_mod_mask_t shift_mask;
336 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400337
338 struct task repeat_task;
339 int repeat_timer_fd;
340 uint32_t repeat_sym;
341 uint32_t repeat_key;
342 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400343};
344
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500345struct output {
346 struct display *display;
347 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800348 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500349 struct rectangle allocation;
350 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600351 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200352 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200353
354 display_output_handler_t destroy_handler;
355 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500356};
357
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500358struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500359 struct widget *widget;
360 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500361 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500362};
363
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500364struct menu {
365 struct window *window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -0500366 struct window *parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500367 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500368 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700369 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500370 const char **entries;
371 uint32_t time;
372 int current;
373 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400374 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500375 menu_func_t func;
376};
377
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300378struct tooltip {
379 struct widget *parent;
380 struct window *window;
381 struct widget *widget;
382 char *entry;
383 struct task tooltip_task;
384 int tooltip_fd;
385 float x, y;
386};
387
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700388struct shm_pool {
389 struct wl_shm_pool *pool;
390 size_t size;
391 size_t used;
392 void *data;
393};
394
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400395enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300396 CURSOR_DEFAULT = 100,
397 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400398};
399
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500400enum window_location {
401 WINDOW_INTERIOR = 0,
402 WINDOW_RESIZING_TOP = 1,
403 WINDOW_RESIZING_BOTTOM = 2,
404 WINDOW_RESIZING_LEFT = 4,
405 WINDOW_RESIZING_TOP_LEFT = 5,
406 WINDOW_RESIZING_BOTTOM_LEFT = 6,
407 WINDOW_RESIZING_RIGHT = 8,
408 WINDOW_RESIZING_TOP_RIGHT = 9,
409 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
410 WINDOW_RESIZING_MASK = 15,
411 WINDOW_EXTERIOR = 16,
412 WINDOW_TITLEBAR = 17,
413 WINDOW_CLIENT_AREA = 18,
414};
415
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200416static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417
Pekka Paalanen97777442013-05-22 10:20:05 +0300418/* #define DEBUG */
419
420#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300421
422static void
423debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
424__attribute__ ((format (printf, 4, 5)));
425
426static void
427debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
428{
429 va_list ap;
430 struct timeval tv;
431
432 gettimeofday(&tv, NULL);
433 fprintf(stderr, "%8ld.%03ld ",
434 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
435
436 if (proxy)
437 fprintf(stderr, "%s@%d ",
438 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
439
440 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
441 fprintf(stderr, "%s ", func);
442
443 va_start(ap, fmt);
444 vfprintf(stderr, fmt, ap);
445 va_end(ap);
446}
447
448#define DBG(fmt, ...) \
449 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
450
451#define DBG_OBJ(obj, fmt, ...) \
452 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
453
454#else
455
456#define DBG(...) do {} while (0)
457#define DBG_OBJ(...) do {} while (0)
458
459#endif
460
Alexander Larsson1818e312013-05-22 14:41:31 +0200461static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200462surface_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 +0200463{
464 int32_t tmp;
465
466 switch (buffer_transform) {
467 case WL_OUTPUT_TRANSFORM_90:
468 case WL_OUTPUT_TRANSFORM_270:
469 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
470 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
471 tmp = *width;
472 *width = *height;
473 *height = tmp;
474 break;
475 default:
476 break;
477 }
478
479 *width *= buffer_scale;
480 *height *= buffer_scale;
481}
482
483static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200484buffer_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 +0200485{
486 int32_t tmp;
487
488 switch (buffer_transform) {
489 case WL_OUTPUT_TRANSFORM_90:
490 case WL_OUTPUT_TRANSFORM_270:
491 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
492 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
493 tmp = *width;
494 *width = *height;
495 *height = tmp;
496 break;
497 default:
498 break;
499 }
500
501 *width /= buffer_scale;
502 *height /= buffer_scale;
503}
504
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500505#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400506
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200507struct egl_window_surface {
508 struct toysurface base;
509 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100510 struct display *display;
511 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200512 struct wl_egl_window *egl_window;
513 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100514};
515
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200516static struct egl_window_surface *
517to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100518{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520}
521
522static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200524 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200525 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100526{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200527 struct egl_window_surface *surface = to_egl_window_surface(base);
528
Alexander Larsson1818e312013-05-22 14:41:31 +0200529 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
530
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200531 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
532 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
533
534 return cairo_surface_reference(surface->cairo_surface);
535}
536
537static void
538egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200539 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200540 struct rectangle *server_allocation)
541{
542 struct egl_window_surface *surface = to_egl_window_surface(base);
543
544 cairo_gl_surface_swapbuffers(surface->cairo_surface);
545 wl_egl_window_get_attached_size(surface->egl_window,
546 &server_allocation->width,
547 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200548
549 buffer_to_surface_size (buffer_transform, buffer_scale,
550 &server_allocation->width,
551 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200552}
553
554static int
555egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
556{
557 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200558 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100559
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200560 device = cairo_surface_get_device(surface->cairo_surface);
561 if (!device)
562 return -1;
563
564 if (!ctx) {
565 if (device == surface->display->argb_device)
566 ctx = surface->display->argb_ctx;
567 else
568 assert(0);
569 }
570
571 cairo_device_flush(device);
572 cairo_device_acquire(device);
573 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
574 surface->egl_surface, ctx))
575 fprintf(stderr, "failed to make surface current\n");
576
577 return 0;
578}
579
580static void
581egl_window_surface_release(struct toysurface *base)
582{
583 struct egl_window_surface *surface = to_egl_window_surface(base);
584 cairo_device_t *device;
585
586 device = cairo_surface_get_device(surface->cairo_surface);
587 if (!device)
588 return;
589
590 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
591 surface->display->argb_ctx))
592 fprintf(stderr, "failed to make context current\n");
593
594 cairo_device_release(device);
595}
596
597static void
598egl_window_surface_destroy(struct toysurface *base)
599{
600 struct egl_window_surface *surface = to_egl_window_surface(base);
601 struct display *d = surface->display;
602
603 cairo_surface_destroy(surface->cairo_surface);
604 eglDestroySurface(d->dpy, surface->egl_surface);
605 wl_egl_window_destroy(surface->egl_window);
606 surface->surface = NULL;
607
608 free(surface);
609}
610
611static struct toysurface *
612egl_window_surface_create(struct display *display,
613 struct wl_surface *wl_surface,
614 uint32_t flags,
615 struct rectangle *rectangle)
616{
617 struct egl_window_surface *surface;
618
Pekka Paalanenb3627362012-11-19 17:16:00 +0200619 if (display->dpy == EGL_NO_DISPLAY)
620 return NULL;
621
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200622 surface = calloc(1, sizeof *surface);
623 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100624 return NULL;
625
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200626 surface->base.prepare = egl_window_surface_prepare;
627 surface->base.swap = egl_window_surface_swap;
628 surface->base.acquire = egl_window_surface_acquire;
629 surface->base.release = egl_window_surface_release;
630 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200632 surface->display = display;
633 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400634
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200635 surface->egl_window = wl_egl_window_create(surface->surface,
636 rectangle->width,
637 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200639 surface->egl_surface = eglCreateWindowSurface(display->dpy,
640 display->argb_config,
641 surface->egl_window,
642 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200644 surface->cairo_surface =
645 cairo_gl_surface_create_for_egl(display->argb_device,
646 surface->egl_surface,
647 rectangle->width,
648 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200650 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100651}
652
Pekka Paalanenb3627362012-11-19 17:16:00 +0200653#else
654
655static struct toysurface *
656egl_window_surface_create(struct display *display,
657 struct wl_surface *wl_surface,
658 uint32_t flags,
659 struct rectangle *rectangle)
660{
661 return NULL;
662}
663
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400664#endif
665
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200666struct shm_surface_data {
667 struct wl_buffer *buffer;
668 struct shm_pool *pool;
669};
670
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400671struct wl_buffer *
672display_get_buffer_for_surface(struct display *display,
673 cairo_surface_t *surface)
674{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200675 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400676
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200677 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400678
679 return data->buffer;
680}
681
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500682static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700683shm_pool_destroy(struct shm_pool *pool);
684
685static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400686shm_surface_data_destroy(void *p)
687{
688 struct shm_surface_data *data = p;
689
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200690 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700691 if (data->pool)
692 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300693
694 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400695}
696
Kristian Høgsberg16626282012-04-03 11:21:27 -0400697static struct wl_shm_pool *
698make_shm_pool(struct display *display, int size, void **data)
699{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400700 struct wl_shm_pool *pool;
701 int fd;
702
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300703 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400704 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300705 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
706 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400707 return NULL;
708 }
709
710 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400711 if (*data == MAP_FAILED) {
712 fprintf(stderr, "mmap failed: %m\n");
713 close(fd);
714 return NULL;
715 }
716
717 pool = wl_shm_create_pool(display->shm, fd, size);
718
719 close(fd);
720
721 return pool;
722}
723
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700724static struct shm_pool *
725shm_pool_create(struct display *display, size_t size)
726{
727 struct shm_pool *pool = malloc(sizeof *pool);
728
729 if (!pool)
730 return NULL;
731
732 pool->pool = make_shm_pool(display, size, &pool->data);
733 if (!pool->pool) {
734 free(pool);
735 return NULL;
736 }
737
738 pool->size = size;
739 pool->used = 0;
740
741 return pool;
742}
743
744static void *
745shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
746{
747 if (pool->used + size > pool->size)
748 return NULL;
749
750 *offset = pool->used;
751 pool->used += size;
752
753 return (char *) pool->data + *offset;
754}
755
756/* destroy the pool. this does not unmap the memory though */
757static void
758shm_pool_destroy(struct shm_pool *pool)
759{
760 munmap(pool->data, pool->size);
761 wl_shm_pool_destroy(pool->pool);
762 free(pool);
763}
764
765/* Start allocating from the beginning of the pool again */
766static void
767shm_pool_reset(struct shm_pool *pool)
768{
769 pool->used = 0;
770}
771
772static int
773data_length_for_shm_surface(struct rectangle *rect)
774{
775 int stride;
776
777 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
778 rect->width);
779 return stride * rect->height;
780}
781
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500782static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700783display_create_shm_surface_from_pool(struct display *display,
784 struct rectangle *rectangle,
785 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400786{
787 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400788 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400789 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200790 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700791 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400792 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400793
794 data = malloc(sizeof *data);
795 if (data == NULL)
796 return NULL;
797
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200798 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
799 cairo_format = CAIRO_FORMAT_RGB16_565;
800 else
801 cairo_format = CAIRO_FORMAT_ARGB32;
802
803 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700804 length = stride * rectangle->height;
805 data->pool = NULL;
806 map = shm_pool_allocate(pool, length, &offset);
807
808 if (!map) {
809 free(data);
810 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400811 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400812
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400813 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200814 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400815 rectangle->width,
816 rectangle->height,
817 stride);
818
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200819 cairo_surface_set_user_data(surface, &shm_surface_data_key,
820 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200822 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
823 format = WL_SHM_FORMAT_RGB565;
824 else {
825 if (flags & SURFACE_OPAQUE)
826 format = WL_SHM_FORMAT_XRGB8888;
827 else
828 format = WL_SHM_FORMAT_ARGB8888;
829 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400830
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200831 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
832 rectangle->width,
833 rectangle->height,
834 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700836 return surface;
837}
838
839static cairo_surface_t *
840display_create_shm_surface(struct display *display,
841 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200842 struct shm_pool *alternate_pool,
843 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700844{
845 struct shm_surface_data *data;
846 struct shm_pool *pool;
847 cairo_surface_t *surface;
848
Pekka Paalanen99436862012-11-19 17:15:59 +0200849 if (alternate_pool) {
850 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700851 surface = display_create_shm_surface_from_pool(display,
852 rectangle,
853 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200854 alternate_pool);
855 if (surface) {
856 data = cairo_surface_get_user_data(surface,
857 &shm_surface_data_key);
858 goto out;
859 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700860 }
861
862 pool = shm_pool_create(display,
863 data_length_for_shm_surface(rectangle));
864 if (!pool)
865 return NULL;
866
867 surface =
868 display_create_shm_surface_from_pool(display, rectangle,
869 flags, pool);
870
871 if (!surface) {
872 shm_pool_destroy(pool);
873 return NULL;
874 }
875
876 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200877 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700878 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400879
Pekka Paalanen99436862012-11-19 17:15:59 +0200880out:
881 if (data_ret)
882 *data_ret = data;
883
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400884 return surface;
885}
886
nobled7b87cb02011-02-01 18:51:47 +0000887static int
888check_size(struct rectangle *rect)
889{
890 if (rect->width && rect->height)
891 return 0;
892
893 fprintf(stderr, "tried to create surface of "
894 "width: %d, height: %d\n", rect->width, rect->height);
895 return -1;
896}
897
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400898cairo_surface_t *
899display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100900 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400901 struct rectangle *rectangle,
902 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400903{
nobled7b87cb02011-02-01 18:51:47 +0000904 if (check_size(rectangle) < 0)
905 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200906
907 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200908 return display_create_shm_surface(display, rectangle, flags,
909 NULL, NULL);
910}
911
Pekka Paalanena4eda732012-11-19 17:16:02 +0200912struct shm_surface_leaf {
913 cairo_surface_t *cairo_surface;
914 /* 'data' is automatically destroyed, when 'cairo_surface' is */
915 struct shm_surface_data *data;
916
917 struct shm_pool *resize_pool;
918 int busy;
919};
920
921static void
922shm_surface_leaf_release(struct shm_surface_leaf *leaf)
923{
924 if (leaf->cairo_surface)
925 cairo_surface_destroy(leaf->cairo_surface);
926 /* leaf->data already destroyed via cairo private */
927
928 if (leaf->resize_pool)
929 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200930
931 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200932}
933
Pekka Paalanenaef02542013-04-25 13:57:47 +0300934#define MAX_LEAVES 3
935
Pekka Paalanen99436862012-11-19 17:15:59 +0200936struct shm_surface {
937 struct toysurface base;
938 struct display *display;
939 struct wl_surface *surface;
940 uint32_t flags;
941 int dx, dy;
942
Pekka Paalanenaef02542013-04-25 13:57:47 +0300943 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200944 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200945};
946
947static struct shm_surface *
948to_shm_surface(struct toysurface *base)
949{
950 return container_of(base, struct shm_surface, base);
951}
952
Pekka Paalanena4eda732012-11-19 17:16:02 +0200953static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300954shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
955{
956#ifdef DEBUG
957 struct shm_surface_leaf *leaf;
958 char bufs[MAX_LEAVES + 1];
959 int i;
960
961 for (i = 0; i < MAX_LEAVES; i++) {
962 leaf = &surface->leaf[i];
963
964 if (leaf->busy)
965 bufs[i] = 'b';
966 else if (leaf->cairo_surface)
967 bufs[i] = 'a';
968 else
969 bufs[i] = ' ';
970 }
971
972 bufs[MAX_LEAVES] = '\0';
973 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
974#endif
975}
976
977static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200978shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
979{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200980 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300981 struct shm_surface_leaf *leaf;
982 int i;
983 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300984
985 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200986
Pekka Paalanenaef02542013-04-25 13:57:47 +0300987 for (i = 0; i < MAX_LEAVES; i++) {
988 leaf = &surface->leaf[i];
989 if (leaf->data && leaf->data->buffer == buffer) {
990 leaf->busy = 0;
991 break;
992 }
993 }
994 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200995
Pekka Paalanenaef02542013-04-25 13:57:47 +0300996 /* Leave one free leaf with storage, release others */
997 free_found = 0;
998 for (i = 0; i < MAX_LEAVES; i++) {
999 leaf = &surface->leaf[i];
1000
1001 if (!leaf->cairo_surface || leaf->busy)
1002 continue;
1003
1004 if (!free_found)
1005 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001006 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001007 shm_surface_leaf_release(leaf);
1008 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001009
Pekka Paalanen97777442013-05-22 10:20:05 +03001010 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001011}
1012
1013static const struct wl_buffer_listener shm_surface_buffer_listener = {
1014 shm_surface_buffer_release
1015};
1016
Pekka Paalanen99436862012-11-19 17:15:59 +02001017static cairo_surface_t *
1018shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001019 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001020 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001021{
Pekka Paalanenec076692012-11-30 13:37:27 +02001022 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001023 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001024 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001025 struct shm_surface_leaf *leaf = NULL;
1026 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001027
1028 surface->dx = dx;
1029 surface->dy = dy;
1030
Pekka Paalanenaef02542013-04-25 13:57:47 +03001031 /* pick a free buffer, preferrably one that already has storage */
1032 for (i = 0; i < MAX_LEAVES; i++) {
1033 if (surface->leaf[i].busy)
1034 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001035
Pekka Paalanenaef02542013-04-25 13:57:47 +03001036 if (!leaf || surface->leaf[i].cairo_surface)
1037 leaf = &surface->leaf[i];
1038 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001039 DBG_OBJ(surface->surface, "pick leaf %d\n",
1040 (int)(leaf - &surface->leaf[0]));
1041
Pekka Paalanenaef02542013-04-25 13:57:47 +03001042 if (!leaf) {
1043 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001044 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001045 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001046 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001047 }
1048
Pekka Paalanena4eda732012-11-19 17:16:02 +02001049 if (!resize_hint && leaf->resize_pool) {
1050 cairo_surface_destroy(leaf->cairo_surface);
1051 leaf->cairo_surface = NULL;
1052 shm_pool_destroy(leaf->resize_pool);
1053 leaf->resize_pool = NULL;
1054 }
1055
Alexander Larsson1818e312013-05-22 14:41:31 +02001056 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1057
Pekka Paalanena4eda732012-11-19 17:16:02 +02001058 if (leaf->cairo_surface &&
1059 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1060 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001061 goto out;
1062
Pekka Paalanena4eda732012-11-19 17:16:02 +02001063 if (leaf->cairo_surface)
1064 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001065
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001066#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001067 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001068 /* Create a big pool to allocate from, while continuously
1069 * resizing. Mmapping a new pool in the server
1070 * is relatively expensive, so reusing a pool performs
1071 * better, but may temporarily reserve unneeded memory.
1072 */
1073 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 leaf->resize_pool = shm_pool_create(surface->display,
1075 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001076 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001077#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001078
Alexander Larsson1818e312013-05-22 14:41:31 +02001079 rect.width = width;
1080 rect.height = height;
1081
Pekka Paalanena4eda732012-11-19 17:16:02 +02001082 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001083 display_create_shm_surface(surface->display, &rect,
1084 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001085 leaf->resize_pool,
1086 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001087 if (!leaf->cairo_surface)
1088 return NULL;
1089
Pekka Paalanena4eda732012-11-19 17:16:02 +02001090 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001091 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001092
1093out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001094 surface->current = leaf;
1095
1096 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001097}
1098
1099static void
1100shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001101 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001102 struct rectangle *server_allocation)
1103{
1104 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001106
1107 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001108 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001109 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001110 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001111
Alexander Larsson1818e312013-05-22 14:41:31 +02001112 buffer_to_surface_size (buffer_transform, buffer_scale,
1113 &server_allocation->width,
1114 &server_allocation->height);
1115
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001117 surface->dx, surface->dy);
1118 wl_surface_damage(surface->surface, 0, 0,
1119 server_allocation->width, server_allocation->height);
1120 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001121
Pekka Paalanen71233882013-04-25 13:57:53 +03001122 DBG_OBJ(surface->surface, "leaf %d busy\n",
1123 (int)(leaf - &surface->leaf[0]));
1124
Pekka Paalanena4eda732012-11-19 17:16:02 +02001125 leaf->busy = 1;
1126 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001127}
1128
1129static int
1130shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1131{
1132 return -1;
1133}
1134
1135static void
1136shm_surface_release(struct toysurface *base)
1137{
1138}
1139
1140static void
1141shm_surface_destroy(struct toysurface *base)
1142{
1143 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001144 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001145
Pekka Paalanenaef02542013-04-25 13:57:47 +03001146 for (i = 0; i < MAX_LEAVES; i++)
1147 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001148
1149 free(surface);
1150}
1151
1152static struct toysurface *
1153shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1154 uint32_t flags, struct rectangle *rectangle)
1155{
1156 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001157 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001158
Brian Lovinbc919262013-08-07 15:34:59 -07001159 surface = xmalloc(sizeof *surface);
1160 memset(surface, 0, sizeof *surface);
1161
Pekka Paalanen99436862012-11-19 17:15:59 +02001162 if (!surface)
1163 return NULL;
1164
1165 surface->base.prepare = shm_surface_prepare;
1166 surface->base.swap = shm_surface_swap;
1167 surface->base.acquire = shm_surface_acquire;
1168 surface->base.release = shm_surface_release;
1169 surface->base.destroy = shm_surface_destroy;
1170
1171 surface->display = display;
1172 surface->surface = wl_surface;
1173 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001174
1175 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001176}
1177
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001178/*
1179 * The following correspondences between file names and cursors was copied
1180 * from: https://bugs.kde.org/attachment.cgi?id=67313
1181 */
1182
1183static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001184 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001185 "sw-resize",
1186 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001187};
1188
1189static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001190 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001191 "se-resize",
1192 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001193};
1194
1195static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001196 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001197 "s-resize",
1198 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001199};
1200
1201static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001202 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001203 "closedhand",
1204 "208530c400c041818281048008011002"
1205};
1206
1207static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001208 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001209 "default",
1210 "top_left_arrow",
1211 "left-arrow"
1212};
1213
1214static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001215 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001216 "w-resize",
1217 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001218};
1219
1220static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "e-resize",
1223 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001228 "nw-resize",
1229 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230};
1231
1232static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001234 "ne-resize",
1235 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236};
1237
1238static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001240 "n-resize",
1241 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242};
1243
1244static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001245 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001246 "ibeam",
1247 "text"
1248};
1249
1250static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001251 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001252 "pointer",
1253 "pointing_hand",
1254 "e29285e634086352946a0e7090d73106"
1255};
1256
1257static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001258 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001259 "wait",
1260 "0426c94ea35c87780ff01dc239897213"
1261};
1262
1263struct cursor_alternatives {
1264 const char **names;
1265 size_t count;
1266};
1267
1268static const struct cursor_alternatives cursors[] = {
1269 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1270 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1271 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1272 {grabbings, ARRAY_LENGTH(grabbings)},
1273 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1274 {left_sides, ARRAY_LENGTH(left_sides)},
1275 {right_sides, ARRAY_LENGTH(right_sides)},
1276 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1277 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1278 {top_sides, ARRAY_LENGTH(top_sides)},
1279 {xterms, ARRAY_LENGTH(xterms)},
1280 {hand1s, ARRAY_LENGTH(hand1s)},
1281 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001282};
1283
1284static void
1285create_cursors(struct display *display)
1286{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001287 struct weston_config *config;
1288 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001289 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001290 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001291 unsigned int i, j;
1292 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001293
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001294 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001295 s = weston_config_get_section(config, "shell", NULL, NULL);
1296 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1297 weston_config_section_get_int(s, "cursor-size", &size, 32);
1298 weston_config_destroy(config);
1299
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001300 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001301 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001302 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001303 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001304
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001305 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001306 cursor = NULL;
1307 for (j = 0; !cursor && j < cursors[i].count; ++j)
1308 cursor = wl_cursor_theme_get_cursor(
1309 display->cursor_theme, cursors[i].names[j]);
1310
1311 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001312 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001313 cursors[i].names[0]);
1314
1315 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001316 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001317}
1318
1319static void
1320destroy_cursors(struct display *display)
1321{
1322 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001323 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001324}
1325
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001326struct wl_cursor_image *
1327display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001328{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001329 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001330
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001331 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001332}
1333
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001334static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001335surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001336{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001337 if (!surface->cairo_surface)
1338 return;
1339
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001340 if (surface->opaque_region) {
1341 wl_surface_set_opaque_region(surface->surface,
1342 surface->opaque_region);
1343 wl_region_destroy(surface->opaque_region);
1344 surface->opaque_region = NULL;
1345 }
1346
1347 if (surface->input_region) {
1348 wl_surface_set_input_region(surface->surface,
1349 surface->input_region);
1350 wl_region_destroy(surface->input_region);
1351 surface->input_region = NULL;
1352 }
1353
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001354 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001355 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001356 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001357
Pekka Paalanen89dee002013-02-13 16:17:20 +02001358 cairo_surface_destroy(surface->cairo_surface);
1359 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001360}
1361
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001362int
1363window_has_focus(struct window *window)
1364{
1365 return window->focus_count > 0;
1366}
1367
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001368static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001369window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001370{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001371 struct surface *surface;
1372
Pekka Paalanen89dee002013-02-13 16:17:20 +02001373 if (window->type == TYPE_NONE) {
1374 window->type = TYPE_TOPLEVEL;
1375 if (window->shell_surface)
1376 wl_shell_surface_set_toplevel(window->shell_surface);
1377 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001378
Pekka Paalanen35e82632013-04-25 13:57:48 +03001379 wl_list_for_each(surface, &window->subsurface_list, link) {
1380 if (surface == window->main_surface)
1381 continue;
1382
1383 surface_flush(surface);
1384 }
1385
Pekka Paalanen89dee002013-02-13 16:17:20 +02001386 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001387}
1388
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001389struct display *
1390window_get_display(struct window *window)
1391{
1392 return window->display;
1393}
1394
Pekka Paalanen89dee002013-02-13 16:17:20 +02001395static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001396surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001397{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001398 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001399 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001400
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001401 if (!surface->toysurface && display->dpy &&
1402 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001403 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001404 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001405 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001406 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001407 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001408 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001409
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001410 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001411 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001412 surface->surface,
1413 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001414
Pekka Paalanen89dee002013-02-13 16:17:20 +02001415 surface->cairo_surface = surface->toysurface->prepare(
1416 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001417 allocation.width, allocation.height, flags,
1418 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001419}
1420
1421static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001422window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001423{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001424 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001425 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001426 int dx = 0;
1427 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001428
Pekka Paalanenec076692012-11-30 13:37:27 +02001429 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001430 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001431
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001432 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1433 flags |= SURFACE_HINT_RGB565;
1434
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001435 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1436 dx = surface->server_allocation.width -
1437 surface->allocation.width;
1438
1439 if (window->resize_edges & WINDOW_RESIZING_TOP)
1440 dy = surface->server_allocation.height -
1441 surface->allocation.height;
1442
1443 window->resize_edges = 0;
1444
Pekka Paalanen89dee002013-02-13 16:17:20 +02001445 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001446}
1447
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001448int
1449window_get_buffer_transform(struct window *window)
1450{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001451 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001452}
1453
1454void
1455window_set_buffer_transform(struct window *window,
1456 enum wl_output_transform transform)
1457{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001458 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001459 wl_surface_set_buffer_transform(window->main_surface->surface,
1460 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001461}
1462
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001463void
1464window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001465 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001466{
1467 window->main_surface->buffer_scale = scale;
1468 wl_surface_set_buffer_scale(window->main_surface->surface,
1469 scale);
1470}
1471
1472uint32_t
1473window_get_buffer_scale(struct window *window)
1474{
1475 return window->main_surface->buffer_scale;
1476}
1477
Alexander Larssond68f5232013-05-22 14:41:33 +02001478uint32_t
1479window_get_output_scale(struct window *window)
1480{
1481 struct window_output *window_output;
1482 struct window_output *window_output_tmp;
1483 int scale = 1;
1484
1485 wl_list_for_each_safe(window_output, window_output_tmp,
1486 &window->window_output_list, link) {
1487 if (window_output->output->scale > scale)
1488 scale = window_output->output->scale;
1489 }
1490
1491 return scale;
1492}
1493
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001494static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001495
Pekka Paalanen4e373742013-02-13 16:17:13 +02001496static void
1497surface_destroy(struct surface *surface)
1498{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001499 if (surface->frame_cb)
1500 wl_callback_destroy(surface->frame_cb);
1501
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001502 if (surface->input_region)
1503 wl_region_destroy(surface->input_region);
1504
1505 if (surface->opaque_region)
1506 wl_region_destroy(surface->opaque_region);
1507
Pekka Paalanen35e82632013-04-25 13:57:48 +03001508 if (surface->subsurface)
1509 wl_subsurface_destroy(surface->subsurface);
1510
Pekka Paalanen4e373742013-02-13 16:17:13 +02001511 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001512
1513 if (surface->toysurface)
1514 surface->toysurface->destroy(surface->toysurface);
1515
Pekka Paalanen35e82632013-04-25 13:57:48 +03001516 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001517 free(surface);
1518}
1519
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001520void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001521window_destroy(struct window *window)
1522{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001523 struct display *display = window->display;
1524 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001525 struct window_output *window_output;
1526 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001527
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001528 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001529
Rusty Lynch1084da52013-08-15 09:10:08 -07001530 wl_list_for_each(input, &display->input_list, link) {
1531 if (input->touch_focus == window)
1532 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001533 if (input->pointer_focus == window)
1534 input->pointer_focus = NULL;
1535 if (input->keyboard_focus == window)
1536 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001537 if (input->focus_widget &&
1538 input->focus_widget->window == window)
1539 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001540 }
1541
Rob Bradford7507b572012-05-15 17:55:34 +01001542 wl_list_for_each_safe(window_output, window_output_tmp,
1543 &window->window_output_list, link) {
1544 free (window_output);
1545 }
1546
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001547 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001548 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001549
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001550 if (window->shell_surface)
1551 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001552
1553 surface_destroy(window->main_surface);
1554
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001555 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001556
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001557 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001558 free(window);
1559}
1560
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001561static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001562widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001563{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001564 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001565
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001566 wl_list_for_each(child, &widget->child_list, link) {
1567 target = widget_find_widget(child, x, y);
1568 if (target)
1569 return target;
1570 }
1571
1572 if (widget->allocation.x <= x &&
1573 x < widget->allocation.x + widget->allocation.width &&
1574 widget->allocation.y <= y &&
1575 y < widget->allocation.y + widget->allocation.height) {
1576 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001577 }
1578
1579 return NULL;
1580}
1581
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001582static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001583window_find_widget(struct window *window, int32_t x, int32_t y)
1584{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001585 struct surface *surface;
1586 struct widget *widget;
1587
1588 wl_list_for_each(surface, &window->subsurface_list, link) {
1589 widget = widget_find_widget(surface->widget, x, y);
1590 if (widget)
1591 return widget;
1592 }
1593
1594 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001595}
1596
1597static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001598widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001599{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001600 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001601
Peter Huttererf3d62272013-08-08 11:57:05 +10001602 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001603 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001604 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001605 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001606 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001607 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001608 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001609 widget->tooltip = NULL;
1610 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001611 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612
1613 return widget;
1614}
1615
1616struct widget *
1617window_add_widget(struct window *window, void *data)
1618{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001619 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001620
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001621 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001622 wl_list_init(&widget->link);
1623 window->main_surface->widget = widget;
1624
1625 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001626}
1627
1628struct widget *
1629widget_add_widget(struct widget *parent, void *data)
1630{
1631 struct widget *widget;
1632
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001633 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001634 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001635
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001636 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001637}
1638
1639void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001640widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001641{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001642 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001643 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001644 struct input *input;
1645
Pekka Paalanen35e82632013-04-25 13:57:48 +03001646 /* Destroy the sub-surface along with the root widget */
1647 if (surface->widget == widget && surface->subsurface)
1648 surface_destroy(widget->surface);
1649
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001650 if (widget->tooltip) {
1651 free(widget->tooltip);
1652 widget->tooltip = NULL;
1653 }
1654
Pekka Paalanene156fb62012-01-19 13:51:38 +02001655 wl_list_for_each(input, &display->input_list, link) {
1656 if (input->focus_widget == widget)
1657 input->focus_widget = NULL;
1658 }
1659
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001660 wl_list_remove(&widget->link);
1661 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001662}
1663
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001665widget_set_default_cursor(struct widget *widget, int cursor)
1666{
1667 widget->default_cursor = cursor;
1668}
1669
1670void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001671widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001672{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001673 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001674}
1675
1676void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001677widget_set_size(struct widget *widget, int32_t width, int32_t height)
1678{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001679 widget->allocation.width = width;
1680 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001681}
1682
1683void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001684widget_set_allocation(struct widget *widget,
1685 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001687 widget->allocation.x = x;
1688 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001689 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690}
1691
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001692void
1693widget_set_transparent(struct widget *widget, int transparent)
1694{
1695 widget->opaque = !transparent;
1696}
1697
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001698void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001699widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001700{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001701 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001702}
1703
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001704static cairo_surface_t *
1705widget_get_cairo_surface(struct widget *widget)
1706{
1707 struct surface *surface = widget->surface;
1708 struct window *window = widget->window;
1709
1710 if (!surface->cairo_surface) {
1711 if (surface == window->main_surface)
1712 window_create_main_surface(window);
1713 else
1714 surface_create_surface(surface, 0, 0, 0);
1715 }
1716
1717 return surface->cairo_surface;
1718}
1719
Alexander Larsson15901f02013-05-22 14:41:25 +02001720static void
1721widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1722{
1723 struct surface *surface = widget->surface;
1724 double angle;
1725 cairo_matrix_t m;
1726 enum wl_output_transform transform;
1727 int surface_width, surface_height;
1728 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001729 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001730
1731 surface_width = surface->allocation.width;
1732 surface_height = surface->allocation.height;
1733
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001734 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001735 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001736
Alexander Larsson15901f02013-05-22 14:41:25 +02001737 switch (transform) {
1738 case WL_OUTPUT_TRANSFORM_FLIPPED:
1739 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1740 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1741 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1742 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1743 break;
1744 default:
1745 cairo_matrix_init_identity(&m);
1746 break;
1747 }
1748
1749 switch (transform) {
1750 case WL_OUTPUT_TRANSFORM_NORMAL:
1751 default:
1752 angle = 0;
1753 translate_x = 0;
1754 translate_y = 0;
1755 break;
1756 case WL_OUTPUT_TRANSFORM_FLIPPED:
1757 angle = 0;
1758 translate_x = surface_width;
1759 translate_y = 0;
1760 break;
1761 case WL_OUTPUT_TRANSFORM_90:
1762 angle = M_PI_2;
1763 translate_x = surface_height;
1764 translate_y = 0;
1765 break;
1766 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1767 angle = M_PI_2;
1768 translate_x = surface_height;
1769 translate_y = surface_width;
1770 break;
1771 case WL_OUTPUT_TRANSFORM_180:
1772 angle = M_PI;
1773 translate_x = surface_width;
1774 translate_y = surface_height;
1775 break;
1776 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1777 angle = M_PI;
1778 translate_x = 0;
1779 translate_y = surface_height;
1780 break;
1781 case WL_OUTPUT_TRANSFORM_270:
1782 angle = M_PI + M_PI_2;
1783 translate_x = 0;
1784 translate_y = surface_width;
1785 break;
1786 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1787 angle = M_PI + M_PI_2;
1788 translate_x = 0;
1789 translate_y = 0;
1790 break;
1791 }
1792
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001793 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001794 cairo_translate(cr, translate_x, translate_y);
1795 cairo_rotate(cr, angle);
1796 cairo_transform(cr, &m);
1797}
1798
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001799cairo_t *
1800widget_cairo_create(struct widget *widget)
1801{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001802 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001803 cairo_surface_t *cairo_surface;
1804 cairo_t *cr;
1805
1806 cairo_surface = widget_get_cairo_surface(widget);
1807 cr = cairo_create(cairo_surface);
1808
Alexander Larsson15901f02013-05-22 14:41:25 +02001809 widget_cairo_update_transform(widget, cr);
1810
Pekka Paalanen35e82632013-04-25 13:57:48 +03001811 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1812
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001813 return cr;
1814}
1815
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001816struct wl_surface *
1817widget_get_wl_surface(struct widget *widget)
1818{
1819 return widget->surface->surface;
1820}
1821
Neil Roberts5e10a042013-09-09 00:40:17 +01001822struct wl_subsurface *
1823widget_get_wl_subsurface(struct widget *widget)
1824{
1825 return widget->surface->subsurface;
1826}
1827
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001828uint32_t
1829widget_get_last_time(struct widget *widget)
1830{
1831 return widget->surface->last_time;
1832}
1833
1834void
1835widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1836{
1837 struct wl_compositor *comp = widget->window->display->compositor;
1838 struct surface *surface = widget->surface;
1839
1840 if (!surface->input_region)
1841 surface->input_region = wl_compositor_create_region(comp);
1842
1843 if (rect) {
1844 wl_region_add(surface->input_region,
1845 rect->x, rect->y, rect->width, rect->height);
1846 }
1847}
1848
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001849void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001850widget_set_resize_handler(struct widget *widget,
1851 widget_resize_handler_t handler)
1852{
1853 widget->resize_handler = handler;
1854}
1855
1856void
1857widget_set_redraw_handler(struct widget *widget,
1858 widget_redraw_handler_t handler)
1859{
1860 widget->redraw_handler = handler;
1861}
1862
1863void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001864widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001865{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001866 widget->enter_handler = handler;
1867}
1868
1869void
1870widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1871{
1872 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001873}
1874
1875void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001876widget_set_motion_handler(struct widget *widget,
1877 widget_motion_handler_t handler)
1878{
1879 widget->motion_handler = handler;
1880}
1881
1882void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001883widget_set_button_handler(struct widget *widget,
1884 widget_button_handler_t handler)
1885{
1886 widget->button_handler = handler;
1887}
1888
1889void
Rusty Lynch041815a2013-08-08 21:20:38 -07001890widget_set_touch_up_handler(struct widget *widget,
1891 widget_touch_up_handler_t handler)
1892{
1893 widget->touch_up_handler = handler;
1894}
1895
1896void
1897widget_set_touch_down_handler(struct widget *widget,
1898 widget_touch_down_handler_t handler)
1899{
1900 widget->touch_down_handler = handler;
1901}
1902
1903void
1904widget_set_touch_motion_handler(struct widget *widget,
1905 widget_touch_motion_handler_t handler)
1906{
1907 widget->touch_motion_handler = handler;
1908}
1909
1910void
1911widget_set_touch_frame_handler(struct widget *widget,
1912 widget_touch_frame_handler_t handler)
1913{
1914 widget->touch_frame_handler = handler;
1915}
1916
1917void
1918widget_set_touch_cancel_handler(struct widget *widget,
1919 widget_touch_cancel_handler_t handler)
1920{
1921 widget->touch_cancel_handler = handler;
1922}
1923
1924void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001925widget_set_axis_handler(struct widget *widget,
1926 widget_axis_handler_t handler)
1927{
1928 widget->axis_handler = handler;
1929}
1930
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001931static void
1932window_schedule_redraw_task(struct window *window);
1933
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001934void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001935widget_schedule_redraw(struct widget *widget)
1936{
Pekka Paalanen71233882013-04-25 13:57:53 +03001937 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001938 widget->surface->redraw_needed = 1;
1939 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001940}
1941
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001942cairo_surface_t *
1943window_get_surface(struct window *window)
1944{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001945 cairo_surface_t *cairo_surface;
1946
1947 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1948
1949 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001950}
1951
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001952struct wl_surface *
1953window_get_wl_surface(struct window *window)
1954{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001955 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001956}
1957
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001958static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001959tooltip_redraw_handler(struct widget *widget, void *data)
1960{
1961 cairo_t *cr;
1962 const int32_t r = 3;
1963 struct tooltip *tooltip = data;
1964 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001965
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001966 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001967 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1968 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1969 cairo_paint(cr);
1970
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001971 width = widget->allocation.width;
1972 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001973 rounded_rect(cr, 0, 0, width, height, r);
1974
1975 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1976 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1977 cairo_fill(cr);
1978
1979 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1980 cairo_move_to(cr, 10, 16);
1981 cairo_show_text(cr, tooltip->entry);
1982 cairo_destroy(cr);
1983}
1984
1985static cairo_text_extents_t
1986get_text_extents(struct tooltip *tooltip)
1987{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001988 cairo_t *cr;
1989 cairo_text_extents_t extents;
1990
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001991 /* Use the dummy_surface because tooltip's surface was not
1992 * created yet, and parent does not have a valid surface
1993 * outside repaint, either.
1994 */
1995 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001996 cairo_text_extents(cr, tooltip->entry, &extents);
1997 cairo_destroy(cr);
1998
1999 return extents;
2000}
2001
2002static int
2003window_create_tooltip(struct tooltip *tooltip)
2004{
2005 struct widget *parent = tooltip->parent;
2006 struct display *display = parent->window->display;
2007 struct window *window;
2008 const int offset_y = 27;
2009 const int margin = 3;
2010 cairo_text_extents_t extents;
2011
2012 if (tooltip->widget)
2013 return 0;
2014
2015 window = window_create_transient(display, parent->window, tooltip->x,
2016 tooltip->y + offset_y,
2017 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2018 if (!window)
2019 return -1;
2020
2021 tooltip->window = window;
2022 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2023
2024 extents = get_text_extents(tooltip);
2025 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2026 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2027
2028 return 0;
2029}
2030
2031void
2032widget_destroy_tooltip(struct widget *parent)
2033{
2034 struct tooltip *tooltip = parent->tooltip;
2035
2036 parent->tooltip_count = 0;
2037 if (!tooltip)
2038 return;
2039
2040 if (tooltip->widget) {
2041 widget_destroy(tooltip->widget);
2042 window_destroy(tooltip->window);
2043 tooltip->widget = NULL;
2044 tooltip->window = NULL;
2045 }
2046
2047 close(tooltip->tooltip_fd);
2048 free(tooltip->entry);
2049 free(tooltip);
2050 parent->tooltip = NULL;
2051}
2052
2053static void
2054tooltip_func(struct task *task, uint32_t events)
2055{
2056 struct tooltip *tooltip =
2057 container_of(task, struct tooltip, tooltip_task);
2058 uint64_t exp;
2059
Martin Olsson8df662a2012-07-08 03:03:47 +02002060 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2061 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002062 window_create_tooltip(tooltip);
2063}
2064
2065#define TOOLTIP_TIMEOUT 500
2066static int
2067tooltip_timer_reset(struct tooltip *tooltip)
2068{
2069 struct itimerspec its;
2070
2071 its.it_interval.tv_sec = 0;
2072 its.it_interval.tv_nsec = 0;
2073 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2074 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2075 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2076 fprintf(stderr, "could not set timerfd\n: %m");
2077 return -1;
2078 }
2079
2080 return 0;
2081}
2082
2083int
2084widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2085{
2086 struct tooltip *tooltip = parent->tooltip;
2087
2088 parent->tooltip_count++;
2089 if (tooltip) {
2090 tooltip->x = x;
2091 tooltip->y = y;
2092 tooltip_timer_reset(tooltip);
2093 return 0;
2094 }
2095
2096 /* the handler might be triggered too fast via input device motion, so
2097 * we need this check here to make sure tooltip is fully initialized */
2098 if (parent->tooltip_count > 1)
2099 return 0;
2100
2101 tooltip = malloc(sizeof *tooltip);
2102 if (!tooltip)
2103 return -1;
2104
2105 parent->tooltip = tooltip;
2106 tooltip->parent = parent;
2107 tooltip->widget = NULL;
2108 tooltip->window = NULL;
2109 tooltip->x = x;
2110 tooltip->y = y;
2111 tooltip->entry = strdup(entry);
2112 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2113 if (tooltip->tooltip_fd < 0) {
2114 fprintf(stderr, "could not create timerfd\n: %m");
2115 return -1;
2116 }
2117
2118 tooltip->tooltip_task.run = tooltip_func;
2119 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2120 EPOLLIN, &tooltip->tooltip_task);
2121 tooltip_timer_reset(tooltip);
2122
2123 return 0;
2124}
2125
2126static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002127workspace_manager_state(void *data,
2128 struct workspace_manager *workspace_manager,
2129 uint32_t current,
2130 uint32_t count)
2131{
2132 struct display *display = data;
2133
2134 display->workspace = current;
2135 display->workspace_count = count;
2136}
2137
2138static const struct workspace_manager_listener workspace_manager_listener = {
2139 workspace_manager_state
2140};
2141
2142static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002143frame_resize_handler(struct widget *widget,
2144 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002145{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002146 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002147 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002148 struct rectangle interior;
2149 struct rectangle input;
2150 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002151
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002152 if (widget->window->type == TYPE_FULLSCREEN) {
2153 interior.x = 0;
2154 interior.y = 0;
2155 interior.width = width;
2156 interior.height = height;
2157 } else {
2158 if (widget->window->type == TYPE_MAXIMIZED) {
2159 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2160 } else {
2161 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2162 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002163
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002164 frame_resize(frame->frame, width, height);
2165 frame_interior(frame->frame, &interior.x, &interior.y,
2166 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002167 }
2168
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002169 widget_set_allocation(child, interior.x, interior.y,
2170 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002171
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 if (child->resize_handler) {
2173 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002174 child->user_data);
2175
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002176 if (widget->window->type == TYPE_FULLSCREEN) {
2177 width = child->allocation.width;
2178 height = child->allocation.height;
2179 } else {
2180 frame_resize_inside(frame->frame,
2181 child->allocation.width,
2182 child->allocation.height);
2183 width = frame_width(frame->frame);
2184 height = frame_height(frame->frame);
2185 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002186 }
2187
Scott Moreauf7e498c2012-05-14 11:39:29 -06002188 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002189
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002190 widget->surface->input_region =
2191 wl_compositor_create_region(widget->window->display->compositor);
2192 if (widget->window->type != TYPE_FULLSCREEN) {
2193 frame_input_rect(frame->frame, &input.x, &input.y,
2194 &input.width, &input.height);
2195 wl_region_add(widget->surface->input_region,
2196 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002197 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002198 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002199 }
2200
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002201 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002202
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002203 if (child->opaque) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002204 if (widget->window->type != TYPE_FULLSCREEN) {
2205 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2206 &opaque.width, &opaque.height);
2207
2208 wl_region_add(widget->surface->opaque_region,
2209 opaque.x, opaque.y,
2210 opaque.width, opaque.height);
2211 } else {
2212 wl_region_add(widget->surface->opaque_region,
2213 0, 0, width, height);
2214 }
Martin Minarik1998b152012-05-10 02:04:35 +02002215 }
2216
Martin Minarik1998b152012-05-10 02:04:35 +02002217
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219}
2220
2221static void
2222frame_redraw_handler(struct widget *widget, void *data)
2223{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002225 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226 struct window *window = widget->window;
2227
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002228 if (window->type == TYPE_FULLSCREEN)
2229 return;
2230
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002231 if (window->focus_count) {
2232 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2233 } else {
2234 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2235 }
2236
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002237 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002238
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002239 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002240
2241 cairo_destroy(cr);
2242}
2243
2244static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002245frame_get_pointer_image_for_location(struct window_frame *frame,
2246 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002247{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002248 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002249
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002250 if (window->type != TYPE_TOPLEVEL)
2251 return CURSOR_LEFT_PTR;
2252
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002253 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002254 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002255 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002256 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002257 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002258 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002259 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002260 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002261 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002262 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002263 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002264 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002265 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002266 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002267 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002268 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002269 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002270 case THEME_LOCATION_EXTERIOR:
2271 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002272 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002273 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002274 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002275}
2276
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002277static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002278frame_menu_func(struct window *window,
2279 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002280{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002281 struct display *display;
2282
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002283 switch (index) {
2284 case 0: /* close */
2285 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002286 window->close_handler(window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002287 else
2288 display_exit(window->display);
2289 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002290 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002291 display = window->display;
2292 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002293 workspace_manager_move_surface(
2294 display->workspace_manager,
2295 window->main_surface->surface,
2296 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002297 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002298 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002299 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002300 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002301 workspace_manager_move_surface(
2302 display->workspace_manager,
2303 window->main_surface->surface,
2304 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002305 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002306 case 3: /* fullscreen */
2307 /* we don't have a way to get out of fullscreen for now */
2308 if (window->fullscreen_handler)
2309 window->fullscreen_handler(window, window->user_data);
2310 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002311 }
2312}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002313
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002314void
2315window_show_frame_menu(struct window *window,
2316 struct input *input, uint32_t time)
2317{
2318 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002319 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002320
2321 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002322 "Close",
2323 "Move to workspace above", "Move to workspace below",
2324 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002325 };
2326
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002327 if (window->fullscreen_handler)
2328 count = ARRAY_LENGTH(entries);
2329 else
2330 count = ARRAY_LENGTH(entries) - 1;
2331
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002332 input_get_position(input, &x, &y);
2333 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002334 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002335}
2336
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002337static int
2338frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002339 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002340{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002341 struct window_frame *frame = data;
2342 enum theme_location location;
2343
2344 location = frame_pointer_enter(frame->frame, input, x, y);
2345 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2346 widget_schedule_redraw(frame->widget);
2347
2348 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002349}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002350
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002351static int
2352frame_motion_handler(struct widget *widget,
2353 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002354 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002355{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002356 struct window_frame *frame = data;
2357 enum theme_location location;
2358
2359 location = frame_pointer_motion(frame->frame, input, x, y);
2360 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2361 widget_schedule_redraw(frame->widget);
2362
2363 return frame_get_pointer_image_for_location(data, location);
2364}
2365
2366static void
2367frame_leave_handler(struct widget *widget,
2368 struct input *input, void *data)
2369{
2370 struct window_frame *frame = data;
2371
2372 frame_pointer_leave(frame->frame, input);
2373 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2374 widget_schedule_redraw(frame->widget);
2375}
2376
2377static void
2378frame_handle_status(struct window_frame *frame, struct input *input,
2379 uint32_t time, enum theme_location location)
2380{
2381 struct window *window = frame->widget->window;
2382 uint32_t status;
2383
2384 status = frame_status(frame->frame);
2385 if (status & FRAME_STATUS_REPAINT)
2386 widget_schedule_redraw(frame->widget);
2387
2388 if (status & FRAME_STATUS_MINIMIZE)
2389 fprintf(stderr,"Minimize stub\n");
2390
2391 if (status & FRAME_STATUS_MENU) {
2392 window_show_frame_menu(window, input, time);
2393 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2394 }
2395
2396 if (status & FRAME_STATUS_MAXIMIZE) {
2397 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2398 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2399 }
2400
2401 if (status & FRAME_STATUS_CLOSE) {
2402 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002403 window->close_handler(window->user_data);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002404 else
2405 display_exit(window->display);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002406 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002407 }
2408
2409 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2410 input_ungrab(input);
2411 wl_shell_surface_move(window->shell_surface,
2412 input_get_seat(input),
2413 window->display->serial);
2414
2415 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2416 }
2417
2418 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2419 input_ungrab(input);
2420
2421 window->resizing = 1;
2422 wl_shell_surface_resize(window->shell_surface,
2423 input_get_seat(input),
2424 window->display->serial,
2425 location);
2426
2427 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2428 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002429}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002430
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002431static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002432frame_button_handler(struct widget *widget,
2433 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002434 uint32_t button, enum wl_pointer_button_state state,
2435 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002436
2437{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002438 struct window_frame *frame = data;
2439 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002440
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002441 location = frame_pointer_button(frame->frame, input, button, state);
2442 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002443}
2444
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002445static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002446frame_touch_down_handler(struct widget *widget, struct input *input,
2447 uint32_t serial, uint32_t time, int32_t id,
2448 float x, float y, void *data)
2449{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002450 struct window_frame *frame = data;
2451
2452 frame_touch_down(frame->frame, input, id, x, y);
2453 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2454}
2455
2456static void
2457frame_touch_up_handler(struct widget *widget,
2458 struct input *input, uint32_t serial, uint32_t time,
2459 int32_t id, void *data)
2460{
2461 struct window_frame *frame = data;
2462
2463 frame_touch_up(frame->frame, input, id);
2464 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002465}
2466
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002467struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002468window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002469{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002470 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002471 uint32_t buttons;
2472
2473 if (window->type == TYPE_CUSTOM) {
2474 buttons = FRAME_BUTTON_NONE;
2475 } else {
2476 buttons = FRAME_BUTTON_ALL;
2477 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002478
Peter Huttererf3d62272013-08-08 11:57:05 +10002479 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002480 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002481 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002482
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002483 frame->widget = window_add_widget(window, frame);
2484 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002485
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002486 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2487 widget_set_resize_handler(frame->widget, frame_resize_handler);
2488 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002489 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002490 widget_set_motion_handler(frame->widget, frame_motion_handler);
2491 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002492 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002493 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002494
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002495 window->frame = frame;
2496
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002497 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002498}
2499
Kristian Høgsberga1627922012-06-20 17:30:03 -04002500void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002501window_frame_set_child_size(struct widget *widget, int child_width,
2502 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002503{
2504 struct display *display = widget->window->display;
2505 struct theme *t = display->theme;
2506 int decoration_width, decoration_height;
2507 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002508 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002509
2510 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002511 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002512 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002513 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002514
2515 width = child_width + decoration_width;
2516 height = child_height + decoration_height;
2517 } else {
2518 width = child_width;
2519 height = child_height;
2520 }
2521
2522 window_schedule_resize(widget->window, width, height);
2523}
2524
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002525static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002526window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002527{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002528 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002529
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002530 /* frame->child must be destroyed by the application */
2531 widget_destroy(frame->widget);
2532 free(frame);
2533}
2534
2535static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002536input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002537 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002538{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002539 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002540 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002541
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002542 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002543 return;
2544
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002545 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002546 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002547 widget = old;
2548 if (input->grab)
2549 widget = input->grab;
2550 if (widget->leave_handler)
2551 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002552 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002553 }
2554
2555 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002556 widget = focus;
2557 if (input->grab)
2558 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002559 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002560 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002561 cursor = widget->enter_handler(focus, input, x, y,
2562 widget->user_data);
2563 else
2564 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002565
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002566 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002567 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002568}
2569
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002570void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002571touch_grab(struct input *input, int32_t touch_id)
2572{
2573 input->touch_grab = 1;
2574 input->touch_grab_id = touch_id;
2575}
2576
2577void
2578touch_ungrab(struct input *input)
2579{
2580 struct touch_point *tp, *tmp;
2581
2582 input->touch_grab = 0;
2583
2584 wl_list_for_each_safe(tp, tmp,
2585 &input->touch_point_list, link) {
2586 if (tp->id != input->touch_grab_id)
2587 continue;
2588 wl_list_remove(&tp->link);
2589 free(tp);
2590
2591 return;
2592 }
2593}
2594
2595void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002596input_grab(struct input *input, struct widget *widget, uint32_t button)
2597{
2598 input->grab = widget;
2599 input->grab_button = button;
2600}
2601
2602void
2603input_ungrab(struct input *input)
2604{
2605 struct widget *widget;
2606
2607 input->grab = NULL;
2608 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002609 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002610 input->sx, input->sy);
2611 input_set_focus_widget(input, widget, input->sx, input->sy);
2612 }
2613}
2614
2615static void
2616input_remove_pointer_focus(struct input *input)
2617{
2618 struct window *window = input->pointer_focus;
2619
2620 if (!window)
2621 return;
2622
2623 input_set_focus_widget(input, NULL, 0, 0);
2624
2625 input->pointer_focus = NULL;
2626 input->current_cursor = CURSOR_UNSET;
2627}
2628
2629static void
2630pointer_handle_enter(void *data, struct wl_pointer *pointer,
2631 uint32_t serial, struct wl_surface *surface,
2632 wl_fixed_t sx_w, wl_fixed_t sy_w)
2633{
2634 struct input *input = data;
2635 struct window *window;
2636 struct widget *widget;
2637 float sx = wl_fixed_to_double(sx_w);
2638 float sy = wl_fixed_to_double(sy_w);
2639
2640 if (!surface) {
2641 /* enter event for a window we've just destroyed */
2642 return;
2643 }
2644
2645 input->display->serial = serial;
2646 input->pointer_enter_serial = serial;
2647 input->pointer_focus = wl_surface_get_user_data(surface);
2648 window = input->pointer_focus;
2649
Pekka Paalanen99436862012-11-19 17:15:59 +02002650 if (window->resizing) {
2651 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002652 /* Schedule a redraw to free the pool */
2653 window_schedule_redraw(window);
2654 }
2655
2656 input->sx = sx;
2657 input->sy = sy;
2658
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002659 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002660 input_set_focus_widget(input, widget, sx, sy);
2661}
2662
2663static void
2664pointer_handle_leave(void *data, struct wl_pointer *pointer,
2665 uint32_t serial, struct wl_surface *surface)
2666{
2667 struct input *input = data;
2668
2669 input->display->serial = serial;
2670 input_remove_pointer_focus(input);
2671}
2672
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002673static void
Daniel Stone37816df2012-05-16 18:45:18 +01002674pointer_handle_motion(void *data, struct wl_pointer *pointer,
2675 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002676{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002677 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002678 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002679 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002680 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002681 float sx = wl_fixed_to_double(sx_w);
2682 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002683
Paul Winwoodb22bf572013-08-29 10:52:54 +01002684 input->sx = sx;
2685 input->sy = sy;
2686
2687 if (!window)
2688 return;
2689
Rob Bradford5f087742013-07-11 19:41:27 +01002690 /* when making the window smaller - e.g. after a unmaximise we might
2691 * still have a pending motion event that the compositor has picked
2692 * based on the old surface dimensions
2693 */
2694 if (sx > window->main_surface->allocation.width ||
2695 sy > window->main_surface->allocation.height)
2696 return;
2697
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002698 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002699 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002700 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002701 }
2702
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002703 if (input->grab)
2704 widget = input->grab;
2705 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002706 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002707 if (widget) {
2708 if (widget->motion_handler)
2709 cursor = widget->motion_handler(input->focus_widget,
2710 input, time, sx, sy,
2711 widget->user_data);
2712 else
2713 cursor = widget->default_cursor;
2714 } else
2715 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002716
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002717 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002718}
2719
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002720static void
Daniel Stone37816df2012-05-16 18:45:18 +01002721pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002722 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002723{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002724 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002725 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002726 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002727
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002728 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002729 if (input->focus_widget && input->grab == NULL &&
2730 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002731 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002732
Neil Roberts6b28aad2012-01-23 19:11:18 +00002733 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002734 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002735 (*widget->button_handler)(widget,
2736 input, time,
2737 button, state,
2738 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002739
Daniel Stone4dbadb12012-05-30 16:31:51 +01002740 if (input->grab && input->grab_button == button &&
2741 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002742 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002743}
2744
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002745static void
Daniel Stone37816df2012-05-16 18:45:18 +01002746pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002747 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002748{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002749 struct input *input = data;
2750 struct widget *widget;
2751
2752 widget = input->focus_widget;
2753 if (input->grab)
2754 widget = input->grab;
2755 if (widget && widget->axis_handler)
2756 (*widget->axis_handler)(widget,
2757 input, time,
2758 axis, value,
2759 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002760}
2761
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002762static const struct wl_pointer_listener pointer_listener = {
2763 pointer_handle_enter,
2764 pointer_handle_leave,
2765 pointer_handle_motion,
2766 pointer_handle_button,
2767 pointer_handle_axis,
2768};
2769
2770static void
2771input_remove_keyboard_focus(struct input *input)
2772{
2773 struct window *window = input->keyboard_focus;
2774 struct itimerspec its;
2775
2776 its.it_interval.tv_sec = 0;
2777 its.it_interval.tv_nsec = 0;
2778 its.it_value.tv_sec = 0;
2779 its.it_value.tv_nsec = 0;
2780 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2781
2782 if (!window)
2783 return;
2784
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002785 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002786 if (window->keyboard_focus_handler)
2787 (*window->keyboard_focus_handler)(window, NULL,
2788 window->user_data);
2789
2790 input->keyboard_focus = NULL;
2791}
2792
2793static void
2794keyboard_repeat_func(struct task *task, uint32_t events)
2795{
2796 struct input *input =
2797 container_of(task, struct input, repeat_task);
2798 struct window *window = input->keyboard_focus;
2799 uint64_t exp;
2800
2801 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2802 /* If we change the timer between the fd becoming
2803 * readable and getting here, there'll be nothing to
2804 * read and we get EAGAIN. */
2805 return;
2806
2807 if (window && window->key_handler) {
2808 (*window->key_handler)(window, input, input->repeat_time,
2809 input->repeat_key, input->repeat_sym,
2810 WL_KEYBOARD_KEY_STATE_PRESSED,
2811 window->user_data);
2812 }
2813}
2814
2815static void
2816keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2817 uint32_t format, int fd, uint32_t size)
2818{
2819 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002820 struct xkb_keymap *keymap;
2821 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002822 char *map_str;
2823
2824 if (!data) {
2825 close(fd);
2826 return;
2827 }
2828
2829 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2830 close(fd);
2831 return;
2832 }
2833
2834 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2835 if (map_str == MAP_FAILED) {
2836 close(fd);
2837 return;
2838 }
2839
Rui Matos3eccb862013-10-10 19:44:22 +02002840 keymap = xkb_map_new_from_string(input->display->xkb_context,
2841 map_str,
2842 XKB_KEYMAP_FORMAT_TEXT_V1,
2843 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002844 munmap(map_str, size);
2845 close(fd);
2846
Rui Matos3eccb862013-10-10 19:44:22 +02002847 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002848 fprintf(stderr, "failed to compile keymap\n");
2849 return;
2850 }
2851
Rui Matos3eccb862013-10-10 19:44:22 +02002852 state = xkb_state_new(keymap);
2853 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002854 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002855 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002856 return;
2857 }
2858
Rui Matos3eccb862013-10-10 19:44:22 +02002859 xkb_keymap_unref(input->xkb.keymap);
2860 xkb_state_unref(input->xkb.state);
2861 input->xkb.keymap = keymap;
2862 input->xkb.state = state;
2863
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002864 input->xkb.control_mask =
2865 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2866 input->xkb.alt_mask =
2867 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2868 input->xkb.shift_mask =
2869 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2870}
2871
2872static void
2873keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2874 uint32_t serial, struct wl_surface *surface,
2875 struct wl_array *keys)
2876{
2877 struct input *input = data;
2878 struct window *window;
2879
2880 input->display->serial = serial;
2881 input->keyboard_focus = wl_surface_get_user_data(surface);
2882
2883 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002884 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002885 if (window->keyboard_focus_handler)
2886 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002887 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002888}
2889
2890static void
2891keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2892 uint32_t serial, struct wl_surface *surface)
2893{
2894 struct input *input = data;
2895
2896 input->display->serial = serial;
2897 input_remove_keyboard_focus(input);
2898}
2899
Scott Moreau210d0792012-03-22 10:47:01 -06002900static void
Daniel Stone37816df2012-05-16 18:45:18 +01002901keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002902 uint32_t serial, uint32_t time, uint32_t key,
2903 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002904{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002905 struct input *input = data;
2906 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002907 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002908 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002909 const xkb_keysym_t *syms;
2910 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002911 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002912
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002913 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002914 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002915 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002916 return;
2917
Daniel Stone97f68542012-05-30 16:32:01 +01002918 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002919
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002920 sym = XKB_KEY_NoSymbol;
2921 if (num_syms == 1)
2922 sym = syms[0];
2923
2924 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002925 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002926 window_set_maximized(window,
2927 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002928 } else if (sym == XKB_KEY_F11 &&
2929 window->fullscreen_handler &&
2930 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2931 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002932 } else if (sym == XKB_KEY_F4 &&
2933 input->modifiers == MOD_ALT_MASK &&
2934 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2935 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002936 window->close_handler(window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002937 else
2938 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002939 } else if (window->key_handler) {
2940 (*window->key_handler)(window, input, time, key,
2941 sym, state, window->user_data);
2942 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002943
2944 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2945 key == input->repeat_key) {
2946 its.it_interval.tv_sec = 0;
2947 its.it_interval.tv_nsec = 0;
2948 its.it_value.tv_sec = 0;
2949 its.it_value.tv_nsec = 0;
2950 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2951 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2952 input->repeat_sym = sym;
2953 input->repeat_key = key;
2954 input->repeat_time = time;
2955 its.it_interval.tv_sec = 0;
2956 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2957 its.it_value.tv_sec = 0;
2958 its.it_value.tv_nsec = 400 * 1000 * 1000;
2959 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2960 }
2961}
2962
2963static void
Daniel Stone351eb612012-05-31 15:27:47 -04002964keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2965 uint32_t serial, uint32_t mods_depressed,
2966 uint32_t mods_latched, uint32_t mods_locked,
2967 uint32_t group)
2968{
2969 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002970 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002971
Matt Ropere61561f2013-06-24 16:52:43 +01002972 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2973 if (!input->xkb.keymap)
2974 return;
2975
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002976 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2977 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002978 mask = xkb_state_serialize_mods(input->xkb.state,
2979 XKB_STATE_DEPRESSED |
2980 XKB_STATE_LATCHED);
2981 input->modifiers = 0;
2982 if (mask & input->xkb.control_mask)
2983 input->modifiers |= MOD_CONTROL_MASK;
2984 if (mask & input->xkb.alt_mask)
2985 input->modifiers |= MOD_ALT_MASK;
2986 if (mask & input->xkb.shift_mask)
2987 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002988}
2989
Daniel Stone37816df2012-05-16 18:45:18 +01002990static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002991 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002992 keyboard_handle_enter,
2993 keyboard_handle_leave,
2994 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002995 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002996};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002997
2998static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002999touch_handle_down(void *data, struct wl_touch *wl_touch,
3000 uint32_t serial, uint32_t time, struct wl_surface *surface,
3001 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3002{
3003 struct input *input = data;
3004 struct widget *widget;
3005 float sx = wl_fixed_to_double(x_w);
3006 float sy = wl_fixed_to_double(y_w);
3007
Rusty Lynch1084da52013-08-15 09:10:08 -07003008 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003009 input->touch_focus = wl_surface_get_user_data(surface);
3010 if (!input->touch_focus) {
3011 DBG("Failed to find to touch focus for surface %p\n", surface);
3012 return;
3013 }
3014
3015 widget = window_find_widget(input->touch_focus,
3016 wl_fixed_to_double(x_w),
3017 wl_fixed_to_double(y_w));
3018 if (widget) {
3019 struct touch_point *tp = xmalloc(sizeof *tp);
3020 if (tp) {
3021 tp->id = id;
3022 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003023 tp->x = sx;
3024 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003025 wl_list_insert(&input->touch_point_list, &tp->link);
3026
3027 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003028 (*widget->touch_down_handler)(widget, input,
3029 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003030 sx, sy,
3031 widget->user_data);
3032 }
3033 }
3034}
3035
3036static void
3037touch_handle_up(void *data, struct wl_touch *wl_touch,
3038 uint32_t serial, uint32_t time, int32_t id)
3039{
3040 struct input *input = data;
3041 struct touch_point *tp, *tmp;
3042
Rusty Lynch041815a2013-08-08 21:20:38 -07003043 if (!input->touch_focus) {
3044 DBG("No touch focus found for touch up event!\n");
3045 return;
3046 }
3047
3048 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3049 if (tp->id != id)
3050 continue;
3051
3052 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003053 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003054 time, id,
3055 tp->widget->user_data);
3056
3057 wl_list_remove(&tp->link);
3058 free(tp);
3059
3060 return;
3061 }
3062}
3063
3064static void
3065touch_handle_motion(void *data, struct wl_touch *wl_touch,
3066 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3067{
3068 struct input *input = data;
3069 struct touch_point *tp;
3070 float sx = wl_fixed_to_double(x_w);
3071 float sy = wl_fixed_to_double(y_w);
3072
3073 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3074
3075 if (!input->touch_focus) {
3076 DBG("No touch focus found for touch motion event!\n");
3077 return;
3078 }
3079
3080 wl_list_for_each(tp, &input->touch_point_list, link) {
3081 if (tp->id != id)
3082 continue;
3083
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003084 tp->x = sx;
3085 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003086 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003087 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003088 id, sx, sy,
3089 tp->widget->user_data);
3090 return;
3091 }
3092}
3093
3094static void
3095touch_handle_frame(void *data, struct wl_touch *wl_touch)
3096{
3097 struct input *input = data;
3098 struct touch_point *tp, *tmp;
3099
3100 DBG("touch_handle_frame\n");
3101
3102 if (!input->touch_focus) {
3103 DBG("No touch focus found for touch frame event!\n");
3104 return;
3105 }
3106
3107 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3108 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003109 (*tp->widget->touch_frame_handler)(tp->widget, input,
3110 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003111
3112 wl_list_remove(&tp->link);
3113 free(tp);
3114 }
3115}
3116
3117static void
3118touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3119{
3120 struct input *input = data;
3121 struct touch_point *tp, *tmp;
3122
3123 DBG("touch_handle_cancel\n");
3124
3125 if (!input->touch_focus) {
3126 DBG("No touch focus found for touch cancel event!\n");
3127 return;
3128 }
3129
3130 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3131 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003132 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3133 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003134
3135 wl_list_remove(&tp->link);
3136 free(tp);
3137 }
3138}
3139
3140static const struct wl_touch_listener touch_listener = {
3141 touch_handle_down,
3142 touch_handle_up,
3143 touch_handle_motion,
3144 touch_handle_frame,
3145 touch_handle_cancel,
3146};
3147
3148static void
Daniel Stone37816df2012-05-16 18:45:18 +01003149seat_handle_capabilities(void *data, struct wl_seat *seat,
3150 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003151{
Daniel Stone37816df2012-05-16 18:45:18 +01003152 struct input *input = data;
3153
3154 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3155 input->pointer = wl_seat_get_pointer(seat);
3156 wl_pointer_set_user_data(input->pointer, input);
3157 wl_pointer_add_listener(input->pointer, &pointer_listener,
3158 input);
3159 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3160 wl_pointer_destroy(input->pointer);
3161 input->pointer = NULL;
3162 }
3163
3164 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3165 input->keyboard = wl_seat_get_keyboard(seat);
3166 wl_keyboard_set_user_data(input->keyboard, input);
3167 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3168 input);
3169 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3170 wl_keyboard_destroy(input->keyboard);
3171 input->keyboard = NULL;
3172 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003173
3174 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3175 input->touch = wl_seat_get_touch(seat);
3176 wl_touch_set_user_data(input->touch, input);
3177 wl_touch_add_listener(input->touch, &touch_listener, input);
3178 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3179 wl_touch_destroy(input->touch);
3180 input->touch = NULL;
3181 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003182}
3183
Rob Bradford08031182013-08-13 20:11:03 +01003184static void
3185seat_handle_name(void *data, struct wl_seat *seat,
3186 const char *name)
3187{
3188
3189}
3190
Daniel Stone37816df2012-05-16 18:45:18 +01003191static const struct wl_seat_listener seat_listener = {
3192 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003193 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003194};
3195
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003196void
3197input_get_position(struct input *input, int32_t *x, int32_t *y)
3198{
3199 *x = input->sx;
3200 *y = input->sy;
3201}
3202
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003203int
3204input_get_touch(struct input *input, int32_t id, float *x, float *y)
3205{
3206 struct touch_point *tp;
3207
3208 wl_list_for_each(tp, &input->touch_point_list, link) {
3209 if (tp->id != id)
3210 continue;
3211
3212 *x = tp->x;
3213 *y = tp->y;
3214 return 0;
3215 }
3216
3217 return -1;
3218}
3219
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003220struct display *
3221input_get_display(struct input *input)
3222{
3223 return input->display;
3224}
3225
Daniel Stone37816df2012-05-16 18:45:18 +01003226struct wl_seat *
3227input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003228{
Daniel Stone37816df2012-05-16 18:45:18 +01003229 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003230}
3231
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003232uint32_t
3233input_get_modifiers(struct input *input)
3234{
3235 return input->modifiers;
3236}
3237
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003238struct widget *
3239input_get_focus_widget(struct input *input)
3240{
3241 return input->focus_widget;
3242}
3243
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003244struct data_offer {
3245 struct wl_data_offer *offer;
3246 struct input *input;
3247 struct wl_array types;
3248 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003249
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003250 struct task io_task;
3251 int fd;
3252 data_func_t func;
3253 int32_t x, y;
3254 void *user_data;
3255};
3256
3257static void
3258data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3259{
3260 struct data_offer *offer = data;
3261 char **p;
3262
3263 p = wl_array_add(&offer->types, sizeof *p);
3264 *p = strdup(type);
3265}
3266
3267static const struct wl_data_offer_listener data_offer_listener = {
3268 data_offer_offer,
3269};
3270
3271static void
3272data_offer_destroy(struct data_offer *offer)
3273{
3274 char **p;
3275
3276 offer->refcount--;
3277 if (offer->refcount == 0) {
3278 wl_data_offer_destroy(offer->offer);
3279 for (p = offer->types.data; *p; p++)
3280 free(*p);
3281 wl_array_release(&offer->types);
3282 free(offer);
3283 }
3284}
3285
3286static void
3287data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003288 struct wl_data_device *data_device,
3289 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003290{
3291 struct data_offer *offer;
3292
Brian Lovinbc919262013-08-07 15:34:59 -07003293 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003294
3295 wl_array_init(&offer->types);
3296 offer->refcount = 1;
3297 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003298 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003299 wl_data_offer_add_listener(offer->offer,
3300 &data_offer_listener, offer);
3301}
3302
3303static void
3304data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003305 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003306 wl_fixed_t x_w, wl_fixed_t y_w,
3307 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003308{
3309 struct input *input = data;
3310 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003311 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003312 float x = wl_fixed_to_double(x_w);
3313 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003314 char **p;
3315
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003316 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003317 input->drag_enter_serial = serial;
3318 input->drag_focus = window,
3319 input->drag_x = x;
3320 input->drag_y = y;
3321
3322 if (!input->touch_grab)
3323 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003324
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003325 if (offer) {
3326 input->drag_offer = wl_data_offer_get_user_data(offer);
3327
3328 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3329 *p = NULL;
3330
3331 types_data = input->drag_offer->types.data;
3332 } else {
3333 input->drag_offer = NULL;
3334 types_data = NULL;
3335 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003336
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003337 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003338 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003339 window->user_data);
3340}
3341
3342static void
3343data_device_leave(void *data, struct wl_data_device *data_device)
3344{
3345 struct input *input = data;
3346
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003347 if (input->drag_offer) {
3348 data_offer_destroy(input->drag_offer);
3349 input->drag_offer = NULL;
3350 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003351}
3352
3353static void
3354data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003355 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003356{
3357 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003358 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003359 float x = wl_fixed_to_double(x_w);
3360 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003361 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003362
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003363 input->drag_x = x;
3364 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003365
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003366 if (input->drag_offer)
3367 types_data = input->drag_offer->types.data;
3368 else
3369 types_data = NULL;
3370
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003371 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003372 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003373 window->user_data);
3374}
3375
3376static void
3377data_device_drop(void *data, struct wl_data_device *data_device)
3378{
3379 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003380 struct window *window = input->drag_focus;
3381 float x, y;
3382
3383 x = input->drag_x;
3384 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003385
3386 if (window->drop_handler)
3387 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003388 x, y, window->user_data);
3389
3390 if (input->touch_grab)
3391 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003392}
3393
3394static void
3395data_device_selection(void *data,
3396 struct wl_data_device *wl_data_device,
3397 struct wl_data_offer *offer)
3398{
3399 struct input *input = data;
3400 char **p;
3401
3402 if (input->selection_offer)
3403 data_offer_destroy(input->selection_offer);
3404
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003405 if (offer) {
3406 input->selection_offer = wl_data_offer_get_user_data(offer);
3407 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3408 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003409 } else {
3410 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003411 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003412}
3413
3414static const struct wl_data_device_listener data_device_listener = {
3415 data_device_data_offer,
3416 data_device_enter,
3417 data_device_leave,
3418 data_device_motion,
3419 data_device_drop,
3420 data_device_selection
3421};
3422
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003423static void
3424input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003425{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003426 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003427 struct wl_cursor *cursor;
3428 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003429
Daniel Stone80972742012-11-07 17:51:39 +11003430 if (!input->pointer)
3431 return;
3432
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003433 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003434 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003435 return;
3436
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003437 if (index >= (int) cursor->image_count) {
3438 fprintf(stderr, "cursor index out of range\n");
3439 return;
3440 }
3441
3442 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003443 buffer = wl_cursor_image_get_buffer(image);
3444 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003445 return;
3446
Kristian Høgsbergae277372012-08-01 09:41:08 -04003447 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003448 input->pointer_surface,
3449 image->hotspot_x, image->hotspot_y);
3450 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3451 wl_surface_damage(input->pointer_surface, 0, 0,
3452 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003453 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003454}
3455
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003456static const struct wl_callback_listener pointer_surface_listener;
3457
3458static void
3459pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3460 uint32_t time)
3461{
3462 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003463 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003464 int i;
3465
3466 if (callback) {
3467 assert(callback == input->cursor_frame_cb);
3468 wl_callback_destroy(callback);
3469 input->cursor_frame_cb = NULL;
3470 }
3471
Daniel Stone80972742012-11-07 17:51:39 +11003472 if (!input->pointer)
3473 return;
3474
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003475 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003476 wl_pointer_set_cursor(input->pointer,
3477 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003478 NULL, 0, 0);
3479 return;
3480 }
3481
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003482 if (input->current_cursor == CURSOR_UNSET)
3483 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003484 cursor = input->display->cursors[input->current_cursor];
3485 if (!cursor)
3486 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003487
3488 /* FIXME We don't have the current time on the first call so we set
3489 * the animation start to the time of the first frame callback. */
3490 if (time == 0)
3491 input->cursor_anim_start = 0;
3492 else if (input->cursor_anim_start == 0)
3493 input->cursor_anim_start = time;
3494
3495 if (time == 0 || input->cursor_anim_start == 0)
3496 i = 0;
3497 else
3498 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3499
Pekka Paalanenbc106382012-10-10 12:49:31 +03003500 if (cursor->image_count > 1) {
3501 input->cursor_frame_cb =
3502 wl_surface_frame(input->pointer_surface);
3503 wl_callback_add_listener(input->cursor_frame_cb,
3504 &pointer_surface_listener, input);
3505 }
3506
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003507 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003508}
3509
3510static const struct wl_callback_listener pointer_surface_listener = {
3511 pointer_surface_frame_callback
3512};
3513
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003514void
3515input_set_pointer_image(struct input *input, int pointer)
3516{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003517 int force = 0;
3518
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003519 if (!input->pointer)
3520 return;
3521
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003522 if (input->pointer_enter_serial > input->cursor_serial)
3523 force = 1;
3524
3525 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003526 return;
3527
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003528 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003529 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003530 if (!input->cursor_frame_cb)
3531 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003532 else if (force) {
3533 /* The current frame callback may be stuck if, for instance,
3534 * the set cursor request was processed by the server after
3535 * this client lost the focus. In this case the cursor surface
3536 * might not be mapped and the frame callback wouldn't ever
3537 * complete. Send a set_cursor and attach to try to map the
3538 * cursor surface again so that the callback will finish */
3539 input_set_pointer_image_index(input, 0);
3540 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003541}
3542
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003543struct wl_data_device *
3544input_get_data_device(struct input *input)
3545{
3546 return input->data_device;
3547}
3548
3549void
3550input_set_selection(struct input *input,
3551 struct wl_data_source *source, uint32_t time)
3552{
3553 wl_data_device_set_selection(input->data_device, source, time);
3554}
3555
3556void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003557input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003558{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003559 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003560 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003561}
3562
3563static void
3564offer_io_func(struct task *task, uint32_t events)
3565{
3566 struct data_offer *offer =
3567 container_of(task, struct data_offer, io_task);
3568 unsigned int len;
3569 char buffer[4096];
3570
3571 len = read(offer->fd, buffer, sizeof buffer);
3572 offer->func(buffer, len,
3573 offer->x, offer->y, offer->user_data);
3574
3575 if (len == 0) {
3576 close(offer->fd);
3577 data_offer_destroy(offer);
3578 }
3579}
3580
3581static void
3582data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3583 data_func_t func, void *user_data)
3584{
3585 int p[2];
3586
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003587 if (pipe2(p, O_CLOEXEC) == -1)
3588 return;
3589
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003590 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3591 close(p[1]);
3592
3593 offer->io_task.run = offer_io_func;
3594 offer->fd = p[0];
3595 offer->func = func;
3596 offer->refcount++;
3597 offer->user_data = user_data;
3598
3599 display_watch_fd(offer->input->display,
3600 offer->fd, EPOLLIN, &offer->io_task);
3601}
3602
3603void
3604input_receive_drag_data(struct input *input, const char *mime_type,
3605 data_func_t func, void *data)
3606{
3607 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003608 input->drag_offer->x = input->drag_x;
3609 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003610}
3611
3612int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003613input_receive_drag_data_to_fd(struct input *input,
3614 const char *mime_type, int fd)
3615{
3616 if (input->drag_offer)
3617 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3618
3619 return 0;
3620}
3621
3622int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003623input_receive_selection_data(struct input *input, const char *mime_type,
3624 data_func_t func, void *data)
3625{
3626 char **p;
3627
3628 if (input->selection_offer == NULL)
3629 return -1;
3630
3631 for (p = input->selection_offer->types.data; *p; p++)
3632 if (strcmp(mime_type, *p) == 0)
3633 break;
3634
3635 if (*p == NULL)
3636 return -1;
3637
3638 data_offer_receive_data(input->selection_offer,
3639 mime_type, func, data);
3640 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003641}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003642
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003643int
3644input_receive_selection_data_to_fd(struct input *input,
3645 const char *mime_type, int fd)
3646{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003647 if (input->selection_offer)
3648 wl_data_offer_receive(input->selection_offer->offer,
3649 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003650
3651 return 0;
3652}
3653
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003654void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003655window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003656{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003657 if (!window->shell_surface)
3658 return;
3659
Daniel Stone37816df2012-05-16 18:45:18 +01003660 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003661}
3662
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003663static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003664surface_set_synchronized(struct surface *surface)
3665{
3666 if (!surface->subsurface)
3667 return;
3668
3669 if (surface->synchronized)
3670 return;
3671
3672 wl_subsurface_set_sync(surface->subsurface);
3673 surface->synchronized = 1;
3674}
3675
3676static void
3677surface_set_synchronized_default(struct surface *surface)
3678{
3679 if (!surface->subsurface)
3680 return;
3681
3682 if (surface->synchronized == surface->synchronized_default)
3683 return;
3684
3685 if (surface->synchronized_default)
3686 wl_subsurface_set_sync(surface->subsurface);
3687 else
3688 wl_subsurface_set_desync(surface->subsurface);
3689
3690 surface->synchronized = surface->synchronized_default;
3691}
3692
3693static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003694surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003695{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003696 struct widget *widget = surface->widget;
3697 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003698
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003699 if (surface->input_region) {
3700 wl_region_destroy(surface->input_region);
3701 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003702 }
3703
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003704 if (surface->opaque_region)
3705 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003706
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003707 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003708
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003709 if (widget->resize_handler)
3710 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003711 widget->allocation.width,
3712 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003713 widget->user_data);
3714
Pekka Paalanen35e82632013-04-25 13:57:48 +03003715 if (surface->subsurface &&
3716 (surface->allocation.x != widget->allocation.x ||
3717 surface->allocation.y != widget->allocation.y)) {
3718 wl_subsurface_set_position(surface->subsurface,
3719 widget->allocation.x,
3720 widget->allocation.y);
3721 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003722 if (surface->allocation.width != widget->allocation.width ||
3723 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003724 window_schedule_redraw(widget->window);
3725 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003726 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003727
3728 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003729 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003730 widget->allocation.width,
3731 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003732}
3733
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003734static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003735hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3736{
3737 /*
3738 * This hack should be removed, when EGL respects
3739 * eglSwapInterval(0).
3740 *
3741 * If this window has sub-surfaces, especially a free-running
3742 * EGL-widget, we need to post the parent surface once with
3743 * all the old state to guarantee, that the EGL-widget will
3744 * receive its frame callback soon. Otherwise, a forced call
3745 * to eglSwapBuffers may end up blocking, waiting for a frame
3746 * event that will never come, because we will commit the parent
3747 * surface with all new state only after eglSwapBuffers returns.
3748 *
3749 * This assumes, that:
3750 * 1. When the EGL widget's resize hook is called, it pauses.
3751 * 2. When the EGL widget's redraw hook is called, it forces a
3752 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3753 * In a single threaded application condition 1 is a no-op.
3754 *
3755 * XXX: This should actually be after the surface_resize() calls,
3756 * but cannot, because then it would commit the incomplete state
3757 * accumulated from the widget resize hooks.
3758 */
3759 if (window->subsurface_list.next != &window->main_surface->link ||
3760 window->subsurface_list.prev != &window->main_surface->link)
3761 wl_surface_commit(window->main_surface->surface);
3762}
3763
3764static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003765window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003766{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003767 struct surface *surface;
3768
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003769 widget_set_allocation(window->main_surface->widget,
3770 window->pending_allocation.x,
3771 window->pending_allocation.y,
3772 window->pending_allocation.width,
3773 window->pending_allocation.height);
3774
3775 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003776
3777 /* The main surface is in the list, too. Main surface's
3778 * resize_handler is responsible for calling widget_set_allocation()
3779 * on all sub-surface root widgets, so they will be resized
3780 * properly.
3781 */
3782 wl_list_for_each(surface, &window->subsurface_list, link) {
3783 if (surface == window->main_surface)
3784 continue;
3785
3786 surface_set_synchronized(surface);
3787 surface_resize(surface);
3788 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003789}
3790
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003791static void
3792idle_resize(struct window *window)
3793{
3794 window->resize_needed = 0;
3795 window->redraw_needed = 1;
3796
3797 DBG("from %dx%d to %dx%d\n",
3798 window->main_surface->server_allocation.width,
3799 window->main_surface->server_allocation.height,
3800 window->pending_allocation.width,
3801 window->pending_allocation.height);
3802
3803 hack_prevent_EGL_sub_surface_deadlock(window);
3804
3805 window_do_resize(window);
3806}
3807
3808static void
3809undo_resize(struct window *window)
3810{
3811 window->pending_allocation.width =
3812 window->main_surface->server_allocation.width;
3813 window->pending_allocation.height =
3814 window->main_surface->server_allocation.height;
3815
3816 DBG("back to %dx%d\n",
3817 window->main_surface->server_allocation.width,
3818 window->main_surface->server_allocation.height);
3819
3820 window_do_resize(window);
3821
3822 if (window->pending_allocation.width == 0 &&
3823 window->pending_allocation.height == 0) {
3824 fprintf(stderr, "Error: Could not draw a surface, "
3825 "most likely due to insufficient disk space in "
3826 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3827 exit(EXIT_FAILURE);
3828 }
3829}
3830
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003831void
3832window_schedule_resize(struct window *window, int width, int height)
3833{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003834 /* We should probably get these numbers from the theme. */
3835 const int min_width = 200, min_height = 200;
3836
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003837 window->pending_allocation.x = 0;
3838 window->pending_allocation.y = 0;
3839 window->pending_allocation.width = width;
3840 window->pending_allocation.height = height;
3841
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003842 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003843 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003844 window->min_allocation.width = min_width;
3845 else
3846 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003847 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003848 window->min_allocation.height = min_height;
3849 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003850 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003851 }
3852
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003853 if (window->pending_allocation.width < window->min_allocation.width)
3854 window->pending_allocation.width = window->min_allocation.width;
3855 if (window->pending_allocation.height < window->min_allocation.height)
3856 window->pending_allocation.height = window->min_allocation.height;
3857
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003858 window->resize_needed = 1;
3859 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003860}
3861
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003862void
3863widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3864{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003865 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003866}
3867
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003868static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003869handle_ping(void *data, struct wl_shell_surface *shell_surface,
3870 uint32_t serial)
3871{
3872 wl_shell_surface_pong(shell_surface, serial);
3873}
3874
3875static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003876handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003877 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003878{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003879 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003880
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003881 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003882 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003883}
3884
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003885static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003886menu_destroy(struct menu *menu)
3887{
3888 widget_destroy(menu->widget);
3889 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003890 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003891 free(menu);
3892}
3893
3894static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003895handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3896{
3897 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003898 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003899
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003900 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003901 * device. Or just use wl_callback. And this really needs to
3902 * be a window vfunc that the menu can set. And we need the
3903 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003904
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003905 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003906 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003907}
3908
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003909static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003910 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003911 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003912 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003913};
3914
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003915void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003916window_get_allocation(struct window *window,
3917 struct rectangle *allocation)
3918{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003919 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003920}
3921
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003922static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003923widget_redraw(struct widget *widget)
3924{
3925 struct widget *child;
3926
3927 if (widget->redraw_handler)
3928 widget->redraw_handler(widget, widget->user_data);
3929 wl_list_for_each(child, &widget->child_list, link)
3930 widget_redraw(child);
3931}
3932
3933static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003934frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3935{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003936 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003937
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003938 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003939 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003940 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003941 surface->frame_cb = NULL;
3942
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003943 surface->last_time = time;
3944
Pekka Paalanen71233882013-04-25 13:57:53 +03003945 if (surface->redraw_needed || surface->window->redraw_needed) {
3946 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003947 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003948 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003949}
3950
3951static const struct wl_callback_listener listener = {
3952 frame_callback
3953};
3954
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003955static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003956surface_redraw(struct surface *surface)
3957{
Pekka Paalanen71233882013-04-25 13:57:53 +03003958 DBG_OBJ(surface->surface, "begin\n");
3959
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003960 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003961 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003962
3963 /* Whole-window redraw forces a redraw even if the previous has
3964 * not yet hit the screen.
3965 */
3966 if (surface->frame_cb) {
3967 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003968 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003969
Pekka Paalanen71233882013-04-25 13:57:53 +03003970 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003971 wl_callback_destroy(surface->frame_cb);
3972 }
3973
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003974 if (!widget_get_cairo_surface(surface->widget)) {
3975 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3976 return -1;
3977 }
3978
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003979 surface->frame_cb = wl_surface_frame(surface->surface);
3980 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003981 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003982
3983 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003984 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003985 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003986 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003987 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003988}
3989
3990static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003991idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003992{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003993 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003994 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003995 int failed = 0;
3996 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003997
Pekka Paalanen71233882013-04-25 13:57:53 +03003998 DBG(" --------- \n");
3999
Pekka Paalaneneebff542013-04-25 13:57:52 +03004000 wl_list_init(&window->redraw_task.link);
4001 window->redraw_task_scheduled = 0;
4002
4003 if (window->resize_needed) {
4004 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004005 if (window->main_surface->frame_cb) {
4006 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004007 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004008 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004009
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004010 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004011 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004012 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004013
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004014 if (surface_redraw(window->main_surface) < 0) {
4015 /*
4016 * Only main_surface failure will cause us to undo the resize.
4017 * If sub-surfaces fail, they will just be broken with old
4018 * content.
4019 */
4020 failed = 1;
4021 } else {
4022 wl_list_for_each(surface, &window->subsurface_list, link) {
4023 if (surface == window->main_surface)
4024 continue;
4025
4026 surface_redraw(surface);
4027 }
4028 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004029
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004030 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004031 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004032
4033 wl_list_for_each(surface, &window->subsurface_list, link)
4034 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004035
4036 if (resized && failed) {
4037 /* Restore widget tree to correspond to what is on screen. */
4038 undo_resize(window);
4039 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004040}
4041
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004042static void
4043window_schedule_redraw_task(struct window *window)
4044{
4045 if (window->configure_requests)
4046 return;
4047 if (!window->redraw_task_scheduled) {
4048 window->redraw_task.run = idle_redraw;
4049 display_defer(window->display, &window->redraw_task);
4050 window->redraw_task_scheduled = 1;
4051 }
4052}
4053
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004054void
4055window_schedule_redraw(struct window *window)
4056{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004057 struct surface *surface;
4058
Pekka Paalanen71233882013-04-25 13:57:53 +03004059 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4060
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004061 wl_list_for_each(surface, &window->subsurface_list, link)
4062 surface->redraw_needed = 1;
4063
4064 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004065}
4066
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004067int
4068window_is_fullscreen(struct window *window)
4069{
4070 return window->type == TYPE_FULLSCREEN;
4071}
4072
Philip Withnallc971d2a2013-11-25 18:01:33 +00004073int
4074window_is_transient(struct window *window)
4075{
4076 return window->type == TYPE_TRANSIENT;
4077}
4078
MoD063a8822013-03-02 23:43:57 +00004079static void
4080configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4081{
4082 struct window *window = data;
4083
4084 wl_callback_destroy(callback);
4085 window->configure_requests--;
4086
4087 if (!window->configure_requests)
4088 window_schedule_redraw(window);
4089}
4090
4091static struct wl_callback_listener configure_request_listener = {
4092 configure_request_completed,
4093};
4094
4095static void
4096window_defer_redraw_until_configure(struct window* window)
4097{
4098 struct wl_callback *callback;
4099
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004100 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00004101 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004102 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00004103 }
4104
4105 callback = wl_display_sync(window->display->display);
4106 wl_callback_add_listener(callback, &configure_request_listener, window);
4107 window->configure_requests++;
4108}
4109
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004110void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004111window_set_fullscreen(struct window *window, int fullscreen)
4112{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004113 if (!window->display->shell)
4114 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004115
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04004116 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004117 return;
4118
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004119 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004120 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01004121 if (window->type == TYPE_TOPLEVEL) {
4122 window->saved_allocation = window->main_surface->allocation;
4123 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004124 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004125 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004126 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004127 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004128 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004129 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004130 if (window->saved_type == TYPE_MAXIMIZED) {
4131 window_set_maximized(window, 1);
4132 } else {
4133 window->type = TYPE_TOPLEVEL;
4134 wl_shell_surface_set_toplevel(window->shell_surface);
4135 window_schedule_resize(window,
4136 window->saved_allocation.width,
4137 window->saved_allocation.height);
4138 }
4139
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004140 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004141}
4142
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004143void
4144window_set_fullscreen_method(struct window *window,
4145 enum wl_shell_surface_fullscreen_method method)
4146{
4147 window->fullscreen_method = method;
4148}
4149
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004150int
4151window_is_maximized(struct window *window)
4152{
4153 return window->type == TYPE_MAXIMIZED;
4154}
4155
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004156void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004157window_set_maximized(struct window *window, int maximized)
4158{
4159 if (!window->display->shell)
4160 return;
4161
4162 if ((window->type == TYPE_MAXIMIZED) == maximized)
4163 return;
4164
4165 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004166 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004167 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4168 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004169 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004170 } else if (window->type == TYPE_FULLSCREEN) {
4171 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4172 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004173 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004174 } else {
4175 wl_shell_surface_set_toplevel(window->shell_surface);
4176 window->type = TYPE_TOPLEVEL;
4177 window_schedule_resize(window,
4178 window->saved_allocation.width,
4179 window->saved_allocation.height);
4180 }
4181}
4182
4183void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004184window_set_user_data(struct window *window, void *data)
4185{
4186 window->user_data = data;
4187}
4188
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004189void *
4190window_get_user_data(struct window *window)
4191{
4192 return window->user_data;
4193}
4194
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004195void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004196window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004197 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004198{
4199 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004200}
4201
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004202void
4203window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004204 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004205{
4206 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004207}
4208
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004209void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004210window_set_data_handler(struct window *window, window_data_handler_t handler)
4211{
4212 window->data_handler = handler;
4213}
4214
4215void
4216window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4217{
4218 window->drop_handler = handler;
4219}
4220
4221void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004222window_set_close_handler(struct window *window,
4223 window_close_handler_t handler)
4224{
4225 window->close_handler = handler;
4226}
4227
4228void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004229window_set_fullscreen_handler(struct window *window,
4230 window_fullscreen_handler_t handler)
4231{
4232 window->fullscreen_handler = handler;
4233}
4234
4235void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004236window_set_output_handler(struct window *window,
4237 window_output_handler_t handler)
4238{
4239 window->output_handler = handler;
4240}
4241
4242void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004243window_set_title(struct window *window, const char *title)
4244{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004245 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004246 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004247 if (window->frame) {
4248 frame_set_title(window->frame->frame, title);
4249 widget_schedule_redraw(window->frame->widget);
4250 }
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004251 if (window->shell_surface)
4252 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004253}
4254
4255const char *
4256window_get_title(struct window *window)
4257{
4258 return window->title;
4259}
4260
4261void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004262window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4263{
4264 struct text_cursor_position *text_cursor_position =
4265 window->display->text_cursor_position;
4266
Scott Moreau9295ce02012-06-01 12:46:10 -06004267 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004268 return;
4269
4270 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004271 window->main_surface->surface,
4272 wl_fixed_from_int(x),
4273 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004274}
4275
4276void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004277window_damage(struct window *window, int32_t x, int32_t y,
4278 int32_t width, int32_t height)
4279{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004280 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004281}
4282
Casey Dahlin9074db52012-04-19 22:50:09 -04004283static void
4284surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004285 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004286{
Rob Bradford7507b572012-05-15 17:55:34 +01004287 struct window *window = data;
4288 struct output *output;
4289 struct output *output_found = NULL;
4290 struct window_output *window_output;
4291
4292 wl_list_for_each(output, &window->display->output_list, link) {
4293 if (output->output == wl_output) {
4294 output_found = output;
4295 break;
4296 }
4297 }
4298
4299 if (!output_found)
4300 return;
4301
Brian Lovinbc919262013-08-07 15:34:59 -07004302 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004303 window_output->output = output_found;
4304
4305 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004306
4307 if (window->output_handler)
4308 window->output_handler(window, output_found, 1,
4309 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004310}
4311
4312static void
4313surface_leave(void *data,
4314 struct wl_surface *wl_surface, struct wl_output *output)
4315{
Rob Bradford7507b572012-05-15 17:55:34 +01004316 struct window *window = data;
4317 struct window_output *window_output;
4318 struct window_output *window_output_found = NULL;
4319
4320 wl_list_for_each(window_output, &window->window_output_list, link) {
4321 if (window_output->output->output == output) {
4322 window_output_found = window_output;
4323 break;
4324 }
4325 }
4326
4327 if (window_output_found) {
4328 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004329
4330 if (window->output_handler)
4331 window->output_handler(window, window_output->output,
4332 0, window->user_data);
4333
Rob Bradford7507b572012-05-15 17:55:34 +01004334 free(window_output_found);
4335 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004336}
4337
4338static const struct wl_surface_listener surface_listener = {
4339 surface_enter,
4340 surface_leave
4341};
4342
Pekka Paalanen4e373742013-02-13 16:17:13 +02004343static struct surface *
4344surface_create(struct window *window)
4345{
4346 struct display *display = window->display;
4347 struct surface *surface;
4348
Brian Lovinbc919262013-08-07 15:34:59 -07004349 surface = xmalloc(sizeof *surface);
4350 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004351 if (!surface)
4352 return NULL;
4353
4354 surface->window = window;
4355 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004356 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004357 wl_surface_add_listener(surface->surface, &surface_listener, window);
4358
Pekka Paalanen35e82632013-04-25 13:57:48 +03004359 wl_list_insert(&window->subsurface_list, &surface->link);
4360
Pekka Paalanen4e373742013-02-13 16:17:13 +02004361 return surface;
4362}
4363
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004364static struct window *
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004365window_create_internal(struct display *display, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004366{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004367 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004368 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004369
Peter Huttererf3d62272013-08-08 11:57:05 +10004370 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004371 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004372 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004373
4374 surface = surface_create(window);
4375 window->main_surface = surface;
4376
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004377 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004378 window->shell_surface =
4379 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004380 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004381 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004382 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004383
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004384 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004385 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004386 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004387 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004388
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004389 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004390#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004391 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004392#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004393 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004394#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004395 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004396 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004397
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004398 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004399 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004400 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004401
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004402 if (window->shell_surface) {
4403 wl_shell_surface_set_user_data(window->shell_surface, window);
4404 wl_shell_surface_add_listener(window->shell_surface,
4405 &shell_surface_listener, window);
4406 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004407
Rob Bradford7507b572012-05-15 17:55:34 +01004408 wl_list_init (&window->window_output_list);
4409
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004410 return window;
4411}
4412
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004413struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004414window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004415{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004416 return window_create_internal(display, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004417}
4418
4419struct window *
4420window_create_custom(struct display *display)
4421{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004422 return window_create_internal(display, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004423}
4424
4425struct window *
4426window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004427 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004428{
4429 struct window *window;
4430
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004431 window = window_create_internal(parent->display, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004432
4433 window->x = x;
4434 window->y = y;
4435
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004436 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004437 wl_shell_surface_set_transient(
4438 window->shell_surface,
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004439 parent->main_surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004440 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004441
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004442 return window;
4443}
4444
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004445static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004446menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004447{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004448 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004449 int next;
4450
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004451 frame_interior(menu->frame, &x, &y, &width, &height);
4452 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004453 if (menu->current != next) {
4454 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004455 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004456 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004457}
4458
4459static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004460menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004461 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004462 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004463{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004464 struct menu *menu = data;
4465
4466 if (widget == menu->widget)
4467 menu_set_item(data, y);
4468
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004469 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004470}
4471
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004472static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004473menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004474 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004475{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004476 struct menu *menu = data;
4477
4478 if (widget == menu->widget)
4479 menu_set_item(data, y);
4480
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004481 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004482}
4483
4484static void
4485menu_leave_handler(struct widget *widget, struct input *input, void *data)
4486{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004487 struct menu *menu = data;
4488
4489 if (widget == menu->widget)
4490 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004491}
4492
4493static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004494menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004495 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004496 uint32_t button, enum wl_pointer_button_state state,
4497 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004498
4499{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004500 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004501
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004502 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4503 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004504 /* Either relase after press-drag-release or
4505 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004506 menu->func(menu->parent, input,
4507 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004508 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004509 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004510 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004511 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004512 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004513}
4514
4515static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004516menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004517{
4518 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004519 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004520 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004521
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004522 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004523
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004524 frame_repaint(menu->frame, cr);
4525 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004526
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004527 theme_set_background_source(menu->window->display->theme,
4528 cr, THEME_FRAME_ACTIVE);
4529 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004530 cairo_fill(cr);
4531
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004532 cairo_select_font_face(cr, "sans",
4533 CAIRO_FONT_SLANT_NORMAL,
4534 CAIRO_FONT_WEIGHT_NORMAL);
4535 cairo_set_font_size(cr, 12);
4536
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004537 for (i = 0; i < menu->count; i++) {
4538 if (i == menu->current) {
4539 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004540 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004541 cairo_fill(cr);
4542 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004543 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004544 cairo_show_text(cr, menu->entries[i]);
4545 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004546 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4547 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004548 cairo_show_text(cr, menu->entries[i]);
4549 }
4550 }
4551
4552 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004553}
4554
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004555void
4556window_show_menu(struct display *display,
4557 struct input *input, uint32_t time, struct window *parent,
4558 int32_t x, int32_t y,
4559 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004560{
4561 struct window *window;
4562 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004563 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004564
4565 menu = malloc(sizeof *menu);
4566 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004567 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004568
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004569 window = window_create_internal(parent->display, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004570 if (!window) {
4571 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004572 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004573 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004574
4575 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004576 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004577 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004578 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4579 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004580 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004581 FRAME_BUTTON_NONE, NULL);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004582 menu->entries = entries;
4583 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004584 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004585 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004586 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004587 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004588 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004589 window->type = TYPE_MENU;
4590 window->x = x;
4591 window->y = y;
4592
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004593 input_ungrab(input);
4594
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004595 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004596 widget_set_enter_handler(menu->widget, menu_enter_handler);
4597 widget_set_leave_handler(menu->widget, menu_leave_handler);
4598 widget_set_motion_handler(menu->widget, menu_motion_handler);
4599 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004600
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004601 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004602 frame_resize_inside(menu->frame, 200, count * 20);
4603 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4604 window_schedule_resize(window, frame_width(menu->frame),
4605 frame_height(menu->frame));
4606
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004607 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4608 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4609 display_get_serial(window->display),
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004610 parent->main_surface->surface,
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004611 window->x - ix, window->y - iy, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004612}
4613
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004614void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004615window_set_buffer_type(struct window *window, enum window_buffer_type type)
4616{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004617 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004618}
4619
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004620void
4621window_set_preferred_format(struct window *window,
4622 enum preferred_format format)
4623{
4624 window->preferred_format = format;
4625}
4626
Pekka Paalanen35e82632013-04-25 13:57:48 +03004627struct widget *
4628window_add_subsurface(struct window *window, void *data,
4629 enum subsurface_mode default_mode)
4630{
4631 struct widget *widget;
4632 struct surface *surface;
4633 struct wl_surface *parent;
4634 struct wl_subcompositor *subcompo = window->display->subcompositor;
4635
Pekka Paalanen35e82632013-04-25 13:57:48 +03004636 surface = surface_create(window);
4637 widget = widget_create(window, surface, data);
4638 wl_list_init(&widget->link);
4639 surface->widget = widget;
4640
4641 parent = window->main_surface->surface;
4642 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4643 surface->surface,
4644 parent);
4645 surface->synchronized = 1;
4646
4647 switch (default_mode) {
4648 case SUBSURFACE_SYNCHRONIZED:
4649 surface->synchronized_default = 1;
4650 break;
4651 case SUBSURFACE_DESYNCHRONIZED:
4652 surface->synchronized_default = 0;
4653 break;
4654 default:
4655 assert(!"bad enum subsurface_mode");
4656 }
4657
4658 return widget;
4659}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004660
4661static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004662display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004663 struct wl_output *wl_output,
4664 int x, int y,
4665 int physical_width,
4666 int physical_height,
4667 int subpixel,
4668 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004669 const char *model,
4670 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004671{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004672 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004673
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004674 output->allocation.x = x;
4675 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004676 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004677}
4678
4679static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004680display_handle_done(void *data,
4681 struct wl_output *wl_output)
4682{
4683}
4684
4685static void
4686display_handle_scale(void *data,
4687 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004688 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004689{
4690 struct output *output = data;
4691
4692 output->scale = scale;
4693}
4694
4695static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004696display_handle_mode(void *data,
4697 struct wl_output *wl_output,
4698 uint32_t flags,
4699 int width,
4700 int height,
4701 int refresh)
4702{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004703 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004704 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004705
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004706 if (flags & WL_OUTPUT_MODE_CURRENT) {
4707 output->allocation.width = width;
4708 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004709 if (display->output_configure_handler)
4710 (*display->output_configure_handler)(
4711 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004712 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004713}
4714
4715static const struct wl_output_listener output_listener = {
4716 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004717 display_handle_mode,
4718 display_handle_done,
4719 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004720};
4721
4722static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004723display_add_output(struct display *d, uint32_t id)
4724{
4725 struct output *output;
4726
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004727 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004728 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004729 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004730 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004731 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004732 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004733 wl_list_insert(d->output_list.prev, &output->link);
4734
4735 wl_output_add_listener(output->output, &output_listener, output);
4736}
4737
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004738static void
4739output_destroy(struct output *output)
4740{
4741 if (output->destroy_handler)
4742 (*output->destroy_handler)(output, output->user_data);
4743
4744 wl_output_destroy(output->output);
4745 wl_list_remove(&output->link);
4746 free(output);
4747}
4748
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004749static void
4750display_destroy_output(struct display *d, uint32_t id)
4751{
4752 struct output *output;
4753
4754 wl_list_for_each(output, &d->output_list, link) {
4755 if (output->server_output_id == id) {
4756 output_destroy(output);
4757 break;
4758 }
4759 }
4760}
4761
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004762void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004763display_set_global_handler(struct display *display,
4764 display_global_handler_t handler)
4765{
4766 struct global *global;
4767
4768 display->global_handler = handler;
4769 if (!handler)
4770 return;
4771
4772 wl_list_for_each(global, &display->global_list, link)
4773 display->global_handler(display,
4774 global->name, global->interface,
4775 global->version, display->user_data);
4776}
4777
4778void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004779display_set_global_handler_remove(struct display *display,
4780 display_global_handler_t remove_handler)
4781{
4782 display->global_handler_remove = remove_handler;
4783 if (!remove_handler)
4784 return;
4785}
4786
4787void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004788display_set_output_configure_handler(struct display *display,
4789 display_output_handler_t handler)
4790{
4791 struct output *output;
4792
4793 display->output_configure_handler = handler;
4794 if (!handler)
4795 return;
4796
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004797 wl_list_for_each(output, &display->output_list, link) {
4798 if (output->allocation.width == 0 &&
4799 output->allocation.height == 0)
4800 continue;
4801
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004802 (*display->output_configure_handler)(output,
4803 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004804 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004805}
4806
4807void
4808output_set_user_data(struct output *output, void *data)
4809{
4810 output->user_data = data;
4811}
4812
4813void *
4814output_get_user_data(struct output *output)
4815{
4816 return output->user_data;
4817}
4818
4819void
4820output_set_destroy_handler(struct output *output,
4821 display_output_handler_t handler)
4822{
4823 output->destroy_handler = handler;
4824 /* FIXME: implement this, once we have way to remove outputs */
4825}
4826
4827void
Scott Moreau4e072362012-09-29 02:03:11 -06004828output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004829{
Scott Moreau4e072362012-09-29 02:03:11 -06004830 struct rectangle allocation = output->allocation;
4831
4832 switch (output->transform) {
4833 case WL_OUTPUT_TRANSFORM_90:
4834 case WL_OUTPUT_TRANSFORM_270:
4835 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4836 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4837 /* Swap width and height */
4838 allocation.width = output->allocation.height;
4839 allocation.height = output->allocation.width;
4840 break;
4841 }
4842
4843 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004844}
4845
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004846struct wl_output *
4847output_get_wl_output(struct output *output)
4848{
4849 return output->output;
4850}
4851
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004852enum wl_output_transform
4853output_get_transform(struct output *output)
4854{
4855 return output->transform;
4856}
4857
Alexander Larssonafd319a2013-05-22 14:41:27 +02004858uint32_t
4859output_get_scale(struct output *output)
4860{
4861 return output->scale;
4862}
4863
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004864static void
Daniel Stone97f68542012-05-30 16:32:01 +01004865fini_xkb(struct input *input)
4866{
4867 xkb_state_unref(input->xkb.state);
4868 xkb_map_unref(input->xkb.keymap);
4869}
4870
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004871#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004872
Daniel Stone97f68542012-05-30 16:32:01 +01004873static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004874display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004875{
4876 struct input *input;
4877
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004878 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004879 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004880 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004881 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004882 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004883 input->pointer_focus = NULL;
4884 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004885 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004886 wl_list_insert(d->input_list.prev, &input->link);
4887
Daniel Stone37816df2012-05-16 18:45:18 +01004888 wl_seat_add_listener(input->seat, &seat_listener, input);
4889 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004890
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004891 input->data_device =
4892 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004893 input->seat);
4894 wl_data_device_add_listener(input->data_device, &data_device_listener,
4895 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004896
4897 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004898
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004899 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4900 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004901 input->repeat_task.run = keyboard_repeat_func;
4902 display_watch_fd(d, input->repeat_timer_fd,
4903 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004904}
4905
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004906static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004907input_destroy(struct input *input)
4908{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004909 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004910 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004911
4912 if (input->drag_offer)
4913 data_offer_destroy(input->drag_offer);
4914
4915 if (input->selection_offer)
4916 data_offer_destroy(input->selection_offer);
4917
4918 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004919
4920 if (input->display->seat_version >= 3) {
4921 if (input->pointer)
4922 wl_pointer_release(input->pointer);
4923 if (input->keyboard)
4924 wl_keyboard_release(input->keyboard);
4925 }
4926
Daniel Stone97f68542012-05-30 16:32:01 +01004927 fini_xkb(input);
4928
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004929 wl_surface_destroy(input->pointer_surface);
4930
Pekka Paalanene1207c72011-12-16 12:02:09 +02004931 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004932 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004933 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004934 free(input);
4935}
4936
4937static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004938init_workspace_manager(struct display *d, uint32_t id)
4939{
4940 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004941 wl_registry_bind(d->registry, id,
4942 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004943 if (d->workspace_manager != NULL)
4944 workspace_manager_add_listener(d->workspace_manager,
4945 &workspace_manager_listener,
4946 d);
4947}
4948
4949static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004950shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4951{
4952 struct display *d = data;
4953
4954 if (format == WL_SHM_FORMAT_RGB565)
4955 d->has_rgb565 = 1;
4956}
4957
4958struct wl_shm_listener shm_listener = {
4959 shm_format
4960};
4961
4962static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004963registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4964 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004965{
4966 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004967 struct global *global;
4968
Brian Lovinbc919262013-08-07 15:34:59 -07004969 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004970 global->name = id;
4971 global->interface = strdup(interface);
4972 global->version = version;
4973 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004974
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004975 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004976 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05004977 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004978 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004979 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004980 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01004981 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004982 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004983 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004984 d->shell = wl_registry_bind(registry,
4985 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004986 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004987 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004988 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004989 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4990 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004991 wl_registry_bind(registry, id,
4992 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004993 } else if (strcmp(interface, "text_cursor_position") == 0) {
4994 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004995 wl_registry_bind(registry, id,
4996 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004997 } else if (strcmp(interface, "workspace_manager") == 0) {
4998 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004999 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5000 d->subcompositor =
5001 wl_registry_bind(registry, id,
5002 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005003 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005004
5005 if (d->global_handler)
5006 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005007}
5008
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005009static void
5010registry_handle_global_remove(void *data, struct wl_registry *registry,
5011 uint32_t name)
5012{
5013 struct display *d = data;
5014 struct global *global;
5015 struct global *tmp;
5016
5017 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5018 if (global->name != name)
5019 continue;
5020
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005021 if (strcmp(global->interface, "wl_output") == 0)
5022 display_destroy_output(d, name);
5023
5024 /* XXX: Should destroy remaining bound globals */
5025
5026 if (d->global_handler_remove)
5027 d->global_handler_remove(d, name, global->interface,
5028 global->version, d->user_data);
5029
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005030 wl_list_remove(&global->link);
5031 free(global->interface);
5032 free(global);
5033 }
5034}
5035
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005036void *
5037display_bind(struct display *display, uint32_t name,
5038 const struct wl_interface *interface, uint32_t version)
5039{
5040 return wl_registry_bind(display->registry, name, interface, version);
5041}
5042
5043static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005044 registry_handle_global,
5045 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005046};
5047
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005048#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005049static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005050init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005051{
5052 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005053 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005054
Rob Clark6396ed32012-03-11 19:48:41 -05005055#ifdef USE_CAIRO_GLESV2
5056# define GL_BIT EGL_OPENGL_ES2_BIT
5057#else
5058# define GL_BIT EGL_OPENGL_BIT
5059#endif
5060
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005061 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005062 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005063 EGL_RED_SIZE, 1,
5064 EGL_GREEN_SIZE, 1,
5065 EGL_BLUE_SIZE, 1,
5066 EGL_ALPHA_SIZE, 1,
5067 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005068 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005069 EGL_NONE
5070 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005071
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005072#ifdef USE_CAIRO_GLESV2
5073 static const EGLint context_attribs[] = {
5074 EGL_CONTEXT_CLIENT_VERSION, 2,
5075 EGL_NONE
5076 };
5077 EGLint api = EGL_OPENGL_ES_API;
5078#else
5079 EGLint *context_attribs = NULL;
5080 EGLint api = EGL_OPENGL_API;
5081#endif
5082
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005083 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005084 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005085 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005086 return -1;
5087 }
5088
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005089 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005090 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005091 return -1;
5092 }
5093
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005094 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5095 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005096 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005097 return -1;
5098 }
5099
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005100 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005101 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005102 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005103 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005104 return -1;
5105 }
5106
Benjamin Franzke0c991632011-09-27 21:57:31 +02005107 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5108 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005109 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005110 return -1;
5111 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005112
5113 return 0;
5114}
5115
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005116static void
5117fini_egl(struct display *display)
5118{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005119 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005120
5121 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5122 EGL_NO_CONTEXT);
5123
5124 eglTerminate(display->dpy);
5125 eglReleaseThread();
5126}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005127#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005128
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005129static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005130init_dummy_surface(struct display *display)
5131{
5132 int len;
5133 void *data;
5134
5135 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5136 data = malloc(len);
5137 display->dummy_surface =
5138 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5139 1, 1, len);
5140 display->dummy_surface_data = data;
5141}
5142
5143static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005144handle_display_data(struct task *task, uint32_t events)
5145{
5146 struct display *display =
5147 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005148 struct epoll_event ep;
5149 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005150
5151 display->display_fd_events = events;
5152
5153 if (events & EPOLLERR || events & EPOLLHUP) {
5154 display_exit(display);
5155 return;
5156 }
5157
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005158 if (events & EPOLLIN) {
5159 ret = wl_display_dispatch(display->display);
5160 if (ret == -1) {
5161 display_exit(display);
5162 return;
5163 }
5164 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005165
5166 if (events & EPOLLOUT) {
5167 ret = wl_display_flush(display->display);
5168 if (ret == 0) {
5169 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5170 ep.data.ptr = &display->display_task;
5171 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5172 display->display_fd, &ep);
5173 } else if (ret == -1 && errno != EAGAIN) {
5174 display_exit(display);
5175 return;
5176 }
5177 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005178}
5179
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005180static void
5181log_handler(const char *format, va_list args)
5182{
5183 vfprintf(stderr, format, args);
5184}
5185
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005186struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005187display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005188{
5189 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005190
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005191 wl_log_set_handler_client(log_handler);
5192
Peter Huttererf3d62272013-08-08 11:57:05 +10005193 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005194 if (d == NULL)
5195 return NULL;
5196
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005197 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005198 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005199 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005200 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005201 return NULL;
5202 }
5203
Rob Bradford5ab9c752013-07-26 16:29:43 +01005204 d->xkb_context = xkb_context_new(0);
5205 if (d->xkb_context == NULL) {
5206 fprintf(stderr, "Failed to create XKB context\n");
5207 free(d);
5208 return NULL;
5209 }
5210
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005211 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005212 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005213 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005214 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5215 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005216
5217 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005218 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005219 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005220 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005221
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005222 d->workspace = 0;
5223 d->workspace_count = 1;
5224
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005225 d->registry = wl_display_get_registry(d->display);
5226 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005227
5228 if (wl_display_dispatch(d->display) < 0) {
5229 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5230 return NULL;
5231 }
5232
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005233#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005234 if (init_egl(d) < 0)
5235 fprintf(stderr, "EGL does not seem to work, "
5236 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005237#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005238
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005239 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005240
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005241 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005242
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005243 wl_list_init(&d->window_list);
5244
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005245 init_dummy_surface(d);
5246
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005247 return d;
5248}
5249
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005250static void
5251display_destroy_outputs(struct display *display)
5252{
5253 struct output *tmp;
5254 struct output *output;
5255
5256 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5257 output_destroy(output);
5258}
5259
Pekka Paalanene1207c72011-12-16 12:02:09 +02005260static void
5261display_destroy_inputs(struct display *display)
5262{
5263 struct input *tmp;
5264 struct input *input;
5265
5266 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5267 input_destroy(input);
5268}
5269
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005270void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005271display_destroy(struct display *display)
5272{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005273 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005274 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5275 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005276
5277 if (!wl_list_empty(&display->deferred_list))
5278 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5279
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005280 cairo_surface_destroy(display->dummy_surface);
5281 free(display->dummy_surface_data);
5282
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005283 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005284 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005285
Daniel Stone97f68542012-05-30 16:32:01 +01005286 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005287
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005288 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005289 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005290
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005291#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005292 if (display->argb_device)
5293 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005294#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005295
Pekka Paalanen35e82632013-04-25 13:57:48 +03005296 if (display->subcompositor)
5297 wl_subcompositor_destroy(display->subcompositor);
5298
Pekka Paalanenc2052982011-12-16 11:41:32 +02005299 if (display->shell)
5300 wl_shell_destroy(display->shell);
5301
5302 if (display->shm)
5303 wl_shm_destroy(display->shm);
5304
5305 if (display->data_device_manager)
5306 wl_data_device_manager_destroy(display->data_device_manager);
5307
5308 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005309 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005310
5311 close(display->epoll_fd);
5312
U. Artie Eoff44874d92012-10-02 21:12:35 -07005313 if (!(display->display_fd_events & EPOLLERR) &&
5314 !(display->display_fd_events & EPOLLHUP))
5315 wl_display_flush(display->display);
5316
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005317 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005318 free(display);
5319}
5320
5321void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005322display_set_user_data(struct display *display, void *data)
5323{
5324 display->user_data = data;
5325}
5326
5327void *
5328display_get_user_data(struct display *display)
5329{
5330 return display->user_data;
5331}
5332
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005333struct wl_display *
5334display_get_display(struct display *display)
5335{
5336 return display->display;
5337}
5338
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005339int
5340display_has_subcompositor(struct display *display)
5341{
5342 if (display->subcompositor)
5343 return 1;
5344
5345 wl_display_roundtrip(display->display);
5346
5347 return display->subcompositor != NULL;
5348}
5349
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005350cairo_device_t *
5351display_get_cairo_device(struct display *display)
5352{
5353 return display->argb_device;
5354}
5355
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005356struct output *
5357display_get_output(struct display *display)
5358{
5359 return container_of(display->output_list.next, struct output, link);
5360}
5361
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005362struct wl_compositor *
5363display_get_compositor(struct display *display)
5364{
5365 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005366}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005367
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005368uint32_t
5369display_get_serial(struct display *display)
5370{
5371 return display->serial;
5372}
5373
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005374EGLDisplay
5375display_get_egl_display(struct display *d)
5376{
5377 return d->dpy;
5378}
5379
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005380struct wl_data_source *
5381display_create_data_source(struct display *display)
5382{
5383 return wl_data_device_manager_create_data_source(display->data_device_manager);
5384}
5385
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005386EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005387display_get_argb_egl_config(struct display *d)
5388{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005389 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005390}
5391
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005392int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005393display_acquire_window_surface(struct display *display,
5394 struct window *window,
5395 EGLContext ctx)
5396{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005397 struct surface *surface = window->main_surface;
5398
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005399 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005400 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005401
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005402 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005403 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005404}
5405
5406void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005407display_release_window_surface(struct display *display,
5408 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005409{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005410 struct surface *surface = window->main_surface;
5411
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005412 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005413 return;
5414
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005415 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005416}
5417
5418void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005419display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005420{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005421 wl_list_insert(&display->deferred_list, &task->link);
5422}
5423
5424void
5425display_watch_fd(struct display *display,
5426 int fd, uint32_t events, struct task *task)
5427{
5428 struct epoll_event ep;
5429
5430 ep.events = events;
5431 ep.data.ptr = task;
5432 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5433}
5434
5435void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005436display_unwatch_fd(struct display *display, int fd)
5437{
5438 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5439}
5440
5441void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005442display_run(struct display *display)
5443{
5444 struct task *task;
5445 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005446 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005447
Pekka Paalanen826d7952011-12-15 10:14:07 +02005448 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005449 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005450 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005451 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005452 struct task, link);
5453 wl_list_remove(&task->link);
5454 task->run(task, 0);
5455 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005456
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005457 wl_display_dispatch_pending(display->display);
5458
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005459 if (!display->running)
5460 break;
5461
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005462 ret = wl_display_flush(display->display);
5463 if (ret < 0 && errno == EAGAIN) {
5464 ep[0].events =
5465 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5466 ep[0].data.ptr = &display->display_task;
5467
5468 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5469 display->display_fd, &ep[0]);
5470 } else if (ret < 0) {
5471 break;
5472 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005473
5474 count = epoll_wait(display->epoll_fd,
5475 ep, ARRAY_LENGTH(ep), -1);
5476 for (i = 0; i < count; i++) {
5477 task = ep[i].data.ptr;
5478 task->run(task, ep[i].events);
5479 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005480 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005481}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005482
5483void
5484display_exit(struct display *display)
5485{
5486 display->running = 0;
5487}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005488
5489void
5490keysym_modifiers_add(struct wl_array *modifiers_map,
5491 const char *name)
5492{
5493 size_t len = strlen(name) + 1;
5494 char *p;
5495
5496 p = wl_array_add(modifiers_map, len);
5497
5498 if (p == NULL)
5499 return;
5500
5501 strncpy(p, name, len);
5502}
5503
5504static xkb_mod_index_t
5505keysym_modifiers_get_index(struct wl_array *modifiers_map,
5506 const char *name)
5507{
5508 xkb_mod_index_t index = 0;
5509 char *p = modifiers_map->data;
5510
5511 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5512 if (strcmp(p, name) == 0)
5513 return index;
5514
5515 index++;
5516 p += strlen(p) + 1;
5517 }
5518
5519 return XKB_MOD_INVALID;
5520}
5521
5522xkb_mod_mask_t
5523keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5524 const char *name)
5525{
5526 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5527
5528 if (index == XKB_MOD_INVALID)
5529 return XKB_MOD_INVALID;
5530
5531 return 1 << index;
5532}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005533
5534void *
5535fail_on_null(void *p)
5536{
5537 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005538 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005539 exit(EXIT_FAILURE);
5540 }
5541
5542 return p;
5543}
5544
5545void *
5546xmalloc(size_t s)
5547{
5548 return fail_on_null(malloc(s));
5549}
5550
Peter Huttererf3d62272013-08-08 11:57:05 +10005551void *
5552xzalloc(size_t s)
5553{
5554 return fail_on_null(zalloc(s));
5555}
5556
Kristian Høgsbergce278412013-07-25 15:20:20 -07005557char *
5558xstrdup(const char *s)
5559{
5560 return fail_on_null(strdup(s));
5561}