blob: cc4fe4ee8121331fc21d355da00665a8ea766921 [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;
Neil Roberts97b747c2013-12-19 16:17:12 +0000288 /* If this is set to false then no cairo surface will be
289 * created before redrawing the surface. This is useful if the
290 * redraw handler is going to do completely custom rendering
291 * such as using EGL directly */
292 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400293};
294
Rusty Lynch041815a2013-08-08 21:20:38 -0700295struct touch_point {
296 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800297 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700298 struct widget *widget;
299 struct wl_list link;
300};
301
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400302struct input {
303 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100304 struct wl_seat *seat;
305 struct wl_pointer *pointer;
306 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700307 struct wl_touch *touch;
308 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400309 struct window *pointer_focus;
310 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700311 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300312 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300313 uint32_t cursor_anim_start;
314 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300315 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400316 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400317 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400318 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400319 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400320 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400321
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500322 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500323 struct widget *grab;
324 uint32_t grab_button;
325
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400326 struct wl_data_device *data_device;
327 struct data_offer *drag_offer;
328 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800329 uint32_t touch_grab;
330 int32_t touch_grab_id;
331 float drag_x, drag_y;
332 struct window *drag_focus;
333 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100334
335 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100336 struct xkb_keymap *keymap;
337 struct xkb_state *state;
338 xkb_mod_mask_t control_mask;
339 xkb_mod_mask_t alt_mask;
340 xkb_mod_mask_t shift_mask;
341 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400342
343 struct task repeat_task;
344 int repeat_timer_fd;
345 uint32_t repeat_sym;
346 uint32_t repeat_key;
347 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400348};
349
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500350struct output {
351 struct display *display;
352 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800353 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500354 struct rectangle allocation;
355 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600356 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200357 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200358
359 display_output_handler_t destroy_handler;
360 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500361};
362
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500363struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500364 struct widget *widget;
365 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500366 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500367};
368
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500369struct menu {
370 struct window *window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -0500371 struct window *parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500372 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500373 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700374 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500375 const char **entries;
376 uint32_t time;
377 int current;
378 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400379 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500380 menu_func_t func;
381};
382
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300383struct tooltip {
384 struct widget *parent;
385 struct window *window;
386 struct widget *widget;
387 char *entry;
388 struct task tooltip_task;
389 int tooltip_fd;
390 float x, y;
391};
392
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700393struct shm_pool {
394 struct wl_shm_pool *pool;
395 size_t size;
396 size_t used;
397 void *data;
398};
399
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400400enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300401 CURSOR_DEFAULT = 100,
402 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400403};
404
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500405enum window_location {
406 WINDOW_INTERIOR = 0,
407 WINDOW_RESIZING_TOP = 1,
408 WINDOW_RESIZING_BOTTOM = 2,
409 WINDOW_RESIZING_LEFT = 4,
410 WINDOW_RESIZING_TOP_LEFT = 5,
411 WINDOW_RESIZING_BOTTOM_LEFT = 6,
412 WINDOW_RESIZING_RIGHT = 8,
413 WINDOW_RESIZING_TOP_RIGHT = 9,
414 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
415 WINDOW_RESIZING_MASK = 15,
416 WINDOW_EXTERIOR = 16,
417 WINDOW_TITLEBAR = 17,
418 WINDOW_CLIENT_AREA = 18,
419};
420
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200421static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422
Pekka Paalanen97777442013-05-22 10:20:05 +0300423/* #define DEBUG */
424
425#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300426
427static void
428debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
429__attribute__ ((format (printf, 4, 5)));
430
431static void
432debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
433{
434 va_list ap;
435 struct timeval tv;
436
437 gettimeofday(&tv, NULL);
438 fprintf(stderr, "%8ld.%03ld ",
439 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
440
441 if (proxy)
442 fprintf(stderr, "%s@%d ",
443 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
444
445 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
446 fprintf(stderr, "%s ", func);
447
448 va_start(ap, fmt);
449 vfprintf(stderr, fmt, ap);
450 va_end(ap);
451}
452
453#define DBG(fmt, ...) \
454 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
455
456#define DBG_OBJ(obj, fmt, ...) \
457 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
458
459#else
460
461#define DBG(...) do {} while (0)
462#define DBG_OBJ(...) do {} while (0)
463
464#endif
465
Alexander Larsson1818e312013-05-22 14:41:31 +0200466static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200467surface_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 +0200468{
469 int32_t tmp;
470
471 switch (buffer_transform) {
472 case WL_OUTPUT_TRANSFORM_90:
473 case WL_OUTPUT_TRANSFORM_270:
474 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
475 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
476 tmp = *width;
477 *width = *height;
478 *height = tmp;
479 break;
480 default:
481 break;
482 }
483
484 *width *= buffer_scale;
485 *height *= buffer_scale;
486}
487
488static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200489buffer_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 +0200490{
491 int32_t tmp;
492
493 switch (buffer_transform) {
494 case WL_OUTPUT_TRANSFORM_90:
495 case WL_OUTPUT_TRANSFORM_270:
496 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
497 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
498 tmp = *width;
499 *width = *height;
500 *height = tmp;
501 break;
502 default:
503 break;
504 }
505
506 *width /= buffer_scale;
507 *height /= buffer_scale;
508}
509
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500510#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400511
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200512struct egl_window_surface {
513 struct toysurface base;
514 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100515 struct display *display;
516 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517 struct wl_egl_window *egl_window;
518 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100519};
520
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200521static struct egl_window_surface *
522to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100523{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200524 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525}
526
527static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200528egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200529 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200530 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100531{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200532 struct egl_window_surface *surface = to_egl_window_surface(base);
533
Alexander Larsson1818e312013-05-22 14:41:31 +0200534 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
535
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200536 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
537 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
538
539 return cairo_surface_reference(surface->cairo_surface);
540}
541
542static void
543egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200544 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200545 struct rectangle *server_allocation)
546{
547 struct egl_window_surface *surface = to_egl_window_surface(base);
548
549 cairo_gl_surface_swapbuffers(surface->cairo_surface);
550 wl_egl_window_get_attached_size(surface->egl_window,
551 &server_allocation->width,
552 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200553
554 buffer_to_surface_size (buffer_transform, buffer_scale,
555 &server_allocation->width,
556 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200557}
558
559static int
560egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
561{
562 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200563 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100564
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200565 device = cairo_surface_get_device(surface->cairo_surface);
566 if (!device)
567 return -1;
568
569 if (!ctx) {
570 if (device == surface->display->argb_device)
571 ctx = surface->display->argb_ctx;
572 else
573 assert(0);
574 }
575
576 cairo_device_flush(device);
577 cairo_device_acquire(device);
578 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
579 surface->egl_surface, ctx))
580 fprintf(stderr, "failed to make surface current\n");
581
582 return 0;
583}
584
585static void
586egl_window_surface_release(struct toysurface *base)
587{
588 struct egl_window_surface *surface = to_egl_window_surface(base);
589 cairo_device_t *device;
590
591 device = cairo_surface_get_device(surface->cairo_surface);
592 if (!device)
593 return;
594
595 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
596 surface->display->argb_ctx))
597 fprintf(stderr, "failed to make context current\n");
598
599 cairo_device_release(device);
600}
601
602static void
603egl_window_surface_destroy(struct toysurface *base)
604{
605 struct egl_window_surface *surface = to_egl_window_surface(base);
606 struct display *d = surface->display;
607
608 cairo_surface_destroy(surface->cairo_surface);
609 eglDestroySurface(d->dpy, surface->egl_surface);
610 wl_egl_window_destroy(surface->egl_window);
611 surface->surface = NULL;
612
613 free(surface);
614}
615
616static struct toysurface *
617egl_window_surface_create(struct display *display,
618 struct wl_surface *wl_surface,
619 uint32_t flags,
620 struct rectangle *rectangle)
621{
622 struct egl_window_surface *surface;
623
Pekka Paalanenb3627362012-11-19 17:16:00 +0200624 if (display->dpy == EGL_NO_DISPLAY)
625 return NULL;
626
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200627 surface = calloc(1, sizeof *surface);
628 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100629 return NULL;
630
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200631 surface->base.prepare = egl_window_surface_prepare;
632 surface->base.swap = egl_window_surface_swap;
633 surface->base.acquire = egl_window_surface_acquire;
634 surface->base.release = egl_window_surface_release;
635 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100636
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200637 surface->display = display;
638 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400639
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200640 surface->egl_window = wl_egl_window_create(surface->surface,
641 rectangle->width,
642 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200644 surface->egl_surface = eglCreateWindowSurface(display->dpy,
645 display->argb_config,
646 surface->egl_window,
647 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100648
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200649 surface->cairo_surface =
650 cairo_gl_surface_create_for_egl(display->argb_device,
651 surface->egl_surface,
652 rectangle->width,
653 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100654
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200655 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100656}
657
Pekka Paalanenb3627362012-11-19 17:16:00 +0200658#else
659
660static struct toysurface *
661egl_window_surface_create(struct display *display,
662 struct wl_surface *wl_surface,
663 uint32_t flags,
664 struct rectangle *rectangle)
665{
666 return NULL;
667}
668
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400669#endif
670
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200671struct shm_surface_data {
672 struct wl_buffer *buffer;
673 struct shm_pool *pool;
674};
675
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400676struct wl_buffer *
677display_get_buffer_for_surface(struct display *display,
678 cairo_surface_t *surface)
679{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200680 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200682 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400683
684 return data->buffer;
685}
686
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500687static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700688shm_pool_destroy(struct shm_pool *pool);
689
690static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400691shm_surface_data_destroy(void *p)
692{
693 struct shm_surface_data *data = p;
694
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200695 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700696 if (data->pool)
697 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300698
699 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400700}
701
Kristian Høgsberg16626282012-04-03 11:21:27 -0400702static struct wl_shm_pool *
703make_shm_pool(struct display *display, int size, void **data)
704{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400705 struct wl_shm_pool *pool;
706 int fd;
707
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300708 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400709 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300710 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
711 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400712 return NULL;
713 }
714
715 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400716 if (*data == MAP_FAILED) {
717 fprintf(stderr, "mmap failed: %m\n");
718 close(fd);
719 return NULL;
720 }
721
722 pool = wl_shm_create_pool(display->shm, fd, size);
723
724 close(fd);
725
726 return pool;
727}
728
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700729static struct shm_pool *
730shm_pool_create(struct display *display, size_t size)
731{
732 struct shm_pool *pool = malloc(sizeof *pool);
733
734 if (!pool)
735 return NULL;
736
737 pool->pool = make_shm_pool(display, size, &pool->data);
738 if (!pool->pool) {
739 free(pool);
740 return NULL;
741 }
742
743 pool->size = size;
744 pool->used = 0;
745
746 return pool;
747}
748
749static void *
750shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
751{
752 if (pool->used + size > pool->size)
753 return NULL;
754
755 *offset = pool->used;
756 pool->used += size;
757
758 return (char *) pool->data + *offset;
759}
760
761/* destroy the pool. this does not unmap the memory though */
762static void
763shm_pool_destroy(struct shm_pool *pool)
764{
765 munmap(pool->data, pool->size);
766 wl_shm_pool_destroy(pool->pool);
767 free(pool);
768}
769
770/* Start allocating from the beginning of the pool again */
771static void
772shm_pool_reset(struct shm_pool *pool)
773{
774 pool->used = 0;
775}
776
777static int
778data_length_for_shm_surface(struct rectangle *rect)
779{
780 int stride;
781
782 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
783 rect->width);
784 return stride * rect->height;
785}
786
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500787static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700788display_create_shm_surface_from_pool(struct display *display,
789 struct rectangle *rectangle,
790 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400791{
792 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400793 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400794 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200795 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700796 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400797 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400798
799 data = malloc(sizeof *data);
800 if (data == NULL)
801 return NULL;
802
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200803 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
804 cairo_format = CAIRO_FORMAT_RGB16_565;
805 else
806 cairo_format = CAIRO_FORMAT_ARGB32;
807
808 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700809 length = stride * rectangle->height;
810 data->pool = NULL;
811 map = shm_pool_allocate(pool, length, &offset);
812
813 if (!map) {
814 free(data);
815 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400816 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400817
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400818 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200819 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400820 rectangle->width,
821 rectangle->height,
822 stride);
823
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200824 cairo_surface_set_user_data(surface, &shm_surface_data_key,
825 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200827 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
828 format = WL_SHM_FORMAT_RGB565;
829 else {
830 if (flags & SURFACE_OPAQUE)
831 format = WL_SHM_FORMAT_XRGB8888;
832 else
833 format = WL_SHM_FORMAT_ARGB8888;
834 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400835
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200836 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
837 rectangle->width,
838 rectangle->height,
839 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400840
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700841 return surface;
842}
843
844static cairo_surface_t *
845display_create_shm_surface(struct display *display,
846 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200847 struct shm_pool *alternate_pool,
848 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700849{
850 struct shm_surface_data *data;
851 struct shm_pool *pool;
852 cairo_surface_t *surface;
853
Pekka Paalanen99436862012-11-19 17:15:59 +0200854 if (alternate_pool) {
855 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700856 surface = display_create_shm_surface_from_pool(display,
857 rectangle,
858 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200859 alternate_pool);
860 if (surface) {
861 data = cairo_surface_get_user_data(surface,
862 &shm_surface_data_key);
863 goto out;
864 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700865 }
866
867 pool = shm_pool_create(display,
868 data_length_for_shm_surface(rectangle));
869 if (!pool)
870 return NULL;
871
872 surface =
873 display_create_shm_surface_from_pool(display, rectangle,
874 flags, pool);
875
876 if (!surface) {
877 shm_pool_destroy(pool);
878 return NULL;
879 }
880
881 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200882 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700883 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400884
Pekka Paalanen99436862012-11-19 17:15:59 +0200885out:
886 if (data_ret)
887 *data_ret = data;
888
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889 return surface;
890}
891
nobled7b87cb02011-02-01 18:51:47 +0000892static int
893check_size(struct rectangle *rect)
894{
895 if (rect->width && rect->height)
896 return 0;
897
898 fprintf(stderr, "tried to create surface of "
899 "width: %d, height: %d\n", rect->width, rect->height);
900 return -1;
901}
902
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400903cairo_surface_t *
904display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400906 struct rectangle *rectangle,
907 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400908{
nobled7b87cb02011-02-01 18:51:47 +0000909 if (check_size(rectangle) < 0)
910 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200911
912 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200913 return display_create_shm_surface(display, rectangle, flags,
914 NULL, NULL);
915}
916
Pekka Paalanena4eda732012-11-19 17:16:02 +0200917struct shm_surface_leaf {
918 cairo_surface_t *cairo_surface;
919 /* 'data' is automatically destroyed, when 'cairo_surface' is */
920 struct shm_surface_data *data;
921
922 struct shm_pool *resize_pool;
923 int busy;
924};
925
926static void
927shm_surface_leaf_release(struct shm_surface_leaf *leaf)
928{
929 if (leaf->cairo_surface)
930 cairo_surface_destroy(leaf->cairo_surface);
931 /* leaf->data already destroyed via cairo private */
932
933 if (leaf->resize_pool)
934 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200935
936 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200937}
938
Pekka Paalanenaef02542013-04-25 13:57:47 +0300939#define MAX_LEAVES 3
940
Pekka Paalanen99436862012-11-19 17:15:59 +0200941struct shm_surface {
942 struct toysurface base;
943 struct display *display;
944 struct wl_surface *surface;
945 uint32_t flags;
946 int dx, dy;
947
Pekka Paalanenaef02542013-04-25 13:57:47 +0300948 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200949 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200950};
951
952static struct shm_surface *
953to_shm_surface(struct toysurface *base)
954{
955 return container_of(base, struct shm_surface, base);
956}
957
Pekka Paalanena4eda732012-11-19 17:16:02 +0200958static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300959shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
960{
961#ifdef DEBUG
962 struct shm_surface_leaf *leaf;
963 char bufs[MAX_LEAVES + 1];
964 int i;
965
966 for (i = 0; i < MAX_LEAVES; i++) {
967 leaf = &surface->leaf[i];
968
969 if (leaf->busy)
970 bufs[i] = 'b';
971 else if (leaf->cairo_surface)
972 bufs[i] = 'a';
973 else
974 bufs[i] = ' ';
975 }
976
977 bufs[MAX_LEAVES] = '\0';
978 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
979#endif
980}
981
982static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200983shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
984{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200985 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300986 struct shm_surface_leaf *leaf;
987 int i;
988 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300989
990 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200991
Pekka Paalanenaef02542013-04-25 13:57:47 +0300992 for (i = 0; i < MAX_LEAVES; i++) {
993 leaf = &surface->leaf[i];
994 if (leaf->data && leaf->data->buffer == buffer) {
995 leaf->busy = 0;
996 break;
997 }
998 }
999 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001000
Pekka Paalanenaef02542013-04-25 13:57:47 +03001001 /* Leave one free leaf with storage, release others */
1002 free_found = 0;
1003 for (i = 0; i < MAX_LEAVES; i++) {
1004 leaf = &surface->leaf[i];
1005
1006 if (!leaf->cairo_surface || leaf->busy)
1007 continue;
1008
1009 if (!free_found)
1010 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001011 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001012 shm_surface_leaf_release(leaf);
1013 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001014
Pekka Paalanen97777442013-05-22 10:20:05 +03001015 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001016}
1017
1018static const struct wl_buffer_listener shm_surface_buffer_listener = {
1019 shm_surface_buffer_release
1020};
1021
Pekka Paalanen99436862012-11-19 17:15:59 +02001022static cairo_surface_t *
1023shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001024 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001025 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001026{
Pekka Paalanenec076692012-11-30 13:37:27 +02001027 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001028 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001029 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001030 struct shm_surface_leaf *leaf = NULL;
1031 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001032
1033 surface->dx = dx;
1034 surface->dy = dy;
1035
Pekka Paalanenaef02542013-04-25 13:57:47 +03001036 /* pick a free buffer, preferrably one that already has storage */
1037 for (i = 0; i < MAX_LEAVES; i++) {
1038 if (surface->leaf[i].busy)
1039 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001040
Pekka Paalanenaef02542013-04-25 13:57:47 +03001041 if (!leaf || surface->leaf[i].cairo_surface)
1042 leaf = &surface->leaf[i];
1043 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001044 DBG_OBJ(surface->surface, "pick leaf %d\n",
1045 (int)(leaf - &surface->leaf[0]));
1046
Pekka Paalanenaef02542013-04-25 13:57:47 +03001047 if (!leaf) {
1048 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001049 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001050 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001051 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001052 }
1053
Pekka Paalanena4eda732012-11-19 17:16:02 +02001054 if (!resize_hint && leaf->resize_pool) {
1055 cairo_surface_destroy(leaf->cairo_surface);
1056 leaf->cairo_surface = NULL;
1057 shm_pool_destroy(leaf->resize_pool);
1058 leaf->resize_pool = NULL;
1059 }
1060
Alexander Larsson1818e312013-05-22 14:41:31 +02001061 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1062
Pekka Paalanena4eda732012-11-19 17:16:02 +02001063 if (leaf->cairo_surface &&
1064 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1065 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001066 goto out;
1067
Pekka Paalanena4eda732012-11-19 17:16:02 +02001068 if (leaf->cairo_surface)
1069 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001070
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001071#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001072 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001073 /* Create a big pool to allocate from, while continuously
1074 * resizing. Mmapping a new pool in the server
1075 * is relatively expensive, so reusing a pool performs
1076 * better, but may temporarily reserve unneeded memory.
1077 */
1078 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 leaf->resize_pool = shm_pool_create(surface->display,
1080 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001081 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001082#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001083
Alexander Larsson1818e312013-05-22 14:41:31 +02001084 rect.width = width;
1085 rect.height = height;
1086
Pekka Paalanena4eda732012-11-19 17:16:02 +02001087 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001088 display_create_shm_surface(surface->display, &rect,
1089 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001090 leaf->resize_pool,
1091 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001092 if (!leaf->cairo_surface)
1093 return NULL;
1094
Pekka Paalanena4eda732012-11-19 17:16:02 +02001095 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001096 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001097
1098out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001099 surface->current = leaf;
1100
1101 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001102}
1103
1104static void
1105shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001106 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001107 struct rectangle *server_allocation)
1108{
1109 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001110 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001111
1112 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001113 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001114 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001115 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001116
Alexander Larsson1818e312013-05-22 14:41:31 +02001117 buffer_to_surface_size (buffer_transform, buffer_scale,
1118 &server_allocation->width,
1119 &server_allocation->height);
1120
Pekka Paalanena4eda732012-11-19 17:16:02 +02001121 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001122 surface->dx, surface->dy);
1123 wl_surface_damage(surface->surface, 0, 0,
1124 server_allocation->width, server_allocation->height);
1125 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001126
Pekka Paalanen71233882013-04-25 13:57:53 +03001127 DBG_OBJ(surface->surface, "leaf %d busy\n",
1128 (int)(leaf - &surface->leaf[0]));
1129
Pekka Paalanena4eda732012-11-19 17:16:02 +02001130 leaf->busy = 1;
1131 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001132}
1133
1134static int
1135shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1136{
1137 return -1;
1138}
1139
1140static void
1141shm_surface_release(struct toysurface *base)
1142{
1143}
1144
1145static void
1146shm_surface_destroy(struct toysurface *base)
1147{
1148 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001149 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001150
Pekka Paalanenaef02542013-04-25 13:57:47 +03001151 for (i = 0; i < MAX_LEAVES; i++)
1152 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001153
1154 free(surface);
1155}
1156
1157static struct toysurface *
1158shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1159 uint32_t flags, struct rectangle *rectangle)
1160{
1161 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001162 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001163
Brian Lovinbc919262013-08-07 15:34:59 -07001164 surface = xmalloc(sizeof *surface);
1165 memset(surface, 0, sizeof *surface);
1166
Pekka Paalanen99436862012-11-19 17:15:59 +02001167 if (!surface)
1168 return NULL;
1169
1170 surface->base.prepare = shm_surface_prepare;
1171 surface->base.swap = shm_surface_swap;
1172 surface->base.acquire = shm_surface_acquire;
1173 surface->base.release = shm_surface_release;
1174 surface->base.destroy = shm_surface_destroy;
1175
1176 surface->display = display;
1177 surface->surface = wl_surface;
1178 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001179
1180 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001181}
1182
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001183/*
1184 * The following correspondences between file names and cursors was copied
1185 * from: https://bugs.kde.org/attachment.cgi?id=67313
1186 */
1187
1188static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001189 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001190 "sw-resize",
1191 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001192};
1193
1194static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001195 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001196 "se-resize",
1197 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001198};
1199
1200static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001201 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001202 "s-resize",
1203 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001204};
1205
1206static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001207 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001208 "closedhand",
1209 "208530c400c041818281048008011002"
1210};
1211
1212static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001213 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001214 "default",
1215 "top_left_arrow",
1216 "left-arrow"
1217};
1218
1219static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001220 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001221 "w-resize",
1222 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001223};
1224
1225static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001226 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001227 "e-resize",
1228 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001229};
1230
1231static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001232 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001233 "nw-resize",
1234 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235};
1236
1237static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001238 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001239 "ne-resize",
1240 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001241};
1242
1243static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001244 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001245 "n-resize",
1246 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001247};
1248
1249static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001250 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001251 "ibeam",
1252 "text"
1253};
1254
1255static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001256 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001257 "pointer",
1258 "pointing_hand",
1259 "e29285e634086352946a0e7090d73106"
1260};
1261
1262static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001263 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001264 "wait",
1265 "0426c94ea35c87780ff01dc239897213"
1266};
1267
1268struct cursor_alternatives {
1269 const char **names;
1270 size_t count;
1271};
1272
1273static const struct cursor_alternatives cursors[] = {
1274 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1275 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1276 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1277 {grabbings, ARRAY_LENGTH(grabbings)},
1278 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1279 {left_sides, ARRAY_LENGTH(left_sides)},
1280 {right_sides, ARRAY_LENGTH(right_sides)},
1281 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1282 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1283 {top_sides, ARRAY_LENGTH(top_sides)},
1284 {xterms, ARRAY_LENGTH(xterms)},
1285 {hand1s, ARRAY_LENGTH(hand1s)},
1286 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001287};
1288
1289static void
1290create_cursors(struct display *display)
1291{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001292 struct weston_config *config;
1293 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001294 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001295 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001296 unsigned int i, j;
1297 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001298
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001299 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001300 s = weston_config_get_section(config, "shell", NULL, NULL);
1301 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1302 weston_config_section_get_int(s, "cursor-size", &size, 32);
1303 weston_config_destroy(config);
1304
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001305 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001306 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001307 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001308 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001309
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001310 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001311 cursor = NULL;
1312 for (j = 0; !cursor && j < cursors[i].count; ++j)
1313 cursor = wl_cursor_theme_get_cursor(
1314 display->cursor_theme, cursors[i].names[j]);
1315
1316 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001317 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001318 cursors[i].names[0]);
1319
1320 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001321 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001322}
1323
1324static void
1325destroy_cursors(struct display *display)
1326{
1327 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001328 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001329}
1330
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001331struct wl_cursor_image *
1332display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001333{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001334 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001335
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001336 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001337}
1338
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001339static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001340surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001341{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001342 if (!surface->cairo_surface)
1343 return;
1344
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001345 if (surface->opaque_region) {
1346 wl_surface_set_opaque_region(surface->surface,
1347 surface->opaque_region);
1348 wl_region_destroy(surface->opaque_region);
1349 surface->opaque_region = NULL;
1350 }
1351
1352 if (surface->input_region) {
1353 wl_surface_set_input_region(surface->surface,
1354 surface->input_region);
1355 wl_region_destroy(surface->input_region);
1356 surface->input_region = NULL;
1357 }
1358
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001359 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001360 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001361 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001362
Pekka Paalanen89dee002013-02-13 16:17:20 +02001363 cairo_surface_destroy(surface->cairo_surface);
1364 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001365}
1366
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001367int
1368window_has_focus(struct window *window)
1369{
1370 return window->focus_count > 0;
1371}
1372
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001373static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001374window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001375{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001376 struct surface *surface;
1377
Pekka Paalanen89dee002013-02-13 16:17:20 +02001378 if (window->type == TYPE_NONE) {
1379 window->type = TYPE_TOPLEVEL;
1380 if (window->shell_surface)
1381 wl_shell_surface_set_toplevel(window->shell_surface);
1382 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001383
Pekka Paalanen35e82632013-04-25 13:57:48 +03001384 wl_list_for_each(surface, &window->subsurface_list, link) {
1385 if (surface == window->main_surface)
1386 continue;
1387
1388 surface_flush(surface);
1389 }
1390
Pekka Paalanen89dee002013-02-13 16:17:20 +02001391 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001392}
1393
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001394struct display *
1395window_get_display(struct window *window)
1396{
1397 return window->display;
1398}
1399
Pekka Paalanen89dee002013-02-13 16:17:20 +02001400static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001401surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001402{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001403 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001404 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001405
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001406 if (!surface->toysurface && display->dpy &&
1407 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001408 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001409 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001410 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001411 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001412 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001413 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001414
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001415 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001416 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001417 surface->surface,
1418 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001419
Pekka Paalanen89dee002013-02-13 16:17:20 +02001420 surface->cairo_surface = surface->toysurface->prepare(
1421 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001422 allocation.width, allocation.height, flags,
1423 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001424}
1425
1426static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001427window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001428{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001429 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001430 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001431 int dx = 0;
1432 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001433
Pekka Paalanenec076692012-11-30 13:37:27 +02001434 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001435 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001436
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001437 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1438 flags |= SURFACE_HINT_RGB565;
1439
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001440 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1441 dx = surface->server_allocation.width -
1442 surface->allocation.width;
1443
1444 if (window->resize_edges & WINDOW_RESIZING_TOP)
1445 dy = surface->server_allocation.height -
1446 surface->allocation.height;
1447
1448 window->resize_edges = 0;
1449
Pekka Paalanen89dee002013-02-13 16:17:20 +02001450 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001451}
1452
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001453int
1454window_get_buffer_transform(struct window *window)
1455{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001456 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001457}
1458
1459void
1460window_set_buffer_transform(struct window *window,
1461 enum wl_output_transform transform)
1462{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001463 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001464 wl_surface_set_buffer_transform(window->main_surface->surface,
1465 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001466}
1467
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001468void
1469window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001470 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001471{
1472 window->main_surface->buffer_scale = scale;
1473 wl_surface_set_buffer_scale(window->main_surface->surface,
1474 scale);
1475}
1476
1477uint32_t
1478window_get_buffer_scale(struct window *window)
1479{
1480 return window->main_surface->buffer_scale;
1481}
1482
Alexander Larssond68f5232013-05-22 14:41:33 +02001483uint32_t
1484window_get_output_scale(struct window *window)
1485{
1486 struct window_output *window_output;
1487 struct window_output *window_output_tmp;
1488 int scale = 1;
1489
1490 wl_list_for_each_safe(window_output, window_output_tmp,
1491 &window->window_output_list, link) {
1492 if (window_output->output->scale > scale)
1493 scale = window_output->output->scale;
1494 }
1495
1496 return scale;
1497}
1498
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001499static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001500
Pekka Paalanen4e373742013-02-13 16:17:13 +02001501static void
1502surface_destroy(struct surface *surface)
1503{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001504 if (surface->frame_cb)
1505 wl_callback_destroy(surface->frame_cb);
1506
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001507 if (surface->input_region)
1508 wl_region_destroy(surface->input_region);
1509
1510 if (surface->opaque_region)
1511 wl_region_destroy(surface->opaque_region);
1512
Pekka Paalanen35e82632013-04-25 13:57:48 +03001513 if (surface->subsurface)
1514 wl_subsurface_destroy(surface->subsurface);
1515
Pekka Paalanen4e373742013-02-13 16:17:13 +02001516 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001517
1518 if (surface->toysurface)
1519 surface->toysurface->destroy(surface->toysurface);
1520
Pekka Paalanen35e82632013-04-25 13:57:48 +03001521 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001522 free(surface);
1523}
1524
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001525void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001526window_destroy(struct window *window)
1527{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001528 struct display *display = window->display;
1529 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001530 struct window_output *window_output;
1531 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001532
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001533 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001534
Rusty Lynch1084da52013-08-15 09:10:08 -07001535 wl_list_for_each(input, &display->input_list, link) {
1536 if (input->touch_focus == window)
1537 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001538 if (input->pointer_focus == window)
1539 input->pointer_focus = NULL;
1540 if (input->keyboard_focus == window)
1541 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001542 if (input->focus_widget &&
1543 input->focus_widget->window == window)
1544 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001545 }
1546
Rob Bradford7507b572012-05-15 17:55:34 +01001547 wl_list_for_each_safe(window_output, window_output_tmp,
1548 &window->window_output_list, link) {
1549 free (window_output);
1550 }
1551
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001552 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001553 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001554
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001555 if (window->shell_surface)
1556 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001557
1558 surface_destroy(window->main_surface);
1559
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001560 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001561
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001562 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001563 free(window);
1564}
1565
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001566static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001567widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001568{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001569 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001570
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001571 wl_list_for_each(child, &widget->child_list, link) {
1572 target = widget_find_widget(child, x, y);
1573 if (target)
1574 return target;
1575 }
1576
1577 if (widget->allocation.x <= x &&
1578 x < widget->allocation.x + widget->allocation.width &&
1579 widget->allocation.y <= y &&
1580 y < widget->allocation.y + widget->allocation.height) {
1581 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001582 }
1583
1584 return NULL;
1585}
1586
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001587static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001588window_find_widget(struct window *window, int32_t x, int32_t y)
1589{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001590 struct surface *surface;
1591 struct widget *widget;
1592
1593 wl_list_for_each(surface, &window->subsurface_list, link) {
1594 widget = widget_find_widget(surface->widget, x, y);
1595 if (widget)
1596 return widget;
1597 }
1598
1599 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001600}
1601
1602static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001603widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001604{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001605 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001606
Peter Huttererf3d62272013-08-08 11:57:05 +10001607 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001608 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001609 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001610 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001611 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001613 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001614 widget->tooltip = NULL;
1615 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001616 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001617 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001618
1619 return widget;
1620}
1621
1622struct widget *
1623window_add_widget(struct window *window, void *data)
1624{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001625 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001626
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001627 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001628 wl_list_init(&widget->link);
1629 window->main_surface->widget = widget;
1630
1631 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001632}
1633
1634struct widget *
1635widget_add_widget(struct widget *parent, void *data)
1636{
1637 struct widget *widget;
1638
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001639 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001640 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001641
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001642 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001643}
1644
1645void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001646widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001647{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001648 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001649 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001650 struct input *input;
1651
Pekka Paalanen35e82632013-04-25 13:57:48 +03001652 /* Destroy the sub-surface along with the root widget */
1653 if (surface->widget == widget && surface->subsurface)
1654 surface_destroy(widget->surface);
1655
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001656 if (widget->tooltip) {
1657 free(widget->tooltip);
1658 widget->tooltip = NULL;
1659 }
1660
Pekka Paalanene156fb62012-01-19 13:51:38 +02001661 wl_list_for_each(input, &display->input_list, link) {
1662 if (input->focus_widget == widget)
1663 input->focus_widget = NULL;
1664 }
1665
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001666 wl_list_remove(&widget->link);
1667 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001668}
1669
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001670void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001671widget_set_default_cursor(struct widget *widget, int cursor)
1672{
1673 widget->default_cursor = cursor;
1674}
1675
1676void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001677widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001678{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001679 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001680}
1681
1682void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001683widget_set_size(struct widget *widget, int32_t width, int32_t height)
1684{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001685 widget->allocation.width = width;
1686 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001687}
1688
1689void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001690widget_set_allocation(struct widget *widget,
1691 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001692{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001693 widget->allocation.x = x;
1694 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001695 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001696}
1697
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001698void
1699widget_set_transparent(struct widget *widget, int transparent)
1700{
1701 widget->opaque = !transparent;
1702}
1703
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001704void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001705widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001707 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708}
1709
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001710static cairo_surface_t *
1711widget_get_cairo_surface(struct widget *widget)
1712{
1713 struct surface *surface = widget->surface;
1714 struct window *window = widget->window;
1715
Neil Roberts97b747c2013-12-19 16:17:12 +00001716 assert(widget->use_cairo);
1717
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001718 if (!surface->cairo_surface) {
1719 if (surface == window->main_surface)
1720 window_create_main_surface(window);
1721 else
1722 surface_create_surface(surface, 0, 0, 0);
1723 }
1724
1725 return surface->cairo_surface;
1726}
1727
Alexander Larsson15901f02013-05-22 14:41:25 +02001728static void
1729widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1730{
1731 struct surface *surface = widget->surface;
1732 double angle;
1733 cairo_matrix_t m;
1734 enum wl_output_transform transform;
1735 int surface_width, surface_height;
1736 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001737 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001738
1739 surface_width = surface->allocation.width;
1740 surface_height = surface->allocation.height;
1741
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001742 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001743 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001744
Alexander Larsson15901f02013-05-22 14:41:25 +02001745 switch (transform) {
1746 case WL_OUTPUT_TRANSFORM_FLIPPED:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1748 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1749 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1750 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1751 break;
1752 default:
1753 cairo_matrix_init_identity(&m);
1754 break;
1755 }
1756
1757 switch (transform) {
1758 case WL_OUTPUT_TRANSFORM_NORMAL:
1759 default:
1760 angle = 0;
1761 translate_x = 0;
1762 translate_y = 0;
1763 break;
1764 case WL_OUTPUT_TRANSFORM_FLIPPED:
1765 angle = 0;
1766 translate_x = surface_width;
1767 translate_y = 0;
1768 break;
1769 case WL_OUTPUT_TRANSFORM_90:
1770 angle = M_PI_2;
1771 translate_x = surface_height;
1772 translate_y = 0;
1773 break;
1774 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1775 angle = M_PI_2;
1776 translate_x = surface_height;
1777 translate_y = surface_width;
1778 break;
1779 case WL_OUTPUT_TRANSFORM_180:
1780 angle = M_PI;
1781 translate_x = surface_width;
1782 translate_y = surface_height;
1783 break;
1784 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1785 angle = M_PI;
1786 translate_x = 0;
1787 translate_y = surface_height;
1788 break;
1789 case WL_OUTPUT_TRANSFORM_270:
1790 angle = M_PI + M_PI_2;
1791 translate_x = 0;
1792 translate_y = surface_width;
1793 break;
1794 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1795 angle = M_PI + M_PI_2;
1796 translate_x = 0;
1797 translate_y = 0;
1798 break;
1799 }
1800
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001801 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001802 cairo_translate(cr, translate_x, translate_y);
1803 cairo_rotate(cr, angle);
1804 cairo_transform(cr, &m);
1805}
1806
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001807cairo_t *
1808widget_cairo_create(struct widget *widget)
1809{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001810 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001811 cairo_surface_t *cairo_surface;
1812 cairo_t *cr;
1813
1814 cairo_surface = widget_get_cairo_surface(widget);
1815 cr = cairo_create(cairo_surface);
1816
Alexander Larsson15901f02013-05-22 14:41:25 +02001817 widget_cairo_update_transform(widget, cr);
1818
Pekka Paalanen35e82632013-04-25 13:57:48 +03001819 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1820
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001821 return cr;
1822}
1823
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001824struct wl_surface *
1825widget_get_wl_surface(struct widget *widget)
1826{
1827 return widget->surface->surface;
1828}
1829
Neil Roberts5e10a042013-09-09 00:40:17 +01001830struct wl_subsurface *
1831widget_get_wl_subsurface(struct widget *widget)
1832{
1833 return widget->surface->subsurface;
1834}
1835
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001836uint32_t
1837widget_get_last_time(struct widget *widget)
1838{
1839 return widget->surface->last_time;
1840}
1841
1842void
1843widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1844{
1845 struct wl_compositor *comp = widget->window->display->compositor;
1846 struct surface *surface = widget->surface;
1847
1848 if (!surface->input_region)
1849 surface->input_region = wl_compositor_create_region(comp);
1850
1851 if (rect) {
1852 wl_region_add(surface->input_region,
1853 rect->x, rect->y, rect->width, rect->height);
1854 }
1855}
1856
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001857void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001858widget_set_resize_handler(struct widget *widget,
1859 widget_resize_handler_t handler)
1860{
1861 widget->resize_handler = handler;
1862}
1863
1864void
1865widget_set_redraw_handler(struct widget *widget,
1866 widget_redraw_handler_t handler)
1867{
1868 widget->redraw_handler = handler;
1869}
1870
1871void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001872widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001873{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001874 widget->enter_handler = handler;
1875}
1876
1877void
1878widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1879{
1880 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001881}
1882
1883void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001884widget_set_motion_handler(struct widget *widget,
1885 widget_motion_handler_t handler)
1886{
1887 widget->motion_handler = handler;
1888}
1889
1890void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001891widget_set_button_handler(struct widget *widget,
1892 widget_button_handler_t handler)
1893{
1894 widget->button_handler = handler;
1895}
1896
1897void
Rusty Lynch041815a2013-08-08 21:20:38 -07001898widget_set_touch_up_handler(struct widget *widget,
1899 widget_touch_up_handler_t handler)
1900{
1901 widget->touch_up_handler = handler;
1902}
1903
1904void
1905widget_set_touch_down_handler(struct widget *widget,
1906 widget_touch_down_handler_t handler)
1907{
1908 widget->touch_down_handler = handler;
1909}
1910
1911void
1912widget_set_touch_motion_handler(struct widget *widget,
1913 widget_touch_motion_handler_t handler)
1914{
1915 widget->touch_motion_handler = handler;
1916}
1917
1918void
1919widget_set_touch_frame_handler(struct widget *widget,
1920 widget_touch_frame_handler_t handler)
1921{
1922 widget->touch_frame_handler = handler;
1923}
1924
1925void
1926widget_set_touch_cancel_handler(struct widget *widget,
1927 widget_touch_cancel_handler_t handler)
1928{
1929 widget->touch_cancel_handler = handler;
1930}
1931
1932void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001933widget_set_axis_handler(struct widget *widget,
1934 widget_axis_handler_t handler)
1935{
1936 widget->axis_handler = handler;
1937}
1938
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001939static void
1940window_schedule_redraw_task(struct window *window);
1941
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001942void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001943widget_schedule_redraw(struct widget *widget)
1944{
Pekka Paalanen71233882013-04-25 13:57:53 +03001945 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001946 widget->surface->redraw_needed = 1;
1947 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001948}
1949
Neil Roberts97b747c2013-12-19 16:17:12 +00001950void
1951widget_set_use_cairo(struct widget *widget,
1952 int use_cairo)
1953{
1954 widget->use_cairo = use_cairo;
1955}
1956
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001957cairo_surface_t *
1958window_get_surface(struct window *window)
1959{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001960 cairo_surface_t *cairo_surface;
1961
1962 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1963
1964 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001965}
1966
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001967struct wl_surface *
1968window_get_wl_surface(struct window *window)
1969{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001970 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001971}
1972
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001973static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001974tooltip_redraw_handler(struct widget *widget, void *data)
1975{
1976 cairo_t *cr;
1977 const int32_t r = 3;
1978 struct tooltip *tooltip = data;
1979 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001980
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001981 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001982 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1983 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1984 cairo_paint(cr);
1985
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001986 width = widget->allocation.width;
1987 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001988 rounded_rect(cr, 0, 0, width, height, r);
1989
1990 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1991 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1992 cairo_fill(cr);
1993
1994 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1995 cairo_move_to(cr, 10, 16);
1996 cairo_show_text(cr, tooltip->entry);
1997 cairo_destroy(cr);
1998}
1999
2000static cairo_text_extents_t
2001get_text_extents(struct tooltip *tooltip)
2002{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002003 cairo_t *cr;
2004 cairo_text_extents_t extents;
2005
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002006 /* Use the dummy_surface because tooltip's surface was not
2007 * created yet, and parent does not have a valid surface
2008 * outside repaint, either.
2009 */
2010 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002011 cairo_text_extents(cr, tooltip->entry, &extents);
2012 cairo_destroy(cr);
2013
2014 return extents;
2015}
2016
2017static int
2018window_create_tooltip(struct tooltip *tooltip)
2019{
2020 struct widget *parent = tooltip->parent;
2021 struct display *display = parent->window->display;
2022 struct window *window;
2023 const int offset_y = 27;
2024 const int margin = 3;
2025 cairo_text_extents_t extents;
2026
2027 if (tooltip->widget)
2028 return 0;
2029
2030 window = window_create_transient(display, parent->window, tooltip->x,
2031 tooltip->y + offset_y,
2032 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2033 if (!window)
2034 return -1;
2035
2036 tooltip->window = window;
2037 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2038
2039 extents = get_text_extents(tooltip);
2040 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2041 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2042
2043 return 0;
2044}
2045
2046void
2047widget_destroy_tooltip(struct widget *parent)
2048{
2049 struct tooltip *tooltip = parent->tooltip;
2050
2051 parent->tooltip_count = 0;
2052 if (!tooltip)
2053 return;
2054
2055 if (tooltip->widget) {
2056 widget_destroy(tooltip->widget);
2057 window_destroy(tooltip->window);
2058 tooltip->widget = NULL;
2059 tooltip->window = NULL;
2060 }
2061
2062 close(tooltip->tooltip_fd);
2063 free(tooltip->entry);
2064 free(tooltip);
2065 parent->tooltip = NULL;
2066}
2067
2068static void
2069tooltip_func(struct task *task, uint32_t events)
2070{
2071 struct tooltip *tooltip =
2072 container_of(task, struct tooltip, tooltip_task);
2073 uint64_t exp;
2074
Martin Olsson8df662a2012-07-08 03:03:47 +02002075 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2076 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002077 window_create_tooltip(tooltip);
2078}
2079
2080#define TOOLTIP_TIMEOUT 500
2081static int
2082tooltip_timer_reset(struct tooltip *tooltip)
2083{
2084 struct itimerspec its;
2085
2086 its.it_interval.tv_sec = 0;
2087 its.it_interval.tv_nsec = 0;
2088 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2089 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2090 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2091 fprintf(stderr, "could not set timerfd\n: %m");
2092 return -1;
2093 }
2094
2095 return 0;
2096}
2097
2098int
2099widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2100{
2101 struct tooltip *tooltip = parent->tooltip;
2102
2103 parent->tooltip_count++;
2104 if (tooltip) {
2105 tooltip->x = x;
2106 tooltip->y = y;
2107 tooltip_timer_reset(tooltip);
2108 return 0;
2109 }
2110
2111 /* the handler might be triggered too fast via input device motion, so
2112 * we need this check here to make sure tooltip is fully initialized */
2113 if (parent->tooltip_count > 1)
2114 return 0;
2115
2116 tooltip = malloc(sizeof *tooltip);
2117 if (!tooltip)
2118 return -1;
2119
2120 parent->tooltip = tooltip;
2121 tooltip->parent = parent;
2122 tooltip->widget = NULL;
2123 tooltip->window = NULL;
2124 tooltip->x = x;
2125 tooltip->y = y;
2126 tooltip->entry = strdup(entry);
2127 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2128 if (tooltip->tooltip_fd < 0) {
2129 fprintf(stderr, "could not create timerfd\n: %m");
2130 return -1;
2131 }
2132
2133 tooltip->tooltip_task.run = tooltip_func;
2134 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2135 EPOLLIN, &tooltip->tooltip_task);
2136 tooltip_timer_reset(tooltip);
2137
2138 return 0;
2139}
2140
2141static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002142workspace_manager_state(void *data,
2143 struct workspace_manager *workspace_manager,
2144 uint32_t current,
2145 uint32_t count)
2146{
2147 struct display *display = data;
2148
2149 display->workspace = current;
2150 display->workspace_count = count;
2151}
2152
2153static const struct workspace_manager_listener workspace_manager_listener = {
2154 workspace_manager_state
2155};
2156
2157static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002158frame_resize_handler(struct widget *widget,
2159 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002160{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002161 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002162 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002163 struct rectangle interior;
2164 struct rectangle input;
2165 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002166
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002167 if (widget->window->type == TYPE_FULLSCREEN) {
2168 interior.x = 0;
2169 interior.y = 0;
2170 interior.width = width;
2171 interior.height = height;
2172 } else {
2173 if (widget->window->type == TYPE_MAXIMIZED) {
2174 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2175 } else {
2176 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2177 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002178
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002179 frame_resize(frame->frame, width, height);
2180 frame_interior(frame->frame, &interior.x, &interior.y,
2181 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002182 }
2183
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002184 widget_set_allocation(child, interior.x, interior.y,
2185 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002186
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002187 if (child->resize_handler) {
2188 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002189 child->user_data);
2190
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002191 if (widget->window->type == TYPE_FULLSCREEN) {
2192 width = child->allocation.width;
2193 height = child->allocation.height;
2194 } else {
2195 frame_resize_inside(frame->frame,
2196 child->allocation.width,
2197 child->allocation.height);
2198 width = frame_width(frame->frame);
2199 height = frame_height(frame->frame);
2200 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002201 }
2202
Scott Moreauf7e498c2012-05-14 11:39:29 -06002203 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002204
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002205 widget->surface->input_region =
2206 wl_compositor_create_region(widget->window->display->compositor);
2207 if (widget->window->type != TYPE_FULLSCREEN) {
2208 frame_input_rect(frame->frame, &input.x, &input.y,
2209 &input.width, &input.height);
2210 wl_region_add(widget->surface->input_region,
2211 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002212 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002213 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002214 }
2215
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002216 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002217
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218 if (child->opaque) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002219 if (widget->window->type != TYPE_FULLSCREEN) {
2220 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2221 &opaque.width, &opaque.height);
2222
2223 wl_region_add(widget->surface->opaque_region,
2224 opaque.x, opaque.y,
2225 opaque.width, opaque.height);
2226 } else {
2227 wl_region_add(widget->surface->opaque_region,
2228 0, 0, width, height);
2229 }
Martin Minarik1998b152012-05-10 02:04:35 +02002230 }
2231
Martin Minarik1998b152012-05-10 02:04:35 +02002232
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002233 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002234}
2235
2236static void
2237frame_redraw_handler(struct widget *widget, void *data)
2238{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002239 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002240 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002241 struct window *window = widget->window;
2242
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002243 if (window->type == TYPE_FULLSCREEN)
2244 return;
2245
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002246 if (window->focus_count) {
2247 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2248 } else {
2249 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2250 }
2251
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002252 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002253
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002254 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002255
2256 cairo_destroy(cr);
2257}
2258
2259static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002260frame_get_pointer_image_for_location(struct window_frame *frame,
2261 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002262{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002263 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002264
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002265 if (window->type != TYPE_TOPLEVEL)
2266 return CURSOR_LEFT_PTR;
2267
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002268 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002269 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002270 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002271 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002272 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002273 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002274 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002275 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002276 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002277 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002278 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002279 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002280 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002281 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002282 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002283 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002284 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002285 case THEME_LOCATION_EXTERIOR:
2286 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002287 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002288 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002289 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002290}
2291
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002292static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002293frame_menu_func(struct window *window,
2294 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002295{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002296 struct display *display;
2297
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002298 switch (index) {
2299 case 0: /* close */
2300 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002301 window->close_handler(window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002302 else
2303 display_exit(window->display);
2304 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002305 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002306 display = window->display;
2307 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002308 workspace_manager_move_surface(
2309 display->workspace_manager,
2310 window->main_surface->surface,
2311 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002312 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002313 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002314 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002315 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002316 workspace_manager_move_surface(
2317 display->workspace_manager,
2318 window->main_surface->surface,
2319 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002320 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002321 case 3: /* fullscreen */
2322 /* we don't have a way to get out of fullscreen for now */
2323 if (window->fullscreen_handler)
2324 window->fullscreen_handler(window, window->user_data);
2325 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002326 }
2327}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002328
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002329void
2330window_show_frame_menu(struct window *window,
2331 struct input *input, uint32_t time)
2332{
2333 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002334 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002335
2336 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002337 "Close",
2338 "Move to workspace above", "Move to workspace below",
2339 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002340 };
2341
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002342 if (window->fullscreen_handler)
2343 count = ARRAY_LENGTH(entries);
2344 else
2345 count = ARRAY_LENGTH(entries) - 1;
2346
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002347 input_get_position(input, &x, &y);
2348 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002349 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002350}
2351
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002352static int
2353frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002354 struct input *input, 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_enter(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);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002364}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002365
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002366static int
2367frame_motion_handler(struct widget *widget,
2368 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002369 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002370{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002371 struct window_frame *frame = data;
2372 enum theme_location location;
2373
2374 location = frame_pointer_motion(frame->frame, input, x, y);
2375 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2376 widget_schedule_redraw(frame->widget);
2377
2378 return frame_get_pointer_image_for_location(data, location);
2379}
2380
2381static void
2382frame_leave_handler(struct widget *widget,
2383 struct input *input, void *data)
2384{
2385 struct window_frame *frame = data;
2386
2387 frame_pointer_leave(frame->frame, input);
2388 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2389 widget_schedule_redraw(frame->widget);
2390}
2391
2392static void
2393frame_handle_status(struct window_frame *frame, struct input *input,
2394 uint32_t time, enum theme_location location)
2395{
2396 struct window *window = frame->widget->window;
2397 uint32_t status;
2398
2399 status = frame_status(frame->frame);
2400 if (status & FRAME_STATUS_REPAINT)
2401 widget_schedule_redraw(frame->widget);
2402
2403 if (status & FRAME_STATUS_MINIMIZE)
2404 fprintf(stderr,"Minimize stub\n");
2405
2406 if (status & FRAME_STATUS_MENU) {
2407 window_show_frame_menu(window, input, time);
2408 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2409 }
2410
2411 if (status & FRAME_STATUS_MAXIMIZE) {
2412 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2413 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2414 }
2415
2416 if (status & FRAME_STATUS_CLOSE) {
2417 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002418 window->close_handler(window->user_data);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002419 else
2420 display_exit(window->display);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002421 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002422 }
2423
2424 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2425 input_ungrab(input);
2426 wl_shell_surface_move(window->shell_surface,
2427 input_get_seat(input),
2428 window->display->serial);
2429
2430 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2431 }
2432
2433 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2434 input_ungrab(input);
2435
2436 window->resizing = 1;
2437 wl_shell_surface_resize(window->shell_surface,
2438 input_get_seat(input),
2439 window->display->serial,
2440 location);
2441
2442 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2443 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002444}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002445
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002446static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002447frame_button_handler(struct widget *widget,
2448 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002449 uint32_t button, enum wl_pointer_button_state state,
2450 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002451
2452{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002453 struct window_frame *frame = data;
2454 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002455
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002456 location = frame_pointer_button(frame->frame, input, button, state);
2457 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002458}
2459
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002460static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002461frame_touch_down_handler(struct widget *widget, struct input *input,
2462 uint32_t serial, uint32_t time, int32_t id,
2463 float x, float y, void *data)
2464{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002465 struct window_frame *frame = data;
2466
2467 frame_touch_down(frame->frame, input, id, x, y);
2468 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2469}
2470
2471static void
2472frame_touch_up_handler(struct widget *widget,
2473 struct input *input, uint32_t serial, uint32_t time,
2474 int32_t id, void *data)
2475{
2476 struct window_frame *frame = data;
2477
2478 frame_touch_up(frame->frame, input, id);
2479 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002480}
2481
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002482struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002483window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002484{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002485 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002486 uint32_t buttons;
2487
2488 if (window->type == TYPE_CUSTOM) {
2489 buttons = FRAME_BUTTON_NONE;
2490 } else {
2491 buttons = FRAME_BUTTON_ALL;
2492 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002493
Peter Huttererf3d62272013-08-08 11:57:05 +10002494 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002495 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002496 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002497
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002498 frame->widget = window_add_widget(window, frame);
2499 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002500
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002501 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2502 widget_set_resize_handler(frame->widget, frame_resize_handler);
2503 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002504 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002505 widget_set_motion_handler(frame->widget, frame_motion_handler);
2506 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002507 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002508 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002509
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002510 window->frame = frame;
2511
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002512 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002513}
2514
Kristian Høgsberga1627922012-06-20 17:30:03 -04002515void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002516window_frame_set_child_size(struct widget *widget, int child_width,
2517 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002518{
2519 struct display *display = widget->window->display;
2520 struct theme *t = display->theme;
2521 int decoration_width, decoration_height;
2522 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002523 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002524
2525 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002526 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002527 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002528 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002529
2530 width = child_width + decoration_width;
2531 height = child_height + decoration_height;
2532 } else {
2533 width = child_width;
2534 height = child_height;
2535 }
2536
2537 window_schedule_resize(widget->window, width, height);
2538}
2539
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002540static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002541window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002542{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002543 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002544
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002545 /* frame->child must be destroyed by the application */
2546 widget_destroy(frame->widget);
2547 free(frame);
2548}
2549
2550static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002551input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002552 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002553{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002554 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002555 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002556
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002557 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002558 return;
2559
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002560 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002561 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002562 widget = old;
2563 if (input->grab)
2564 widget = input->grab;
2565 if (widget->leave_handler)
2566 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002567 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002568 }
2569
2570 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002571 widget = focus;
2572 if (input->grab)
2573 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002574 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002575 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002576 cursor = widget->enter_handler(focus, input, x, y,
2577 widget->user_data);
2578 else
2579 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002580
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002581 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002582 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002583}
2584
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002585void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002586touch_grab(struct input *input, int32_t touch_id)
2587{
2588 input->touch_grab = 1;
2589 input->touch_grab_id = touch_id;
2590}
2591
2592void
2593touch_ungrab(struct input *input)
2594{
2595 struct touch_point *tp, *tmp;
2596
2597 input->touch_grab = 0;
2598
2599 wl_list_for_each_safe(tp, tmp,
2600 &input->touch_point_list, link) {
2601 if (tp->id != input->touch_grab_id)
2602 continue;
2603 wl_list_remove(&tp->link);
2604 free(tp);
2605
2606 return;
2607 }
2608}
2609
2610void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002611input_grab(struct input *input, struct widget *widget, uint32_t button)
2612{
2613 input->grab = widget;
2614 input->grab_button = button;
2615}
2616
2617void
2618input_ungrab(struct input *input)
2619{
2620 struct widget *widget;
2621
2622 input->grab = NULL;
2623 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002624 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002625 input->sx, input->sy);
2626 input_set_focus_widget(input, widget, input->sx, input->sy);
2627 }
2628}
2629
2630static void
2631input_remove_pointer_focus(struct input *input)
2632{
2633 struct window *window = input->pointer_focus;
2634
2635 if (!window)
2636 return;
2637
2638 input_set_focus_widget(input, NULL, 0, 0);
2639
2640 input->pointer_focus = NULL;
2641 input->current_cursor = CURSOR_UNSET;
2642}
2643
2644static void
2645pointer_handle_enter(void *data, struct wl_pointer *pointer,
2646 uint32_t serial, struct wl_surface *surface,
2647 wl_fixed_t sx_w, wl_fixed_t sy_w)
2648{
2649 struct input *input = data;
2650 struct window *window;
2651 struct widget *widget;
2652 float sx = wl_fixed_to_double(sx_w);
2653 float sy = wl_fixed_to_double(sy_w);
2654
2655 if (!surface) {
2656 /* enter event for a window we've just destroyed */
2657 return;
2658 }
2659
2660 input->display->serial = serial;
2661 input->pointer_enter_serial = serial;
2662 input->pointer_focus = wl_surface_get_user_data(surface);
2663 window = input->pointer_focus;
2664
Pekka Paalanen99436862012-11-19 17:15:59 +02002665 if (window->resizing) {
2666 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002667 /* Schedule a redraw to free the pool */
2668 window_schedule_redraw(window);
2669 }
2670
2671 input->sx = sx;
2672 input->sy = sy;
2673
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002674 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002675 input_set_focus_widget(input, widget, sx, sy);
2676}
2677
2678static void
2679pointer_handle_leave(void *data, struct wl_pointer *pointer,
2680 uint32_t serial, struct wl_surface *surface)
2681{
2682 struct input *input = data;
2683
2684 input->display->serial = serial;
2685 input_remove_pointer_focus(input);
2686}
2687
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002688static void
Daniel Stone37816df2012-05-16 18:45:18 +01002689pointer_handle_motion(void *data, struct wl_pointer *pointer,
2690 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002691{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002692 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002693 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002694 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002695 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002696 float sx = wl_fixed_to_double(sx_w);
2697 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002698
Paul Winwoodb22bf572013-08-29 10:52:54 +01002699 input->sx = sx;
2700 input->sy = sy;
2701
2702 if (!window)
2703 return;
2704
Rob Bradford5f087742013-07-11 19:41:27 +01002705 /* when making the window smaller - e.g. after a unmaximise we might
2706 * still have a pending motion event that the compositor has picked
2707 * based on the old surface dimensions
2708 */
2709 if (sx > window->main_surface->allocation.width ||
2710 sy > window->main_surface->allocation.height)
2711 return;
2712
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002713 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002714 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002715 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002716 }
2717
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002718 if (input->grab)
2719 widget = input->grab;
2720 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002721 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002722 if (widget) {
2723 if (widget->motion_handler)
2724 cursor = widget->motion_handler(input->focus_widget,
2725 input, time, sx, sy,
2726 widget->user_data);
2727 else
2728 cursor = widget->default_cursor;
2729 } else
2730 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002731
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002732 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002733}
2734
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002735static void
Daniel Stone37816df2012-05-16 18:45:18 +01002736pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002737 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002738{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002739 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002740 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002741 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002742
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002743 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002744 if (input->focus_widget && input->grab == NULL &&
2745 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002746 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002747
Neil Roberts6b28aad2012-01-23 19:11:18 +00002748 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002749 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002750 (*widget->button_handler)(widget,
2751 input, time,
2752 button, state,
2753 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002754
Daniel Stone4dbadb12012-05-30 16:31:51 +01002755 if (input->grab && input->grab_button == button &&
2756 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002757 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002758}
2759
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002760static void
Daniel Stone37816df2012-05-16 18:45:18 +01002761pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002762 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002763{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002764 struct input *input = data;
2765 struct widget *widget;
2766
2767 widget = input->focus_widget;
2768 if (input->grab)
2769 widget = input->grab;
2770 if (widget && widget->axis_handler)
2771 (*widget->axis_handler)(widget,
2772 input, time,
2773 axis, value,
2774 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002775}
2776
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002777static const struct wl_pointer_listener pointer_listener = {
2778 pointer_handle_enter,
2779 pointer_handle_leave,
2780 pointer_handle_motion,
2781 pointer_handle_button,
2782 pointer_handle_axis,
2783};
2784
2785static void
2786input_remove_keyboard_focus(struct input *input)
2787{
2788 struct window *window = input->keyboard_focus;
2789 struct itimerspec its;
2790
2791 its.it_interval.tv_sec = 0;
2792 its.it_interval.tv_nsec = 0;
2793 its.it_value.tv_sec = 0;
2794 its.it_value.tv_nsec = 0;
2795 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2796
2797 if (!window)
2798 return;
2799
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002800 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002801 if (window->keyboard_focus_handler)
2802 (*window->keyboard_focus_handler)(window, NULL,
2803 window->user_data);
2804
2805 input->keyboard_focus = NULL;
2806}
2807
2808static void
2809keyboard_repeat_func(struct task *task, uint32_t events)
2810{
2811 struct input *input =
2812 container_of(task, struct input, repeat_task);
2813 struct window *window = input->keyboard_focus;
2814 uint64_t exp;
2815
2816 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2817 /* If we change the timer between the fd becoming
2818 * readable and getting here, there'll be nothing to
2819 * read and we get EAGAIN. */
2820 return;
2821
2822 if (window && window->key_handler) {
2823 (*window->key_handler)(window, input, input->repeat_time,
2824 input->repeat_key, input->repeat_sym,
2825 WL_KEYBOARD_KEY_STATE_PRESSED,
2826 window->user_data);
2827 }
2828}
2829
2830static void
2831keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2832 uint32_t format, int fd, uint32_t size)
2833{
2834 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002835 struct xkb_keymap *keymap;
2836 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002837 char *map_str;
2838
2839 if (!data) {
2840 close(fd);
2841 return;
2842 }
2843
2844 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2845 close(fd);
2846 return;
2847 }
2848
2849 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2850 if (map_str == MAP_FAILED) {
2851 close(fd);
2852 return;
2853 }
2854
Rui Matos3eccb862013-10-10 19:44:22 +02002855 keymap = xkb_map_new_from_string(input->display->xkb_context,
2856 map_str,
2857 XKB_KEYMAP_FORMAT_TEXT_V1,
2858 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002859 munmap(map_str, size);
2860 close(fd);
2861
Rui Matos3eccb862013-10-10 19:44:22 +02002862 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002863 fprintf(stderr, "failed to compile keymap\n");
2864 return;
2865 }
2866
Rui Matos3eccb862013-10-10 19:44:22 +02002867 state = xkb_state_new(keymap);
2868 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002869 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002870 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002871 return;
2872 }
2873
Rui Matos3eccb862013-10-10 19:44:22 +02002874 xkb_keymap_unref(input->xkb.keymap);
2875 xkb_state_unref(input->xkb.state);
2876 input->xkb.keymap = keymap;
2877 input->xkb.state = state;
2878
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002879 input->xkb.control_mask =
2880 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2881 input->xkb.alt_mask =
2882 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2883 input->xkb.shift_mask =
2884 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2885}
2886
2887static void
2888keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2889 uint32_t serial, struct wl_surface *surface,
2890 struct wl_array *keys)
2891{
2892 struct input *input = data;
2893 struct window *window;
2894
2895 input->display->serial = serial;
2896 input->keyboard_focus = wl_surface_get_user_data(surface);
2897
2898 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002899 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002900 if (window->keyboard_focus_handler)
2901 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002902 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002903}
2904
2905static void
2906keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2907 uint32_t serial, struct wl_surface *surface)
2908{
2909 struct input *input = data;
2910
2911 input->display->serial = serial;
2912 input_remove_keyboard_focus(input);
2913}
2914
Scott Moreau210d0792012-03-22 10:47:01 -06002915static void
Daniel Stone37816df2012-05-16 18:45:18 +01002916keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002917 uint32_t serial, uint32_t time, uint32_t key,
2918 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002919{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002920 struct input *input = data;
2921 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002922 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002923 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002924 const xkb_keysym_t *syms;
2925 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002926 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002927
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002928 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002929 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002930 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002931 return;
2932
Daniel Stone97f68542012-05-30 16:32:01 +01002933 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002934
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002935 sym = XKB_KEY_NoSymbol;
2936 if (num_syms == 1)
2937 sym = syms[0];
2938
2939 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002940 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002941 window_set_maximized(window,
2942 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002943 } else if (sym == XKB_KEY_F11 &&
2944 window->fullscreen_handler &&
2945 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2946 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002947 } else if (sym == XKB_KEY_F4 &&
2948 input->modifiers == MOD_ALT_MASK &&
2949 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2950 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002951 window->close_handler(window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002952 else
2953 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002954 } else if (window->key_handler) {
2955 (*window->key_handler)(window, input, time, key,
2956 sym, state, window->user_data);
2957 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002958
2959 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2960 key == input->repeat_key) {
2961 its.it_interval.tv_sec = 0;
2962 its.it_interval.tv_nsec = 0;
2963 its.it_value.tv_sec = 0;
2964 its.it_value.tv_nsec = 0;
2965 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2966 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2967 input->repeat_sym = sym;
2968 input->repeat_key = key;
2969 input->repeat_time = time;
2970 its.it_interval.tv_sec = 0;
2971 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2972 its.it_value.tv_sec = 0;
2973 its.it_value.tv_nsec = 400 * 1000 * 1000;
2974 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2975 }
2976}
2977
2978static void
Daniel Stone351eb612012-05-31 15:27:47 -04002979keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2980 uint32_t serial, uint32_t mods_depressed,
2981 uint32_t mods_latched, uint32_t mods_locked,
2982 uint32_t group)
2983{
2984 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002985 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002986
Matt Ropere61561f2013-06-24 16:52:43 +01002987 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2988 if (!input->xkb.keymap)
2989 return;
2990
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002991 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2992 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002993 mask = xkb_state_serialize_mods(input->xkb.state,
2994 XKB_STATE_DEPRESSED |
2995 XKB_STATE_LATCHED);
2996 input->modifiers = 0;
2997 if (mask & input->xkb.control_mask)
2998 input->modifiers |= MOD_CONTROL_MASK;
2999 if (mask & input->xkb.alt_mask)
3000 input->modifiers |= MOD_ALT_MASK;
3001 if (mask & input->xkb.shift_mask)
3002 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003003}
3004
Daniel Stone37816df2012-05-16 18:45:18 +01003005static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003006 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003007 keyboard_handle_enter,
3008 keyboard_handle_leave,
3009 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003010 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003011};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003012
3013static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003014touch_handle_down(void *data, struct wl_touch *wl_touch,
3015 uint32_t serial, uint32_t time, struct wl_surface *surface,
3016 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3017{
3018 struct input *input = data;
3019 struct widget *widget;
3020 float sx = wl_fixed_to_double(x_w);
3021 float sy = wl_fixed_to_double(y_w);
3022
Rusty Lynch1084da52013-08-15 09:10:08 -07003023 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003024 input->touch_focus = wl_surface_get_user_data(surface);
3025 if (!input->touch_focus) {
3026 DBG("Failed to find to touch focus for surface %p\n", surface);
3027 return;
3028 }
3029
3030 widget = window_find_widget(input->touch_focus,
3031 wl_fixed_to_double(x_w),
3032 wl_fixed_to_double(y_w));
3033 if (widget) {
3034 struct touch_point *tp = xmalloc(sizeof *tp);
3035 if (tp) {
3036 tp->id = id;
3037 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003038 tp->x = sx;
3039 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003040 wl_list_insert(&input->touch_point_list, &tp->link);
3041
3042 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003043 (*widget->touch_down_handler)(widget, input,
3044 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003045 sx, sy,
3046 widget->user_data);
3047 }
3048 }
3049}
3050
3051static void
3052touch_handle_up(void *data, struct wl_touch *wl_touch,
3053 uint32_t serial, uint32_t time, int32_t id)
3054{
3055 struct input *input = data;
3056 struct touch_point *tp, *tmp;
3057
Rusty Lynch041815a2013-08-08 21:20:38 -07003058 if (!input->touch_focus) {
3059 DBG("No touch focus found for touch up event!\n");
3060 return;
3061 }
3062
3063 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3064 if (tp->id != id)
3065 continue;
3066
3067 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003068 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003069 time, id,
3070 tp->widget->user_data);
3071
3072 wl_list_remove(&tp->link);
3073 free(tp);
3074
3075 return;
3076 }
3077}
3078
3079static void
3080touch_handle_motion(void *data, struct wl_touch *wl_touch,
3081 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3082{
3083 struct input *input = data;
3084 struct touch_point *tp;
3085 float sx = wl_fixed_to_double(x_w);
3086 float sy = wl_fixed_to_double(y_w);
3087
3088 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3089
3090 if (!input->touch_focus) {
3091 DBG("No touch focus found for touch motion event!\n");
3092 return;
3093 }
3094
3095 wl_list_for_each(tp, &input->touch_point_list, link) {
3096 if (tp->id != id)
3097 continue;
3098
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003099 tp->x = sx;
3100 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003101 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003102 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003103 id, sx, sy,
3104 tp->widget->user_data);
3105 return;
3106 }
3107}
3108
3109static void
3110touch_handle_frame(void *data, struct wl_touch *wl_touch)
3111{
3112 struct input *input = data;
3113 struct touch_point *tp, *tmp;
3114
3115 DBG("touch_handle_frame\n");
3116
3117 if (!input->touch_focus) {
3118 DBG("No touch focus found for touch frame event!\n");
3119 return;
3120 }
3121
3122 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3123 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003124 (*tp->widget->touch_frame_handler)(tp->widget, input,
3125 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003126
3127 wl_list_remove(&tp->link);
3128 free(tp);
3129 }
3130}
3131
3132static void
3133touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3134{
3135 struct input *input = data;
3136 struct touch_point *tp, *tmp;
3137
3138 DBG("touch_handle_cancel\n");
3139
3140 if (!input->touch_focus) {
3141 DBG("No touch focus found for touch cancel event!\n");
3142 return;
3143 }
3144
3145 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3146 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003147 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3148 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003149
3150 wl_list_remove(&tp->link);
3151 free(tp);
3152 }
3153}
3154
3155static const struct wl_touch_listener touch_listener = {
3156 touch_handle_down,
3157 touch_handle_up,
3158 touch_handle_motion,
3159 touch_handle_frame,
3160 touch_handle_cancel,
3161};
3162
3163static void
Daniel Stone37816df2012-05-16 18:45:18 +01003164seat_handle_capabilities(void *data, struct wl_seat *seat,
3165 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003166{
Daniel Stone37816df2012-05-16 18:45:18 +01003167 struct input *input = data;
3168
3169 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3170 input->pointer = wl_seat_get_pointer(seat);
3171 wl_pointer_set_user_data(input->pointer, input);
3172 wl_pointer_add_listener(input->pointer, &pointer_listener,
3173 input);
3174 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3175 wl_pointer_destroy(input->pointer);
3176 input->pointer = NULL;
3177 }
3178
3179 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3180 input->keyboard = wl_seat_get_keyboard(seat);
3181 wl_keyboard_set_user_data(input->keyboard, input);
3182 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3183 input);
3184 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3185 wl_keyboard_destroy(input->keyboard);
3186 input->keyboard = NULL;
3187 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003188
3189 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3190 input->touch = wl_seat_get_touch(seat);
3191 wl_touch_set_user_data(input->touch, input);
3192 wl_touch_add_listener(input->touch, &touch_listener, input);
3193 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3194 wl_touch_destroy(input->touch);
3195 input->touch = NULL;
3196 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003197}
3198
Rob Bradford08031182013-08-13 20:11:03 +01003199static void
3200seat_handle_name(void *data, struct wl_seat *seat,
3201 const char *name)
3202{
3203
3204}
3205
Daniel Stone37816df2012-05-16 18:45:18 +01003206static const struct wl_seat_listener seat_listener = {
3207 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003208 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003209};
3210
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003211void
3212input_get_position(struct input *input, int32_t *x, int32_t *y)
3213{
3214 *x = input->sx;
3215 *y = input->sy;
3216}
3217
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003218int
3219input_get_touch(struct input *input, int32_t id, float *x, float *y)
3220{
3221 struct touch_point *tp;
3222
3223 wl_list_for_each(tp, &input->touch_point_list, link) {
3224 if (tp->id != id)
3225 continue;
3226
3227 *x = tp->x;
3228 *y = tp->y;
3229 return 0;
3230 }
3231
3232 return -1;
3233}
3234
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003235struct display *
3236input_get_display(struct input *input)
3237{
3238 return input->display;
3239}
3240
Daniel Stone37816df2012-05-16 18:45:18 +01003241struct wl_seat *
3242input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003243{
Daniel Stone37816df2012-05-16 18:45:18 +01003244 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003245}
3246
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003247uint32_t
3248input_get_modifiers(struct input *input)
3249{
3250 return input->modifiers;
3251}
3252
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003253struct widget *
3254input_get_focus_widget(struct input *input)
3255{
3256 return input->focus_widget;
3257}
3258
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003259struct data_offer {
3260 struct wl_data_offer *offer;
3261 struct input *input;
3262 struct wl_array types;
3263 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003264
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003265 struct task io_task;
3266 int fd;
3267 data_func_t func;
3268 int32_t x, y;
3269 void *user_data;
3270};
3271
3272static void
3273data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3274{
3275 struct data_offer *offer = data;
3276 char **p;
3277
3278 p = wl_array_add(&offer->types, sizeof *p);
3279 *p = strdup(type);
3280}
3281
3282static const struct wl_data_offer_listener data_offer_listener = {
3283 data_offer_offer,
3284};
3285
3286static void
3287data_offer_destroy(struct data_offer *offer)
3288{
3289 char **p;
3290
3291 offer->refcount--;
3292 if (offer->refcount == 0) {
3293 wl_data_offer_destroy(offer->offer);
3294 for (p = offer->types.data; *p; p++)
3295 free(*p);
3296 wl_array_release(&offer->types);
3297 free(offer);
3298 }
3299}
3300
3301static void
3302data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003303 struct wl_data_device *data_device,
3304 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003305{
3306 struct data_offer *offer;
3307
Brian Lovinbc919262013-08-07 15:34:59 -07003308 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003309
3310 wl_array_init(&offer->types);
3311 offer->refcount = 1;
3312 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003313 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003314 wl_data_offer_add_listener(offer->offer,
3315 &data_offer_listener, offer);
3316}
3317
3318static void
3319data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003320 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003321 wl_fixed_t x_w, wl_fixed_t y_w,
3322 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003323{
3324 struct input *input = data;
3325 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003326 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003327 float x = wl_fixed_to_double(x_w);
3328 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003329 char **p;
3330
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003331 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003332 input->drag_enter_serial = serial;
3333 input->drag_focus = window,
3334 input->drag_x = x;
3335 input->drag_y = y;
3336
3337 if (!input->touch_grab)
3338 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003339
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003340 if (offer) {
3341 input->drag_offer = wl_data_offer_get_user_data(offer);
3342
3343 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3344 *p = NULL;
3345
3346 types_data = input->drag_offer->types.data;
3347 } else {
3348 input->drag_offer = NULL;
3349 types_data = NULL;
3350 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003351
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003352 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003353 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003354 window->user_data);
3355}
3356
3357static void
3358data_device_leave(void *data, struct wl_data_device *data_device)
3359{
3360 struct input *input = data;
3361
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003362 if (input->drag_offer) {
3363 data_offer_destroy(input->drag_offer);
3364 input->drag_offer = NULL;
3365 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003366}
3367
3368static void
3369data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003370 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003371{
3372 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003373 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003374 float x = wl_fixed_to_double(x_w);
3375 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003376 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003377
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003378 input->drag_x = x;
3379 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003380
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003381 if (input->drag_offer)
3382 types_data = input->drag_offer->types.data;
3383 else
3384 types_data = NULL;
3385
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003386 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003387 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003388 window->user_data);
3389}
3390
3391static void
3392data_device_drop(void *data, struct wl_data_device *data_device)
3393{
3394 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003395 struct window *window = input->drag_focus;
3396 float x, y;
3397
3398 x = input->drag_x;
3399 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003400
3401 if (window->drop_handler)
3402 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003403 x, y, window->user_data);
3404
3405 if (input->touch_grab)
3406 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003407}
3408
3409static void
3410data_device_selection(void *data,
3411 struct wl_data_device *wl_data_device,
3412 struct wl_data_offer *offer)
3413{
3414 struct input *input = data;
3415 char **p;
3416
3417 if (input->selection_offer)
3418 data_offer_destroy(input->selection_offer);
3419
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003420 if (offer) {
3421 input->selection_offer = wl_data_offer_get_user_data(offer);
3422 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3423 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003424 } else {
3425 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003426 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003427}
3428
3429static const struct wl_data_device_listener data_device_listener = {
3430 data_device_data_offer,
3431 data_device_enter,
3432 data_device_leave,
3433 data_device_motion,
3434 data_device_drop,
3435 data_device_selection
3436};
3437
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003438static void
3439input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003440{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003441 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003442 struct wl_cursor *cursor;
3443 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003444
Daniel Stone80972742012-11-07 17:51:39 +11003445 if (!input->pointer)
3446 return;
3447
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003448 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003449 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003450 return;
3451
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003452 if (index >= (int) cursor->image_count) {
3453 fprintf(stderr, "cursor index out of range\n");
3454 return;
3455 }
3456
3457 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003458 buffer = wl_cursor_image_get_buffer(image);
3459 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003460 return;
3461
Kristian Høgsbergae277372012-08-01 09:41:08 -04003462 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003463 input->pointer_surface,
3464 image->hotspot_x, image->hotspot_y);
3465 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3466 wl_surface_damage(input->pointer_surface, 0, 0,
3467 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003468 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003469}
3470
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003471static const struct wl_callback_listener pointer_surface_listener;
3472
3473static void
3474pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3475 uint32_t time)
3476{
3477 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003478 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003479 int i;
3480
3481 if (callback) {
3482 assert(callback == input->cursor_frame_cb);
3483 wl_callback_destroy(callback);
3484 input->cursor_frame_cb = NULL;
3485 }
3486
Daniel Stone80972742012-11-07 17:51:39 +11003487 if (!input->pointer)
3488 return;
3489
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003490 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003491 wl_pointer_set_cursor(input->pointer,
3492 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003493 NULL, 0, 0);
3494 return;
3495 }
3496
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003497 if (input->current_cursor == CURSOR_UNSET)
3498 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003499 cursor = input->display->cursors[input->current_cursor];
3500 if (!cursor)
3501 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003502
3503 /* FIXME We don't have the current time on the first call so we set
3504 * the animation start to the time of the first frame callback. */
3505 if (time == 0)
3506 input->cursor_anim_start = 0;
3507 else if (input->cursor_anim_start == 0)
3508 input->cursor_anim_start = time;
3509
3510 if (time == 0 || input->cursor_anim_start == 0)
3511 i = 0;
3512 else
3513 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3514
Pekka Paalanenbc106382012-10-10 12:49:31 +03003515 if (cursor->image_count > 1) {
3516 input->cursor_frame_cb =
3517 wl_surface_frame(input->pointer_surface);
3518 wl_callback_add_listener(input->cursor_frame_cb,
3519 &pointer_surface_listener, input);
3520 }
3521
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003522 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003523}
3524
3525static const struct wl_callback_listener pointer_surface_listener = {
3526 pointer_surface_frame_callback
3527};
3528
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003529void
3530input_set_pointer_image(struct input *input, int pointer)
3531{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003532 int force = 0;
3533
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003534 if (!input->pointer)
3535 return;
3536
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003537 if (input->pointer_enter_serial > input->cursor_serial)
3538 force = 1;
3539
3540 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003541 return;
3542
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003543 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003544 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003545 if (!input->cursor_frame_cb)
3546 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003547 else if (force) {
3548 /* The current frame callback may be stuck if, for instance,
3549 * the set cursor request was processed by the server after
3550 * this client lost the focus. In this case the cursor surface
3551 * might not be mapped and the frame callback wouldn't ever
3552 * complete. Send a set_cursor and attach to try to map the
3553 * cursor surface again so that the callback will finish */
3554 input_set_pointer_image_index(input, 0);
3555 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003556}
3557
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003558struct wl_data_device *
3559input_get_data_device(struct input *input)
3560{
3561 return input->data_device;
3562}
3563
3564void
3565input_set_selection(struct input *input,
3566 struct wl_data_source *source, uint32_t time)
3567{
3568 wl_data_device_set_selection(input->data_device, source, time);
3569}
3570
3571void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003572input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003573{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003574 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003575 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003576}
3577
3578static void
3579offer_io_func(struct task *task, uint32_t events)
3580{
3581 struct data_offer *offer =
3582 container_of(task, struct data_offer, io_task);
3583 unsigned int len;
3584 char buffer[4096];
3585
3586 len = read(offer->fd, buffer, sizeof buffer);
3587 offer->func(buffer, len,
3588 offer->x, offer->y, offer->user_data);
3589
3590 if (len == 0) {
3591 close(offer->fd);
3592 data_offer_destroy(offer);
3593 }
3594}
3595
3596static void
3597data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3598 data_func_t func, void *user_data)
3599{
3600 int p[2];
3601
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003602 if (pipe2(p, O_CLOEXEC) == -1)
3603 return;
3604
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003605 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3606 close(p[1]);
3607
3608 offer->io_task.run = offer_io_func;
3609 offer->fd = p[0];
3610 offer->func = func;
3611 offer->refcount++;
3612 offer->user_data = user_data;
3613
3614 display_watch_fd(offer->input->display,
3615 offer->fd, EPOLLIN, &offer->io_task);
3616}
3617
3618void
3619input_receive_drag_data(struct input *input, const char *mime_type,
3620 data_func_t func, void *data)
3621{
3622 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003623 input->drag_offer->x = input->drag_x;
3624 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003625}
3626
3627int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003628input_receive_drag_data_to_fd(struct input *input,
3629 const char *mime_type, int fd)
3630{
3631 if (input->drag_offer)
3632 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3633
3634 return 0;
3635}
3636
3637int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003638input_receive_selection_data(struct input *input, const char *mime_type,
3639 data_func_t func, void *data)
3640{
3641 char **p;
3642
3643 if (input->selection_offer == NULL)
3644 return -1;
3645
3646 for (p = input->selection_offer->types.data; *p; p++)
3647 if (strcmp(mime_type, *p) == 0)
3648 break;
3649
3650 if (*p == NULL)
3651 return -1;
3652
3653 data_offer_receive_data(input->selection_offer,
3654 mime_type, func, data);
3655 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003656}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003657
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003658int
3659input_receive_selection_data_to_fd(struct input *input,
3660 const char *mime_type, int fd)
3661{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003662 if (input->selection_offer)
3663 wl_data_offer_receive(input->selection_offer->offer,
3664 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003665
3666 return 0;
3667}
3668
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003669void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003670window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003671{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003672 if (!window->shell_surface)
3673 return;
3674
Daniel Stone37816df2012-05-16 18:45:18 +01003675 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003676}
3677
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003678static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003679surface_set_synchronized(struct surface *surface)
3680{
3681 if (!surface->subsurface)
3682 return;
3683
3684 if (surface->synchronized)
3685 return;
3686
3687 wl_subsurface_set_sync(surface->subsurface);
3688 surface->synchronized = 1;
3689}
3690
3691static void
3692surface_set_synchronized_default(struct surface *surface)
3693{
3694 if (!surface->subsurface)
3695 return;
3696
3697 if (surface->synchronized == surface->synchronized_default)
3698 return;
3699
3700 if (surface->synchronized_default)
3701 wl_subsurface_set_sync(surface->subsurface);
3702 else
3703 wl_subsurface_set_desync(surface->subsurface);
3704
3705 surface->synchronized = surface->synchronized_default;
3706}
3707
3708static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003709surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003710{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003711 struct widget *widget = surface->widget;
3712 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003713
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003714 if (surface->input_region) {
3715 wl_region_destroy(surface->input_region);
3716 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003717 }
3718
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003719 if (surface->opaque_region)
3720 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003721
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003722 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003723
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003724 if (widget->resize_handler)
3725 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003726 widget->allocation.width,
3727 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003728 widget->user_data);
3729
Pekka Paalanen35e82632013-04-25 13:57:48 +03003730 if (surface->subsurface &&
3731 (surface->allocation.x != widget->allocation.x ||
3732 surface->allocation.y != widget->allocation.y)) {
3733 wl_subsurface_set_position(surface->subsurface,
3734 widget->allocation.x,
3735 widget->allocation.y);
3736 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003737 if (surface->allocation.width != widget->allocation.width ||
3738 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003739 window_schedule_redraw(widget->window);
3740 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003741 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003742
3743 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003744 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003745 widget->allocation.width,
3746 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003747}
3748
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003749static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003750hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3751{
3752 /*
3753 * This hack should be removed, when EGL respects
3754 * eglSwapInterval(0).
3755 *
3756 * If this window has sub-surfaces, especially a free-running
3757 * EGL-widget, we need to post the parent surface once with
3758 * all the old state to guarantee, that the EGL-widget will
3759 * receive its frame callback soon. Otherwise, a forced call
3760 * to eglSwapBuffers may end up blocking, waiting for a frame
3761 * event that will never come, because we will commit the parent
3762 * surface with all new state only after eglSwapBuffers returns.
3763 *
3764 * This assumes, that:
3765 * 1. When the EGL widget's resize hook is called, it pauses.
3766 * 2. When the EGL widget's redraw hook is called, it forces a
3767 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3768 * In a single threaded application condition 1 is a no-op.
3769 *
3770 * XXX: This should actually be after the surface_resize() calls,
3771 * but cannot, because then it would commit the incomplete state
3772 * accumulated from the widget resize hooks.
3773 */
3774 if (window->subsurface_list.next != &window->main_surface->link ||
3775 window->subsurface_list.prev != &window->main_surface->link)
3776 wl_surface_commit(window->main_surface->surface);
3777}
3778
3779static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003780window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003781{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003782 struct surface *surface;
3783
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003784 widget_set_allocation(window->main_surface->widget,
3785 window->pending_allocation.x,
3786 window->pending_allocation.y,
3787 window->pending_allocation.width,
3788 window->pending_allocation.height);
3789
3790 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003791
3792 /* The main surface is in the list, too. Main surface's
3793 * resize_handler is responsible for calling widget_set_allocation()
3794 * on all sub-surface root widgets, so they will be resized
3795 * properly.
3796 */
3797 wl_list_for_each(surface, &window->subsurface_list, link) {
3798 if (surface == window->main_surface)
3799 continue;
3800
3801 surface_set_synchronized(surface);
3802 surface_resize(surface);
3803 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003804}
3805
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003806static void
3807idle_resize(struct window *window)
3808{
3809 window->resize_needed = 0;
3810 window->redraw_needed = 1;
3811
3812 DBG("from %dx%d to %dx%d\n",
3813 window->main_surface->server_allocation.width,
3814 window->main_surface->server_allocation.height,
3815 window->pending_allocation.width,
3816 window->pending_allocation.height);
3817
3818 hack_prevent_EGL_sub_surface_deadlock(window);
3819
3820 window_do_resize(window);
3821}
3822
3823static void
3824undo_resize(struct window *window)
3825{
3826 window->pending_allocation.width =
3827 window->main_surface->server_allocation.width;
3828 window->pending_allocation.height =
3829 window->main_surface->server_allocation.height;
3830
3831 DBG("back to %dx%d\n",
3832 window->main_surface->server_allocation.width,
3833 window->main_surface->server_allocation.height);
3834
3835 window_do_resize(window);
3836
3837 if (window->pending_allocation.width == 0 &&
3838 window->pending_allocation.height == 0) {
3839 fprintf(stderr, "Error: Could not draw a surface, "
3840 "most likely due to insufficient disk space in "
3841 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3842 exit(EXIT_FAILURE);
3843 }
3844}
3845
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003846void
3847window_schedule_resize(struct window *window, int width, int height)
3848{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003849 /* We should probably get these numbers from the theme. */
3850 const int min_width = 200, min_height = 200;
3851
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003852 window->pending_allocation.x = 0;
3853 window->pending_allocation.y = 0;
3854 window->pending_allocation.width = width;
3855 window->pending_allocation.height = height;
3856
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003857 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003858 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003859 window->min_allocation.width = min_width;
3860 else
3861 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003862 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003863 window->min_allocation.height = min_height;
3864 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003865 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003866 }
3867
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003868 if (window->pending_allocation.width < window->min_allocation.width)
3869 window->pending_allocation.width = window->min_allocation.width;
3870 if (window->pending_allocation.height < window->min_allocation.height)
3871 window->pending_allocation.height = window->min_allocation.height;
3872
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003873 window->resize_needed = 1;
3874 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003875}
3876
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003877void
3878widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3879{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003880 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003881}
3882
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003883static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003884handle_ping(void *data, struct wl_shell_surface *shell_surface,
3885 uint32_t serial)
3886{
3887 wl_shell_surface_pong(shell_surface, serial);
3888}
3889
3890static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003891handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003892 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003893{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003894 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003895
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003896 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003897 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003898}
3899
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003900static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003901menu_destroy(struct menu *menu)
3902{
3903 widget_destroy(menu->widget);
3904 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003905 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003906 free(menu);
3907}
3908
3909static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003910handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3911{
3912 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003913 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003914
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003915 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003916 * device. Or just use wl_callback. And this really needs to
3917 * be a window vfunc that the menu can set. And we need the
3918 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003919
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003920 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003921 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003922}
3923
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003924static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003925 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003926 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003927 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003928};
3929
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003930void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003931window_get_allocation(struct window *window,
3932 struct rectangle *allocation)
3933{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003934 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003935}
3936
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003937static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003938widget_redraw(struct widget *widget)
3939{
3940 struct widget *child;
3941
3942 if (widget->redraw_handler)
3943 widget->redraw_handler(widget, widget->user_data);
3944 wl_list_for_each(child, &widget->child_list, link)
3945 widget_redraw(child);
3946}
3947
3948static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003949frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3950{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003951 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003952
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003953 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003954 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003955 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003956 surface->frame_cb = NULL;
3957
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003958 surface->last_time = time;
3959
Pekka Paalanen71233882013-04-25 13:57:53 +03003960 if (surface->redraw_needed || surface->window->redraw_needed) {
3961 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003962 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003963 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003964}
3965
3966static const struct wl_callback_listener listener = {
3967 frame_callback
3968};
3969
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003970static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003971surface_redraw(struct surface *surface)
3972{
Pekka Paalanen71233882013-04-25 13:57:53 +03003973 DBG_OBJ(surface->surface, "begin\n");
3974
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003975 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003976 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003977
3978 /* Whole-window redraw forces a redraw even if the previous has
3979 * not yet hit the screen.
3980 */
3981 if (surface->frame_cb) {
3982 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003983 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003984
Pekka Paalanen71233882013-04-25 13:57:53 +03003985 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003986 wl_callback_destroy(surface->frame_cb);
3987 }
3988
Neil Roberts97b747c2013-12-19 16:17:12 +00003989 if (surface->widget->use_cairo &&
3990 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003991 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3992 return -1;
3993 }
3994
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003995 surface->frame_cb = wl_surface_frame(surface->surface);
3996 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003997 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003998
3999 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004000 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004001 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004002 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004003 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004004}
4005
4006static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004007idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004008{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004009 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004010 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004011 int failed = 0;
4012 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004013
Pekka Paalanen71233882013-04-25 13:57:53 +03004014 DBG(" --------- \n");
4015
Pekka Paalaneneebff542013-04-25 13:57:52 +03004016 wl_list_init(&window->redraw_task.link);
4017 window->redraw_task_scheduled = 0;
4018
4019 if (window->resize_needed) {
4020 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004021 if (window->main_surface->frame_cb) {
4022 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004023 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004024 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004025
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004026 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004027 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004028 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004029
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004030 if (surface_redraw(window->main_surface) < 0) {
4031 /*
4032 * Only main_surface failure will cause us to undo the resize.
4033 * If sub-surfaces fail, they will just be broken with old
4034 * content.
4035 */
4036 failed = 1;
4037 } else {
4038 wl_list_for_each(surface, &window->subsurface_list, link) {
4039 if (surface == window->main_surface)
4040 continue;
4041
4042 surface_redraw(surface);
4043 }
4044 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004045
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004046 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004047 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004048
4049 wl_list_for_each(surface, &window->subsurface_list, link)
4050 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004051
4052 if (resized && failed) {
4053 /* Restore widget tree to correspond to what is on screen. */
4054 undo_resize(window);
4055 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004056}
4057
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004058static void
4059window_schedule_redraw_task(struct window *window)
4060{
4061 if (window->configure_requests)
4062 return;
4063 if (!window->redraw_task_scheduled) {
4064 window->redraw_task.run = idle_redraw;
4065 display_defer(window->display, &window->redraw_task);
4066 window->redraw_task_scheduled = 1;
4067 }
4068}
4069
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004070void
4071window_schedule_redraw(struct window *window)
4072{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004073 struct surface *surface;
4074
Pekka Paalanen71233882013-04-25 13:57:53 +03004075 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4076
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004077 wl_list_for_each(surface, &window->subsurface_list, link)
4078 surface->redraw_needed = 1;
4079
4080 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004081}
4082
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004083int
4084window_is_fullscreen(struct window *window)
4085{
4086 return window->type == TYPE_FULLSCREEN;
4087}
4088
Philip Withnallc971d2a2013-11-25 18:01:33 +00004089int
4090window_is_transient(struct window *window)
4091{
4092 return window->type == TYPE_TRANSIENT;
4093}
4094
MoD063a8822013-03-02 23:43:57 +00004095static void
4096configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4097{
4098 struct window *window = data;
4099
4100 wl_callback_destroy(callback);
4101 window->configure_requests--;
4102
4103 if (!window->configure_requests)
4104 window_schedule_redraw(window);
4105}
4106
4107static struct wl_callback_listener configure_request_listener = {
4108 configure_request_completed,
4109};
4110
4111static void
4112window_defer_redraw_until_configure(struct window* window)
4113{
4114 struct wl_callback *callback;
4115
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004116 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00004117 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004118 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00004119 }
4120
4121 callback = wl_display_sync(window->display->display);
4122 wl_callback_add_listener(callback, &configure_request_listener, window);
4123 window->configure_requests++;
4124}
4125
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004126void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004127window_set_fullscreen(struct window *window, int fullscreen)
4128{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004129 if (!window->display->shell)
4130 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004131
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04004132 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004133 return;
4134
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004135 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004136 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01004137 if (window->type == TYPE_TOPLEVEL) {
4138 window->saved_allocation = window->main_surface->allocation;
4139 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004140 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004141 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004142 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004143 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004144 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004145 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004146 if (window->saved_type == TYPE_MAXIMIZED) {
4147 window_set_maximized(window, 1);
4148 } else {
4149 window->type = TYPE_TOPLEVEL;
4150 wl_shell_surface_set_toplevel(window->shell_surface);
4151 window_schedule_resize(window,
4152 window->saved_allocation.width,
4153 window->saved_allocation.height);
4154 }
4155
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004156 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004157}
4158
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004159void
4160window_set_fullscreen_method(struct window *window,
4161 enum wl_shell_surface_fullscreen_method method)
4162{
4163 window->fullscreen_method = method;
4164}
4165
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004166int
4167window_is_maximized(struct window *window)
4168{
4169 return window->type == TYPE_MAXIMIZED;
4170}
4171
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004172void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004173window_set_maximized(struct window *window, int maximized)
4174{
4175 if (!window->display->shell)
4176 return;
4177
4178 if ((window->type == TYPE_MAXIMIZED) == maximized)
4179 return;
4180
4181 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004182 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004183 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4184 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004185 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004186 } else if (window->type == TYPE_FULLSCREEN) {
4187 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4188 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004189 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004190 } else {
4191 wl_shell_surface_set_toplevel(window->shell_surface);
4192 window->type = TYPE_TOPLEVEL;
4193 window_schedule_resize(window,
4194 window->saved_allocation.width,
4195 window->saved_allocation.height);
4196 }
4197}
4198
4199void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004200window_set_user_data(struct window *window, void *data)
4201{
4202 window->user_data = data;
4203}
4204
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004205void *
4206window_get_user_data(struct window *window)
4207{
4208 return window->user_data;
4209}
4210
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004211void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004212window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004213 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004214{
4215 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004216}
4217
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004218void
4219window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004220 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004221{
4222 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004223}
4224
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004225void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004226window_set_data_handler(struct window *window, window_data_handler_t handler)
4227{
4228 window->data_handler = handler;
4229}
4230
4231void
4232window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4233{
4234 window->drop_handler = handler;
4235}
4236
4237void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004238window_set_close_handler(struct window *window,
4239 window_close_handler_t handler)
4240{
4241 window->close_handler = handler;
4242}
4243
4244void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004245window_set_fullscreen_handler(struct window *window,
4246 window_fullscreen_handler_t handler)
4247{
4248 window->fullscreen_handler = handler;
4249}
4250
4251void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004252window_set_output_handler(struct window *window,
4253 window_output_handler_t handler)
4254{
4255 window->output_handler = handler;
4256}
4257
4258void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004259window_set_title(struct window *window, const char *title)
4260{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004261 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004262 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004263 if (window->frame) {
4264 frame_set_title(window->frame->frame, title);
4265 widget_schedule_redraw(window->frame->widget);
4266 }
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004267 if (window->shell_surface)
4268 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004269}
4270
4271const char *
4272window_get_title(struct window *window)
4273{
4274 return window->title;
4275}
4276
4277void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004278window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4279{
4280 struct text_cursor_position *text_cursor_position =
4281 window->display->text_cursor_position;
4282
Scott Moreau9295ce02012-06-01 12:46:10 -06004283 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004284 return;
4285
4286 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004287 window->main_surface->surface,
4288 wl_fixed_from_int(x),
4289 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004290}
4291
4292void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004293window_damage(struct window *window, int32_t x, int32_t y,
4294 int32_t width, int32_t height)
4295{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004296 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004297}
4298
Casey Dahlin9074db52012-04-19 22:50:09 -04004299static void
4300surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004301 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004302{
Rob Bradford7507b572012-05-15 17:55:34 +01004303 struct window *window = data;
4304 struct output *output;
4305 struct output *output_found = NULL;
4306 struct window_output *window_output;
4307
4308 wl_list_for_each(output, &window->display->output_list, link) {
4309 if (output->output == wl_output) {
4310 output_found = output;
4311 break;
4312 }
4313 }
4314
4315 if (!output_found)
4316 return;
4317
Brian Lovinbc919262013-08-07 15:34:59 -07004318 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004319 window_output->output = output_found;
4320
4321 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004322
4323 if (window->output_handler)
4324 window->output_handler(window, output_found, 1,
4325 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004326}
4327
4328static void
4329surface_leave(void *data,
4330 struct wl_surface *wl_surface, struct wl_output *output)
4331{
Rob Bradford7507b572012-05-15 17:55:34 +01004332 struct window *window = data;
4333 struct window_output *window_output;
4334 struct window_output *window_output_found = NULL;
4335
4336 wl_list_for_each(window_output, &window->window_output_list, link) {
4337 if (window_output->output->output == output) {
4338 window_output_found = window_output;
4339 break;
4340 }
4341 }
4342
4343 if (window_output_found) {
4344 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004345
4346 if (window->output_handler)
4347 window->output_handler(window, window_output->output,
4348 0, window->user_data);
4349
Rob Bradford7507b572012-05-15 17:55:34 +01004350 free(window_output_found);
4351 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004352}
4353
4354static const struct wl_surface_listener surface_listener = {
4355 surface_enter,
4356 surface_leave
4357};
4358
Pekka Paalanen4e373742013-02-13 16:17:13 +02004359static struct surface *
4360surface_create(struct window *window)
4361{
4362 struct display *display = window->display;
4363 struct surface *surface;
4364
Brian Lovinbc919262013-08-07 15:34:59 -07004365 surface = xmalloc(sizeof *surface);
4366 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004367 if (!surface)
4368 return NULL;
4369
4370 surface->window = window;
4371 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004372 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004373 wl_surface_add_listener(surface->surface, &surface_listener, window);
4374
Pekka Paalanen35e82632013-04-25 13:57:48 +03004375 wl_list_insert(&window->subsurface_list, &surface->link);
4376
Pekka Paalanen4e373742013-02-13 16:17:13 +02004377 return surface;
4378}
4379
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004380static struct window *
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004381window_create_internal(struct display *display, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004382{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004383 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004384 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004385
Peter Huttererf3d62272013-08-08 11:57:05 +10004386 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004387 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004388 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004389
4390 surface = surface_create(window);
4391 window->main_surface = surface;
4392
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004393 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004394 window->shell_surface =
4395 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004396 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004397 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004398 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004399
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004400 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004401 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004402 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004403 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004404
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004405 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004406#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004407 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004408#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004409 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004410#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004411 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004412 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004413
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004414 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004415 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004416 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004417
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004418 if (window->shell_surface) {
4419 wl_shell_surface_set_user_data(window->shell_surface, window);
4420 wl_shell_surface_add_listener(window->shell_surface,
4421 &shell_surface_listener, window);
4422 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004423
Rob Bradford7507b572012-05-15 17:55:34 +01004424 wl_list_init (&window->window_output_list);
4425
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004426 return window;
4427}
4428
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004429struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004430window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004431{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004432 return window_create_internal(display, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004433}
4434
4435struct window *
4436window_create_custom(struct display *display)
4437{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004438 return window_create_internal(display, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004439}
4440
4441struct window *
4442window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004443 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004444{
4445 struct window *window;
4446
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004447 window = window_create_internal(parent->display, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004448
4449 window->x = x;
4450 window->y = y;
4451
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004452 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004453 wl_shell_surface_set_transient(
4454 window->shell_surface,
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004455 parent->main_surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004456 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004457
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004458 return window;
4459}
4460
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004461static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004462menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004463{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004464 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004465 int next;
4466
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004467 frame_interior(menu->frame, &x, &y, &width, &height);
4468 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004469 if (menu->current != next) {
4470 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004471 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004472 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004473}
4474
4475static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004476menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004477 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004478 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004479{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004480 struct menu *menu = data;
4481
4482 if (widget == menu->widget)
4483 menu_set_item(data, y);
4484
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004485 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004486}
4487
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004488static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004489menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004490 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004491{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004492 struct menu *menu = data;
4493
4494 if (widget == menu->widget)
4495 menu_set_item(data, y);
4496
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004497 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004498}
4499
4500static void
4501menu_leave_handler(struct widget *widget, struct input *input, void *data)
4502{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004503 struct menu *menu = data;
4504
4505 if (widget == menu->widget)
4506 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004507}
4508
4509static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004510menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004511 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004512 uint32_t button, enum wl_pointer_button_state state,
4513 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004514
4515{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004516 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004517
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004518 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4519 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004520 /* Either relase after press-drag-release or
4521 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004522 menu->func(menu->parent, input,
4523 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004524 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004525 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004526 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004527 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004528 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004529}
4530
4531static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004532menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004533{
4534 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004535 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004536 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004537
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004538 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004539
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004540 frame_repaint(menu->frame, cr);
4541 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004542
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004543 theme_set_background_source(menu->window->display->theme,
4544 cr, THEME_FRAME_ACTIVE);
4545 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004546 cairo_fill(cr);
4547
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004548 cairo_select_font_face(cr, "sans",
4549 CAIRO_FONT_SLANT_NORMAL,
4550 CAIRO_FONT_WEIGHT_NORMAL);
4551 cairo_set_font_size(cr, 12);
4552
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004553 for (i = 0; i < menu->count; i++) {
4554 if (i == menu->current) {
4555 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004556 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004557 cairo_fill(cr);
4558 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004559 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004560 cairo_show_text(cr, menu->entries[i]);
4561 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004562 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4563 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004564 cairo_show_text(cr, menu->entries[i]);
4565 }
4566 }
4567
4568 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004569}
4570
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004571void
4572window_show_menu(struct display *display,
4573 struct input *input, uint32_t time, struct window *parent,
4574 int32_t x, int32_t y,
4575 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004576{
4577 struct window *window;
4578 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004579 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004580
4581 menu = malloc(sizeof *menu);
4582 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004583 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004584
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004585 window = window_create_internal(parent->display, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004586 if (!window) {
4587 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004588 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004589 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004590
4591 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004592 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004593 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004594 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4595 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004596 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004597 FRAME_BUTTON_NONE, NULL);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004598 menu->entries = entries;
4599 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004600 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004601 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004602 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004603 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004604 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004605 window->type = TYPE_MENU;
4606 window->x = x;
4607 window->y = y;
4608
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004609 input_ungrab(input);
4610
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004611 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004612 widget_set_enter_handler(menu->widget, menu_enter_handler);
4613 widget_set_leave_handler(menu->widget, menu_leave_handler);
4614 widget_set_motion_handler(menu->widget, menu_motion_handler);
4615 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004616
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004617 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004618 frame_resize_inside(menu->frame, 200, count * 20);
4619 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4620 window_schedule_resize(window, frame_width(menu->frame),
4621 frame_height(menu->frame));
4622
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004623 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4624 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4625 display_get_serial(window->display),
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004626 parent->main_surface->surface,
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004627 window->x - ix, window->y - iy, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004628}
4629
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004630void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004631window_set_buffer_type(struct window *window, enum window_buffer_type type)
4632{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004633 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004634}
4635
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004636void
4637window_set_preferred_format(struct window *window,
4638 enum preferred_format format)
4639{
4640 window->preferred_format = format;
4641}
4642
Pekka Paalanen35e82632013-04-25 13:57:48 +03004643struct widget *
4644window_add_subsurface(struct window *window, void *data,
4645 enum subsurface_mode default_mode)
4646{
4647 struct widget *widget;
4648 struct surface *surface;
4649 struct wl_surface *parent;
4650 struct wl_subcompositor *subcompo = window->display->subcompositor;
4651
Pekka Paalanen35e82632013-04-25 13:57:48 +03004652 surface = surface_create(window);
4653 widget = widget_create(window, surface, data);
4654 wl_list_init(&widget->link);
4655 surface->widget = widget;
4656
4657 parent = window->main_surface->surface;
4658 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4659 surface->surface,
4660 parent);
4661 surface->synchronized = 1;
4662
4663 switch (default_mode) {
4664 case SUBSURFACE_SYNCHRONIZED:
4665 surface->synchronized_default = 1;
4666 break;
4667 case SUBSURFACE_DESYNCHRONIZED:
4668 surface->synchronized_default = 0;
4669 break;
4670 default:
4671 assert(!"bad enum subsurface_mode");
4672 }
4673
4674 return widget;
4675}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004676
4677static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004678display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004679 struct wl_output *wl_output,
4680 int x, int y,
4681 int physical_width,
4682 int physical_height,
4683 int subpixel,
4684 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004685 const char *model,
4686 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004687{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004688 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004689
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004690 output->allocation.x = x;
4691 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004692 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004693}
4694
4695static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004696display_handle_done(void *data,
4697 struct wl_output *wl_output)
4698{
4699}
4700
4701static void
4702display_handle_scale(void *data,
4703 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004704 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004705{
4706 struct output *output = data;
4707
4708 output->scale = scale;
4709}
4710
4711static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004712display_handle_mode(void *data,
4713 struct wl_output *wl_output,
4714 uint32_t flags,
4715 int width,
4716 int height,
4717 int refresh)
4718{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004719 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004720 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004721
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004722 if (flags & WL_OUTPUT_MODE_CURRENT) {
4723 output->allocation.width = width;
4724 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004725 if (display->output_configure_handler)
4726 (*display->output_configure_handler)(
4727 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004728 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004729}
4730
4731static const struct wl_output_listener output_listener = {
4732 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004733 display_handle_mode,
4734 display_handle_done,
4735 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004736};
4737
4738static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004739display_add_output(struct display *d, uint32_t id)
4740{
4741 struct output *output;
4742
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004743 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004744 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004745 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004746 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004747 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004748 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004749 wl_list_insert(d->output_list.prev, &output->link);
4750
4751 wl_output_add_listener(output->output, &output_listener, output);
4752}
4753
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004754static void
4755output_destroy(struct output *output)
4756{
4757 if (output->destroy_handler)
4758 (*output->destroy_handler)(output, output->user_data);
4759
4760 wl_output_destroy(output->output);
4761 wl_list_remove(&output->link);
4762 free(output);
4763}
4764
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004765static void
4766display_destroy_output(struct display *d, uint32_t id)
4767{
4768 struct output *output;
4769
4770 wl_list_for_each(output, &d->output_list, link) {
4771 if (output->server_output_id == id) {
4772 output_destroy(output);
4773 break;
4774 }
4775 }
4776}
4777
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004778void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004779display_set_global_handler(struct display *display,
4780 display_global_handler_t handler)
4781{
4782 struct global *global;
4783
4784 display->global_handler = handler;
4785 if (!handler)
4786 return;
4787
4788 wl_list_for_each(global, &display->global_list, link)
4789 display->global_handler(display,
4790 global->name, global->interface,
4791 global->version, display->user_data);
4792}
4793
4794void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004795display_set_global_handler_remove(struct display *display,
4796 display_global_handler_t remove_handler)
4797{
4798 display->global_handler_remove = remove_handler;
4799 if (!remove_handler)
4800 return;
4801}
4802
4803void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004804display_set_output_configure_handler(struct display *display,
4805 display_output_handler_t handler)
4806{
4807 struct output *output;
4808
4809 display->output_configure_handler = handler;
4810 if (!handler)
4811 return;
4812
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004813 wl_list_for_each(output, &display->output_list, link) {
4814 if (output->allocation.width == 0 &&
4815 output->allocation.height == 0)
4816 continue;
4817
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004818 (*display->output_configure_handler)(output,
4819 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004820 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004821}
4822
4823void
4824output_set_user_data(struct output *output, void *data)
4825{
4826 output->user_data = data;
4827}
4828
4829void *
4830output_get_user_data(struct output *output)
4831{
4832 return output->user_data;
4833}
4834
4835void
4836output_set_destroy_handler(struct output *output,
4837 display_output_handler_t handler)
4838{
4839 output->destroy_handler = handler;
4840 /* FIXME: implement this, once we have way to remove outputs */
4841}
4842
4843void
Scott Moreau4e072362012-09-29 02:03:11 -06004844output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004845{
Scott Moreau4e072362012-09-29 02:03:11 -06004846 struct rectangle allocation = output->allocation;
4847
4848 switch (output->transform) {
4849 case WL_OUTPUT_TRANSFORM_90:
4850 case WL_OUTPUT_TRANSFORM_270:
4851 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4852 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4853 /* Swap width and height */
4854 allocation.width = output->allocation.height;
4855 allocation.height = output->allocation.width;
4856 break;
4857 }
4858
4859 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004860}
4861
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004862struct wl_output *
4863output_get_wl_output(struct output *output)
4864{
4865 return output->output;
4866}
4867
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004868enum wl_output_transform
4869output_get_transform(struct output *output)
4870{
4871 return output->transform;
4872}
4873
Alexander Larssonafd319a2013-05-22 14:41:27 +02004874uint32_t
4875output_get_scale(struct output *output)
4876{
4877 return output->scale;
4878}
4879
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004880static void
Daniel Stone97f68542012-05-30 16:32:01 +01004881fini_xkb(struct input *input)
4882{
4883 xkb_state_unref(input->xkb.state);
4884 xkb_map_unref(input->xkb.keymap);
4885}
4886
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004887#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004888
Daniel Stone97f68542012-05-30 16:32:01 +01004889static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004890display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004891{
4892 struct input *input;
4893
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004894 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004895 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004896 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004897 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004898 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004899 input->pointer_focus = NULL;
4900 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004901 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004902 wl_list_insert(d->input_list.prev, &input->link);
4903
Daniel Stone37816df2012-05-16 18:45:18 +01004904 wl_seat_add_listener(input->seat, &seat_listener, input);
4905 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004906
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004907 input->data_device =
4908 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004909 input->seat);
4910 wl_data_device_add_listener(input->data_device, &data_device_listener,
4911 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004912
4913 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004914
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004915 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4916 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004917 input->repeat_task.run = keyboard_repeat_func;
4918 display_watch_fd(d, input->repeat_timer_fd,
4919 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004920}
4921
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004922static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004923input_destroy(struct input *input)
4924{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004925 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004926 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004927
4928 if (input->drag_offer)
4929 data_offer_destroy(input->drag_offer);
4930
4931 if (input->selection_offer)
4932 data_offer_destroy(input->selection_offer);
4933
4934 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004935
4936 if (input->display->seat_version >= 3) {
4937 if (input->pointer)
4938 wl_pointer_release(input->pointer);
4939 if (input->keyboard)
4940 wl_keyboard_release(input->keyboard);
4941 }
4942
Daniel Stone97f68542012-05-30 16:32:01 +01004943 fini_xkb(input);
4944
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004945 wl_surface_destroy(input->pointer_surface);
4946
Pekka Paalanene1207c72011-12-16 12:02:09 +02004947 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004948 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004949 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004950 free(input);
4951}
4952
4953static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004954init_workspace_manager(struct display *d, uint32_t id)
4955{
4956 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004957 wl_registry_bind(d->registry, id,
4958 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004959 if (d->workspace_manager != NULL)
4960 workspace_manager_add_listener(d->workspace_manager,
4961 &workspace_manager_listener,
4962 d);
4963}
4964
4965static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004966shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4967{
4968 struct display *d = data;
4969
4970 if (format == WL_SHM_FORMAT_RGB565)
4971 d->has_rgb565 = 1;
4972}
4973
4974struct wl_shm_listener shm_listener = {
4975 shm_format
4976};
4977
4978static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004979registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4980 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004981{
4982 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004983 struct global *global;
4984
Brian Lovinbc919262013-08-07 15:34:59 -07004985 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004986 global->name = id;
4987 global->interface = strdup(interface);
4988 global->version = version;
4989 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004990
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004991 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004992 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05004993 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004994 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004995 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004996 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01004997 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004998 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004999 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005000 d->shell = wl_registry_bind(registry,
5001 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005002 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005003 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005004 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005005 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5006 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005007 wl_registry_bind(registry, id,
5008 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005009 } else if (strcmp(interface, "text_cursor_position") == 0) {
5010 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005011 wl_registry_bind(registry, id,
5012 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005013 } else if (strcmp(interface, "workspace_manager") == 0) {
5014 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005015 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5016 d->subcompositor =
5017 wl_registry_bind(registry, id,
5018 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005019 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005020
5021 if (d->global_handler)
5022 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005023}
5024
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005025static void
5026registry_handle_global_remove(void *data, struct wl_registry *registry,
5027 uint32_t name)
5028{
5029 struct display *d = data;
5030 struct global *global;
5031 struct global *tmp;
5032
5033 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5034 if (global->name != name)
5035 continue;
5036
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005037 if (strcmp(global->interface, "wl_output") == 0)
5038 display_destroy_output(d, name);
5039
5040 /* XXX: Should destroy remaining bound globals */
5041
5042 if (d->global_handler_remove)
5043 d->global_handler_remove(d, name, global->interface,
5044 global->version, d->user_data);
5045
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005046 wl_list_remove(&global->link);
5047 free(global->interface);
5048 free(global);
5049 }
5050}
5051
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005052void *
5053display_bind(struct display *display, uint32_t name,
5054 const struct wl_interface *interface, uint32_t version)
5055{
5056 return wl_registry_bind(display->registry, name, interface, version);
5057}
5058
5059static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005060 registry_handle_global,
5061 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005062};
5063
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005064#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005065static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005066init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005067{
5068 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005069 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005070
Rob Clark6396ed32012-03-11 19:48:41 -05005071#ifdef USE_CAIRO_GLESV2
5072# define GL_BIT EGL_OPENGL_ES2_BIT
5073#else
5074# define GL_BIT EGL_OPENGL_BIT
5075#endif
5076
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005077 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005078 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005079 EGL_RED_SIZE, 1,
5080 EGL_GREEN_SIZE, 1,
5081 EGL_BLUE_SIZE, 1,
5082 EGL_ALPHA_SIZE, 1,
5083 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005084 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005085 EGL_NONE
5086 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005087
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005088#ifdef USE_CAIRO_GLESV2
5089 static const EGLint context_attribs[] = {
5090 EGL_CONTEXT_CLIENT_VERSION, 2,
5091 EGL_NONE
5092 };
5093 EGLint api = EGL_OPENGL_ES_API;
5094#else
5095 EGLint *context_attribs = NULL;
5096 EGLint api = EGL_OPENGL_API;
5097#endif
5098
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005099 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005100 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005101 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005102 return -1;
5103 }
5104
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005105 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005106 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005107 return -1;
5108 }
5109
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005110 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5111 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005112 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005113 return -1;
5114 }
5115
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005116 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005117 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005118 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005119 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005120 return -1;
5121 }
5122
Benjamin Franzke0c991632011-09-27 21:57:31 +02005123 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5124 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005125 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005126 return -1;
5127 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005128
5129 return 0;
5130}
5131
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005132static void
5133fini_egl(struct display *display)
5134{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005135 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005136
5137 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5138 EGL_NO_CONTEXT);
5139
5140 eglTerminate(display->dpy);
5141 eglReleaseThread();
5142}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005143#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005144
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005145static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005146init_dummy_surface(struct display *display)
5147{
5148 int len;
5149 void *data;
5150
5151 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5152 data = malloc(len);
5153 display->dummy_surface =
5154 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5155 1, 1, len);
5156 display->dummy_surface_data = data;
5157}
5158
5159static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005160handle_display_data(struct task *task, uint32_t events)
5161{
5162 struct display *display =
5163 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005164 struct epoll_event ep;
5165 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005166
5167 display->display_fd_events = events;
5168
5169 if (events & EPOLLERR || events & EPOLLHUP) {
5170 display_exit(display);
5171 return;
5172 }
5173
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005174 if (events & EPOLLIN) {
5175 ret = wl_display_dispatch(display->display);
5176 if (ret == -1) {
5177 display_exit(display);
5178 return;
5179 }
5180 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005181
5182 if (events & EPOLLOUT) {
5183 ret = wl_display_flush(display->display);
5184 if (ret == 0) {
5185 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5186 ep.data.ptr = &display->display_task;
5187 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5188 display->display_fd, &ep);
5189 } else if (ret == -1 && errno != EAGAIN) {
5190 display_exit(display);
5191 return;
5192 }
5193 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005194}
5195
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005196static void
5197log_handler(const char *format, va_list args)
5198{
5199 vfprintf(stderr, format, args);
5200}
5201
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005202struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005203display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005204{
5205 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005206
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005207 wl_log_set_handler_client(log_handler);
5208
Peter Huttererf3d62272013-08-08 11:57:05 +10005209 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005210 if (d == NULL)
5211 return NULL;
5212
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005213 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005214 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005215 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005216 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005217 return NULL;
5218 }
5219
Rob Bradford5ab9c752013-07-26 16:29:43 +01005220 d->xkb_context = xkb_context_new(0);
5221 if (d->xkb_context == NULL) {
5222 fprintf(stderr, "Failed to create XKB context\n");
5223 free(d);
5224 return NULL;
5225 }
5226
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005227 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005228 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005229 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005230 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5231 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005232
5233 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005234 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005235 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005236 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005237
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005238 d->workspace = 0;
5239 d->workspace_count = 1;
5240
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005241 d->registry = wl_display_get_registry(d->display);
5242 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005243
5244 if (wl_display_dispatch(d->display) < 0) {
5245 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5246 return NULL;
5247 }
5248
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005249#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005250 if (init_egl(d) < 0)
5251 fprintf(stderr, "EGL does not seem to work, "
5252 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005253#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005254
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005255 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005256
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005257 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005258
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005259 wl_list_init(&d->window_list);
5260
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005261 init_dummy_surface(d);
5262
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005263 return d;
5264}
5265
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005266static void
5267display_destroy_outputs(struct display *display)
5268{
5269 struct output *tmp;
5270 struct output *output;
5271
5272 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5273 output_destroy(output);
5274}
5275
Pekka Paalanene1207c72011-12-16 12:02:09 +02005276static void
5277display_destroy_inputs(struct display *display)
5278{
5279 struct input *tmp;
5280 struct input *input;
5281
5282 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5283 input_destroy(input);
5284}
5285
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005286void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005287display_destroy(struct display *display)
5288{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005289 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005290 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5291 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005292
5293 if (!wl_list_empty(&display->deferred_list))
5294 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5295
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005296 cairo_surface_destroy(display->dummy_surface);
5297 free(display->dummy_surface_data);
5298
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005299 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005300 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005301
Daniel Stone97f68542012-05-30 16:32:01 +01005302 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005303
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005304 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005305 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005306
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005307#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005308 if (display->argb_device)
5309 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005310#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005311
Pekka Paalanen35e82632013-04-25 13:57:48 +03005312 if (display->subcompositor)
5313 wl_subcompositor_destroy(display->subcompositor);
5314
Pekka Paalanenc2052982011-12-16 11:41:32 +02005315 if (display->shell)
5316 wl_shell_destroy(display->shell);
5317
5318 if (display->shm)
5319 wl_shm_destroy(display->shm);
5320
5321 if (display->data_device_manager)
5322 wl_data_device_manager_destroy(display->data_device_manager);
5323
5324 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005325 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005326
5327 close(display->epoll_fd);
5328
U. Artie Eoff44874d92012-10-02 21:12:35 -07005329 if (!(display->display_fd_events & EPOLLERR) &&
5330 !(display->display_fd_events & EPOLLHUP))
5331 wl_display_flush(display->display);
5332
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005333 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005334 free(display);
5335}
5336
5337void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005338display_set_user_data(struct display *display, void *data)
5339{
5340 display->user_data = data;
5341}
5342
5343void *
5344display_get_user_data(struct display *display)
5345{
5346 return display->user_data;
5347}
5348
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005349struct wl_display *
5350display_get_display(struct display *display)
5351{
5352 return display->display;
5353}
5354
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005355int
5356display_has_subcompositor(struct display *display)
5357{
5358 if (display->subcompositor)
5359 return 1;
5360
5361 wl_display_roundtrip(display->display);
5362
5363 return display->subcompositor != NULL;
5364}
5365
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005366cairo_device_t *
5367display_get_cairo_device(struct display *display)
5368{
5369 return display->argb_device;
5370}
5371
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005372struct output *
5373display_get_output(struct display *display)
5374{
5375 return container_of(display->output_list.next, struct output, link);
5376}
5377
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005378struct wl_compositor *
5379display_get_compositor(struct display *display)
5380{
5381 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005382}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005383
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005384uint32_t
5385display_get_serial(struct display *display)
5386{
5387 return display->serial;
5388}
5389
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005390EGLDisplay
5391display_get_egl_display(struct display *d)
5392{
5393 return d->dpy;
5394}
5395
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005396struct wl_data_source *
5397display_create_data_source(struct display *display)
5398{
5399 return wl_data_device_manager_create_data_source(display->data_device_manager);
5400}
5401
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005402EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005403display_get_argb_egl_config(struct display *d)
5404{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005405 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005406}
5407
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005408int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005409display_acquire_window_surface(struct display *display,
5410 struct window *window,
5411 EGLContext ctx)
5412{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005413 struct surface *surface = window->main_surface;
5414
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005415 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005416 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005417
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005418 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005419 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005420}
5421
5422void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005423display_release_window_surface(struct display *display,
5424 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005425{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005426 struct surface *surface = window->main_surface;
5427
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005428 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005429 return;
5430
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005431 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005432}
5433
5434void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005435display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005436{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005437 wl_list_insert(&display->deferred_list, &task->link);
5438}
5439
5440void
5441display_watch_fd(struct display *display,
5442 int fd, uint32_t events, struct task *task)
5443{
5444 struct epoll_event ep;
5445
5446 ep.events = events;
5447 ep.data.ptr = task;
5448 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5449}
5450
5451void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005452display_unwatch_fd(struct display *display, int fd)
5453{
5454 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5455}
5456
5457void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005458display_run(struct display *display)
5459{
5460 struct task *task;
5461 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005462 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005463
Pekka Paalanen826d7952011-12-15 10:14:07 +02005464 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005465 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005466 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005467 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005468 struct task, link);
5469 wl_list_remove(&task->link);
5470 task->run(task, 0);
5471 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005472
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005473 wl_display_dispatch_pending(display->display);
5474
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005475 if (!display->running)
5476 break;
5477
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005478 ret = wl_display_flush(display->display);
5479 if (ret < 0 && errno == EAGAIN) {
5480 ep[0].events =
5481 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5482 ep[0].data.ptr = &display->display_task;
5483
5484 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5485 display->display_fd, &ep[0]);
5486 } else if (ret < 0) {
5487 break;
5488 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005489
5490 count = epoll_wait(display->epoll_fd,
5491 ep, ARRAY_LENGTH(ep), -1);
5492 for (i = 0; i < count; i++) {
5493 task = ep[i].data.ptr;
5494 task->run(task, ep[i].events);
5495 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005496 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005497}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005498
5499void
5500display_exit(struct display *display)
5501{
5502 display->running = 0;
5503}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005504
5505void
5506keysym_modifiers_add(struct wl_array *modifiers_map,
5507 const char *name)
5508{
5509 size_t len = strlen(name) + 1;
5510 char *p;
5511
5512 p = wl_array_add(modifiers_map, len);
5513
5514 if (p == NULL)
5515 return;
5516
5517 strncpy(p, name, len);
5518}
5519
5520static xkb_mod_index_t
5521keysym_modifiers_get_index(struct wl_array *modifiers_map,
5522 const char *name)
5523{
5524 xkb_mod_index_t index = 0;
5525 char *p = modifiers_map->data;
5526
5527 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5528 if (strcmp(p, name) == 0)
5529 return index;
5530
5531 index++;
5532 p += strlen(p) + 1;
5533 }
5534
5535 return XKB_MOD_INVALID;
5536}
5537
5538xkb_mod_mask_t
5539keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5540 const char *name)
5541{
5542 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5543
5544 if (index == XKB_MOD_INVALID)
5545 return XKB_MOD_INVALID;
5546
5547 return 1 << index;
5548}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005549
5550void *
5551fail_on_null(void *p)
5552{
5553 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005554 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005555 exit(EXIT_FAILURE);
5556 }
5557
5558 return p;
5559}
5560
5561void *
5562xmalloc(size_t s)
5563{
5564 return fail_on_null(malloc(s));
5565}
5566
Peter Huttererf3d62272013-08-08 11:57:05 +10005567void *
5568xzalloc(size_t s)
5569{
5570 return fail_on_null(zalloc(s));
5571}
5572
Kristian Høgsbergce278412013-07-25 15:20:20 -07005573char *
5574xstrdup(const char *s)
5575{
5576 return fail_on_null(strdup(s));
5577}