blob: 0b80172b5eb0ddc71e48e775a6f2daeb8b2c4da0 [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;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120
121 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100122
123 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200124
125 uint32_t workspace;
126 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200127
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200131
132 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100133 int seat_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500134};
135
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400136enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400137 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400138 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400139 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500140 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400141 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500142 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400143 TYPE_CUSTOM
144};
Rob Bradford7507b572012-05-15 17:55:34 +0100145
146struct window_output {
147 struct output *output;
148 struct wl_list link;
149};
150
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200151struct toysurface {
152 /*
153 * Prepare the surface for drawing. Makes sure there is a surface
154 * of the right size available for rendering, and returns it.
155 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200156 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200157 * If flags has SURFACE_HINT_RESIZE set, the user is
158 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200159 * Returns the Cairo surface to draw to.
160 */
161 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200162 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200163 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164
165 /*
166 * Post the surface to the server, returning the server allocation
167 * rectangle. The Cairo surface from prepare() must be destroyed
168 * after calling this.
169 */
170 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200171 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200172 struct rectangle *server_allocation);
173
174 /*
175 * Make the toysurface current with the given EGL context.
176 * Returns 0 on success, and negative of failure.
177 */
178 int (*acquire)(struct toysurface *base, EGLContext ctx);
179
180 /*
181 * Release the toysurface from the EGL context, returning control
182 * to Cairo.
183 */
184 void (*release)(struct toysurface *base);
185
186 /*
187 * Destroy the toysurface, including the Cairo surface, any
188 * backing storage, and the Wayland protocol objects.
189 */
190 void (*destroy)(struct toysurface *base);
191};
192
Pekka Paalanen4e373742013-02-13 16:17:13 +0200193struct surface {
194 struct window *window;
195
196 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300197 struct wl_subsurface *subsurface;
198 int synchronized;
199 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200200 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200201 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300202 int redraw_needed;
203 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300204 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200205
206 struct rectangle allocation;
207 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200208
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200209 struct wl_region *input_region;
210 struct wl_region *opaque_region;
211
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200212 enum window_buffer_type buffer_type;
213 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200214 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200215
216 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300217
218 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200219};
220
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500221struct window {
222 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500223 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100224 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500225 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200226 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400227 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500228 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500229 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400230 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400231 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300232 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400233 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400234 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100235 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400236 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400237 int focus_count;
238
Pekka Paalanen99436862012-11-19 17:15:59 +0200239 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200240 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000241 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400242
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200243 enum preferred_format preferred_format;
244
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500245 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500246 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400247 window_data_handler_t data_handler;
248 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500249 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400250 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200251 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400252
Pekka Paalanen4e373742013-02-13 16:17:13 +0200253 struct surface *main_surface;
254 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300255
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500256 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500257
Pekka Paalanen35e82632013-04-25 13:57:48 +0300258 /* struct surface::link, contains also main_surface */
259 struct wl_list subsurface_list;
260
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500261 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400262 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500263};
264
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500265struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500266 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200267 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300268 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500269 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400270 struct wl_list link;
271 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500272 widget_resize_handler_t resize_handler;
273 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500274 widget_enter_handler_t enter_handler;
275 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500276 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500277 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700278 widget_touch_down_handler_t touch_down_handler;
279 widget_touch_up_handler_t touch_up_handler;
280 widget_touch_motion_handler_t touch_motion_handler;
281 widget_touch_frame_handler_t touch_frame_handler;
282 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200283 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400284 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500285 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300286 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500287 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400288};
289
Rusty Lynch041815a2013-08-08 21:20:38 -0700290struct touch_point {
291 int32_t id;
292 struct widget *widget;
293 struct wl_list link;
294};
295
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400296struct input {
297 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400303 struct window *pointer_focus;
304 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300306 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300309 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400310 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400311 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400312 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400313 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400315
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500316 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500317 struct widget *grab;
318 uint32_t grab_button;
319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100323
324 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100325 struct xkb_keymap *keymap;
326 struct xkb_state *state;
327 xkb_mod_mask_t control_mask;
328 xkb_mod_mask_t alt_mask;
329 xkb_mod_mask_t shift_mask;
330 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400331
332 struct task repeat_task;
333 int repeat_timer_fd;
334 uint32_t repeat_sym;
335 uint32_t repeat_key;
336 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400337};
338
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500339struct output {
340 struct display *display;
341 struct wl_output *output;
342 struct rectangle allocation;
343 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600344 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200345 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200346
347 display_output_handler_t destroy_handler;
348 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500349};
350
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500351struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500352 struct widget *widget;
353 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500354 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500355};
356
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500357struct menu {
358 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500359 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500360 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500361 const char **entries;
362 uint32_t time;
363 int current;
364 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400365 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500366 menu_func_t func;
367};
368
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300369struct tooltip {
370 struct widget *parent;
371 struct window *window;
372 struct widget *widget;
373 char *entry;
374 struct task tooltip_task;
375 int tooltip_fd;
376 float x, y;
377};
378
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700379struct shm_pool {
380 struct wl_shm_pool *pool;
381 size_t size;
382 size_t used;
383 void *data;
384};
385
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400386enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300387 CURSOR_DEFAULT = 100,
388 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400389};
390
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500391enum window_location {
392 WINDOW_INTERIOR = 0,
393 WINDOW_RESIZING_TOP = 1,
394 WINDOW_RESIZING_BOTTOM = 2,
395 WINDOW_RESIZING_LEFT = 4,
396 WINDOW_RESIZING_TOP_LEFT = 5,
397 WINDOW_RESIZING_BOTTOM_LEFT = 6,
398 WINDOW_RESIZING_RIGHT = 8,
399 WINDOW_RESIZING_TOP_RIGHT = 9,
400 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
401 WINDOW_RESIZING_MASK = 15,
402 WINDOW_EXTERIOR = 16,
403 WINDOW_TITLEBAR = 17,
404 WINDOW_CLIENT_AREA = 18,
405};
406
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200407static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400408
Pekka Paalanen97777442013-05-22 10:20:05 +0300409/* #define DEBUG */
410
411#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300412
413static void
414debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
415__attribute__ ((format (printf, 4, 5)));
416
417static void
418debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
419{
420 va_list ap;
421 struct timeval tv;
422
423 gettimeofday(&tv, NULL);
424 fprintf(stderr, "%8ld.%03ld ",
425 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
426
427 if (proxy)
428 fprintf(stderr, "%s@%d ",
429 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
430
431 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
432 fprintf(stderr, "%s ", func);
433
434 va_start(ap, fmt);
435 vfprintf(stderr, fmt, ap);
436 va_end(ap);
437}
438
439#define DBG(fmt, ...) \
440 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
441
442#define DBG_OBJ(obj, fmt, ...) \
443 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
444
445#else
446
447#define DBG(...) do {} while (0)
448#define DBG_OBJ(...) do {} while (0)
449
450#endif
451
Alexander Larsson1818e312013-05-22 14:41:31 +0200452static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200453surface_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 +0200454{
455 int32_t tmp;
456
457 switch (buffer_transform) {
458 case WL_OUTPUT_TRANSFORM_90:
459 case WL_OUTPUT_TRANSFORM_270:
460 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
461 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
462 tmp = *width;
463 *width = *height;
464 *height = tmp;
465 break;
466 default:
467 break;
468 }
469
470 *width *= buffer_scale;
471 *height *= buffer_scale;
472}
473
474static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200475buffer_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 +0200476{
477 int32_t tmp;
478
479 switch (buffer_transform) {
480 case WL_OUTPUT_TRANSFORM_90:
481 case WL_OUTPUT_TRANSFORM_270:
482 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
483 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
484 tmp = *width;
485 *width = *height;
486 *height = tmp;
487 break;
488 default:
489 break;
490 }
491
492 *width /= buffer_scale;
493 *height /= buffer_scale;
494}
495
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500496#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400497
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200498struct egl_window_surface {
499 struct toysurface base;
500 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100501 struct display *display;
502 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200503 struct wl_egl_window *egl_window;
504 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100505};
506
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200507static struct egl_window_surface *
508to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100509{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200510 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100511}
512
513static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200514egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200515 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200516 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100517{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518 struct egl_window_surface *surface = to_egl_window_surface(base);
519
Alexander Larsson1818e312013-05-22 14:41:31 +0200520 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
521
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
523 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
524
525 return cairo_surface_reference(surface->cairo_surface);
526}
527
528static void
529egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200530 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200531 struct rectangle *server_allocation)
532{
533 struct egl_window_surface *surface = to_egl_window_surface(base);
534
535 cairo_gl_surface_swapbuffers(surface->cairo_surface);
536 wl_egl_window_get_attached_size(surface->egl_window,
537 &server_allocation->width,
538 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200539
540 buffer_to_surface_size (buffer_transform, buffer_scale,
541 &server_allocation->width,
542 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200543}
544
545static int
546egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
547{
548 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200549 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100550
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200551 device = cairo_surface_get_device(surface->cairo_surface);
552 if (!device)
553 return -1;
554
555 if (!ctx) {
556 if (device == surface->display->argb_device)
557 ctx = surface->display->argb_ctx;
558 else
559 assert(0);
560 }
561
562 cairo_device_flush(device);
563 cairo_device_acquire(device);
564 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
565 surface->egl_surface, ctx))
566 fprintf(stderr, "failed to make surface current\n");
567
568 return 0;
569}
570
571static void
572egl_window_surface_release(struct toysurface *base)
573{
574 struct egl_window_surface *surface = to_egl_window_surface(base);
575 cairo_device_t *device;
576
577 device = cairo_surface_get_device(surface->cairo_surface);
578 if (!device)
579 return;
580
581 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
582 surface->display->argb_ctx))
583 fprintf(stderr, "failed to make context current\n");
584
585 cairo_device_release(device);
586}
587
588static void
589egl_window_surface_destroy(struct toysurface *base)
590{
591 struct egl_window_surface *surface = to_egl_window_surface(base);
592 struct display *d = surface->display;
593
594 cairo_surface_destroy(surface->cairo_surface);
595 eglDestroySurface(d->dpy, surface->egl_surface);
596 wl_egl_window_destroy(surface->egl_window);
597 surface->surface = NULL;
598
599 free(surface);
600}
601
602static struct toysurface *
603egl_window_surface_create(struct display *display,
604 struct wl_surface *wl_surface,
605 uint32_t flags,
606 struct rectangle *rectangle)
607{
608 struct egl_window_surface *surface;
609
Pekka Paalanenb3627362012-11-19 17:16:00 +0200610 if (display->dpy == EGL_NO_DISPLAY)
611 return NULL;
612
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200613 surface = calloc(1, sizeof *surface);
614 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100615 return NULL;
616
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200617 surface->base.prepare = egl_window_surface_prepare;
618 surface->base.swap = egl_window_surface_swap;
619 surface->base.acquire = egl_window_surface_acquire;
620 surface->base.release = egl_window_surface_release;
621 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100622
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200623 surface->display = display;
624 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400625
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200626 surface->egl_window = wl_egl_window_create(surface->surface,
627 rectangle->width,
628 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100629
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200630 surface->egl_surface = eglCreateWindowSurface(display->dpy,
631 display->argb_config,
632 surface->egl_window,
633 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200635 surface->cairo_surface =
636 cairo_gl_surface_create_for_egl(display->argb_device,
637 surface->egl_surface,
638 rectangle->width,
639 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100640
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200641 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642}
643
Pekka Paalanenb3627362012-11-19 17:16:00 +0200644#else
645
646static struct toysurface *
647egl_window_surface_create(struct display *display,
648 struct wl_surface *wl_surface,
649 uint32_t flags,
650 struct rectangle *rectangle)
651{
652 return NULL;
653}
654
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400655#endif
656
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200657struct shm_surface_data {
658 struct wl_buffer *buffer;
659 struct shm_pool *pool;
660};
661
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400662struct wl_buffer *
663display_get_buffer_for_surface(struct display *display,
664 cairo_surface_t *surface)
665{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200666 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400667
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200668 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400669
670 return data->buffer;
671}
672
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500673static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700674shm_pool_destroy(struct shm_pool *pool);
675
676static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400677shm_surface_data_destroy(void *p)
678{
679 struct shm_surface_data *data = p;
680
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200681 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700682 if (data->pool)
683 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300684
685 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400686}
687
Kristian Høgsberg16626282012-04-03 11:21:27 -0400688static struct wl_shm_pool *
689make_shm_pool(struct display *display, int size, void **data)
690{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400691 struct wl_shm_pool *pool;
692 int fd;
693
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300694 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400695 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300696 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
697 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400698 return NULL;
699 }
700
701 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400702 if (*data == MAP_FAILED) {
703 fprintf(stderr, "mmap failed: %m\n");
704 close(fd);
705 return NULL;
706 }
707
708 pool = wl_shm_create_pool(display->shm, fd, size);
709
710 close(fd);
711
712 return pool;
713}
714
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700715static struct shm_pool *
716shm_pool_create(struct display *display, size_t size)
717{
718 struct shm_pool *pool = malloc(sizeof *pool);
719
720 if (!pool)
721 return NULL;
722
723 pool->pool = make_shm_pool(display, size, &pool->data);
724 if (!pool->pool) {
725 free(pool);
726 return NULL;
727 }
728
729 pool->size = size;
730 pool->used = 0;
731
732 return pool;
733}
734
735static void *
736shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
737{
738 if (pool->used + size > pool->size)
739 return NULL;
740
741 *offset = pool->used;
742 pool->used += size;
743
744 return (char *) pool->data + *offset;
745}
746
747/* destroy the pool. this does not unmap the memory though */
748static void
749shm_pool_destroy(struct shm_pool *pool)
750{
751 munmap(pool->data, pool->size);
752 wl_shm_pool_destroy(pool->pool);
753 free(pool);
754}
755
756/* Start allocating from the beginning of the pool again */
757static void
758shm_pool_reset(struct shm_pool *pool)
759{
760 pool->used = 0;
761}
762
763static int
764data_length_for_shm_surface(struct rectangle *rect)
765{
766 int stride;
767
768 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
769 rect->width);
770 return stride * rect->height;
771}
772
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500773static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700774display_create_shm_surface_from_pool(struct display *display,
775 struct rectangle *rectangle,
776 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400777{
778 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400779 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400780 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200781 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700782 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400783 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400784
785 data = malloc(sizeof *data);
786 if (data == NULL)
787 return NULL;
788
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200789 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
790 cairo_format = CAIRO_FORMAT_RGB16_565;
791 else
792 cairo_format = CAIRO_FORMAT_ARGB32;
793
794 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700795 length = stride * rectangle->height;
796 data->pool = NULL;
797 map = shm_pool_allocate(pool, length, &offset);
798
799 if (!map) {
800 free(data);
801 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400802 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400803
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400804 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200805 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400806 rectangle->width,
807 rectangle->height,
808 stride);
809
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200810 cairo_surface_set_user_data(surface, &shm_surface_data_key,
811 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400812
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200813 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
814 format = WL_SHM_FORMAT_RGB565;
815 else {
816 if (flags & SURFACE_OPAQUE)
817 format = WL_SHM_FORMAT_XRGB8888;
818 else
819 format = WL_SHM_FORMAT_ARGB8888;
820 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400821
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200822 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
823 rectangle->width,
824 rectangle->height,
825 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700827 return surface;
828}
829
830static cairo_surface_t *
831display_create_shm_surface(struct display *display,
832 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200833 struct shm_pool *alternate_pool,
834 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700835{
836 struct shm_surface_data *data;
837 struct shm_pool *pool;
838 cairo_surface_t *surface;
839
Pekka Paalanen99436862012-11-19 17:15:59 +0200840 if (alternate_pool) {
841 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700842 surface = display_create_shm_surface_from_pool(display,
843 rectangle,
844 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200845 alternate_pool);
846 if (surface) {
847 data = cairo_surface_get_user_data(surface,
848 &shm_surface_data_key);
849 goto out;
850 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700851 }
852
853 pool = shm_pool_create(display,
854 data_length_for_shm_surface(rectangle));
855 if (!pool)
856 return NULL;
857
858 surface =
859 display_create_shm_surface_from_pool(display, rectangle,
860 flags, pool);
861
862 if (!surface) {
863 shm_pool_destroy(pool);
864 return NULL;
865 }
866
867 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200868 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700869 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400870
Pekka Paalanen99436862012-11-19 17:15:59 +0200871out:
872 if (data_ret)
873 *data_ret = data;
874
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400875 return surface;
876}
877
nobled7b87cb02011-02-01 18:51:47 +0000878static int
879check_size(struct rectangle *rect)
880{
881 if (rect->width && rect->height)
882 return 0;
883
884 fprintf(stderr, "tried to create surface of "
885 "width: %d, height: %d\n", rect->width, rect->height);
886 return -1;
887}
888
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889cairo_surface_t *
890display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100891 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400892 struct rectangle *rectangle,
893 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400894{
nobled7b87cb02011-02-01 18:51:47 +0000895 if (check_size(rectangle) < 0)
896 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200897
898 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200899 return display_create_shm_surface(display, rectangle, flags,
900 NULL, NULL);
901}
902
Pekka Paalanena4eda732012-11-19 17:16:02 +0200903struct shm_surface_leaf {
904 cairo_surface_t *cairo_surface;
905 /* 'data' is automatically destroyed, when 'cairo_surface' is */
906 struct shm_surface_data *data;
907
908 struct shm_pool *resize_pool;
909 int busy;
910};
911
912static void
913shm_surface_leaf_release(struct shm_surface_leaf *leaf)
914{
915 if (leaf->cairo_surface)
916 cairo_surface_destroy(leaf->cairo_surface);
917 /* leaf->data already destroyed via cairo private */
918
919 if (leaf->resize_pool)
920 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200921
922 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200923}
924
Pekka Paalanenaef02542013-04-25 13:57:47 +0300925#define MAX_LEAVES 3
926
Pekka Paalanen99436862012-11-19 17:15:59 +0200927struct shm_surface {
928 struct toysurface base;
929 struct display *display;
930 struct wl_surface *surface;
931 uint32_t flags;
932 int dx, dy;
933
Pekka Paalanenaef02542013-04-25 13:57:47 +0300934 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200935 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200936};
937
938static struct shm_surface *
939to_shm_surface(struct toysurface *base)
940{
941 return container_of(base, struct shm_surface, base);
942}
943
Pekka Paalanena4eda732012-11-19 17:16:02 +0200944static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300945shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
946{
947#ifdef DEBUG
948 struct shm_surface_leaf *leaf;
949 char bufs[MAX_LEAVES + 1];
950 int i;
951
952 for (i = 0; i < MAX_LEAVES; i++) {
953 leaf = &surface->leaf[i];
954
955 if (leaf->busy)
956 bufs[i] = 'b';
957 else if (leaf->cairo_surface)
958 bufs[i] = 'a';
959 else
960 bufs[i] = ' ';
961 }
962
963 bufs[MAX_LEAVES] = '\0';
964 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
965#endif
966}
967
968static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200969shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
970{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200971 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300972 struct shm_surface_leaf *leaf;
973 int i;
974 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300975
976 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200977
Pekka Paalanenaef02542013-04-25 13:57:47 +0300978 for (i = 0; i < MAX_LEAVES; i++) {
979 leaf = &surface->leaf[i];
980 if (leaf->data && leaf->data->buffer == buffer) {
981 leaf->busy = 0;
982 break;
983 }
984 }
985 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200986
Pekka Paalanenaef02542013-04-25 13:57:47 +0300987 /* Leave one free leaf with storage, release others */
988 free_found = 0;
989 for (i = 0; i < MAX_LEAVES; i++) {
990 leaf = &surface->leaf[i];
991
992 if (!leaf->cairo_surface || leaf->busy)
993 continue;
994
995 if (!free_found)
996 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300997 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300998 shm_surface_leaf_release(leaf);
999 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001000
Pekka Paalanen97777442013-05-22 10:20:05 +03001001 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001002}
1003
1004static const struct wl_buffer_listener shm_surface_buffer_listener = {
1005 shm_surface_buffer_release
1006};
1007
Pekka Paalanen99436862012-11-19 17:15:59 +02001008static cairo_surface_t *
1009shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001010 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001011 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001012{
Pekka Paalanenec076692012-11-30 13:37:27 +02001013 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001014 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001015 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001016 struct shm_surface_leaf *leaf = NULL;
1017 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001018
1019 surface->dx = dx;
1020 surface->dy = dy;
1021
Pekka Paalanenaef02542013-04-25 13:57:47 +03001022 /* pick a free buffer, preferrably one that already has storage */
1023 for (i = 0; i < MAX_LEAVES; i++) {
1024 if (surface->leaf[i].busy)
1025 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001026
Pekka Paalanenaef02542013-04-25 13:57:47 +03001027 if (!leaf || surface->leaf[i].cairo_surface)
1028 leaf = &surface->leaf[i];
1029 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001030 DBG_OBJ(surface->surface, "pick leaf %d\n",
1031 (int)(leaf - &surface->leaf[0]));
1032
Pekka Paalanenaef02542013-04-25 13:57:47 +03001033 if (!leaf) {
1034 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001035 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001036 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001037 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001038 }
1039
Pekka Paalanena4eda732012-11-19 17:16:02 +02001040 if (!resize_hint && leaf->resize_pool) {
1041 cairo_surface_destroy(leaf->cairo_surface);
1042 leaf->cairo_surface = NULL;
1043 shm_pool_destroy(leaf->resize_pool);
1044 leaf->resize_pool = NULL;
1045 }
1046
Alexander Larsson1818e312013-05-22 14:41:31 +02001047 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1048
Pekka Paalanena4eda732012-11-19 17:16:02 +02001049 if (leaf->cairo_surface &&
1050 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1051 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001052 goto out;
1053
Pekka Paalanena4eda732012-11-19 17:16:02 +02001054 if (leaf->cairo_surface)
1055 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001056
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001057#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001058 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001059 /* Create a big pool to allocate from, while continuously
1060 * resizing. Mmapping a new pool in the server
1061 * is relatively expensive, so reusing a pool performs
1062 * better, but may temporarily reserve unneeded memory.
1063 */
1064 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001065 leaf->resize_pool = shm_pool_create(surface->display,
1066 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001067 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001068#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001069
Alexander Larsson1818e312013-05-22 14:41:31 +02001070 rect.width = width;
1071 rect.height = height;
1072
Pekka Paalanena4eda732012-11-19 17:16:02 +02001073 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001074 display_create_shm_surface(surface->display, &rect,
1075 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001076 leaf->resize_pool,
1077 &leaf->data);
1078 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001079 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001080
1081out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001082 surface->current = leaf;
1083
1084 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001085}
1086
1087static void
1088shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001089 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001090 struct rectangle *server_allocation)
1091{
1092 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001094
1095 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001097 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001098 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001099
Alexander Larsson1818e312013-05-22 14:41:31 +02001100 buffer_to_surface_size (buffer_transform, buffer_scale,
1101 &server_allocation->width,
1102 &server_allocation->height);
1103
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001105 surface->dx, surface->dy);
1106 wl_surface_damage(surface->surface, 0, 0,
1107 server_allocation->width, server_allocation->height);
1108 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001109
Pekka Paalanen71233882013-04-25 13:57:53 +03001110 DBG_OBJ(surface->surface, "leaf %d busy\n",
1111 (int)(leaf - &surface->leaf[0]));
1112
Pekka Paalanena4eda732012-11-19 17:16:02 +02001113 leaf->busy = 1;
1114 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001115}
1116
1117static int
1118shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1119{
1120 return -1;
1121}
1122
1123static void
1124shm_surface_release(struct toysurface *base)
1125{
1126}
1127
1128static void
1129shm_surface_destroy(struct toysurface *base)
1130{
1131 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001132 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001133
Pekka Paalanenaef02542013-04-25 13:57:47 +03001134 for (i = 0; i < MAX_LEAVES; i++)
1135 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001136
1137 free(surface);
1138}
1139
1140static struct toysurface *
1141shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1142 uint32_t flags, struct rectangle *rectangle)
1143{
1144 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001145 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001146
Brian Lovinbc919262013-08-07 15:34:59 -07001147 surface = xmalloc(sizeof *surface);
1148 memset(surface, 0, sizeof *surface);
1149
Pekka Paalanen99436862012-11-19 17:15:59 +02001150 if (!surface)
1151 return NULL;
1152
1153 surface->base.prepare = shm_surface_prepare;
1154 surface->base.swap = shm_surface_swap;
1155 surface->base.acquire = shm_surface_acquire;
1156 surface->base.release = shm_surface_release;
1157 surface->base.destroy = shm_surface_destroy;
1158
1159 surface->display = display;
1160 surface->surface = wl_surface;
1161 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001162
1163 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001164}
1165
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001166/*
1167 * The following correspondences between file names and cursors was copied
1168 * from: https://bugs.kde.org/attachment.cgi?id=67313
1169 */
1170
1171static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001172 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001173 "sw-resize",
1174 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001175};
1176
1177static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001178 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001179 "se-resize",
1180 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001181};
1182
1183static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001184 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001185 "s-resize",
1186 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001187};
1188
1189static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001190 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001191 "closedhand",
1192 "208530c400c041818281048008011002"
1193};
1194
1195static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001196 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001197 "default",
1198 "top_left_arrow",
1199 "left-arrow"
1200};
1201
1202static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001203 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001204 "w-resize",
1205 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001206};
1207
1208static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001209 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001210 "e-resize",
1211 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001212};
1213
1214static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001215 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001216 "nw-resize",
1217 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001218};
1219
1220static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "ne-resize",
1223 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001228 "n-resize",
1229 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230};
1231
1232static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001234 "ibeam",
1235 "text"
1236};
1237
1238static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001240 "pointer",
1241 "pointing_hand",
1242 "e29285e634086352946a0e7090d73106"
1243};
1244
1245static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001246 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001247 "wait",
1248 "0426c94ea35c87780ff01dc239897213"
1249};
1250
1251struct cursor_alternatives {
1252 const char **names;
1253 size_t count;
1254};
1255
1256static const struct cursor_alternatives cursors[] = {
1257 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1258 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1259 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1260 {grabbings, ARRAY_LENGTH(grabbings)},
1261 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1262 {left_sides, ARRAY_LENGTH(left_sides)},
1263 {right_sides, ARRAY_LENGTH(right_sides)},
1264 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1265 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1266 {top_sides, ARRAY_LENGTH(top_sides)},
1267 {xterms, ARRAY_LENGTH(xterms)},
1268 {hand1s, ARRAY_LENGTH(hand1s)},
1269 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001270};
1271
1272static void
1273create_cursors(struct display *display)
1274{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001275 struct weston_config *config;
1276 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001277 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001278 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001279 unsigned int i, j;
1280 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001281
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001282 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001283 s = weston_config_get_section(config, "shell", NULL, NULL);
1284 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1285 weston_config_section_get_int(s, "cursor-size", &size, 32);
1286 weston_config_destroy(config);
1287
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001288 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001289 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001290 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001291 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001292
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001293 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001294 cursor = NULL;
1295 for (j = 0; !cursor && j < cursors[i].count; ++j)
1296 cursor = wl_cursor_theme_get_cursor(
1297 display->cursor_theme, cursors[i].names[j]);
1298
1299 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001300 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001301 cursors[i].names[0]);
1302
1303 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001304 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001305}
1306
1307static void
1308destroy_cursors(struct display *display)
1309{
1310 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001311 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001312}
1313
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001314struct wl_cursor_image *
1315display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001316{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001317 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001318
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001319 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001320}
1321
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001322static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001323surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001324{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001325 if (!surface->cairo_surface)
1326 return;
1327
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001328 if (surface->opaque_region) {
1329 wl_surface_set_opaque_region(surface->surface,
1330 surface->opaque_region);
1331 wl_region_destroy(surface->opaque_region);
1332 surface->opaque_region = NULL;
1333 }
1334
1335 if (surface->input_region) {
1336 wl_surface_set_input_region(surface->surface,
1337 surface->input_region);
1338 wl_region_destroy(surface->input_region);
1339 surface->input_region = NULL;
1340 }
1341
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001342 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001343 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001344 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001345
Pekka Paalanen89dee002013-02-13 16:17:20 +02001346 cairo_surface_destroy(surface->cairo_surface);
1347 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001348}
1349
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001350int
1351window_has_focus(struct window *window)
1352{
1353 return window->focus_count > 0;
1354}
1355
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001356static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001357window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001358{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001359 struct surface *surface;
1360
Pekka Paalanen89dee002013-02-13 16:17:20 +02001361 if (window->type == TYPE_NONE) {
1362 window->type = TYPE_TOPLEVEL;
1363 if (window->shell_surface)
1364 wl_shell_surface_set_toplevel(window->shell_surface);
1365 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001366
Pekka Paalanen35e82632013-04-25 13:57:48 +03001367 wl_list_for_each(surface, &window->subsurface_list, link) {
1368 if (surface == window->main_surface)
1369 continue;
1370
1371 surface_flush(surface);
1372 }
1373
Pekka Paalanen89dee002013-02-13 16:17:20 +02001374 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001375}
1376
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001377struct display *
1378window_get_display(struct window *window)
1379{
1380 return window->display;
1381}
1382
Pekka Paalanen89dee002013-02-13 16:17:20 +02001383static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001384surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001385{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001386 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001387 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001388
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001389 if (!surface->toysurface && display->dpy &&
1390 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001391 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001392 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001393 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001394 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001395 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001396 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001397
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001398 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001399 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001400 surface->surface,
1401 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001402
Pekka Paalanen89dee002013-02-13 16:17:20 +02001403 surface->cairo_surface = surface->toysurface->prepare(
1404 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001405 allocation.width, allocation.height, flags,
1406 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407}
1408
1409static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001410window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001411{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001412 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001413 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001414 int dx = 0;
1415 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001416
Pekka Paalanenec076692012-11-30 13:37:27 +02001417 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001418 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001419
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001420 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1421 flags |= SURFACE_HINT_RGB565;
1422
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001423 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1424 dx = surface->server_allocation.width -
1425 surface->allocation.width;
1426
1427 if (window->resize_edges & WINDOW_RESIZING_TOP)
1428 dy = surface->server_allocation.height -
1429 surface->allocation.height;
1430
1431 window->resize_edges = 0;
1432
Pekka Paalanen89dee002013-02-13 16:17:20 +02001433 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001434}
1435
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001436int
1437window_get_buffer_transform(struct window *window)
1438{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001439 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001440}
1441
1442void
1443window_set_buffer_transform(struct window *window,
1444 enum wl_output_transform transform)
1445{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001446 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001447 wl_surface_set_buffer_transform(window->main_surface->surface,
1448 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001449}
1450
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001451void
1452window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001453 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001454{
1455 window->main_surface->buffer_scale = scale;
1456 wl_surface_set_buffer_scale(window->main_surface->surface,
1457 scale);
1458}
1459
1460uint32_t
1461window_get_buffer_scale(struct window *window)
1462{
1463 return window->main_surface->buffer_scale;
1464}
1465
Alexander Larssond68f5232013-05-22 14:41:33 +02001466uint32_t
1467window_get_output_scale(struct window *window)
1468{
1469 struct window_output *window_output;
1470 struct window_output *window_output_tmp;
1471 int scale = 1;
1472
1473 wl_list_for_each_safe(window_output, window_output_tmp,
1474 &window->window_output_list, link) {
1475 if (window_output->output->scale > scale)
1476 scale = window_output->output->scale;
1477 }
1478
1479 return scale;
1480}
1481
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001482static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001483
Pekka Paalanen4e373742013-02-13 16:17:13 +02001484static void
1485surface_destroy(struct surface *surface)
1486{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001487 if (surface->frame_cb)
1488 wl_callback_destroy(surface->frame_cb);
1489
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001490 if (surface->input_region)
1491 wl_region_destroy(surface->input_region);
1492
1493 if (surface->opaque_region)
1494 wl_region_destroy(surface->opaque_region);
1495
Pekka Paalanen35e82632013-04-25 13:57:48 +03001496 if (surface->subsurface)
1497 wl_subsurface_destroy(surface->subsurface);
1498
Pekka Paalanen4e373742013-02-13 16:17:13 +02001499 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001500
1501 if (surface->toysurface)
1502 surface->toysurface->destroy(surface->toysurface);
1503
Pekka Paalanen35e82632013-04-25 13:57:48 +03001504 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001505 free(surface);
1506}
1507
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001508void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001509window_destroy(struct window *window)
1510{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001511 struct display *display = window->display;
1512 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001513 struct window_output *window_output;
1514 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001515
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001516 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001517
Rusty Lynch1084da52013-08-15 09:10:08 -07001518 wl_list_for_each(input, &display->input_list, link) {
1519 if (input->touch_focus == window)
1520 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001521 if (input->pointer_focus == window)
1522 input->pointer_focus = NULL;
1523 if (input->keyboard_focus == window)
1524 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001525 if (input->focus_widget &&
1526 input->focus_widget->window == window)
1527 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001528 }
1529
Rob Bradford7507b572012-05-15 17:55:34 +01001530 wl_list_for_each_safe(window_output, window_output_tmp,
1531 &window->window_output_list, link) {
1532 free (window_output);
1533 }
1534
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001535 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001536 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001537
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001538 if (window->shell_surface)
1539 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001540
1541 surface_destroy(window->main_surface);
1542
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001543 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001544
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001545 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001546 free(window);
1547}
1548
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001549static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001550widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001551{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001552 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001553
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001554 wl_list_for_each(child, &widget->child_list, link) {
1555 target = widget_find_widget(child, x, y);
1556 if (target)
1557 return target;
1558 }
1559
1560 if (widget->allocation.x <= x &&
1561 x < widget->allocation.x + widget->allocation.width &&
1562 widget->allocation.y <= y &&
1563 y < widget->allocation.y + widget->allocation.height) {
1564 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001565 }
1566
1567 return NULL;
1568}
1569
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001570static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001571window_find_widget(struct window *window, int32_t x, int32_t y)
1572{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001573 struct surface *surface;
1574 struct widget *widget;
1575
1576 wl_list_for_each(surface, &window->subsurface_list, link) {
1577 widget = widget_find_widget(surface->widget, x, y);
1578 if (widget)
1579 return widget;
1580 }
1581
1582 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001583}
1584
1585static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001586widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001587{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001588 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001589
Peter Huttererf3d62272013-08-08 11:57:05 +10001590 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001591 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001592 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001593 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001594 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001595 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001596 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001597 widget->tooltip = NULL;
1598 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001599 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001600
1601 return widget;
1602}
1603
1604struct widget *
1605window_add_widget(struct window *window, void *data)
1606{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001607 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001608
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001609 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001610 wl_list_init(&widget->link);
1611 window->main_surface->widget = widget;
1612
1613 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001614}
1615
1616struct widget *
1617widget_add_widget(struct widget *parent, void *data)
1618{
1619 struct widget *widget;
1620
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001621 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001622 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001623
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001624 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001625}
1626
1627void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001628widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001629{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001630 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001631 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001632 struct input *input;
1633
Pekka Paalanen35e82632013-04-25 13:57:48 +03001634 /* Destroy the sub-surface along with the root widget */
1635 if (surface->widget == widget && surface->subsurface)
1636 surface_destroy(widget->surface);
1637
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001638 if (widget->tooltip) {
1639 free(widget->tooltip);
1640 widget->tooltip = NULL;
1641 }
1642
Pekka Paalanene156fb62012-01-19 13:51:38 +02001643 wl_list_for_each(input, &display->input_list, link) {
1644 if (input->focus_widget == widget)
1645 input->focus_widget = NULL;
1646 }
1647
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001648 wl_list_remove(&widget->link);
1649 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001650}
1651
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001652void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001653widget_set_default_cursor(struct widget *widget, int cursor)
1654{
1655 widget->default_cursor = cursor;
1656}
1657
1658void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001659widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001660{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001661 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001662}
1663
1664void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001665widget_set_size(struct widget *widget, int32_t width, int32_t height)
1666{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001667 widget->allocation.width = width;
1668 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001669}
1670
1671void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001672widget_set_allocation(struct widget *widget,
1673 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001674{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001675 widget->allocation.x = x;
1676 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001677 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001678}
1679
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001680void
1681widget_set_transparent(struct widget *widget, int transparent)
1682{
1683 widget->opaque = !transparent;
1684}
1685
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001687widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001688{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001689 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690}
1691
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001692static cairo_surface_t *
1693widget_get_cairo_surface(struct widget *widget)
1694{
1695 struct surface *surface = widget->surface;
1696 struct window *window = widget->window;
1697
1698 if (!surface->cairo_surface) {
1699 if (surface == window->main_surface)
1700 window_create_main_surface(window);
1701 else
1702 surface_create_surface(surface, 0, 0, 0);
1703 }
1704
1705 return surface->cairo_surface;
1706}
1707
Alexander Larsson15901f02013-05-22 14:41:25 +02001708static void
1709widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1710{
1711 struct surface *surface = widget->surface;
1712 double angle;
1713 cairo_matrix_t m;
1714 enum wl_output_transform transform;
1715 int surface_width, surface_height;
1716 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001717 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001718
1719 surface_width = surface->allocation.width;
1720 surface_height = surface->allocation.height;
1721
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001722 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001723 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001724
Alexander Larsson15901f02013-05-22 14:41:25 +02001725 switch (transform) {
1726 case WL_OUTPUT_TRANSFORM_FLIPPED:
1727 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1728 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1729 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1730 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1731 break;
1732 default:
1733 cairo_matrix_init_identity(&m);
1734 break;
1735 }
1736
1737 switch (transform) {
1738 case WL_OUTPUT_TRANSFORM_NORMAL:
1739 default:
1740 angle = 0;
1741 translate_x = 0;
1742 translate_y = 0;
1743 break;
1744 case WL_OUTPUT_TRANSFORM_FLIPPED:
1745 angle = 0;
1746 translate_x = surface_width;
1747 translate_y = 0;
1748 break;
1749 case WL_OUTPUT_TRANSFORM_90:
1750 angle = M_PI_2;
1751 translate_x = surface_height;
1752 translate_y = 0;
1753 break;
1754 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1755 angle = M_PI_2;
1756 translate_x = surface_height;
1757 translate_y = surface_width;
1758 break;
1759 case WL_OUTPUT_TRANSFORM_180:
1760 angle = M_PI;
1761 translate_x = surface_width;
1762 translate_y = surface_height;
1763 break;
1764 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1765 angle = M_PI;
1766 translate_x = 0;
1767 translate_y = surface_height;
1768 break;
1769 case WL_OUTPUT_TRANSFORM_270:
1770 angle = M_PI + M_PI_2;
1771 translate_x = 0;
1772 translate_y = surface_width;
1773 break;
1774 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1775 angle = M_PI + M_PI_2;
1776 translate_x = 0;
1777 translate_y = 0;
1778 break;
1779 }
1780
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001781 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001782 cairo_translate(cr, translate_x, translate_y);
1783 cairo_rotate(cr, angle);
1784 cairo_transform(cr, &m);
1785}
1786
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001787cairo_t *
1788widget_cairo_create(struct widget *widget)
1789{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001790 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001791 cairo_surface_t *cairo_surface;
1792 cairo_t *cr;
1793
1794 cairo_surface = widget_get_cairo_surface(widget);
1795 cr = cairo_create(cairo_surface);
1796
Alexander Larsson15901f02013-05-22 14:41:25 +02001797 widget_cairo_update_transform(widget, cr);
1798
Pekka Paalanen35e82632013-04-25 13:57:48 +03001799 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1800
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001801 return cr;
1802}
1803
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001804struct wl_surface *
1805widget_get_wl_surface(struct widget *widget)
1806{
1807 return widget->surface->surface;
1808}
1809
1810uint32_t
1811widget_get_last_time(struct widget *widget)
1812{
1813 return widget->surface->last_time;
1814}
1815
1816void
1817widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1818{
1819 struct wl_compositor *comp = widget->window->display->compositor;
1820 struct surface *surface = widget->surface;
1821
1822 if (!surface->input_region)
1823 surface->input_region = wl_compositor_create_region(comp);
1824
1825 if (rect) {
1826 wl_region_add(surface->input_region,
1827 rect->x, rect->y, rect->width, rect->height);
1828 }
1829}
1830
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001831void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001832widget_set_resize_handler(struct widget *widget,
1833 widget_resize_handler_t handler)
1834{
1835 widget->resize_handler = handler;
1836}
1837
1838void
1839widget_set_redraw_handler(struct widget *widget,
1840 widget_redraw_handler_t handler)
1841{
1842 widget->redraw_handler = handler;
1843}
1844
1845void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001846widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001847{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001848 widget->enter_handler = handler;
1849}
1850
1851void
1852widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1853{
1854 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001855}
1856
1857void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001858widget_set_motion_handler(struct widget *widget,
1859 widget_motion_handler_t handler)
1860{
1861 widget->motion_handler = handler;
1862}
1863
1864void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001865widget_set_button_handler(struct widget *widget,
1866 widget_button_handler_t handler)
1867{
1868 widget->button_handler = handler;
1869}
1870
1871void
Rusty Lynch041815a2013-08-08 21:20:38 -07001872widget_set_touch_up_handler(struct widget *widget,
1873 widget_touch_up_handler_t handler)
1874{
1875 widget->touch_up_handler = handler;
1876}
1877
1878void
1879widget_set_touch_down_handler(struct widget *widget,
1880 widget_touch_down_handler_t handler)
1881{
1882 widget->touch_down_handler = handler;
1883}
1884
1885void
1886widget_set_touch_motion_handler(struct widget *widget,
1887 widget_touch_motion_handler_t handler)
1888{
1889 widget->touch_motion_handler = handler;
1890}
1891
1892void
1893widget_set_touch_frame_handler(struct widget *widget,
1894 widget_touch_frame_handler_t handler)
1895{
1896 widget->touch_frame_handler = handler;
1897}
1898
1899void
1900widget_set_touch_cancel_handler(struct widget *widget,
1901 widget_touch_cancel_handler_t handler)
1902{
1903 widget->touch_cancel_handler = handler;
1904}
1905
1906void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001907widget_set_axis_handler(struct widget *widget,
1908 widget_axis_handler_t handler)
1909{
1910 widget->axis_handler = handler;
1911}
1912
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001913static void
1914window_schedule_redraw_task(struct window *window);
1915
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001916void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001917widget_schedule_redraw(struct widget *widget)
1918{
Pekka Paalanen71233882013-04-25 13:57:53 +03001919 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001920 widget->surface->redraw_needed = 1;
1921 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001922}
1923
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001924cairo_surface_t *
1925window_get_surface(struct window *window)
1926{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001927 cairo_surface_t *cairo_surface;
1928
1929 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1930
1931 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001932}
1933
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001934struct wl_surface *
1935window_get_wl_surface(struct window *window)
1936{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001937 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001938}
1939
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001940struct wl_shell_surface *
1941window_get_wl_shell_surface(struct window *window)
1942{
1943 return window->shell_surface;
1944}
1945
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001946static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001947tooltip_redraw_handler(struct widget *widget, void *data)
1948{
1949 cairo_t *cr;
1950 const int32_t r = 3;
1951 struct tooltip *tooltip = data;
1952 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001953
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001954 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001955 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1956 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1957 cairo_paint(cr);
1958
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001959 width = widget->allocation.width;
1960 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001961 rounded_rect(cr, 0, 0, width, height, r);
1962
1963 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1964 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1965 cairo_fill(cr);
1966
1967 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1968 cairo_move_to(cr, 10, 16);
1969 cairo_show_text(cr, tooltip->entry);
1970 cairo_destroy(cr);
1971}
1972
1973static cairo_text_extents_t
1974get_text_extents(struct tooltip *tooltip)
1975{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001976 cairo_t *cr;
1977 cairo_text_extents_t extents;
1978
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001979 /* Use the dummy_surface because tooltip's surface was not
1980 * created yet, and parent does not have a valid surface
1981 * outside repaint, either.
1982 */
1983 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001984 cairo_text_extents(cr, tooltip->entry, &extents);
1985 cairo_destroy(cr);
1986
1987 return extents;
1988}
1989
1990static int
1991window_create_tooltip(struct tooltip *tooltip)
1992{
1993 struct widget *parent = tooltip->parent;
1994 struct display *display = parent->window->display;
1995 struct window *window;
1996 const int offset_y = 27;
1997 const int margin = 3;
1998 cairo_text_extents_t extents;
1999
2000 if (tooltip->widget)
2001 return 0;
2002
2003 window = window_create_transient(display, parent->window, tooltip->x,
2004 tooltip->y + offset_y,
2005 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2006 if (!window)
2007 return -1;
2008
2009 tooltip->window = window;
2010 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2011
2012 extents = get_text_extents(tooltip);
2013 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2014 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2015
2016 return 0;
2017}
2018
2019void
2020widget_destroy_tooltip(struct widget *parent)
2021{
2022 struct tooltip *tooltip = parent->tooltip;
2023
2024 parent->tooltip_count = 0;
2025 if (!tooltip)
2026 return;
2027
2028 if (tooltip->widget) {
2029 widget_destroy(tooltip->widget);
2030 window_destroy(tooltip->window);
2031 tooltip->widget = NULL;
2032 tooltip->window = NULL;
2033 }
2034
2035 close(tooltip->tooltip_fd);
2036 free(tooltip->entry);
2037 free(tooltip);
2038 parent->tooltip = NULL;
2039}
2040
2041static void
2042tooltip_func(struct task *task, uint32_t events)
2043{
2044 struct tooltip *tooltip =
2045 container_of(task, struct tooltip, tooltip_task);
2046 uint64_t exp;
2047
Martin Olsson8df662a2012-07-08 03:03:47 +02002048 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2049 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002050 window_create_tooltip(tooltip);
2051}
2052
2053#define TOOLTIP_TIMEOUT 500
2054static int
2055tooltip_timer_reset(struct tooltip *tooltip)
2056{
2057 struct itimerspec its;
2058
2059 its.it_interval.tv_sec = 0;
2060 its.it_interval.tv_nsec = 0;
2061 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2062 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2063 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2064 fprintf(stderr, "could not set timerfd\n: %m");
2065 return -1;
2066 }
2067
2068 return 0;
2069}
2070
2071int
2072widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2073{
2074 struct tooltip *tooltip = parent->tooltip;
2075
2076 parent->tooltip_count++;
2077 if (tooltip) {
2078 tooltip->x = x;
2079 tooltip->y = y;
2080 tooltip_timer_reset(tooltip);
2081 return 0;
2082 }
2083
2084 /* the handler might be triggered too fast via input device motion, so
2085 * we need this check here to make sure tooltip is fully initialized */
2086 if (parent->tooltip_count > 1)
2087 return 0;
2088
2089 tooltip = malloc(sizeof *tooltip);
2090 if (!tooltip)
2091 return -1;
2092
2093 parent->tooltip = tooltip;
2094 tooltip->parent = parent;
2095 tooltip->widget = NULL;
2096 tooltip->window = NULL;
2097 tooltip->x = x;
2098 tooltip->y = y;
2099 tooltip->entry = strdup(entry);
2100 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2101 if (tooltip->tooltip_fd < 0) {
2102 fprintf(stderr, "could not create timerfd\n: %m");
2103 return -1;
2104 }
2105
2106 tooltip->tooltip_task.run = tooltip_func;
2107 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2108 EPOLLIN, &tooltip->tooltip_task);
2109 tooltip_timer_reset(tooltip);
2110
2111 return 0;
2112}
2113
2114static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002115workspace_manager_state(void *data,
2116 struct workspace_manager *workspace_manager,
2117 uint32_t current,
2118 uint32_t count)
2119{
2120 struct display *display = data;
2121
2122 display->workspace = current;
2123 display->workspace_count = count;
2124}
2125
2126static const struct workspace_manager_listener workspace_manager_listener = {
2127 workspace_manager_state
2128};
2129
2130static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002131frame_resize_handler(struct widget *widget,
2132 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002133{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002134 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002135 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002136 struct rectangle interior;
2137 struct rectangle input;
2138 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002139
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002140 if (widget->window->type == TYPE_FULLSCREEN) {
2141 interior.x = 0;
2142 interior.y = 0;
2143 interior.width = width;
2144 interior.height = height;
2145 } else {
2146 if (widget->window->type == TYPE_MAXIMIZED) {
2147 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2148 } else {
2149 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2150 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002151
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002152 frame_resize(frame->frame, width, height);
2153 frame_interior(frame->frame, &interior.x, &interior.y,
2154 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002155 }
2156
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002157 widget_set_allocation(child, interior.x, interior.y,
2158 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002159
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002160 if (child->resize_handler) {
2161 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002162 child->user_data);
2163
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002164 if (widget->window->type == TYPE_FULLSCREEN) {
2165 width = child->allocation.width;
2166 height = child->allocation.height;
2167 } else {
2168 frame_resize_inside(frame->frame,
2169 child->allocation.width,
2170 child->allocation.height);
2171 width = frame_width(frame->frame);
2172 height = frame_height(frame->frame);
2173 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002174 }
2175
Scott Moreauf7e498c2012-05-14 11:39:29 -06002176 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002177
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002178 widget->surface->input_region =
2179 wl_compositor_create_region(widget->window->display->compositor);
2180 if (widget->window->type != TYPE_FULLSCREEN) {
2181 frame_input_rect(frame->frame, &input.x, &input.y,
2182 &input.width, &input.height);
2183 wl_region_add(widget->surface->input_region,
2184 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002185 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002186 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002187 }
2188
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002189 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002190
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002191 if (child->opaque) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002192 if (widget->window->type != TYPE_FULLSCREEN) {
2193 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2194 &opaque.width, &opaque.height);
2195
2196 wl_region_add(widget->surface->opaque_region,
2197 opaque.x, opaque.y,
2198 opaque.width, opaque.height);
2199 } else {
2200 wl_region_add(widget->surface->opaque_region,
2201 0, 0, width, height);
2202 }
Martin Minarik1998b152012-05-10 02:04:35 +02002203 }
2204
Martin Minarik1998b152012-05-10 02:04:35 +02002205
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002206 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002207}
2208
2209static void
2210frame_redraw_handler(struct widget *widget, void *data)
2211{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002213 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002214 struct window *window = widget->window;
2215
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002216 if (window->type == TYPE_FULLSCREEN)
2217 return;
2218
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002219 if (window->focus_count) {
2220 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2221 } else {
2222 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2223 }
2224
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002225 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002227 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002228
2229 cairo_destroy(cr);
2230}
2231
2232static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002233frame_get_pointer_image_for_location(struct window_frame *frame,
2234 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002235{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002236 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002237
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002238 if (window->type != TYPE_TOPLEVEL)
2239 return CURSOR_LEFT_PTR;
2240
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002241 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002242 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002243 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002244 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002245 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002246 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002247 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002248 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002249 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002250 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002251 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002252 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002253 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002254 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002255 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002256 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002257 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002258 case THEME_LOCATION_EXTERIOR:
2259 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002260 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002261 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002262 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002263}
2264
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002265static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002266frame_menu_func(struct window *window,
2267 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002268{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002269 struct display *display;
2270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002271 switch (index) {
2272 case 0: /* close */
2273 if (window->close_handler)
2274 window->close_handler(window->parent,
2275 window->user_data);
2276 else
2277 display_exit(window->display);
2278 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002279 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002280 display = window->display;
2281 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002282 workspace_manager_move_surface(
2283 display->workspace_manager,
2284 window->main_surface->surface,
2285 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002286 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002287 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002288 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002289 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002290 workspace_manager_move_surface(
2291 display->workspace_manager,
2292 window->main_surface->surface,
2293 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002294 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002295 case 3: /* fullscreen */
2296 /* we don't have a way to get out of fullscreen for now */
2297 if (window->fullscreen_handler)
2298 window->fullscreen_handler(window, window->user_data);
2299 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002300 }
2301}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002302
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002303void
2304window_show_frame_menu(struct window *window,
2305 struct input *input, uint32_t time)
2306{
2307 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002308 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002309
2310 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002311 "Close",
2312 "Move to workspace above", "Move to workspace below",
2313 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002314 };
2315
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002316 if (window->fullscreen_handler)
2317 count = ARRAY_LENGTH(entries);
2318 else
2319 count = ARRAY_LENGTH(entries) - 1;
2320
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002321 input_get_position(input, &x, &y);
2322 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002323 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002324}
2325
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002326static int
2327frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002328 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002329{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002330 struct window_frame *frame = data;
2331 enum theme_location location;
2332
2333 location = frame_pointer_enter(frame->frame, input, x, y);
2334 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2335 widget_schedule_redraw(frame->widget);
2336
2337 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002338}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002339
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002340static int
2341frame_motion_handler(struct widget *widget,
2342 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002343 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002344{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002345 struct window_frame *frame = data;
2346 enum theme_location location;
2347
2348 location = frame_pointer_motion(frame->frame, input, x, y);
2349 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2350 widget_schedule_redraw(frame->widget);
2351
2352 return frame_get_pointer_image_for_location(data, location);
2353}
2354
2355static void
2356frame_leave_handler(struct widget *widget,
2357 struct input *input, void *data)
2358{
2359 struct window_frame *frame = data;
2360
2361 frame_pointer_leave(frame->frame, input);
2362 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2363 widget_schedule_redraw(frame->widget);
2364}
2365
2366static void
2367frame_handle_status(struct window_frame *frame, struct input *input,
2368 uint32_t time, enum theme_location location)
2369{
2370 struct window *window = frame->widget->window;
2371 uint32_t status;
2372
2373 status = frame_status(frame->frame);
2374 if (status & FRAME_STATUS_REPAINT)
2375 widget_schedule_redraw(frame->widget);
2376
2377 if (status & FRAME_STATUS_MINIMIZE)
2378 fprintf(stderr,"Minimize stub\n");
2379
2380 if (status & FRAME_STATUS_MENU) {
2381 window_show_frame_menu(window, input, time);
2382 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2383 }
2384
2385 if (status & FRAME_STATUS_MAXIMIZE) {
2386 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2387 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2388 }
2389
2390 if (status & FRAME_STATUS_CLOSE) {
2391 if (window->close_handler)
2392 window->close_handler(window->parent,
2393 window->user_data);
2394 else
2395 display_exit(window->display);
2396 frame_status_clear(frame->frame, FRAME_STATUS_CLOSE);
2397 }
2398
2399 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2400 input_ungrab(input);
2401 wl_shell_surface_move(window->shell_surface,
2402 input_get_seat(input),
2403 window->display->serial);
2404
2405 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2406 }
2407
2408 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2409 input_ungrab(input);
2410
2411 window->resizing = 1;
2412 wl_shell_surface_resize(window->shell_surface,
2413 input_get_seat(input),
2414 window->display->serial,
2415 location);
2416
2417 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2418 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002419}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002420
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002421static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002422frame_button_handler(struct widget *widget,
2423 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002424 uint32_t button, enum wl_pointer_button_state state,
2425 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002426
2427{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002428 struct window_frame *frame = data;
2429 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002430
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002431 location = frame_pointer_button(frame->frame, input, button, state);
2432 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002433}
2434
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002435static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002436frame_touch_down_handler(struct widget *widget, struct input *input,
2437 uint32_t serial, uint32_t time, int32_t id,
2438 float x, float y, void *data)
2439{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002440 struct window_frame *frame = data;
2441
2442 frame_touch_down(frame->frame, input, id, x, y);
2443 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2444}
2445
2446static void
2447frame_touch_up_handler(struct widget *widget,
2448 struct input *input, uint32_t serial, uint32_t time,
2449 int32_t id, void *data)
2450{
2451 struct window_frame *frame = data;
2452
2453 frame_touch_up(frame->frame, input, id);
2454 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002455}
2456
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002457struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002458window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002459{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002460 struct window_frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002461
Peter Huttererf3d62272013-08-08 11:57:05 +10002462 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002463 frame->frame = frame_create(window->display->theme, 0, 0,
2464 FRAME_BUTTON_ALL, window->title);
2465
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002466 frame->widget = window_add_widget(window, frame);
2467 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002468
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002469 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2470 widget_set_resize_handler(frame->widget, frame_resize_handler);
2471 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002472 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002473 widget_set_motion_handler(frame->widget, frame_motion_handler);
2474 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002475 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002476 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002477
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002478 window->frame = frame;
2479
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002480 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002481}
2482
Kristian Høgsberga1627922012-06-20 17:30:03 -04002483void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002484window_frame_set_child_size(struct widget *widget, int child_width,
2485 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002486{
2487 struct display *display = widget->window->display;
2488 struct theme *t = display->theme;
2489 int decoration_width, decoration_height;
2490 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002491 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002492
2493 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002494 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002495 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002496 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002497
2498 width = child_width + decoration_width;
2499 height = child_height + decoration_height;
2500 } else {
2501 width = child_width;
2502 height = child_height;
2503 }
2504
2505 window_schedule_resize(widget->window, width, height);
2506}
2507
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002508static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002509window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002510{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002511 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002512
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002513 /* frame->child must be destroyed by the application */
2514 widget_destroy(frame->widget);
2515 free(frame);
2516}
2517
2518static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002519input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002520 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002521{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002522 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002523 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002524
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002525 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002526 return;
2527
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002528 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002529 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002530 widget = old;
2531 if (input->grab)
2532 widget = input->grab;
2533 if (widget->leave_handler)
2534 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002535 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002536 }
2537
2538 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002539 widget = focus;
2540 if (input->grab)
2541 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002542 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002543 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002544 cursor = widget->enter_handler(focus, input, x, y,
2545 widget->user_data);
2546 else
2547 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002548
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002549 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002550 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002551}
2552
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002553void
2554input_grab(struct input *input, struct widget *widget, uint32_t button)
2555{
2556 input->grab = widget;
2557 input->grab_button = button;
2558}
2559
2560void
2561input_ungrab(struct input *input)
2562{
2563 struct widget *widget;
2564
2565 input->grab = NULL;
2566 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002567 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002568 input->sx, input->sy);
2569 input_set_focus_widget(input, widget, input->sx, input->sy);
2570 }
2571}
2572
2573static void
2574input_remove_pointer_focus(struct input *input)
2575{
2576 struct window *window = input->pointer_focus;
2577
2578 if (!window)
2579 return;
2580
2581 input_set_focus_widget(input, NULL, 0, 0);
2582
2583 input->pointer_focus = NULL;
2584 input->current_cursor = CURSOR_UNSET;
2585}
2586
2587static void
2588pointer_handle_enter(void *data, struct wl_pointer *pointer,
2589 uint32_t serial, struct wl_surface *surface,
2590 wl_fixed_t sx_w, wl_fixed_t sy_w)
2591{
2592 struct input *input = data;
2593 struct window *window;
2594 struct widget *widget;
2595 float sx = wl_fixed_to_double(sx_w);
2596 float sy = wl_fixed_to_double(sy_w);
2597
2598 if (!surface) {
2599 /* enter event for a window we've just destroyed */
2600 return;
2601 }
2602
2603 input->display->serial = serial;
2604 input->pointer_enter_serial = serial;
2605 input->pointer_focus = wl_surface_get_user_data(surface);
2606 window = input->pointer_focus;
2607
Pekka Paalanen99436862012-11-19 17:15:59 +02002608 if (window->resizing) {
2609 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002610 /* Schedule a redraw to free the pool */
2611 window_schedule_redraw(window);
2612 }
2613
2614 input->sx = sx;
2615 input->sy = sy;
2616
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002617 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002618 input_set_focus_widget(input, widget, sx, sy);
2619}
2620
2621static void
2622pointer_handle_leave(void *data, struct wl_pointer *pointer,
2623 uint32_t serial, struct wl_surface *surface)
2624{
2625 struct input *input = data;
2626
2627 input->display->serial = serial;
2628 input_remove_pointer_focus(input);
2629}
2630
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002631static void
Daniel Stone37816df2012-05-16 18:45:18 +01002632pointer_handle_motion(void *data, struct wl_pointer *pointer,
2633 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002634{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002635 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002636 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002637 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002638 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002639 float sx = wl_fixed_to_double(sx_w);
2640 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002641
Paul Winwoodb22bf572013-08-29 10:52:54 +01002642 input->sx = sx;
2643 input->sy = sy;
2644
2645 if (!window)
2646 return;
2647
Rob Bradford5f087742013-07-11 19:41:27 +01002648 /* when making the window smaller - e.g. after a unmaximise we might
2649 * still have a pending motion event that the compositor has picked
2650 * based on the old surface dimensions
2651 */
2652 if (sx > window->main_surface->allocation.width ||
2653 sy > window->main_surface->allocation.height)
2654 return;
2655
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002656 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002657 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002658 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002659 }
2660
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002661 if (input->grab)
2662 widget = input->grab;
2663 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002664 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002665 if (widget) {
2666 if (widget->motion_handler)
2667 cursor = widget->motion_handler(input->focus_widget,
2668 input, time, sx, sy,
2669 widget->user_data);
2670 else
2671 cursor = widget->default_cursor;
2672 } else
2673 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002674
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002675 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002676}
2677
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002678static void
Daniel Stone37816df2012-05-16 18:45:18 +01002679pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002680 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002681{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002682 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002683 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002684 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002685
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002686 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002687 if (input->focus_widget && input->grab == NULL &&
2688 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002689 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002690
Neil Roberts6b28aad2012-01-23 19:11:18 +00002691 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002692 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002693 (*widget->button_handler)(widget,
2694 input, time,
2695 button, state,
2696 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002697
Daniel Stone4dbadb12012-05-30 16:31:51 +01002698 if (input->grab && input->grab_button == button &&
2699 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002700 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002701}
2702
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002703static void
Daniel Stone37816df2012-05-16 18:45:18 +01002704pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002705 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002706{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002707 struct input *input = data;
2708 struct widget *widget;
2709
2710 widget = input->focus_widget;
2711 if (input->grab)
2712 widget = input->grab;
2713 if (widget && widget->axis_handler)
2714 (*widget->axis_handler)(widget,
2715 input, time,
2716 axis, value,
2717 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002718}
2719
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002720static const struct wl_pointer_listener pointer_listener = {
2721 pointer_handle_enter,
2722 pointer_handle_leave,
2723 pointer_handle_motion,
2724 pointer_handle_button,
2725 pointer_handle_axis,
2726};
2727
2728static void
2729input_remove_keyboard_focus(struct input *input)
2730{
2731 struct window *window = input->keyboard_focus;
2732 struct itimerspec its;
2733
2734 its.it_interval.tv_sec = 0;
2735 its.it_interval.tv_nsec = 0;
2736 its.it_value.tv_sec = 0;
2737 its.it_value.tv_nsec = 0;
2738 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2739
2740 if (!window)
2741 return;
2742
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002743 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002744 if (window->keyboard_focus_handler)
2745 (*window->keyboard_focus_handler)(window, NULL,
2746 window->user_data);
2747
2748 input->keyboard_focus = NULL;
2749}
2750
2751static void
2752keyboard_repeat_func(struct task *task, uint32_t events)
2753{
2754 struct input *input =
2755 container_of(task, struct input, repeat_task);
2756 struct window *window = input->keyboard_focus;
2757 uint64_t exp;
2758
2759 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2760 /* If we change the timer between the fd becoming
2761 * readable and getting here, there'll be nothing to
2762 * read and we get EAGAIN. */
2763 return;
2764
2765 if (window && window->key_handler) {
2766 (*window->key_handler)(window, input, input->repeat_time,
2767 input->repeat_key, input->repeat_sym,
2768 WL_KEYBOARD_KEY_STATE_PRESSED,
2769 window->user_data);
2770 }
2771}
2772
2773static void
2774keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2775 uint32_t format, int fd, uint32_t size)
2776{
2777 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002778 struct xkb_keymap *keymap;
2779 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002780 char *map_str;
2781
2782 if (!data) {
2783 close(fd);
2784 return;
2785 }
2786
2787 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2788 close(fd);
2789 return;
2790 }
2791
2792 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2793 if (map_str == MAP_FAILED) {
2794 close(fd);
2795 return;
2796 }
2797
Rui Matos3eccb862013-10-10 19:44:22 +02002798 keymap = xkb_map_new_from_string(input->display->xkb_context,
2799 map_str,
2800 XKB_KEYMAP_FORMAT_TEXT_V1,
2801 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002802 munmap(map_str, size);
2803 close(fd);
2804
Rui Matos3eccb862013-10-10 19:44:22 +02002805 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002806 fprintf(stderr, "failed to compile keymap\n");
2807 return;
2808 }
2809
Rui Matos3eccb862013-10-10 19:44:22 +02002810 state = xkb_state_new(keymap);
2811 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002812 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002813 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002814 return;
2815 }
2816
Rui Matos3eccb862013-10-10 19:44:22 +02002817 xkb_keymap_unref(input->xkb.keymap);
2818 xkb_state_unref(input->xkb.state);
2819 input->xkb.keymap = keymap;
2820 input->xkb.state = state;
2821
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002822 input->xkb.control_mask =
2823 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2824 input->xkb.alt_mask =
2825 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2826 input->xkb.shift_mask =
2827 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2828}
2829
2830static void
2831keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2832 uint32_t serial, struct wl_surface *surface,
2833 struct wl_array *keys)
2834{
2835 struct input *input = data;
2836 struct window *window;
2837
2838 input->display->serial = serial;
2839 input->keyboard_focus = wl_surface_get_user_data(surface);
2840
2841 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002842 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002843 if (window->keyboard_focus_handler)
2844 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002845 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002846}
2847
2848static void
2849keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2850 uint32_t serial, struct wl_surface *surface)
2851{
2852 struct input *input = data;
2853
2854 input->display->serial = serial;
2855 input_remove_keyboard_focus(input);
2856}
2857
Scott Moreau210d0792012-03-22 10:47:01 -06002858static void
Daniel Stone37816df2012-05-16 18:45:18 +01002859keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002860 uint32_t serial, uint32_t time, uint32_t key,
2861 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002862{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002863 struct input *input = data;
2864 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002865 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002866 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002867 const xkb_keysym_t *syms;
2868 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002869 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002870
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002871 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002872 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002873 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002874 return;
2875
Daniel Stone97f68542012-05-30 16:32:01 +01002876 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002877
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002878 sym = XKB_KEY_NoSymbol;
2879 if (num_syms == 1)
2880 sym = syms[0];
2881
2882 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002883 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002884 window_set_maximized(window,
2885 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002886 } else if (sym == XKB_KEY_F11 &&
2887 window->fullscreen_handler &&
2888 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2889 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002890 } else if (sym == XKB_KEY_F4 &&
2891 input->modifiers == MOD_ALT_MASK &&
2892 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2893 if (window->close_handler)
2894 window->close_handler(window->parent,
2895 window->user_data);
2896 else
2897 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002898 } else if (window->key_handler) {
2899 (*window->key_handler)(window, input, time, key,
2900 sym, state, window->user_data);
2901 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002902
2903 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2904 key == input->repeat_key) {
2905 its.it_interval.tv_sec = 0;
2906 its.it_interval.tv_nsec = 0;
2907 its.it_value.tv_sec = 0;
2908 its.it_value.tv_nsec = 0;
2909 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2910 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2911 input->repeat_sym = sym;
2912 input->repeat_key = key;
2913 input->repeat_time = time;
2914 its.it_interval.tv_sec = 0;
2915 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2916 its.it_value.tv_sec = 0;
2917 its.it_value.tv_nsec = 400 * 1000 * 1000;
2918 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2919 }
2920}
2921
2922static void
Daniel Stone351eb612012-05-31 15:27:47 -04002923keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2924 uint32_t serial, uint32_t mods_depressed,
2925 uint32_t mods_latched, uint32_t mods_locked,
2926 uint32_t group)
2927{
2928 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002929 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002930
Matt Ropere61561f2013-06-24 16:52:43 +01002931 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2932 if (!input->xkb.keymap)
2933 return;
2934
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002935 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2936 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002937 mask = xkb_state_serialize_mods(input->xkb.state,
2938 XKB_STATE_DEPRESSED |
2939 XKB_STATE_LATCHED);
2940 input->modifiers = 0;
2941 if (mask & input->xkb.control_mask)
2942 input->modifiers |= MOD_CONTROL_MASK;
2943 if (mask & input->xkb.alt_mask)
2944 input->modifiers |= MOD_ALT_MASK;
2945 if (mask & input->xkb.shift_mask)
2946 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002947}
2948
Daniel Stone37816df2012-05-16 18:45:18 +01002949static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002950 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002951 keyboard_handle_enter,
2952 keyboard_handle_leave,
2953 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002954 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002955};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002956
2957static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002958touch_handle_down(void *data, struct wl_touch *wl_touch,
2959 uint32_t serial, uint32_t time, struct wl_surface *surface,
2960 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2961{
2962 struct input *input = data;
2963 struct widget *widget;
2964 float sx = wl_fixed_to_double(x_w);
2965 float sy = wl_fixed_to_double(y_w);
2966
Rusty Lynch1084da52013-08-15 09:10:08 -07002967 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002968 input->touch_focus = wl_surface_get_user_data(surface);
2969 if (!input->touch_focus) {
2970 DBG("Failed to find to touch focus for surface %p\n", surface);
2971 return;
2972 }
2973
2974 widget = window_find_widget(input->touch_focus,
2975 wl_fixed_to_double(x_w),
2976 wl_fixed_to_double(y_w));
2977 if (widget) {
2978 struct touch_point *tp = xmalloc(sizeof *tp);
2979 if (tp) {
2980 tp->id = id;
2981 tp->widget = widget;
2982 wl_list_insert(&input->touch_point_list, &tp->link);
2983
2984 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07002985 (*widget->touch_down_handler)(widget, input,
2986 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07002987 sx, sy,
2988 widget->user_data);
2989 }
2990 }
2991}
2992
2993static void
2994touch_handle_up(void *data, struct wl_touch *wl_touch,
2995 uint32_t serial, uint32_t time, int32_t id)
2996{
2997 struct input *input = data;
2998 struct touch_point *tp, *tmp;
2999
Rusty Lynch041815a2013-08-08 21:20:38 -07003000 if (!input->touch_focus) {
3001 DBG("No touch focus found for touch up event!\n");
3002 return;
3003 }
3004
3005 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3006 if (tp->id != id)
3007 continue;
3008
3009 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003010 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003011 time, id,
3012 tp->widget->user_data);
3013
3014 wl_list_remove(&tp->link);
3015 free(tp);
3016
3017 return;
3018 }
3019}
3020
3021static void
3022touch_handle_motion(void *data, struct wl_touch *wl_touch,
3023 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3024{
3025 struct input *input = data;
3026 struct touch_point *tp;
3027 float sx = wl_fixed_to_double(x_w);
3028 float sy = wl_fixed_to_double(y_w);
3029
3030 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3031
3032 if (!input->touch_focus) {
3033 DBG("No touch focus found for touch motion event!\n");
3034 return;
3035 }
3036
3037 wl_list_for_each(tp, &input->touch_point_list, link) {
3038 if (tp->id != id)
3039 continue;
3040
3041 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003042 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003043 id, sx, sy,
3044 tp->widget->user_data);
3045 return;
3046 }
3047}
3048
3049static void
3050touch_handle_frame(void *data, struct wl_touch *wl_touch)
3051{
3052 struct input *input = data;
3053 struct touch_point *tp, *tmp;
3054
3055 DBG("touch_handle_frame\n");
3056
3057 if (!input->touch_focus) {
3058 DBG("No touch focus found for touch frame event!\n");
3059 return;
3060 }
3061
3062 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3063 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003064 (*tp->widget->touch_frame_handler)(tp->widget, input,
3065 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003066
3067 wl_list_remove(&tp->link);
3068 free(tp);
3069 }
3070}
3071
3072static void
3073touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3074{
3075 struct input *input = data;
3076 struct touch_point *tp, *tmp;
3077
3078 DBG("touch_handle_cancel\n");
3079
3080 if (!input->touch_focus) {
3081 DBG("No touch focus found for touch cancel event!\n");
3082 return;
3083 }
3084
3085 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3086 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003087 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3088 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003089
3090 wl_list_remove(&tp->link);
3091 free(tp);
3092 }
3093}
3094
3095static const struct wl_touch_listener touch_listener = {
3096 touch_handle_down,
3097 touch_handle_up,
3098 touch_handle_motion,
3099 touch_handle_frame,
3100 touch_handle_cancel,
3101};
3102
3103static void
Daniel Stone37816df2012-05-16 18:45:18 +01003104seat_handle_capabilities(void *data, struct wl_seat *seat,
3105 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003106{
Daniel Stone37816df2012-05-16 18:45:18 +01003107 struct input *input = data;
3108
3109 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3110 input->pointer = wl_seat_get_pointer(seat);
3111 wl_pointer_set_user_data(input->pointer, input);
3112 wl_pointer_add_listener(input->pointer, &pointer_listener,
3113 input);
3114 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3115 wl_pointer_destroy(input->pointer);
3116 input->pointer = NULL;
3117 }
3118
3119 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3120 input->keyboard = wl_seat_get_keyboard(seat);
3121 wl_keyboard_set_user_data(input->keyboard, input);
3122 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3123 input);
3124 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3125 wl_keyboard_destroy(input->keyboard);
3126 input->keyboard = NULL;
3127 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003128
3129 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3130 input->touch = wl_seat_get_touch(seat);
3131 wl_touch_set_user_data(input->touch, input);
3132 wl_touch_add_listener(input->touch, &touch_listener, input);
3133 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3134 wl_touch_destroy(input->touch);
3135 input->touch = NULL;
3136 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003137}
3138
Rob Bradford08031182013-08-13 20:11:03 +01003139static void
3140seat_handle_name(void *data, struct wl_seat *seat,
3141 const char *name)
3142{
3143
3144}
3145
Daniel Stone37816df2012-05-16 18:45:18 +01003146static const struct wl_seat_listener seat_listener = {
3147 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003148 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003149};
3150
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003151void
3152input_get_position(struct input *input, int32_t *x, int32_t *y)
3153{
3154 *x = input->sx;
3155 *y = input->sy;
3156}
3157
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003158struct display *
3159input_get_display(struct input *input)
3160{
3161 return input->display;
3162}
3163
Daniel Stone37816df2012-05-16 18:45:18 +01003164struct wl_seat *
3165input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003166{
Daniel Stone37816df2012-05-16 18:45:18 +01003167 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003168}
3169
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003170uint32_t
3171input_get_modifiers(struct input *input)
3172{
3173 return input->modifiers;
3174}
3175
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003176struct widget *
3177input_get_focus_widget(struct input *input)
3178{
3179 return input->focus_widget;
3180}
3181
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003182struct data_offer {
3183 struct wl_data_offer *offer;
3184 struct input *input;
3185 struct wl_array types;
3186 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003187
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003188 struct task io_task;
3189 int fd;
3190 data_func_t func;
3191 int32_t x, y;
3192 void *user_data;
3193};
3194
3195static void
3196data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3197{
3198 struct data_offer *offer = data;
3199 char **p;
3200
3201 p = wl_array_add(&offer->types, sizeof *p);
3202 *p = strdup(type);
3203}
3204
3205static const struct wl_data_offer_listener data_offer_listener = {
3206 data_offer_offer,
3207};
3208
3209static void
3210data_offer_destroy(struct data_offer *offer)
3211{
3212 char **p;
3213
3214 offer->refcount--;
3215 if (offer->refcount == 0) {
3216 wl_data_offer_destroy(offer->offer);
3217 for (p = offer->types.data; *p; p++)
3218 free(*p);
3219 wl_array_release(&offer->types);
3220 free(offer);
3221 }
3222}
3223
3224static void
3225data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003226 struct wl_data_device *data_device,
3227 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003228{
3229 struct data_offer *offer;
3230
Brian Lovinbc919262013-08-07 15:34:59 -07003231 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003232
3233 wl_array_init(&offer->types);
3234 offer->refcount = 1;
3235 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003236 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003237 wl_data_offer_add_listener(offer->offer,
3238 &data_offer_listener, offer);
3239}
3240
3241static void
3242data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003243 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003244 wl_fixed_t x_w, wl_fixed_t y_w,
3245 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003246{
3247 struct input *input = data;
3248 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003249 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003250 float x = wl_fixed_to_double(x_w);
3251 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003252 char **p;
3253
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003254 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003255 window = wl_surface_get_user_data(surface);
3256 input->pointer_focus = window;
3257
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003258 if (offer) {
3259 input->drag_offer = wl_data_offer_get_user_data(offer);
3260
3261 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3262 *p = NULL;
3263
3264 types_data = input->drag_offer->types.data;
3265 } else {
3266 input->drag_offer = NULL;
3267 types_data = NULL;
3268 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003269
3270 window = input->pointer_focus;
3271 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003272 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003273 window->user_data);
3274}
3275
3276static void
3277data_device_leave(void *data, struct wl_data_device *data_device)
3278{
3279 struct input *input = data;
3280
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003281 if (input->drag_offer) {
3282 data_offer_destroy(input->drag_offer);
3283 input->drag_offer = NULL;
3284 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003285}
3286
3287static void
3288data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003289 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003290{
3291 struct input *input = data;
3292 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003293 float x = wl_fixed_to_double(x_w);
3294 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003295 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003296
3297 input->sx = x;
3298 input->sy = y;
3299
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003300 if (input->drag_offer)
3301 types_data = input->drag_offer->types.data;
3302 else
3303 types_data = NULL;
3304
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003305 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003306 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003307 window->user_data);
3308}
3309
3310static void
3311data_device_drop(void *data, struct wl_data_device *data_device)
3312{
3313 struct input *input = data;
3314 struct window *window = input->pointer_focus;
3315
3316 if (window->drop_handler)
3317 window->drop_handler(window, input,
3318 input->sx, input->sy, window->user_data);
3319}
3320
3321static void
3322data_device_selection(void *data,
3323 struct wl_data_device *wl_data_device,
3324 struct wl_data_offer *offer)
3325{
3326 struct input *input = data;
3327 char **p;
3328
3329 if (input->selection_offer)
3330 data_offer_destroy(input->selection_offer);
3331
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003332 if (offer) {
3333 input->selection_offer = wl_data_offer_get_user_data(offer);
3334 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3335 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003336 } else {
3337 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003338 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003339}
3340
3341static const struct wl_data_device_listener data_device_listener = {
3342 data_device_data_offer,
3343 data_device_enter,
3344 data_device_leave,
3345 data_device_motion,
3346 data_device_drop,
3347 data_device_selection
3348};
3349
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003350static void
3351input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003352{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003353 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003354 struct wl_cursor *cursor;
3355 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003356
Daniel Stone80972742012-11-07 17:51:39 +11003357 if (!input->pointer)
3358 return;
3359
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003360 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003361 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003362 return;
3363
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003364 if (index >= (int) cursor->image_count) {
3365 fprintf(stderr, "cursor index out of range\n");
3366 return;
3367 }
3368
3369 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003370 buffer = wl_cursor_image_get_buffer(image);
3371 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003372 return;
3373
Kristian Høgsbergae277372012-08-01 09:41:08 -04003374 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003375 input->pointer_surface,
3376 image->hotspot_x, image->hotspot_y);
3377 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3378 wl_surface_damage(input->pointer_surface, 0, 0,
3379 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003380 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003381}
3382
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003383static const struct wl_callback_listener pointer_surface_listener;
3384
3385static void
3386pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3387 uint32_t time)
3388{
3389 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003390 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003391 int i;
3392
3393 if (callback) {
3394 assert(callback == input->cursor_frame_cb);
3395 wl_callback_destroy(callback);
3396 input->cursor_frame_cb = NULL;
3397 }
3398
Daniel Stone80972742012-11-07 17:51:39 +11003399 if (!input->pointer)
3400 return;
3401
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003402 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003403 wl_pointer_set_cursor(input->pointer,
3404 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003405 NULL, 0, 0);
3406 return;
3407 }
3408
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003409 if (input->current_cursor == CURSOR_UNSET)
3410 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003411 cursor = input->display->cursors[input->current_cursor];
3412 if (!cursor)
3413 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003414
3415 /* FIXME We don't have the current time on the first call so we set
3416 * the animation start to the time of the first frame callback. */
3417 if (time == 0)
3418 input->cursor_anim_start = 0;
3419 else if (input->cursor_anim_start == 0)
3420 input->cursor_anim_start = time;
3421
3422 if (time == 0 || input->cursor_anim_start == 0)
3423 i = 0;
3424 else
3425 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3426
Pekka Paalanenbc106382012-10-10 12:49:31 +03003427 if (cursor->image_count > 1) {
3428 input->cursor_frame_cb =
3429 wl_surface_frame(input->pointer_surface);
3430 wl_callback_add_listener(input->cursor_frame_cb,
3431 &pointer_surface_listener, input);
3432 }
3433
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003434 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003435}
3436
3437static const struct wl_callback_listener pointer_surface_listener = {
3438 pointer_surface_frame_callback
3439};
3440
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003441void
3442input_set_pointer_image(struct input *input, int pointer)
3443{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003444 int force = 0;
3445
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003446 if (!input->pointer)
3447 return;
3448
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003449 if (input->pointer_enter_serial > input->cursor_serial)
3450 force = 1;
3451
3452 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003453 return;
3454
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003455 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003456 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003457 if (!input->cursor_frame_cb)
3458 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003459 else if (force) {
3460 /* The current frame callback may be stuck if, for instance,
3461 * the set cursor request was processed by the server after
3462 * this client lost the focus. In this case the cursor surface
3463 * might not be mapped and the frame callback wouldn't ever
3464 * complete. Send a set_cursor and attach to try to map the
3465 * cursor surface again so that the callback will finish */
3466 input_set_pointer_image_index(input, 0);
3467 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003468}
3469
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003470struct wl_data_device *
3471input_get_data_device(struct input *input)
3472{
3473 return input->data_device;
3474}
3475
3476void
3477input_set_selection(struct input *input,
3478 struct wl_data_source *source, uint32_t time)
3479{
3480 wl_data_device_set_selection(input->data_device, source, time);
3481}
3482
3483void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003484input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003485{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003486 wl_data_offer_accept(input->drag_offer->offer,
3487 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003488}
3489
3490static void
3491offer_io_func(struct task *task, uint32_t events)
3492{
3493 struct data_offer *offer =
3494 container_of(task, struct data_offer, io_task);
3495 unsigned int len;
3496 char buffer[4096];
3497
3498 len = read(offer->fd, buffer, sizeof buffer);
3499 offer->func(buffer, len,
3500 offer->x, offer->y, offer->user_data);
3501
3502 if (len == 0) {
3503 close(offer->fd);
3504 data_offer_destroy(offer);
3505 }
3506}
3507
3508static void
3509data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3510 data_func_t func, void *user_data)
3511{
3512 int p[2];
3513
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003514 if (pipe2(p, O_CLOEXEC) == -1)
3515 return;
3516
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003517 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3518 close(p[1]);
3519
3520 offer->io_task.run = offer_io_func;
3521 offer->fd = p[0];
3522 offer->func = func;
3523 offer->refcount++;
3524 offer->user_data = user_data;
3525
3526 display_watch_fd(offer->input->display,
3527 offer->fd, EPOLLIN, &offer->io_task);
3528}
3529
3530void
3531input_receive_drag_data(struct input *input, const char *mime_type,
3532 data_func_t func, void *data)
3533{
3534 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3535 input->drag_offer->x = input->sx;
3536 input->drag_offer->y = input->sy;
3537}
3538
3539int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003540input_receive_drag_data_to_fd(struct input *input,
3541 const char *mime_type, int fd)
3542{
3543 if (input->drag_offer)
3544 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3545
3546 return 0;
3547}
3548
3549int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003550input_receive_selection_data(struct input *input, const char *mime_type,
3551 data_func_t func, void *data)
3552{
3553 char **p;
3554
3555 if (input->selection_offer == NULL)
3556 return -1;
3557
3558 for (p = input->selection_offer->types.data; *p; p++)
3559 if (strcmp(mime_type, *p) == 0)
3560 break;
3561
3562 if (*p == NULL)
3563 return -1;
3564
3565 data_offer_receive_data(input->selection_offer,
3566 mime_type, func, data);
3567 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003568}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003569
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003570int
3571input_receive_selection_data_to_fd(struct input *input,
3572 const char *mime_type, int fd)
3573{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003574 if (input->selection_offer)
3575 wl_data_offer_receive(input->selection_offer->offer,
3576 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003577
3578 return 0;
3579}
3580
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003581void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003582window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003583{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003584 if (!window->shell_surface)
3585 return;
3586
Daniel Stone37816df2012-05-16 18:45:18 +01003587 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003588}
3589
Rusty Lynch1084da52013-08-15 09:10:08 -07003590void
3591window_touch_move(struct window *window, struct input *input, uint32_t serial)
3592{
3593 if (!window->shell_surface)
3594 return;
3595
3596 wl_shell_surface_move(window->shell_surface, input->seat,
3597 window->display->serial);
3598}
3599
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003600static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003601surface_set_synchronized(struct surface *surface)
3602{
3603 if (!surface->subsurface)
3604 return;
3605
3606 if (surface->synchronized)
3607 return;
3608
3609 wl_subsurface_set_sync(surface->subsurface);
3610 surface->synchronized = 1;
3611}
3612
3613static void
3614surface_set_synchronized_default(struct surface *surface)
3615{
3616 if (!surface->subsurface)
3617 return;
3618
3619 if (surface->synchronized == surface->synchronized_default)
3620 return;
3621
3622 if (surface->synchronized_default)
3623 wl_subsurface_set_sync(surface->subsurface);
3624 else
3625 wl_subsurface_set_desync(surface->subsurface);
3626
3627 surface->synchronized = surface->synchronized_default;
3628}
3629
3630static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003631surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003632{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003633 struct widget *widget = surface->widget;
3634 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003635
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003636 if (surface->input_region) {
3637 wl_region_destroy(surface->input_region);
3638 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003639 }
3640
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003641 if (surface->opaque_region)
3642 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003643
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003644 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003645
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003646 if (widget->resize_handler)
3647 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003648 widget->allocation.width,
3649 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003650 widget->user_data);
3651
Pekka Paalanen35e82632013-04-25 13:57:48 +03003652 if (surface->subsurface &&
3653 (surface->allocation.x != widget->allocation.x ||
3654 surface->allocation.y != widget->allocation.y)) {
3655 wl_subsurface_set_position(surface->subsurface,
3656 widget->allocation.x,
3657 widget->allocation.y);
3658 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003659 if (surface->allocation.width != widget->allocation.width ||
3660 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003661 window_schedule_redraw(widget->window);
3662 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003663 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003664
3665 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003666 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003667 widget->allocation.width,
3668 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003669}
3670
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003671static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003672hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3673{
3674 /*
3675 * This hack should be removed, when EGL respects
3676 * eglSwapInterval(0).
3677 *
3678 * If this window has sub-surfaces, especially a free-running
3679 * EGL-widget, we need to post the parent surface once with
3680 * all the old state to guarantee, that the EGL-widget will
3681 * receive its frame callback soon. Otherwise, a forced call
3682 * to eglSwapBuffers may end up blocking, waiting for a frame
3683 * event that will never come, because we will commit the parent
3684 * surface with all new state only after eglSwapBuffers returns.
3685 *
3686 * This assumes, that:
3687 * 1. When the EGL widget's resize hook is called, it pauses.
3688 * 2. When the EGL widget's redraw hook is called, it forces a
3689 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3690 * In a single threaded application condition 1 is a no-op.
3691 *
3692 * XXX: This should actually be after the surface_resize() calls,
3693 * but cannot, because then it would commit the incomplete state
3694 * accumulated from the widget resize hooks.
3695 */
3696 if (window->subsurface_list.next != &window->main_surface->link ||
3697 window->subsurface_list.prev != &window->main_surface->link)
3698 wl_surface_commit(window->main_surface->surface);
3699}
3700
3701static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003702idle_resize(struct window *window)
3703{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003704 struct surface *surface;
3705
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003706 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003707 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003708
Pekka Paalanen71233882013-04-25 13:57:53 +03003709 DBG("from %dx%d to %dx%d\n",
3710 window->main_surface->server_allocation.width,
3711 window->main_surface->server_allocation.height,
3712 window->pending_allocation.width,
3713 window->pending_allocation.height);
3714
Pekka Paalanene9297f82013-04-25 13:57:51 +03003715 hack_prevent_EGL_sub_surface_deadlock(window);
3716
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003717 widget_set_allocation(window->main_surface->widget,
3718 window->pending_allocation.x,
3719 window->pending_allocation.y,
3720 window->pending_allocation.width,
3721 window->pending_allocation.height);
3722
3723 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003724
3725 /* The main surface is in the list, too. Main surface's
3726 * resize_handler is responsible for calling widget_set_allocation()
3727 * on all sub-surface root widgets, so they will be resized
3728 * properly.
3729 */
3730 wl_list_for_each(surface, &window->subsurface_list, link) {
3731 if (surface == window->main_surface)
3732 continue;
3733
3734 surface_set_synchronized(surface);
3735 surface_resize(surface);
3736 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003737}
3738
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003739void
3740window_schedule_resize(struct window *window, int width, int height)
3741{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003742 /* We should probably get these numbers from the theme. */
3743 const int min_width = 200, min_height = 200;
3744
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003745 window->pending_allocation.x = 0;
3746 window->pending_allocation.y = 0;
3747 window->pending_allocation.width = width;
3748 window->pending_allocation.height = height;
3749
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003750 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003751 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003752 window->min_allocation.width = min_width;
3753 else
3754 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003755 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003756 window->min_allocation.height = min_height;
3757 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003758 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003759 }
3760
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003761 if (window->pending_allocation.width < window->min_allocation.width)
3762 window->pending_allocation.width = window->min_allocation.width;
3763 if (window->pending_allocation.height < window->min_allocation.height)
3764 window->pending_allocation.height = window->min_allocation.height;
3765
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003766 window->resize_needed = 1;
3767 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003768}
3769
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003770void
3771widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3772{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003773 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003774}
3775
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003776static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003777handle_ping(void *data, struct wl_shell_surface *shell_surface,
3778 uint32_t serial)
3779{
3780 wl_shell_surface_pong(shell_surface, serial);
3781}
3782
3783static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003784handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003785 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003786{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003787 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003788
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003789 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003790 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003791}
3792
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003793static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003794menu_destroy(struct menu *menu)
3795{
3796 widget_destroy(menu->widget);
3797 window_destroy(menu->window);
3798 free(menu);
3799}
3800
3801static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003802handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3803{
3804 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003805 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003806
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003807 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003808 * device. Or just use wl_callback. And this really needs to
3809 * be a window vfunc that the menu can set. And we need the
3810 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003811
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003812 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003813 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003814}
3815
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003816static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003817 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003818 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003819 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003820};
3821
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003822void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003823window_get_allocation(struct window *window,
3824 struct rectangle *allocation)
3825{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003826 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003827}
3828
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003829static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003830widget_redraw(struct widget *widget)
3831{
3832 struct widget *child;
3833
3834 if (widget->redraw_handler)
3835 widget->redraw_handler(widget, widget->user_data);
3836 wl_list_for_each(child, &widget->child_list, link)
3837 widget_redraw(child);
3838}
3839
3840static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003841frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3842{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003843 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003844
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003845 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003846 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003847 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003848 surface->frame_cb = NULL;
3849
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003850 surface->last_time = time;
3851
Pekka Paalanen71233882013-04-25 13:57:53 +03003852 if (surface->redraw_needed || surface->window->redraw_needed) {
3853 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003854 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003855 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003856}
3857
3858static const struct wl_callback_listener listener = {
3859 frame_callback
3860};
3861
3862static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003863surface_redraw(struct surface *surface)
3864{
Pekka Paalanen71233882013-04-25 13:57:53 +03003865 DBG_OBJ(surface->surface, "begin\n");
3866
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003867 if (!surface->window->redraw_needed && !surface->redraw_needed)
3868 return;
3869
3870 /* Whole-window redraw forces a redraw even if the previous has
3871 * not yet hit the screen.
3872 */
3873 if (surface->frame_cb) {
3874 if (!surface->window->redraw_needed)
3875 return;
3876
Pekka Paalanen71233882013-04-25 13:57:53 +03003877 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003878 wl_callback_destroy(surface->frame_cb);
3879 }
3880
3881 surface->frame_cb = wl_surface_frame(surface->surface);
3882 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003883 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003884
3885 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003886 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003887 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003888 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003889}
3890
3891static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003892idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003893{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003894 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003895 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003896
Pekka Paalanen71233882013-04-25 13:57:53 +03003897 DBG(" --------- \n");
3898
Pekka Paalaneneebff542013-04-25 13:57:52 +03003899 wl_list_init(&window->redraw_task.link);
3900 window->redraw_task_scheduled = 0;
3901
3902 if (window->resize_needed) {
3903 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003904 if (window->main_surface->frame_cb) {
3905 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003906 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003907 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003908
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003909 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003910 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003911
Pekka Paalanen35e82632013-04-25 13:57:48 +03003912 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003913 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003914
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003915 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003916 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003917
3918 wl_list_for_each(surface, &window->subsurface_list, link)
3919 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003920}
3921
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003922static void
3923window_schedule_redraw_task(struct window *window)
3924{
3925 if (window->configure_requests)
3926 return;
3927 if (!window->redraw_task_scheduled) {
3928 window->redraw_task.run = idle_redraw;
3929 display_defer(window->display, &window->redraw_task);
3930 window->redraw_task_scheduled = 1;
3931 }
3932}
3933
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003934void
3935window_schedule_redraw(struct window *window)
3936{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003937 struct surface *surface;
3938
Pekka Paalanen71233882013-04-25 13:57:53 +03003939 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3940
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003941 wl_list_for_each(surface, &window->subsurface_list, link)
3942 surface->redraw_needed = 1;
3943
3944 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003945}
3946
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003947int
3948window_is_fullscreen(struct window *window)
3949{
3950 return window->type == TYPE_FULLSCREEN;
3951}
3952
MoD063a8822013-03-02 23:43:57 +00003953static void
3954configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3955{
3956 struct window *window = data;
3957
3958 wl_callback_destroy(callback);
3959 window->configure_requests--;
3960
3961 if (!window->configure_requests)
3962 window_schedule_redraw(window);
3963}
3964
3965static struct wl_callback_listener configure_request_listener = {
3966 configure_request_completed,
3967};
3968
3969static void
3970window_defer_redraw_until_configure(struct window* window)
3971{
3972 struct wl_callback *callback;
3973
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003974 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003975 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003976 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003977 }
3978
3979 callback = wl_display_sync(window->display->display);
3980 wl_callback_add_listener(callback, &configure_request_listener, window);
3981 window->configure_requests++;
3982}
3983
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003984void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003985window_set_fullscreen(struct window *window, int fullscreen)
3986{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003987 if (!window->display->shell)
3988 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003989
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003990 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003991 return;
3992
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003993 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003994 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003995 if (window->type == TYPE_TOPLEVEL) {
3996 window->saved_allocation = window->main_surface->allocation;
3997 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003998 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003999 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004000 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004001 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004002 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004003 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004004 if (window->saved_type == TYPE_MAXIMIZED) {
4005 window_set_maximized(window, 1);
4006 } else {
4007 window->type = TYPE_TOPLEVEL;
4008 wl_shell_surface_set_toplevel(window->shell_surface);
4009 window_schedule_resize(window,
4010 window->saved_allocation.width,
4011 window->saved_allocation.height);
4012 }
4013
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004014 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004015}
4016
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004017void
4018window_set_fullscreen_method(struct window *window,
4019 enum wl_shell_surface_fullscreen_method method)
4020{
4021 window->fullscreen_method = method;
4022}
4023
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004024int
4025window_is_maximized(struct window *window)
4026{
4027 return window->type == TYPE_MAXIMIZED;
4028}
4029
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004030void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004031window_set_maximized(struct window *window, int maximized)
4032{
4033 if (!window->display->shell)
4034 return;
4035
4036 if ((window->type == TYPE_MAXIMIZED) == maximized)
4037 return;
4038
4039 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004040 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004041 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4042 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004043 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004044 } else if (window->type == TYPE_FULLSCREEN) {
4045 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4046 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004047 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004048 } else {
4049 wl_shell_surface_set_toplevel(window->shell_surface);
4050 window->type = TYPE_TOPLEVEL;
4051 window_schedule_resize(window,
4052 window->saved_allocation.width,
4053 window->saved_allocation.height);
4054 }
4055}
4056
4057void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004058window_set_user_data(struct window *window, void *data)
4059{
4060 window->user_data = data;
4061}
4062
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004063void *
4064window_get_user_data(struct window *window)
4065{
4066 return window->user_data;
4067}
4068
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004069void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004070window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004071 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004072{
4073 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004074}
4075
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004076void
4077window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004078 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004079{
4080 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004081}
4082
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004083void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004084window_set_data_handler(struct window *window, window_data_handler_t handler)
4085{
4086 window->data_handler = handler;
4087}
4088
4089void
4090window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4091{
4092 window->drop_handler = handler;
4093}
4094
4095void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004096window_set_close_handler(struct window *window,
4097 window_close_handler_t handler)
4098{
4099 window->close_handler = handler;
4100}
4101
4102void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004103window_set_fullscreen_handler(struct window *window,
4104 window_fullscreen_handler_t handler)
4105{
4106 window->fullscreen_handler = handler;
4107}
4108
4109void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004110window_set_output_handler(struct window *window,
4111 window_output_handler_t handler)
4112{
4113 window->output_handler = handler;
4114}
4115
4116void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004117window_set_title(struct window *window, const char *title)
4118{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004119 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004120 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004121 if (window->frame) {
4122 frame_set_title(window->frame->frame, title);
4123 widget_schedule_redraw(window->frame->widget);
4124 }
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004125 if (window->shell_surface)
4126 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004127}
4128
4129const char *
4130window_get_title(struct window *window)
4131{
4132 return window->title;
4133}
4134
4135void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004136window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4137{
4138 struct text_cursor_position *text_cursor_position =
4139 window->display->text_cursor_position;
4140
Scott Moreau9295ce02012-06-01 12:46:10 -06004141 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004142 return;
4143
4144 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004145 window->main_surface->surface,
4146 wl_fixed_from_int(x),
4147 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004148}
4149
4150void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004151window_damage(struct window *window, int32_t x, int32_t y,
4152 int32_t width, int32_t height)
4153{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004154 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004155}
4156
Casey Dahlin9074db52012-04-19 22:50:09 -04004157static void
4158surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004159 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004160{
Rob Bradford7507b572012-05-15 17:55:34 +01004161 struct window *window = data;
4162 struct output *output;
4163 struct output *output_found = NULL;
4164 struct window_output *window_output;
4165
4166 wl_list_for_each(output, &window->display->output_list, link) {
4167 if (output->output == wl_output) {
4168 output_found = output;
4169 break;
4170 }
4171 }
4172
4173 if (!output_found)
4174 return;
4175
Brian Lovinbc919262013-08-07 15:34:59 -07004176 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004177 window_output->output = output_found;
4178
4179 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004180
4181 if (window->output_handler)
4182 window->output_handler(window, output_found, 1,
4183 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004184}
4185
4186static void
4187surface_leave(void *data,
4188 struct wl_surface *wl_surface, struct wl_output *output)
4189{
Rob Bradford7507b572012-05-15 17:55:34 +01004190 struct window *window = data;
4191 struct window_output *window_output;
4192 struct window_output *window_output_found = NULL;
4193
4194 wl_list_for_each(window_output, &window->window_output_list, link) {
4195 if (window_output->output->output == output) {
4196 window_output_found = window_output;
4197 break;
4198 }
4199 }
4200
4201 if (window_output_found) {
4202 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004203
4204 if (window->output_handler)
4205 window->output_handler(window, window_output->output,
4206 0, window->user_data);
4207
Rob Bradford7507b572012-05-15 17:55:34 +01004208 free(window_output_found);
4209 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004210}
4211
4212static const struct wl_surface_listener surface_listener = {
4213 surface_enter,
4214 surface_leave
4215};
4216
Pekka Paalanen4e373742013-02-13 16:17:13 +02004217static struct surface *
4218surface_create(struct window *window)
4219{
4220 struct display *display = window->display;
4221 struct surface *surface;
4222
Brian Lovinbc919262013-08-07 15:34:59 -07004223 surface = xmalloc(sizeof *surface);
4224 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004225 if (!surface)
4226 return NULL;
4227
4228 surface->window = window;
4229 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004230 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004231 wl_surface_add_listener(surface->surface, &surface_listener, window);
4232
Pekka Paalanen35e82632013-04-25 13:57:48 +03004233 wl_list_insert(&window->subsurface_list, &surface->link);
4234
Pekka Paalanen4e373742013-02-13 16:17:13 +02004235 return surface;
4236}
4237
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004238static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004239window_create_internal(struct display *display,
4240 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004241{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004242 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004243 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004244
Peter Huttererf3d62272013-08-08 11:57:05 +10004245 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004246 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004247 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004248 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004249
4250 surface = surface_create(window);
4251 window->main_surface = surface;
4252
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004253 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004254 window->shell_surface =
4255 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004256 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004257 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004258 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004259
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004260 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004261 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004262 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004263 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004264
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004265 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004266#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004267 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004268#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004269 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004270#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004271 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004272 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004273
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004274 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004275 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004276 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004277
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004278 if (window->shell_surface) {
4279 wl_shell_surface_set_user_data(window->shell_surface, window);
4280 wl_shell_surface_add_listener(window->shell_surface,
4281 &shell_surface_listener, window);
4282 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004283
Rob Bradford7507b572012-05-15 17:55:34 +01004284 wl_list_init (&window->window_output_list);
4285
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004286 return window;
4287}
4288
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004289struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004290window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004291{
Kristian Høgsberg41624832013-07-25 16:53:10 -07004292 return window_create_internal(display, NULL, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004293}
4294
4295struct window *
4296window_create_custom(struct display *display)
4297{
Kristian Høgsbergce278412013-07-25 15:20:20 -07004298 return window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004299}
4300
4301struct window *
4302window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004303 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004304{
4305 struct window *window;
4306
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004307 window = window_create_internal(parent->display,
4308 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004309
4310 window->x = x;
4311 window->y = y;
4312
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004313 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004314 wl_shell_surface_set_transient(
4315 window->shell_surface,
4316 window->parent->main_surface->surface,
4317 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004318
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004319 return window;
4320}
4321
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004322static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004323menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004324{
4325 int next;
4326
4327 next = (sy - 8) / 20;
4328 if (menu->current != next) {
4329 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004330 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004331 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004332}
4333
4334static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004335menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004336 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004337 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004338{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004339 struct menu *menu = data;
4340
4341 if (widget == menu->widget)
4342 menu_set_item(data, y);
4343
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004344 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004345}
4346
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004347static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004348menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004349 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004350{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004351 struct menu *menu = data;
4352
4353 if (widget == menu->widget)
4354 menu_set_item(data, y);
4355
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004356 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004357}
4358
4359static void
4360menu_leave_handler(struct widget *widget, struct input *input, void *data)
4361{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004362 struct menu *menu = data;
4363
4364 if (widget == menu->widget)
4365 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004366}
4367
4368static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004369menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004370 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004371 uint32_t button, enum wl_pointer_button_state state,
4372 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004373
4374{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004375 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004376
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004377 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4378 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004379 /* Either relase after press-drag-release or
4380 * click-motion-click. */
Kristian Høgsberg67b82152013-10-23 16:52:05 -07004381 menu->func(menu->window->parent, input,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004382 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004383 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004384 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004385 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004386 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004387 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004388}
4389
4390static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004391menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004392{
4393 cairo_t *cr;
4394 const int32_t r = 3, margin = 3;
4395 struct menu *menu = data;
4396 int32_t width, height, i;
4397
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004398 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004399 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4400 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4401 cairo_paint(cr);
4402
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004403 width = widget->allocation.width;
4404 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004405 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004406
4407 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004408 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4409 cairo_fill(cr);
4410
4411 for (i = 0; i < menu->count; i++) {
4412 if (i == menu->current) {
4413 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4414 cairo_rectangle(cr, margin, i * 20 + margin,
4415 width - 2 * margin, 20);
4416 cairo_fill(cr);
4417 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4418 cairo_move_to(cr, 10, i * 20 + 16);
4419 cairo_show_text(cr, menu->entries[i]);
4420 } else {
4421 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4422 cairo_move_to(cr, 10, i * 20 + 16);
4423 cairo_show_text(cr, menu->entries[i]);
4424 }
4425 }
4426
4427 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004428}
4429
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004430void
4431window_show_menu(struct display *display,
4432 struct input *input, uint32_t time, struct window *parent,
4433 int32_t x, int32_t y,
4434 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004435{
4436 struct window *window;
4437 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004438 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004439
4440 menu = malloc(sizeof *menu);
4441 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004442 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004443
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004444 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004445 if (!window) {
4446 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004447 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004448 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004449
4450 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004451 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004452 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4453 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004454 menu->entries = entries;
4455 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004456 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004457 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004458 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004459 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004460 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004461 window->type = TYPE_MENU;
4462 window->x = x;
4463 window->y = y;
4464
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004465 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004466 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004467 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004468 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004469 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004470
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004471 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004472 widget_set_enter_handler(menu->widget, menu_enter_handler);
4473 widget_set_leave_handler(menu->widget, menu_leave_handler);
4474 widget_set_motion_handler(menu->widget, menu_motion_handler);
4475 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004476
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004477 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004478 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004479}
4480
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004481void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004482window_set_buffer_type(struct window *window, enum window_buffer_type type)
4483{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004484 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004485}
4486
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004487void
4488window_set_preferred_format(struct window *window,
4489 enum preferred_format format)
4490{
4491 window->preferred_format = format;
4492}
4493
Pekka Paalanen35e82632013-04-25 13:57:48 +03004494struct widget *
4495window_add_subsurface(struct window *window, void *data,
4496 enum subsurface_mode default_mode)
4497{
4498 struct widget *widget;
4499 struct surface *surface;
4500 struct wl_surface *parent;
4501 struct wl_subcompositor *subcompo = window->display->subcompositor;
4502
Pekka Paalanen35e82632013-04-25 13:57:48 +03004503 surface = surface_create(window);
4504 widget = widget_create(window, surface, data);
4505 wl_list_init(&widget->link);
4506 surface->widget = widget;
4507
4508 parent = window->main_surface->surface;
4509 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4510 surface->surface,
4511 parent);
4512 surface->synchronized = 1;
4513
4514 switch (default_mode) {
4515 case SUBSURFACE_SYNCHRONIZED:
4516 surface->synchronized_default = 1;
4517 break;
4518 case SUBSURFACE_DESYNCHRONIZED:
4519 surface->synchronized_default = 0;
4520 break;
4521 default:
4522 assert(!"bad enum subsurface_mode");
4523 }
4524
4525 return widget;
4526}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004527
4528static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004529display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004530 struct wl_output *wl_output,
4531 int x, int y,
4532 int physical_width,
4533 int physical_height,
4534 int subpixel,
4535 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004536 const char *model,
4537 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004538{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004539 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004540
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004541 output->allocation.x = x;
4542 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004543 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004544}
4545
4546static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004547display_handle_done(void *data,
4548 struct wl_output *wl_output)
4549{
4550}
4551
4552static void
4553display_handle_scale(void *data,
4554 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004555 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004556{
4557 struct output *output = data;
4558
4559 output->scale = scale;
4560}
4561
4562static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004563display_handle_mode(void *data,
4564 struct wl_output *wl_output,
4565 uint32_t flags,
4566 int width,
4567 int height,
4568 int refresh)
4569{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004570 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004571 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004572
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004573 if (flags & WL_OUTPUT_MODE_CURRENT) {
4574 output->allocation.width = width;
4575 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004576 if (display->output_configure_handler)
4577 (*display->output_configure_handler)(
4578 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004579 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004580}
4581
4582static const struct wl_output_listener output_listener = {
4583 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004584 display_handle_mode,
4585 display_handle_done,
4586 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004587};
4588
4589static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004590display_add_output(struct display *d, uint32_t id)
4591{
4592 struct output *output;
4593
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004594 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004595 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004596 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004597 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004598 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004599 wl_list_insert(d->output_list.prev, &output->link);
4600
4601 wl_output_add_listener(output->output, &output_listener, output);
4602}
4603
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004604static void
4605output_destroy(struct output *output)
4606{
4607 if (output->destroy_handler)
4608 (*output->destroy_handler)(output, output->user_data);
4609
4610 wl_output_destroy(output->output);
4611 wl_list_remove(&output->link);
4612 free(output);
4613}
4614
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004615void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004616display_set_global_handler(struct display *display,
4617 display_global_handler_t handler)
4618{
4619 struct global *global;
4620
4621 display->global_handler = handler;
4622 if (!handler)
4623 return;
4624
4625 wl_list_for_each(global, &display->global_list, link)
4626 display->global_handler(display,
4627 global->name, global->interface,
4628 global->version, display->user_data);
4629}
4630
4631void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004632display_set_output_configure_handler(struct display *display,
4633 display_output_handler_t handler)
4634{
4635 struct output *output;
4636
4637 display->output_configure_handler = handler;
4638 if (!handler)
4639 return;
4640
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004641 wl_list_for_each(output, &display->output_list, link) {
4642 if (output->allocation.width == 0 &&
4643 output->allocation.height == 0)
4644 continue;
4645
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004646 (*display->output_configure_handler)(output,
4647 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004648 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004649}
4650
4651void
4652output_set_user_data(struct output *output, void *data)
4653{
4654 output->user_data = data;
4655}
4656
4657void *
4658output_get_user_data(struct output *output)
4659{
4660 return output->user_data;
4661}
4662
4663void
4664output_set_destroy_handler(struct output *output,
4665 display_output_handler_t handler)
4666{
4667 output->destroy_handler = handler;
4668 /* FIXME: implement this, once we have way to remove outputs */
4669}
4670
4671void
Scott Moreau4e072362012-09-29 02:03:11 -06004672output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004673{
Scott Moreau4e072362012-09-29 02:03:11 -06004674 struct rectangle allocation = output->allocation;
4675
4676 switch (output->transform) {
4677 case WL_OUTPUT_TRANSFORM_90:
4678 case WL_OUTPUT_TRANSFORM_270:
4679 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4680 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4681 /* Swap width and height */
4682 allocation.width = output->allocation.height;
4683 allocation.height = output->allocation.width;
4684 break;
4685 }
4686
4687 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004688}
4689
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004690struct wl_output *
4691output_get_wl_output(struct output *output)
4692{
4693 return output->output;
4694}
4695
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004696enum wl_output_transform
4697output_get_transform(struct output *output)
4698{
4699 return output->transform;
4700}
4701
Alexander Larssonafd319a2013-05-22 14:41:27 +02004702uint32_t
4703output_get_scale(struct output *output)
4704{
4705 return output->scale;
4706}
4707
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004708static void
Daniel Stone97f68542012-05-30 16:32:01 +01004709fini_xkb(struct input *input)
4710{
4711 xkb_state_unref(input->xkb.state);
4712 xkb_map_unref(input->xkb.keymap);
4713}
4714
Rob Bradford08031182013-08-13 20:11:03 +01004715#define MAX(a,b) ((a) > (b) ? a : b)
4716
Daniel Stone97f68542012-05-30 16:32:01 +01004717static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004718display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004719{
4720 struct input *input;
4721
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004722 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004723 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004724 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4725 MAX(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004726 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004727 input->pointer_focus = NULL;
4728 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004729 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004730 wl_list_insert(d->input_list.prev, &input->link);
4731
Daniel Stone37816df2012-05-16 18:45:18 +01004732 wl_seat_add_listener(input->seat, &seat_listener, input);
4733 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004734
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004735 input->data_device =
4736 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004737 input->seat);
4738 wl_data_device_add_listener(input->data_device, &data_device_listener,
4739 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004740
4741 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004742
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004743 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4744 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004745 input->repeat_task.run = keyboard_repeat_func;
4746 display_watch_fd(d, input->repeat_timer_fd,
4747 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004748}
4749
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004750static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004751input_destroy(struct input *input)
4752{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004753 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004754 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004755
4756 if (input->drag_offer)
4757 data_offer_destroy(input->drag_offer);
4758
4759 if (input->selection_offer)
4760 data_offer_destroy(input->selection_offer);
4761
4762 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004763
4764 if (input->display->seat_version >= 3) {
4765 if (input->pointer)
4766 wl_pointer_release(input->pointer);
4767 if (input->keyboard)
4768 wl_keyboard_release(input->keyboard);
4769 }
4770
Daniel Stone97f68542012-05-30 16:32:01 +01004771 fini_xkb(input);
4772
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004773 wl_surface_destroy(input->pointer_surface);
4774
Pekka Paalanene1207c72011-12-16 12:02:09 +02004775 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004776 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004777 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004778 free(input);
4779}
4780
4781static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004782init_workspace_manager(struct display *d, uint32_t id)
4783{
4784 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004785 wl_registry_bind(d->registry, id,
4786 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004787 if (d->workspace_manager != NULL)
4788 workspace_manager_add_listener(d->workspace_manager,
4789 &workspace_manager_listener,
4790 d);
4791}
4792
4793static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004794shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4795{
4796 struct display *d = data;
4797
4798 if (format == WL_SHM_FORMAT_RGB565)
4799 d->has_rgb565 = 1;
4800}
4801
4802struct wl_shm_listener shm_listener = {
4803 shm_format
4804};
4805
4806static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004807registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4808 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004809{
4810 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004811 struct global *global;
4812
Brian Lovinbc919262013-08-07 15:34:59 -07004813 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004814 global->name = id;
4815 global->interface = strdup(interface);
4816 global->version = version;
4817 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004818
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004819 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004820 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05004821 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004822 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004823 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004824 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01004825 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004826 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004827 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004828 d->shell = wl_registry_bind(registry,
4829 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004830 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004831 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004832 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004833 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4834 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004835 wl_registry_bind(registry, id,
4836 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004837 } else if (strcmp(interface, "text_cursor_position") == 0) {
4838 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004839 wl_registry_bind(registry, id,
4840 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004841 } else if (strcmp(interface, "workspace_manager") == 0) {
4842 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004843 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4844 d->subcompositor =
4845 wl_registry_bind(registry, id,
4846 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004847 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004848
4849 if (d->global_handler)
4850 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004851}
4852
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004853static void
4854registry_handle_global_remove(void *data, struct wl_registry *registry,
4855 uint32_t name)
4856{
4857 struct display *d = data;
4858 struct global *global;
4859 struct global *tmp;
4860
4861 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4862 if (global->name != name)
4863 continue;
4864
4865 /* XXX: Should destroy bound globals, and call
4866 * the counterpart of display::global_handler
4867 */
4868 wl_list_remove(&global->link);
4869 free(global->interface);
4870 free(global);
4871 }
4872}
4873
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004874void *
4875display_bind(struct display *display, uint32_t name,
4876 const struct wl_interface *interface, uint32_t version)
4877{
4878 return wl_registry_bind(display->registry, name, interface, version);
4879}
4880
4881static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004882 registry_handle_global,
4883 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004884};
4885
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004886#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004887static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004888init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004889{
4890 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004891 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004892
Rob Clark6396ed32012-03-11 19:48:41 -05004893#ifdef USE_CAIRO_GLESV2
4894# define GL_BIT EGL_OPENGL_ES2_BIT
4895#else
4896# define GL_BIT EGL_OPENGL_BIT
4897#endif
4898
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004899 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004900 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004901 EGL_RED_SIZE, 1,
4902 EGL_GREEN_SIZE, 1,
4903 EGL_BLUE_SIZE, 1,
4904 EGL_ALPHA_SIZE, 1,
4905 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004906 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004907 EGL_NONE
4908 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004909
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004910#ifdef USE_CAIRO_GLESV2
4911 static const EGLint context_attribs[] = {
4912 EGL_CONTEXT_CLIENT_VERSION, 2,
4913 EGL_NONE
4914 };
4915 EGLint api = EGL_OPENGL_ES_API;
4916#else
4917 EGLint *context_attribs = NULL;
4918 EGLint api = EGL_OPENGL_API;
4919#endif
4920
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004921 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004922 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004923 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004924 return -1;
4925 }
4926
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004927 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004928 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004929 return -1;
4930 }
4931
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004932 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4933 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004934 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004935 return -1;
4936 }
4937
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004938 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004939 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004940 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004941 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004942 return -1;
4943 }
4944
Benjamin Franzke0c991632011-09-27 21:57:31 +02004945 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4946 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004947 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004948 return -1;
4949 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004950
4951 return 0;
4952}
4953
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004954static void
4955fini_egl(struct display *display)
4956{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004957 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004958
4959 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4960 EGL_NO_CONTEXT);
4961
4962 eglTerminate(display->dpy);
4963 eglReleaseThread();
4964}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004965#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004966
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004967static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004968init_dummy_surface(struct display *display)
4969{
4970 int len;
4971 void *data;
4972
4973 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4974 data = malloc(len);
4975 display->dummy_surface =
4976 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4977 1, 1, len);
4978 display->dummy_surface_data = data;
4979}
4980
4981static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004982handle_display_data(struct task *task, uint32_t events)
4983{
4984 struct display *display =
4985 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004986 struct epoll_event ep;
4987 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004988
4989 display->display_fd_events = events;
4990
4991 if (events & EPOLLERR || events & EPOLLHUP) {
4992 display_exit(display);
4993 return;
4994 }
4995
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004996 if (events & EPOLLIN) {
4997 ret = wl_display_dispatch(display->display);
4998 if (ret == -1) {
4999 display_exit(display);
5000 return;
5001 }
5002 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005003
5004 if (events & EPOLLOUT) {
5005 ret = wl_display_flush(display->display);
5006 if (ret == 0) {
5007 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5008 ep.data.ptr = &display->display_task;
5009 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5010 display->display_fd, &ep);
5011 } else if (ret == -1 && errno != EAGAIN) {
5012 display_exit(display);
5013 return;
5014 }
5015 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005016}
5017
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005018static void
5019log_handler(const char *format, va_list args)
5020{
5021 vfprintf(stderr, format, args);
5022}
5023
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005024struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005025display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005026{
5027 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005028
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005029 wl_log_set_handler_client(log_handler);
5030
Peter Huttererf3d62272013-08-08 11:57:05 +10005031 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005032 if (d == NULL)
5033 return NULL;
5034
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005035 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005036 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005037 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005038 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005039 return NULL;
5040 }
5041
Rob Bradford5ab9c752013-07-26 16:29:43 +01005042 d->xkb_context = xkb_context_new(0);
5043 if (d->xkb_context == NULL) {
5044 fprintf(stderr, "Failed to create XKB context\n");
5045 free(d);
5046 return NULL;
5047 }
5048
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005049 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005050 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005051 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005052 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5053 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005054
5055 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005056 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005057 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005058 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005059
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005060 d->workspace = 0;
5061 d->workspace_count = 1;
5062
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005063 d->registry = wl_display_get_registry(d->display);
5064 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005065
5066 if (wl_display_dispatch(d->display) < 0) {
5067 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5068 return NULL;
5069 }
5070
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005071#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005072 if (init_egl(d) < 0)
5073 fprintf(stderr, "EGL does not seem to work, "
5074 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005075#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005076
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005077 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005078
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005079 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005080
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005081 wl_list_init(&d->window_list);
5082
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005083 init_dummy_surface(d);
5084
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005085 return d;
5086}
5087
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005088static void
5089display_destroy_outputs(struct display *display)
5090{
5091 struct output *tmp;
5092 struct output *output;
5093
5094 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5095 output_destroy(output);
5096}
5097
Pekka Paalanene1207c72011-12-16 12:02:09 +02005098static void
5099display_destroy_inputs(struct display *display)
5100{
5101 struct input *tmp;
5102 struct input *input;
5103
5104 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5105 input_destroy(input);
5106}
5107
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005108void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005109display_destroy(struct display *display)
5110{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005111 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005112 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5113 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005114
5115 if (!wl_list_empty(&display->deferred_list))
5116 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5117
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005118 cairo_surface_destroy(display->dummy_surface);
5119 free(display->dummy_surface_data);
5120
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005121 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005122 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005123
Daniel Stone97f68542012-05-30 16:32:01 +01005124 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005125
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005126 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005127 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005128
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005129#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005130 if (display->argb_device)
5131 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005132#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005133
Pekka Paalanen35e82632013-04-25 13:57:48 +03005134 if (display->subcompositor)
5135 wl_subcompositor_destroy(display->subcompositor);
5136
Pekka Paalanenc2052982011-12-16 11:41:32 +02005137 if (display->shell)
5138 wl_shell_destroy(display->shell);
5139
5140 if (display->shm)
5141 wl_shm_destroy(display->shm);
5142
5143 if (display->data_device_manager)
5144 wl_data_device_manager_destroy(display->data_device_manager);
5145
5146 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005147 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005148
5149 close(display->epoll_fd);
5150
U. Artie Eoff44874d92012-10-02 21:12:35 -07005151 if (!(display->display_fd_events & EPOLLERR) &&
5152 !(display->display_fd_events & EPOLLHUP))
5153 wl_display_flush(display->display);
5154
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005155 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005156 free(display);
5157}
5158
5159void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005160display_set_user_data(struct display *display, void *data)
5161{
5162 display->user_data = data;
5163}
5164
5165void *
5166display_get_user_data(struct display *display)
5167{
5168 return display->user_data;
5169}
5170
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005171struct wl_display *
5172display_get_display(struct display *display)
5173{
5174 return display->display;
5175}
5176
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005177int
5178display_has_subcompositor(struct display *display)
5179{
5180 if (display->subcompositor)
5181 return 1;
5182
5183 wl_display_roundtrip(display->display);
5184
5185 return display->subcompositor != NULL;
5186}
5187
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005188cairo_device_t *
5189display_get_cairo_device(struct display *display)
5190{
5191 return display->argb_device;
5192}
5193
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005194struct output *
5195display_get_output(struct display *display)
5196{
5197 return container_of(display->output_list.next, struct output, link);
5198}
5199
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005200struct wl_compositor *
5201display_get_compositor(struct display *display)
5202{
5203 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005204}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005205
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005206uint32_t
5207display_get_serial(struct display *display)
5208{
5209 return display->serial;
5210}
5211
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005212EGLDisplay
5213display_get_egl_display(struct display *d)
5214{
5215 return d->dpy;
5216}
5217
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005218struct wl_data_source *
5219display_create_data_source(struct display *display)
5220{
5221 return wl_data_device_manager_create_data_source(display->data_device_manager);
5222}
5223
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005224EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005225display_get_argb_egl_config(struct display *d)
5226{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005227 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005228}
5229
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005230struct wl_shell *
5231display_get_shell(struct display *display)
5232{
5233 return display->shell;
5234}
5235
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005236int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005237display_acquire_window_surface(struct display *display,
5238 struct window *window,
5239 EGLContext ctx)
5240{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005241 struct surface *surface = window->main_surface;
5242
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005243 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005244 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005245
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005246 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005247 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005248}
5249
5250void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005251display_release_window_surface(struct display *display,
5252 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005253{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005254 struct surface *surface = window->main_surface;
5255
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005256 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005257 return;
5258
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005259 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005260}
5261
5262void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005263display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005264{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005265 wl_list_insert(&display->deferred_list, &task->link);
5266}
5267
5268void
5269display_watch_fd(struct display *display,
5270 int fd, uint32_t events, struct task *task)
5271{
5272 struct epoll_event ep;
5273
5274 ep.events = events;
5275 ep.data.ptr = task;
5276 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5277}
5278
5279void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005280display_unwatch_fd(struct display *display, int fd)
5281{
5282 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5283}
5284
5285void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005286display_run(struct display *display)
5287{
5288 struct task *task;
5289 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005290 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005291
Pekka Paalanen826d7952011-12-15 10:14:07 +02005292 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005293 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005294 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005295 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005296 struct task, link);
5297 wl_list_remove(&task->link);
5298 task->run(task, 0);
5299 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005300
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005301 wl_display_dispatch_pending(display->display);
5302
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005303 if (!display->running)
5304 break;
5305
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005306 ret = wl_display_flush(display->display);
5307 if (ret < 0 && errno == EAGAIN) {
5308 ep[0].events =
5309 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5310 ep[0].data.ptr = &display->display_task;
5311
5312 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5313 display->display_fd, &ep[0]);
5314 } else if (ret < 0) {
5315 break;
5316 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005317
5318 count = epoll_wait(display->epoll_fd,
5319 ep, ARRAY_LENGTH(ep), -1);
5320 for (i = 0; i < count; i++) {
5321 task = ep[i].data.ptr;
5322 task->run(task, ep[i].events);
5323 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005324 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005325}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005326
5327void
5328display_exit(struct display *display)
5329{
5330 display->running = 0;
5331}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005332
5333void
5334keysym_modifiers_add(struct wl_array *modifiers_map,
5335 const char *name)
5336{
5337 size_t len = strlen(name) + 1;
5338 char *p;
5339
5340 p = wl_array_add(modifiers_map, len);
5341
5342 if (p == NULL)
5343 return;
5344
5345 strncpy(p, name, len);
5346}
5347
5348static xkb_mod_index_t
5349keysym_modifiers_get_index(struct wl_array *modifiers_map,
5350 const char *name)
5351{
5352 xkb_mod_index_t index = 0;
5353 char *p = modifiers_map->data;
5354
5355 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5356 if (strcmp(p, name) == 0)
5357 return index;
5358
5359 index++;
5360 p += strlen(p) + 1;
5361 }
5362
5363 return XKB_MOD_INVALID;
5364}
5365
5366xkb_mod_mask_t
5367keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5368 const char *name)
5369{
5370 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5371
5372 if (index == XKB_MOD_INVALID)
5373 return XKB_MOD_INVALID;
5374
5375 return 1 << index;
5376}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005377
5378void *
5379fail_on_null(void *p)
5380{
5381 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005382 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005383 exit(EXIT_FAILURE);
5384 }
5385
5386 return p;
5387}
5388
5389void *
5390xmalloc(size_t s)
5391{
5392 return fail_on_null(malloc(s));
5393}
5394
Peter Huttererf3d62272013-08-08 11:57:05 +10005395void *
5396xzalloc(size_t s)
5397{
5398 return fail_on_null(zalloc(s));
5399}
5400
Kristian Høgsbergce278412013-07-25 15:20:20 -07005401char *
5402xstrdup(const char *s)
5403{
5404 return fail_on_null(strdup(s));
5405}