blob: adcca7620fa90df0c7f276b29b974b114b4b9cf7 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen99436862012-11-19 17:15:59 +02003 * Copyright © 2012 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
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040024#define _GNU_SOURCE
25
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026#include "../config.h"
27
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040034#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020036#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include <time.h>
38#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030041#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040042
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040043#include <pixman.h>
44
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050046#include <wayland-egl.h>
47
Rob Clark6396ed32012-03-11 19:48:41 -050048#ifdef USE_CAIRO_GLESV2
49#include <GLES2/gl2.h>
50#include <GLES2/gl2ext.h>
51#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050053#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <EGL/egl.h>
55#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040057#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030058#else /* HAVE_CAIRO_EGL */
59typedef void *EGLDisplay;
60typedef void *EGLConfig;
61typedef void *EGLContext;
62#define EGL_NO_DISPLAY ((EGLDisplay)0)
63#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050064
Daniel Stone9d4f0302012-02-15 16:33:21 +000065#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030066#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040067
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050068#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020069#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040070#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060071#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020072#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030073#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050074
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050075#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050076
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070077struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030078
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040079struct global {
80 uint32_t name;
81 char *interface;
82 uint32_t version;
83 struct wl_list link;
84};
85
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050086struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050087 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040088 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050089 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040090 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040091 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040092 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060093 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020094 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040095 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050096 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020098 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040099 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400100
101 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700102 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400103 struct task display_task;
104
105 int epoll_fd;
106 struct wl_list deferred_list;
107
Pekka Paalanen826d7952011-12-15 10:14:07 +0200108 int running;
109
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400110 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400111 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400112 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500113 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400114
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400115 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400116
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300117 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300118 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400119
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400121 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100124
125 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200126
127 uint32_t workspace;
128 uint32_t workspace_count;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500129};
130
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400131enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400132 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400133 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400134 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500135 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400136 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500137 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400138 TYPE_CUSTOM
139};
Rob Bradford7507b572012-05-15 17:55:34 +0100140
141struct window_output {
142 struct output *output;
143 struct wl_list link;
144};
145
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200146struct toysurface {
147 /*
148 * Prepare the surface for drawing. Makes sure there is a surface
149 * of the right size available for rendering, and returns it.
150 * dx,dy are the x,y of wl_surface.attach.
151 * width,height are the new surface size.
Pekka Paalanen99436862012-11-19 17:15:59 +0200152 * If resize_hint is non-zero, the user is doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200153 * Returns the Cairo surface to draw to.
154 */
155 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanen99436862012-11-19 17:15:59 +0200156 int width, int height, int resize_hint);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157
158 /*
159 * Post the surface to the server, returning the server allocation
160 * rectangle. The Cairo surface from prepare() must be destroyed
161 * after calling this.
162 */
163 void (*swap)(struct toysurface *base,
164 struct rectangle *server_allocation);
165
166 /*
167 * Make the toysurface current with the given EGL context.
168 * Returns 0 on success, and negative of failure.
169 */
170 int (*acquire)(struct toysurface *base, EGLContext ctx);
171
172 /*
173 * Release the toysurface from the EGL context, returning control
174 * to Cairo.
175 */
176 void (*release)(struct toysurface *base);
177
178 /*
179 * Destroy the toysurface, including the Cairo surface, any
180 * backing storage, and the Wayland protocol objects.
181 */
182 void (*destroy)(struct toysurface *base);
183};
184
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500185struct window {
186 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500187 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100188 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500189 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200190 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500191 struct wl_region *input_region;
192 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500193 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500194 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400195 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500196 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500197 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400198 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400199 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400200 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400201 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400202 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400203 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400204 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400205 int focus_count;
206
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400207 enum window_buffer_type buffer_type;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200208 struct toysurface *toysurface;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400209 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500210
Pekka Paalanen99436862012-11-19 17:15:59 +0200211 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400212
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500213 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500214 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400215 window_data_handler_t data_handler;
216 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500217 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400218 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400219
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300220 struct wl_callback *frame_cb;
221
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200222 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500223 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500224
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500225 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400226 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500227};
228
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500229struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500230 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300231 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500232 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400233 struct wl_list link;
234 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500235 widget_resize_handler_t resize_handler;
236 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500237 widget_enter_handler_t enter_handler;
238 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500239 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500240 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200241 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400242 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500243 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300244 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400245};
246
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400247struct input {
248 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100249 struct wl_seat *seat;
250 struct wl_pointer *pointer;
251 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400252 struct window *pointer_focus;
253 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300254 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300255 uint32_t cursor_anim_start;
256 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300257 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400258 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400259 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400260 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400261 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400262 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400263
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500264 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500265 struct widget *grab;
266 uint32_t grab_button;
267
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400268 struct wl_data_device *data_device;
269 struct data_offer *drag_offer;
270 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100271
272 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100273 struct xkb_keymap *keymap;
274 struct xkb_state *state;
275 xkb_mod_mask_t control_mask;
276 xkb_mod_mask_t alt_mask;
277 xkb_mod_mask_t shift_mask;
278 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400279
280 struct task repeat_task;
281 int repeat_timer_fd;
282 uint32_t repeat_sym;
283 uint32_t repeat_key;
284 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400285};
286
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500287struct output {
288 struct display *display;
289 struct wl_output *output;
290 struct rectangle allocation;
291 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600292 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200293
294 display_output_handler_t destroy_handler;
295 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500296};
297
Martin Minarik1998b152012-05-10 02:04:35 +0200298enum frame_button_action {
299 FRAME_BUTTON_NULL = 0,
300 FRAME_BUTTON_ICON = 1,
301 FRAME_BUTTON_CLOSE = 2,
302 FRAME_BUTTON_MINIMIZE = 3,
303 FRAME_BUTTON_MAXIMIZE = 4,
304};
305
306enum frame_button_pointer {
307 FRAME_BUTTON_DEFAULT = 0,
308 FRAME_BUTTON_OVER = 1,
309 FRAME_BUTTON_ACTIVE = 2,
310};
311
312enum frame_button_align {
313 FRAME_BUTTON_RIGHT = 0,
314 FRAME_BUTTON_LEFT = 1,
315};
316
317enum frame_button_decoration {
318 FRAME_BUTTON_NONE = 0,
319 FRAME_BUTTON_FANCY = 1,
320};
321
322struct frame_button {
323 struct widget *widget;
324 struct frame *frame;
325 cairo_surface_t *icon;
326 enum frame_button_action type;
327 enum frame_button_pointer state;
328 struct wl_list link; /* buttons_list */
329 enum frame_button_align align;
330 enum frame_button_decoration decoration;
331};
332
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500333struct frame {
334 struct widget *widget;
335 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200336 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500337};
338
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500339struct menu {
340 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500341 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500342 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500343 const char **entries;
344 uint32_t time;
345 int current;
346 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400347 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500348 menu_func_t func;
349};
350
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300351struct tooltip {
352 struct widget *parent;
353 struct window *window;
354 struct widget *widget;
355 char *entry;
356 struct task tooltip_task;
357 int tooltip_fd;
358 float x, y;
359};
360
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700361struct shm_pool {
362 struct wl_shm_pool *pool;
363 size_t size;
364 size_t used;
365 void *data;
366};
367
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400368enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300369 CURSOR_DEFAULT = 100,
370 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400371};
372
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500373enum window_location {
374 WINDOW_INTERIOR = 0,
375 WINDOW_RESIZING_TOP = 1,
376 WINDOW_RESIZING_BOTTOM = 2,
377 WINDOW_RESIZING_LEFT = 4,
378 WINDOW_RESIZING_TOP_LEFT = 5,
379 WINDOW_RESIZING_BOTTOM_LEFT = 6,
380 WINDOW_RESIZING_RIGHT = 8,
381 WINDOW_RESIZING_TOP_RIGHT = 9,
382 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
383 WINDOW_RESIZING_MASK = 15,
384 WINDOW_EXTERIOR = 16,
385 WINDOW_TITLEBAR = 17,
386 WINDOW_CLIENT_AREA = 18,
387};
388
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200389static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500391#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200393struct egl_window_surface {
394 struct toysurface base;
395 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100396 struct display *display;
397 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200398 struct wl_egl_window *egl_window;
399 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100400};
401
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200402static struct egl_window_surface *
403to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100404{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200405 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100406}
407
408static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200409egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanen99436862012-11-19 17:15:59 +0200410 int width, int height, int resize_hint)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100411{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200412 struct egl_window_surface *surface = to_egl_window_surface(base);
413
414 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
415 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
416
417 return cairo_surface_reference(surface->cairo_surface);
418}
419
420static void
421egl_window_surface_swap(struct toysurface *base,
422 struct rectangle *server_allocation)
423{
424 struct egl_window_surface *surface = to_egl_window_surface(base);
425
426 cairo_gl_surface_swapbuffers(surface->cairo_surface);
427 wl_egl_window_get_attached_size(surface->egl_window,
428 &server_allocation->width,
429 &server_allocation->height);
430}
431
432static int
433egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
434{
435 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200436 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100437
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200438 device = cairo_surface_get_device(surface->cairo_surface);
439 if (!device)
440 return -1;
441
442 if (!ctx) {
443 if (device == surface->display->argb_device)
444 ctx = surface->display->argb_ctx;
445 else
446 assert(0);
447 }
448
449 cairo_device_flush(device);
450 cairo_device_acquire(device);
451 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
452 surface->egl_surface, ctx))
453 fprintf(stderr, "failed to make surface current\n");
454
455 return 0;
456}
457
458static void
459egl_window_surface_release(struct toysurface *base)
460{
461 struct egl_window_surface *surface = to_egl_window_surface(base);
462 cairo_device_t *device;
463
464 device = cairo_surface_get_device(surface->cairo_surface);
465 if (!device)
466 return;
467
468 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
469 surface->display->argb_ctx))
470 fprintf(stderr, "failed to make context current\n");
471
472 cairo_device_release(device);
473}
474
475static void
476egl_window_surface_destroy(struct toysurface *base)
477{
478 struct egl_window_surface *surface = to_egl_window_surface(base);
479 struct display *d = surface->display;
480
481 cairo_surface_destroy(surface->cairo_surface);
482 eglDestroySurface(d->dpy, surface->egl_surface);
483 wl_egl_window_destroy(surface->egl_window);
484 surface->surface = NULL;
485
486 free(surface);
487}
488
489static struct toysurface *
490egl_window_surface_create(struct display *display,
491 struct wl_surface *wl_surface,
492 uint32_t flags,
493 struct rectangle *rectangle)
494{
495 struct egl_window_surface *surface;
496
497 surface = calloc(1, sizeof *surface);
498 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100499 return NULL;
500
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200501 surface->base.prepare = egl_window_surface_prepare;
502 surface->base.swap = egl_window_surface_swap;
503 surface->base.acquire = egl_window_surface_acquire;
504 surface->base.release = egl_window_surface_release;
505 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100506
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200507 surface->display = display;
508 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400509
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200510 surface->egl_window = wl_egl_window_create(surface->surface,
511 rectangle->width,
512 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100513
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200514 surface->egl_surface = eglCreateWindowSurface(display->dpy,
515 display->argb_config,
516 surface->egl_window,
517 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100518
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519 surface->cairo_surface =
520 cairo_gl_surface_create_for_egl(display->argb_device,
521 surface->egl_surface,
522 rectangle->width,
523 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100524
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200525 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100526}
527
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400528#endif
529
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200530struct shm_surface_data {
531 struct wl_buffer *buffer;
532 struct shm_pool *pool;
533};
534
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400535struct wl_buffer *
536display_get_buffer_for_surface(struct display *display,
537 cairo_surface_t *surface)
538{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200539 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200541 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400542
543 return data->buffer;
544}
545
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500546static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700547shm_pool_destroy(struct shm_pool *pool);
548
549static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550shm_surface_data_destroy(void *p)
551{
552 struct shm_surface_data *data = p;
553
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200554 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700555 if (data->pool)
556 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300557
558 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559}
560
Kristian Høgsberg16626282012-04-03 11:21:27 -0400561static struct wl_shm_pool *
562make_shm_pool(struct display *display, int size, void **data)
563{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400564 struct wl_shm_pool *pool;
565 int fd;
566
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300567 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400568 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300569 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
570 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400571 return NULL;
572 }
573
574 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400575 if (*data == MAP_FAILED) {
576 fprintf(stderr, "mmap failed: %m\n");
577 close(fd);
578 return NULL;
579 }
580
581 pool = wl_shm_create_pool(display->shm, fd, size);
582
583 close(fd);
584
585 return pool;
586}
587
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700588static struct shm_pool *
589shm_pool_create(struct display *display, size_t size)
590{
591 struct shm_pool *pool = malloc(sizeof *pool);
592
593 if (!pool)
594 return NULL;
595
596 pool->pool = make_shm_pool(display, size, &pool->data);
597 if (!pool->pool) {
598 free(pool);
599 return NULL;
600 }
601
602 pool->size = size;
603 pool->used = 0;
604
605 return pool;
606}
607
608static void *
609shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
610{
611 if (pool->used + size > pool->size)
612 return NULL;
613
614 *offset = pool->used;
615 pool->used += size;
616
617 return (char *) pool->data + *offset;
618}
619
620/* destroy the pool. this does not unmap the memory though */
621static void
622shm_pool_destroy(struct shm_pool *pool)
623{
624 munmap(pool->data, pool->size);
625 wl_shm_pool_destroy(pool->pool);
626 free(pool);
627}
628
629/* Start allocating from the beginning of the pool again */
630static void
631shm_pool_reset(struct shm_pool *pool)
632{
633 pool->used = 0;
634}
635
636static int
637data_length_for_shm_surface(struct rectangle *rect)
638{
639 int stride;
640
641 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
642 rect->width);
643 return stride * rect->height;
644}
645
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500646static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700647display_create_shm_surface_from_pool(struct display *display,
648 struct rectangle *rectangle,
649 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650{
651 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400652 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400653 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700654 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400655 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400656
657 data = malloc(sizeof *data);
658 if (data == NULL)
659 return NULL;
660
661 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
662 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700663 length = stride * rectangle->height;
664 data->pool = NULL;
665 map = shm_pool_allocate(pool, length, &offset);
666
667 if (!map) {
668 free(data);
669 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400670 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400671
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400672 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400673 CAIRO_FORMAT_ARGB32,
674 rectangle->width,
675 rectangle->height,
676 stride);
677
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200678 cairo_surface_set_user_data(surface, &shm_surface_data_key,
679 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400680
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400681 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500682 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400683 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500684 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400685
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200686 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
687 rectangle->width,
688 rectangle->height,
689 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400690
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700691 return surface;
692}
693
694static cairo_surface_t *
695display_create_shm_surface(struct display *display,
696 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200697 struct shm_pool *alternate_pool,
698 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700699{
700 struct shm_surface_data *data;
701 struct shm_pool *pool;
702 cairo_surface_t *surface;
703
Pekka Paalanen99436862012-11-19 17:15:59 +0200704 if (alternate_pool) {
705 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700706 surface = display_create_shm_surface_from_pool(display,
707 rectangle,
708 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200709 alternate_pool);
710 if (surface) {
711 data = cairo_surface_get_user_data(surface,
712 &shm_surface_data_key);
713 goto out;
714 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700715 }
716
717 pool = shm_pool_create(display,
718 data_length_for_shm_surface(rectangle));
719 if (!pool)
720 return NULL;
721
722 surface =
723 display_create_shm_surface_from_pool(display, rectangle,
724 flags, pool);
725
726 if (!surface) {
727 shm_pool_destroy(pool);
728 return NULL;
729 }
730
731 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200732 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700733 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400734
Pekka Paalanen99436862012-11-19 17:15:59 +0200735out:
736 if (data_ret)
737 *data_ret = data;
738
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400739 return surface;
740}
741
nobled7b87cb02011-02-01 18:51:47 +0000742static int
743check_size(struct rectangle *rect)
744{
745 if (rect->width && rect->height)
746 return 0;
747
748 fprintf(stderr, "tried to create surface of "
749 "width: %d, height: %d\n", rect->width, rect->height);
750 return -1;
751}
752
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400753cairo_surface_t *
754display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100755 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400756 struct rectangle *rectangle,
757 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400758{
nobled7b87cb02011-02-01 18:51:47 +0000759 if (check_size(rectangle) < 0)
760 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200761
762 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200763 return display_create_shm_surface(display, rectangle, flags,
764 NULL, NULL);
765}
766
767struct shm_surface {
768 struct toysurface base;
769 struct display *display;
770 struct wl_surface *surface;
771 uint32_t flags;
772 int dx, dy;
773
774 cairo_surface_t *cairo_surface;
775 /* 'data' is automatically destroyed, when 'cairo_surface' is */
776 struct shm_surface_data *data;
777
778 struct shm_pool *resize_pool;
779};
780
781static struct shm_surface *
782to_shm_surface(struct toysurface *base)
783{
784 return container_of(base, struct shm_surface, base);
785}
786
787static cairo_surface_t *
788shm_surface_prepare(struct toysurface *base, int dx, int dy,
789 int width, int height, int resize_hint)
790{
791 struct shm_surface *surface = to_shm_surface(base);
792 struct rectangle rect = { 0, 0, width, height };
793
794 surface->dx = dx;
795 surface->dy = dy;
796
797 if (!resize_hint && surface->resize_pool) {
798 cairo_surface_destroy(surface->cairo_surface);
799 shm_pool_destroy(surface->resize_pool);
800 surface->resize_pool = NULL;
801 surface->cairo_surface = NULL;
802 }
803
804 if (surface->cairo_surface &&
805 cairo_image_surface_get_width(surface->cairo_surface) == width &&
806 cairo_image_surface_get_height(surface->cairo_surface) == height)
807 goto out;
808
809 if (surface->cairo_surface)
810 cairo_surface_destroy(surface->cairo_surface);
811
812 if (resize_hint && !surface->resize_pool) {
813 /* Create a big pool to allocate from, while continuously
814 * resizing. Mmapping a new pool in the server
815 * is relatively expensive, so reusing a pool performs
816 * better, but may temporarily reserve unneeded memory.
817 */
818 /* We should probably base this number on the output size. */
819 surface->resize_pool = shm_pool_create(surface->display,
820 6 * 1024 * 1024);
821 }
822
823 surface->cairo_surface =
824 display_create_shm_surface(surface->display, &rect,
825 surface->flags,
826 surface->resize_pool,
827 &surface->data);
828
829out:
830 return cairo_surface_reference(surface->cairo_surface);
831}
832
833static void
834shm_surface_swap(struct toysurface *base,
835 struct rectangle *server_allocation)
836{
837 struct shm_surface *surface = to_shm_surface(base);
838
839 server_allocation->width =
840 cairo_image_surface_get_width(surface->cairo_surface);
841 server_allocation->height =
842 cairo_image_surface_get_height(surface->cairo_surface);
843
844 wl_surface_attach(surface->surface, surface->data->buffer,
845 surface->dx, surface->dy);
846 wl_surface_damage(surface->surface, 0, 0,
847 server_allocation->width, server_allocation->height);
848 wl_surface_commit(surface->surface);
849}
850
851static int
852shm_surface_acquire(struct toysurface *base, EGLContext ctx)
853{
854 return -1;
855}
856
857static void
858shm_surface_release(struct toysurface *base)
859{
860}
861
862static void
863shm_surface_destroy(struct toysurface *base)
864{
865 struct shm_surface *surface = to_shm_surface(base);
866
867 /* this destroys surface->data, too */
868 cairo_surface_destroy(surface->cairo_surface);
869
870 if (surface->resize_pool)
871 shm_pool_destroy(surface->resize_pool);
872
873 free(surface);
874}
875
876static struct toysurface *
877shm_surface_create(struct display *display, struct wl_surface *wl_surface,
878 uint32_t flags, struct rectangle *rectangle)
879{
880 struct shm_surface *surface;
881
882 surface = calloc(1, sizeof *surface);
883 if (!surface)
884 return NULL;
885
886 surface->base.prepare = shm_surface_prepare;
887 surface->base.swap = shm_surface_swap;
888 surface->base.acquire = shm_surface_acquire;
889 surface->base.release = shm_surface_release;
890 surface->base.destroy = shm_surface_destroy;
891
892 surface->display = display;
893 surface->surface = wl_surface;
894 surface->flags = flags;
895 surface->cairo_surface = display_create_shm_surface(display, rectangle,
896 flags, NULL,
897 &surface->data);
898 if (!surface->cairo_surface) {
899 free(surface);
900 return NULL;
901 }
902
903 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400904}
905
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200906/*
907 * The following correspondences between file names and cursors was copied
908 * from: https://bugs.kde.org/attachment.cgi?id=67313
909 */
910
911static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300912 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200913 "sw-resize"
914};
915
916static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300917 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200918 "se-resize"
919};
920
921static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300922 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200923 "s-resize"
924};
925
926static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300927 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200928 "closedhand",
929 "208530c400c041818281048008011002"
930};
931
932static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300933 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200934 "default",
935 "top_left_arrow",
936 "left-arrow"
937};
938
939static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300940 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200941 "w-resize"
942};
943
944static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300945 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200946 "e-resize"
947};
948
949static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300950 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200951 "nw-resize"
952};
953
954static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300955 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200956 "ne-resize"
957};
958
959static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300960 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200961 "n-resize"
962};
963
964static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300965 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200966 "ibeam",
967 "text"
968};
969
970static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300971 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200972 "pointer",
973 "pointing_hand",
974 "e29285e634086352946a0e7090d73106"
975};
976
977static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400978 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200979 "wait",
980 "0426c94ea35c87780ff01dc239897213"
981};
982
983struct cursor_alternatives {
984 const char **names;
985 size_t count;
986};
987
988static const struct cursor_alternatives cursors[] = {
989 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
990 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
991 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
992 {grabbings, ARRAY_LENGTH(grabbings)},
993 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
994 {left_sides, ARRAY_LENGTH(left_sides)},
995 {right_sides, ARRAY_LENGTH(right_sides)},
996 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
997 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
998 {top_sides, ARRAY_LENGTH(top_sides)},
999 {xterms, ARRAY_LENGTH(xterms)},
1000 {hand1s, ARRAY_LENGTH(hand1s)},
1001 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001002};
1003
1004static void
1005create_cursors(struct display *display)
1006{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001007 char *config_file;
1008 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001009 unsigned int i, j;
1010 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001011 struct config_key shell_keys[] = {
1012 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1013 };
1014 struct config_section cs[] = {
1015 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1016 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001017
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001018 config_file = config_file_path("weston.ini");
1019 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1020 free(config_file);
1021
1022 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001023 display->cursors =
1024 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1025
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001026 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001027 cursor = NULL;
1028 for (j = 0; !cursor && j < cursors[i].count; ++j)
1029 cursor = wl_cursor_theme_get_cursor(
1030 display->cursor_theme, cursors[i].names[j]);
1031
1032 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001033 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001034 cursors[i].names[0]);
1035
1036 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001037 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001038}
1039
1040static void
1041destroy_cursors(struct display *display)
1042{
1043 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001044 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001045}
1046
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001047struct wl_cursor_image *
1048display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001049{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001050 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001051
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001052 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001053}
1054
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001055static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001056window_get_resize_dx_dy(struct window *window, int *x, int *y)
1057{
1058 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1059 *x = window->server_allocation.width - window->allocation.width;
1060 else
1061 *x = 0;
1062
1063 if (window->resize_edges & WINDOW_RESIZING_TOP)
1064 *y = window->server_allocation.height -
1065 window->allocation.height;
1066 else
1067 *y = 0;
1068
1069 window->resize_edges = 0;
1070}
1071
1072static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001073window_attach_surface(struct window *window)
1074{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001075 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001076
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001077 if (window->type == TYPE_NONE) {
1078 window->type = TYPE_TOPLEVEL;
1079 if (display->shell)
1080 wl_shell_surface_set_toplevel(window->shell_surface);
1081 }
1082
Pekka Paalanen512dde82012-10-10 12:49:27 +03001083 if (window->opaque_region) {
1084 wl_surface_set_opaque_region(window->surface,
1085 window->opaque_region);
1086 wl_region_destroy(window->opaque_region);
1087 window->opaque_region = NULL;
1088 }
1089
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001090 if (window->input_region) {
1091 wl_surface_set_input_region(window->surface,
1092 window->input_region);
1093 wl_region_destroy(window->input_region);
1094 window->input_region = NULL;
1095 }
1096
Pekka Paalanen99436862012-11-19 17:15:59 +02001097 window->toysurface->swap(window->toysurface,
1098 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001099}
1100
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001101int
1102window_has_focus(struct window *window)
1103{
1104 return window->focus_count > 0;
1105}
1106
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001107static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001108window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001109{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001110 if (!window->cairo_surface)
1111 return;
1112
1113 window_attach_surface(window);
1114 cairo_surface_destroy(window->cairo_surface);
1115 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001116}
1117
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001118struct display *
1119window_get_display(struct window *window)
1120{
1121 return window->display;
1122}
1123
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001124static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001125window_create_surface(struct window *window)
1126{
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001127 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001128 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001129
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001130 if (!window->transparent)
1131 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001132
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001133 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001134#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001135 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001136 if (!window->toysurface && window->display->dpy)
1137 window->toysurface =
1138 egl_window_surface_create(window->display,
1139 window->surface,
1140 flags,
1141 &window->allocation);
1142
Pekka Paalanen99436862012-11-19 17:15:59 +02001143 if (window->toysurface)
Pekka Paalanen768117f2012-11-19 17:15:57 +02001144 break;
Pekka Paalanen768117f2012-11-19 17:15:57 +02001145 /* fall through */
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001146#endif
1147 case WINDOW_BUFFER_TYPE_SHM:
Pekka Paalanen99436862012-11-19 17:15:59 +02001148 window->toysurface = shm_surface_create(window->display,
1149 window->surface, flags,
1150 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001151 break;
Pekka Paalanen99436862012-11-19 17:15:59 +02001152 default:
1153 assert(0);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001154 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001155
Pekka Paalanen99436862012-11-19 17:15:59 +02001156 window_get_resize_dx_dy(window, &dx, &dy);
1157 window->cairo_surface =
1158 window->toysurface->prepare(window->toysurface, dx, dy,
1159 window->allocation.width,
1160 window->allocation.height,
1161 window->resizing);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001162}
1163
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001164static void frame_destroy(struct frame *frame);
1165
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001166void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001167window_destroy(struct window *window)
1168{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001169 struct display *display = window->display;
1170 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001171 struct window_output *window_output;
1172 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001173
1174 if (window->redraw_scheduled)
1175 wl_list_remove(&window->redraw_task.link);
1176
1177 wl_list_for_each(input, &display->input_list, link) {
1178 if (input->pointer_focus == window)
1179 input->pointer_focus = NULL;
1180 if (input->keyboard_focus == window)
1181 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001182 if (input->focus_widget &&
1183 input->focus_widget->window == window)
1184 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001185 }
1186
Rob Bradford7507b572012-05-15 17:55:34 +01001187 wl_list_for_each_safe(window_output, window_output_tmp,
1188 &window->window_output_list, link) {
1189 free (window_output);
1190 }
1191
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001192 if (window->input_region)
1193 wl_region_destroy(window->input_region);
1194 if (window->opaque_region)
1195 wl_region_destroy(window->opaque_region);
1196
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001197 if (window->frame)
1198 frame_destroy(window->frame);
1199
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001200 if (window->shell_surface)
1201 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001202 wl_surface_destroy(window->surface);
1203 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001204
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001205 if (window->toysurface)
1206 window->toysurface->destroy(window->toysurface);
1207
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001208 if (window->frame_cb)
1209 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001210 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001211 free(window);
1212}
1213
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001214static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001215widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001216{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001217 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001218
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001219 wl_list_for_each(child, &widget->child_list, link) {
1220 target = widget_find_widget(child, x, y);
1221 if (target)
1222 return target;
1223 }
1224
1225 if (widget->allocation.x <= x &&
1226 x < widget->allocation.x + widget->allocation.width &&
1227 widget->allocation.y <= y &&
1228 y < widget->allocation.y + widget->allocation.height) {
1229 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001230 }
1231
1232 return NULL;
1233}
1234
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001235static struct widget *
1236widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001237{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001238 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001239
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001240 widget = malloc(sizeof *widget);
1241 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001242 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001243 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001244 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001245 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001246 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001247 widget->tooltip = NULL;
1248 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001249
1250 return widget;
1251}
1252
1253struct widget *
1254window_add_widget(struct window *window, void *data)
1255{
1256 window->widget = widget_create(window, data);
1257 wl_list_init(&window->widget->link);
1258
1259 return window->widget;
1260}
1261
1262struct widget *
1263widget_add_widget(struct widget *parent, void *data)
1264{
1265 struct widget *widget;
1266
1267 widget = widget_create(parent->window, data);
1268 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001269
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001270 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001271}
1272
1273void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001274widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001275{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001276 struct display *display = widget->window->display;
1277 struct input *input;
1278
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001279 if (widget->tooltip) {
1280 free(widget->tooltip);
1281 widget->tooltip = NULL;
1282 }
1283
Pekka Paalanene156fb62012-01-19 13:51:38 +02001284 wl_list_for_each(input, &display->input_list, link) {
1285 if (input->focus_widget == widget)
1286 input->focus_widget = NULL;
1287 }
1288
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001289 wl_list_remove(&widget->link);
1290 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001291}
1292
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001293void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001294widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001295{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001296 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001297}
1298
1299void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001300widget_set_size(struct widget *widget, int32_t width, int32_t height)
1301{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001302 widget->allocation.width = width;
1303 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001304}
1305
1306void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001307widget_set_allocation(struct widget *widget,
1308 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001309{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001310 widget->allocation.x = x;
1311 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001312 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001313}
1314
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001315void
1316widget_set_transparent(struct widget *widget, int transparent)
1317{
1318 widget->opaque = !transparent;
1319}
1320
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001321void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001322widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001323{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001324 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001325}
1326
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001327void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001328widget_set_resize_handler(struct widget *widget,
1329 widget_resize_handler_t handler)
1330{
1331 widget->resize_handler = handler;
1332}
1333
1334void
1335widget_set_redraw_handler(struct widget *widget,
1336 widget_redraw_handler_t handler)
1337{
1338 widget->redraw_handler = handler;
1339}
1340
1341void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001342widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001343{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001344 widget->enter_handler = handler;
1345}
1346
1347void
1348widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1349{
1350 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001351}
1352
1353void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001354widget_set_motion_handler(struct widget *widget,
1355 widget_motion_handler_t handler)
1356{
1357 widget->motion_handler = handler;
1358}
1359
1360void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001361widget_set_button_handler(struct widget *widget,
1362 widget_button_handler_t handler)
1363{
1364 widget->button_handler = handler;
1365}
1366
1367void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001368widget_set_axis_handler(struct widget *widget,
1369 widget_axis_handler_t handler)
1370{
1371 widget->axis_handler = handler;
1372}
1373
1374void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001375widget_schedule_redraw(struct widget *widget)
1376{
1377 window_schedule_redraw(widget->window);
1378}
1379
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001380cairo_surface_t *
1381window_get_surface(struct window *window)
1382{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001383 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001384}
1385
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001386struct wl_surface *
1387window_get_wl_surface(struct window *window)
1388{
1389 return window->surface;
1390}
1391
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001392struct wl_shell_surface *
1393window_get_wl_shell_surface(struct window *window)
1394{
1395 return window->shell_surface;
1396}
1397
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001398static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001399tooltip_redraw_handler(struct widget *widget, void *data)
1400{
1401 cairo_t *cr;
1402 const int32_t r = 3;
1403 struct tooltip *tooltip = data;
1404 int32_t width, height;
1405 struct window *window = widget->window;
1406
1407 cr = cairo_create(window->cairo_surface);
1408 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1409 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1410 cairo_paint(cr);
1411
1412 width = window->allocation.width;
1413 height = window->allocation.height;
1414 rounded_rect(cr, 0, 0, width, height, r);
1415
1416 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1417 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1418 cairo_fill(cr);
1419
1420 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1421 cairo_move_to(cr, 10, 16);
1422 cairo_show_text(cr, tooltip->entry);
1423 cairo_destroy(cr);
1424}
1425
1426static cairo_text_extents_t
1427get_text_extents(struct tooltip *tooltip)
1428{
1429 struct window *window;
1430 cairo_t *cr;
1431 cairo_text_extents_t extents;
1432
1433 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1434 * created yet */
1435 window = tooltip->widget->window->parent;
1436 cr = cairo_create(window->cairo_surface);
1437 cairo_text_extents(cr, tooltip->entry, &extents);
1438 cairo_destroy(cr);
1439
1440 return extents;
1441}
1442
1443static int
1444window_create_tooltip(struct tooltip *tooltip)
1445{
1446 struct widget *parent = tooltip->parent;
1447 struct display *display = parent->window->display;
1448 struct window *window;
1449 const int offset_y = 27;
1450 const int margin = 3;
1451 cairo_text_extents_t extents;
1452
1453 if (tooltip->widget)
1454 return 0;
1455
1456 window = window_create_transient(display, parent->window, tooltip->x,
1457 tooltip->y + offset_y,
1458 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1459 if (!window)
1460 return -1;
1461
1462 tooltip->window = window;
1463 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1464
1465 extents = get_text_extents(tooltip);
1466 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1467 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1468
1469 return 0;
1470}
1471
1472void
1473widget_destroy_tooltip(struct widget *parent)
1474{
1475 struct tooltip *tooltip = parent->tooltip;
1476
1477 parent->tooltip_count = 0;
1478 if (!tooltip)
1479 return;
1480
1481 if (tooltip->widget) {
1482 widget_destroy(tooltip->widget);
1483 window_destroy(tooltip->window);
1484 tooltip->widget = NULL;
1485 tooltip->window = NULL;
1486 }
1487
1488 close(tooltip->tooltip_fd);
1489 free(tooltip->entry);
1490 free(tooltip);
1491 parent->tooltip = NULL;
1492}
1493
1494static void
1495tooltip_func(struct task *task, uint32_t events)
1496{
1497 struct tooltip *tooltip =
1498 container_of(task, struct tooltip, tooltip_task);
1499 uint64_t exp;
1500
Martin Olsson8df662a2012-07-08 03:03:47 +02001501 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1502 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001503 window_create_tooltip(tooltip);
1504}
1505
1506#define TOOLTIP_TIMEOUT 500
1507static int
1508tooltip_timer_reset(struct tooltip *tooltip)
1509{
1510 struct itimerspec its;
1511
1512 its.it_interval.tv_sec = 0;
1513 its.it_interval.tv_nsec = 0;
1514 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1515 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1516 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1517 fprintf(stderr, "could not set timerfd\n: %m");
1518 return -1;
1519 }
1520
1521 return 0;
1522}
1523
1524int
1525widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1526{
1527 struct tooltip *tooltip = parent->tooltip;
1528
1529 parent->tooltip_count++;
1530 if (tooltip) {
1531 tooltip->x = x;
1532 tooltip->y = y;
1533 tooltip_timer_reset(tooltip);
1534 return 0;
1535 }
1536
1537 /* the handler might be triggered too fast via input device motion, so
1538 * we need this check here to make sure tooltip is fully initialized */
1539 if (parent->tooltip_count > 1)
1540 return 0;
1541
1542 tooltip = malloc(sizeof *tooltip);
1543 if (!tooltip)
1544 return -1;
1545
1546 parent->tooltip = tooltip;
1547 tooltip->parent = parent;
1548 tooltip->widget = NULL;
1549 tooltip->window = NULL;
1550 tooltip->x = x;
1551 tooltip->y = y;
1552 tooltip->entry = strdup(entry);
1553 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1554 if (tooltip->tooltip_fd < 0) {
1555 fprintf(stderr, "could not create timerfd\n: %m");
1556 return -1;
1557 }
1558
1559 tooltip->tooltip_task.run = tooltip_func;
1560 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1561 EPOLLIN, &tooltip->tooltip_task);
1562 tooltip_timer_reset(tooltip);
1563
1564 return 0;
1565}
1566
1567static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001568workspace_manager_state(void *data,
1569 struct workspace_manager *workspace_manager,
1570 uint32_t current,
1571 uint32_t count)
1572{
1573 struct display *display = data;
1574
1575 display->workspace = current;
1576 display->workspace_count = count;
1577}
1578
1579static const struct workspace_manager_listener workspace_manager_listener = {
1580 workspace_manager_state
1581};
1582
1583static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001584frame_resize_handler(struct widget *widget,
1585 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001586{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001587 struct frame *frame = data;
1588 struct widget *child = frame->child;
1589 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001590 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001591 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001592 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001593 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001594 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001595 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001596
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001597 switch (widget->window->type) {
1598 case TYPE_FULLSCREEN:
1599 decoration_width = 0;
1600 decoration_height = 0;
1601
1602 allocation.x = 0;
1603 allocation.y = 0;
1604 allocation.width = width;
1605 allocation.height = height;
1606 opaque_margin = 0;
1607
1608 wl_list_for_each(button, &frame->buttons_list, link)
1609 button->widget->opaque = 1;
1610 break;
1611 case TYPE_MAXIMIZED:
1612 decoration_width = t->width * 2;
1613 decoration_height = t->width + t->titlebar_height;
1614
1615 allocation.x = t->width;
1616 allocation.y = t->titlebar_height;
1617 allocation.width = width - decoration_width;
1618 allocation.height = height - decoration_height;
1619
1620 opaque_margin = 0;
1621
1622 wl_list_for_each(button, &frame->buttons_list, link)
1623 button->widget->opaque = 0;
1624 break;
1625 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001626 decoration_width = (t->width + t->margin) * 2;
1627 decoration_height = t->width +
1628 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001629
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001630 allocation.x = t->width + t->margin;
1631 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001632 allocation.width = width - decoration_width;
1633 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001634
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001635 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001636
1637 wl_list_for_each(button, &frame->buttons_list, link)
1638 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001639 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001640 }
1641
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 widget_set_allocation(child, allocation.x, allocation.y,
1643 allocation.width, allocation.height);
1644
1645 if (child->resize_handler)
1646 child->resize_handler(child,
1647 allocation.width,
1648 allocation.height,
1649 child->user_data);
1650
Scott Moreauf7e498c2012-05-14 11:39:29 -06001651 width = child->allocation.width + decoration_width;
1652 height = child->allocation.height + decoration_height;
1653
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001654 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1655
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001656 widget->window->input_region =
1657 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001658 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001659 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001660 shadow_margin, shadow_margin,
1661 width - 2 * shadow_margin,
1662 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001663 } else {
1664 wl_region_add(widget->window->input_region,
1665 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001666 }
1667
Scott Moreauf7e498c2012-05-14 11:39:29 -06001668 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001669
1670 if (child->opaque) {
1671 widget->window->opaque_region =
1672 wl_compositor_create_region(display->compositor);
1673 wl_region_add(widget->window->opaque_region,
1674 opaque_margin, opaque_margin,
1675 widget->allocation.width - 2 * opaque_margin,
1676 widget->allocation.height - 2 * opaque_margin);
1677 }
Martin Minarik1998b152012-05-10 02:04:35 +02001678
1679 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001680 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1681 x_l = t->width + shadow_margin;
1682 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001683 wl_list_for_each(button, &frame->buttons_list, link) {
1684 const int button_padding = 4;
1685 w = cairo_image_surface_get_width(button->icon);
1686 h = cairo_image_surface_get_height(button->icon);
1687
1688 if (button->decoration == FRAME_BUTTON_FANCY)
1689 w += 10;
1690
1691 if (button->align == FRAME_BUTTON_LEFT) {
1692 widget_set_allocation(button->widget,
1693 x_l, y , w + 1, h + 1);
1694 x_l += w;
1695 x_l += button_padding;
1696 } else {
1697 x_r -= w;
1698 widget_set_allocation(button->widget,
1699 x_r, y , w + 1, h + 1);
1700 x_r -= button_padding;
1701 }
1702 }
1703}
1704
1705static int
1706frame_button_enter_handler(struct widget *widget,
1707 struct input *input, float x, float y, void *data)
1708{
1709 struct frame_button *frame_button = data;
1710
1711 widget_schedule_redraw(frame_button->widget);
1712 frame_button->state = FRAME_BUTTON_OVER;
1713
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001714 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001715}
1716
1717static void
1718frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1719{
1720 struct frame_button *frame_button = data;
1721
1722 widget_schedule_redraw(frame_button->widget);
1723 frame_button->state = FRAME_BUTTON_DEFAULT;
1724}
1725
1726static void
1727frame_button_button_handler(struct widget *widget,
1728 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001729 uint32_t button,
1730 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001731{
1732 struct frame_button *frame_button = data;
1733 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001734 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001735
1736 if (button != BTN_LEFT)
1737 return;
1738
1739 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001740 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001741 frame_button->state = FRAME_BUTTON_ACTIVE;
1742 widget_schedule_redraw(frame_button->widget);
1743
1744 if (frame_button->type == FRAME_BUTTON_ICON)
1745 window_show_frame_menu(window, input, time);
1746 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001747 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001748 frame_button->state = FRAME_BUTTON_DEFAULT;
1749 widget_schedule_redraw(frame_button->widget);
1750 break;
1751 }
1752
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001753 if (!was_pressed)
1754 return;
1755
Martin Minarik1998b152012-05-10 02:04:35 +02001756 switch (frame_button->type) {
1757 case FRAME_BUTTON_CLOSE:
1758 if (window->close_handler)
1759 window->close_handler(window->parent,
1760 window->user_data);
1761 else
1762 display_exit(window->display);
1763 break;
1764 case FRAME_BUTTON_MINIMIZE:
1765 fprintf(stderr,"Minimize stub\n");
1766 break;
1767 case FRAME_BUTTON_MAXIMIZE:
1768 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1769 break;
1770 default:
1771 /* Unknown operation */
1772 break;
1773 }
1774}
1775
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001776static int
1777frame_button_motion_handler(struct widget *widget,
1778 struct input *input, uint32_t time,
1779 float x, float y, void *data)
1780{
1781 struct frame_button *frame_button = data;
1782 enum frame_button_pointer previous_button_state = frame_button->state;
1783
1784 /* only track state for a pressed button */
1785 if (input->grab != widget)
1786 return CURSOR_LEFT_PTR;
1787
1788 if (x > widget->allocation.x &&
1789 x < (widget->allocation.x + widget->allocation.width) &&
1790 y > widget->allocation.y &&
1791 y < (widget->allocation.y + widget->allocation.height)) {
1792 frame_button->state = FRAME_BUTTON_ACTIVE;
1793 } else {
1794 frame_button->state = FRAME_BUTTON_DEFAULT;
1795 }
1796
1797 if (frame_button->state != previous_button_state)
1798 widget_schedule_redraw(frame_button->widget);
1799
1800 return CURSOR_LEFT_PTR;
1801}
1802
Martin Minarik1998b152012-05-10 02:04:35 +02001803static void
1804frame_button_redraw_handler(struct widget *widget, void *data)
1805{
1806 struct frame_button *frame_button = data;
1807 cairo_t *cr;
1808 int width, height, x, y;
1809 struct window *window = widget->window;
1810
1811 x = widget->allocation.x;
1812 y = widget->allocation.y;
1813 width = widget->allocation.width;
1814 height = widget->allocation.height;
1815
1816 if (!width)
1817 return;
1818 if (!height)
1819 return;
1820 if (widget->opaque)
1821 return;
1822
1823 cr = cairo_create(window->cairo_surface);
1824
1825 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1826 cairo_set_line_width(cr, 1);
1827
1828 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1829 cairo_rectangle (cr, x, y, 25, 16);
1830
1831 cairo_stroke_preserve(cr);
1832
1833 switch (frame_button->state) {
1834 case FRAME_BUTTON_DEFAULT:
1835 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1836 break;
1837 case FRAME_BUTTON_OVER:
1838 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1839 break;
1840 case FRAME_BUTTON_ACTIVE:
1841 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1842 break;
1843 }
1844
1845 cairo_fill (cr);
1846
1847 x += 4;
1848 }
1849
1850 cairo_set_source_surface(cr, frame_button->icon, x, y);
1851 cairo_paint(cr);
1852
1853 cairo_destroy(cr);
1854}
1855
1856static struct widget *
1857frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1858 enum frame_button_align align, enum frame_button_decoration style)
1859{
1860 struct frame_button *frame_button;
1861 const char *icon = data;
1862
1863 frame_button = malloc (sizeof *frame_button);
1864 memset(frame_button, 0, sizeof *frame_button);
1865
1866 frame_button->icon = cairo_image_surface_create_from_png(icon);
1867 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1868 frame_button->frame = frame;
1869 frame_button->type = type;
1870 frame_button->align = align;
1871 frame_button->decoration = style;
1872
1873 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1874
1875 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1876 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1877 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1878 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001879 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001880 return frame_button->widget;
1881}
1882
1883static void
1884frame_button_destroy(struct frame_button *frame_button)
1885{
1886 widget_destroy(frame_button->widget);
1887 wl_list_remove(&frame_button->link);
1888 cairo_surface_destroy(frame_button->icon);
1889 free(frame_button);
1890
1891 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001892}
1893
1894static void
1895frame_redraw_handler(struct widget *widget, void *data)
1896{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001897 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001898 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001899 struct theme *t = window->display->theme;
1900 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001901
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001902 if (window->type == TYPE_FULLSCREEN)
1903 return;
1904
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001905 cr = cairo_create(window->cairo_surface);
1906
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001907 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001908 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001909 if (window->type == TYPE_MAXIMIZED)
1910 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001911 theme_render_frame(t, cr, widget->allocation.width,
1912 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001913
1914 cairo_destroy(cr);
1915}
1916
1917static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001918frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001919{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001920 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001921 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001922 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001923
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001924 location = theme_get_location(t, input->sx, input->sy,
1925 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001926 frame->widget->allocation.height,
1927 window->type == TYPE_MAXIMIZED ?
1928 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001929
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001930 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001931 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001932 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001933 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001934 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001935 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001936 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001937 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001938 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001939 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001940 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001941 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001942 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001943 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001944 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001945 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001946 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001947 case THEME_LOCATION_EXTERIOR:
1948 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001949 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001950 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001951 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001952}
1953
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001954static void
1955frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001956{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001957 struct display *display;
1958
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001959 switch (index) {
1960 case 0: /* close */
1961 if (window->close_handler)
1962 window->close_handler(window->parent,
1963 window->user_data);
1964 else
1965 display_exit(window->display);
1966 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001967 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001968 display = window->display;
1969 if (display->workspace > 0)
1970 workspace_manager_move_surface(display->workspace_manager,
1971 window->surface,
1972 display->workspace - 1);
1973 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001974 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001975 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001976 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001977 workspace_manager_move_surface(display->workspace_manager,
1978 window->surface,
1979 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05001980 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001981 case 3: /* fullscreen */
1982 /* we don't have a way to get out of fullscreen for now */
1983 if (window->fullscreen_handler)
1984 window->fullscreen_handler(window, window->user_data);
1985 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001986 }
1987}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001988
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001989void
1990window_show_frame_menu(struct window *window,
1991 struct input *input, uint32_t time)
1992{
1993 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001994 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001995
1996 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001997 "Close",
1998 "Move to workspace above", "Move to workspace below",
1999 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002000 };
2001
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002002 if (window->fullscreen_handler)
2003 count = ARRAY_LENGTH(entries);
2004 else
2005 count = ARRAY_LENGTH(entries) - 1;
2006
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002007 input_get_position(input, &x, &y);
2008 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002009 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002010}
2011
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002012static int
2013frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002014 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002015{
2016 return frame_get_pointer_image_for_location(data, input);
2017}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002018
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002019static int
2020frame_motion_handler(struct widget *widget,
2021 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002022 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002023{
2024 return frame_get_pointer_image_for_location(data, input);
2025}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002026
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002027static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002028frame_button_handler(struct widget *widget,
2029 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002030 uint32_t button, enum wl_pointer_button_state state,
2031 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002032
2033{
2034 struct frame *frame = data;
2035 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002036 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002037 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002038
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002039 location = theme_get_location(display->theme, input->sx, input->sy,
2040 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002041 frame->widget->allocation.height,
2042 window->type == TYPE_MAXIMIZED ?
2043 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002044
Daniel Stone4dbadb12012-05-30 16:31:51 +01002045 if (window->display->shell && button == BTN_LEFT &&
2046 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002047 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002048 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002049 if (!window->shell_surface)
2050 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002051 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002052 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002053 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002054 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002055 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002056 case THEME_LOCATION_RESIZING_TOP:
2057 case THEME_LOCATION_RESIZING_BOTTOM:
2058 case THEME_LOCATION_RESIZING_LEFT:
2059 case THEME_LOCATION_RESIZING_RIGHT:
2060 case THEME_LOCATION_RESIZING_TOP_LEFT:
2061 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2062 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2063 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002064 if (!window->shell_surface)
2065 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002066 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002067
Pekka Paalanen99436862012-11-19 17:15:59 +02002068 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002069 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002070 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002071 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002072 break;
2073 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002074 } else if (button == BTN_RIGHT &&
2075 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002076 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002077 }
2078}
2079
2080struct widget *
2081frame_create(struct window *window, void *data)
2082{
2083 struct frame *frame;
2084
2085 frame = malloc(sizeof *frame);
2086 memset(frame, 0, sizeof *frame);
2087
2088 frame->widget = window_add_widget(window, frame);
2089 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002090
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002091 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2092 widget_set_resize_handler(frame->widget, frame_resize_handler);
2093 widget_set_enter_handler(frame->widget, frame_enter_handler);
2094 widget_set_motion_handler(frame->widget, frame_motion_handler);
2095 widget_set_button_handler(frame->widget, frame_button_handler);
2096
Martin Minarik1998b152012-05-10 02:04:35 +02002097 /* Create empty list for frame buttons */
2098 wl_list_init(&frame->buttons_list);
2099
2100 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2101 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2102
2103 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2104 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2105
2106 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2107 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2108
2109 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2110 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2111
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002112 window->frame = frame;
2113
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002114 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002115}
2116
Kristian Høgsberga1627922012-06-20 17:30:03 -04002117void
2118frame_set_child_size(struct widget *widget, int child_width, int child_height)
2119{
2120 struct display *display = widget->window->display;
2121 struct theme *t = display->theme;
2122 int decoration_width, decoration_height;
2123 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002124 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002125
2126 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002127 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002128 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002129 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002130
2131 width = child_width + decoration_width;
2132 height = child_height + decoration_height;
2133 } else {
2134 width = child_width;
2135 height = child_height;
2136 }
2137
2138 window_schedule_resize(widget->window, width, height);
2139}
2140
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002141static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002142frame_destroy(struct frame *frame)
2143{
Martin Minarik1998b152012-05-10 02:04:35 +02002144 struct frame_button *button, *tmp;
2145
2146 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2147 frame_button_destroy(button);
2148
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002149 /* frame->child must be destroyed by the application */
2150 widget_destroy(frame->widget);
2151 free(frame);
2152}
2153
2154static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002155input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002156 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002157{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002158 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002159 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002160
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002161 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002162 return;
2163
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002164 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002165 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002166 widget = old;
2167 if (input->grab)
2168 widget = input->grab;
2169 if (widget->leave_handler)
2170 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002171 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002172 }
2173
2174 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002175 widget = focus;
2176 if (input->grab)
2177 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002178 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002179 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002180 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002181 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002182
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002183 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002184 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002185}
2186
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002187void
2188input_grab(struct input *input, struct widget *widget, uint32_t button)
2189{
2190 input->grab = widget;
2191 input->grab_button = button;
2192}
2193
2194void
2195input_ungrab(struct input *input)
2196{
2197 struct widget *widget;
2198
2199 input->grab = NULL;
2200 if (input->pointer_focus) {
2201 widget = widget_find_widget(input->pointer_focus->widget,
2202 input->sx, input->sy);
2203 input_set_focus_widget(input, widget, input->sx, input->sy);
2204 }
2205}
2206
2207static void
2208input_remove_pointer_focus(struct input *input)
2209{
2210 struct window *window = input->pointer_focus;
2211
2212 if (!window)
2213 return;
2214
2215 input_set_focus_widget(input, NULL, 0, 0);
2216
2217 input->pointer_focus = NULL;
2218 input->current_cursor = CURSOR_UNSET;
2219}
2220
2221static void
2222pointer_handle_enter(void *data, struct wl_pointer *pointer,
2223 uint32_t serial, struct wl_surface *surface,
2224 wl_fixed_t sx_w, wl_fixed_t sy_w)
2225{
2226 struct input *input = data;
2227 struct window *window;
2228 struct widget *widget;
2229 float sx = wl_fixed_to_double(sx_w);
2230 float sy = wl_fixed_to_double(sy_w);
2231
2232 if (!surface) {
2233 /* enter event for a window we've just destroyed */
2234 return;
2235 }
2236
2237 input->display->serial = serial;
2238 input->pointer_enter_serial = serial;
2239 input->pointer_focus = wl_surface_get_user_data(surface);
2240 window = input->pointer_focus;
2241
Pekka Paalanen99436862012-11-19 17:15:59 +02002242 if (window->resizing) {
2243 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002244 /* Schedule a redraw to free the pool */
2245 window_schedule_redraw(window);
2246 }
2247
2248 input->sx = sx;
2249 input->sy = sy;
2250
2251 widget = widget_find_widget(window->widget, sx, sy);
2252 input_set_focus_widget(input, widget, sx, sy);
2253}
2254
2255static void
2256pointer_handle_leave(void *data, struct wl_pointer *pointer,
2257 uint32_t serial, struct wl_surface *surface)
2258{
2259 struct input *input = data;
2260
2261 input->display->serial = serial;
2262 input_remove_pointer_focus(input);
2263}
2264
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002265static void
Daniel Stone37816df2012-05-16 18:45:18 +01002266pointer_handle_motion(void *data, struct wl_pointer *pointer,
2267 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002268{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002269 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002270 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002271 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002272 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002273 float sx = wl_fixed_to_double(sx_w);
2274 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002275
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002276 input->sx = sx;
2277 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002278
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002279 if (!window)
2280 return;
2281
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002282 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002283 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002284 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002285 }
2286
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002287 if (input->grab)
2288 widget = input->grab;
2289 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002290 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002291 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002292 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002293 input, time, sx, sy,
2294 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002295
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002296 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002297}
2298
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002299static void
Daniel Stone37816df2012-05-16 18:45:18 +01002300pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002301 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002302{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002303 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002304 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002305 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002306
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002307 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002308 if (input->focus_widget && input->grab == NULL &&
2309 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002310 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002311
Neil Roberts6b28aad2012-01-23 19:11:18 +00002312 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002313 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002314 (*widget->button_handler)(widget,
2315 input, time,
2316 button, state,
2317 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002318
Daniel Stone4dbadb12012-05-30 16:31:51 +01002319 if (input->grab && input->grab_button == button &&
2320 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002321 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002322}
2323
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002324static void
Daniel Stone37816df2012-05-16 18:45:18 +01002325pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002326 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002327{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002328 struct input *input = data;
2329 struct widget *widget;
2330
2331 widget = input->focus_widget;
2332 if (input->grab)
2333 widget = input->grab;
2334 if (widget && widget->axis_handler)
2335 (*widget->axis_handler)(widget,
2336 input, time,
2337 axis, value,
2338 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002339}
2340
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002341static const struct wl_pointer_listener pointer_listener = {
2342 pointer_handle_enter,
2343 pointer_handle_leave,
2344 pointer_handle_motion,
2345 pointer_handle_button,
2346 pointer_handle_axis,
2347};
2348
2349static void
2350input_remove_keyboard_focus(struct input *input)
2351{
2352 struct window *window = input->keyboard_focus;
2353 struct itimerspec its;
2354
2355 its.it_interval.tv_sec = 0;
2356 its.it_interval.tv_nsec = 0;
2357 its.it_value.tv_sec = 0;
2358 its.it_value.tv_nsec = 0;
2359 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2360
2361 if (!window)
2362 return;
2363
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002364 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002365 if (window->keyboard_focus_handler)
2366 (*window->keyboard_focus_handler)(window, NULL,
2367 window->user_data);
2368
2369 input->keyboard_focus = NULL;
2370}
2371
2372static void
2373keyboard_repeat_func(struct task *task, uint32_t events)
2374{
2375 struct input *input =
2376 container_of(task, struct input, repeat_task);
2377 struct window *window = input->keyboard_focus;
2378 uint64_t exp;
2379
2380 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2381 /* If we change the timer between the fd becoming
2382 * readable and getting here, there'll be nothing to
2383 * read and we get EAGAIN. */
2384 return;
2385
2386 if (window && window->key_handler) {
2387 (*window->key_handler)(window, input, input->repeat_time,
2388 input->repeat_key, input->repeat_sym,
2389 WL_KEYBOARD_KEY_STATE_PRESSED,
2390 window->user_data);
2391 }
2392}
2393
2394static void
2395keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2396 uint32_t format, int fd, uint32_t size)
2397{
2398 struct input *input = data;
2399 char *map_str;
2400
2401 if (!data) {
2402 close(fd);
2403 return;
2404 }
2405
2406 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2407 close(fd);
2408 return;
2409 }
2410
2411 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2412 if (map_str == MAP_FAILED) {
2413 close(fd);
2414 return;
2415 }
2416
2417 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2418 map_str,
2419 XKB_KEYMAP_FORMAT_TEXT_V1,
2420 0);
2421 munmap(map_str, size);
2422 close(fd);
2423
2424 if (!input->xkb.keymap) {
2425 fprintf(stderr, "failed to compile keymap\n");
2426 return;
2427 }
2428
2429 input->xkb.state = xkb_state_new(input->xkb.keymap);
2430 if (!input->xkb.state) {
2431 fprintf(stderr, "failed to create XKB state\n");
2432 xkb_map_unref(input->xkb.keymap);
2433 input->xkb.keymap = NULL;
2434 return;
2435 }
2436
2437 input->xkb.control_mask =
2438 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2439 input->xkb.alt_mask =
2440 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2441 input->xkb.shift_mask =
2442 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2443}
2444
2445static void
2446keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2447 uint32_t serial, struct wl_surface *surface,
2448 struct wl_array *keys)
2449{
2450 struct input *input = data;
2451 struct window *window;
2452
2453 input->display->serial = serial;
2454 input->keyboard_focus = wl_surface_get_user_data(surface);
2455
2456 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002457 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002458 if (window->keyboard_focus_handler)
2459 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002460 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002461}
2462
2463static void
2464keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2465 uint32_t serial, struct wl_surface *surface)
2466{
2467 struct input *input = data;
2468
2469 input->display->serial = serial;
2470 input_remove_keyboard_focus(input);
2471}
2472
Scott Moreau210d0792012-03-22 10:47:01 -06002473static void
Daniel Stone37816df2012-05-16 18:45:18 +01002474keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002475 uint32_t serial, uint32_t time, uint32_t key,
2476 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002477{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002478 struct input *input = data;
2479 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002480 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002481 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002482 const xkb_keysym_t *syms;
2483 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002484 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002485
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002486 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002487 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002488 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002489 return;
2490
Daniel Stone97f68542012-05-30 16:32:01 +01002491 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002492
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002493 sym = XKB_KEY_NoSymbol;
2494 if (num_syms == 1)
2495 sym = syms[0];
2496
2497 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002498 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002499 window_set_maximized(window,
2500 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002501 } else if (sym == XKB_KEY_F11 &&
2502 window->fullscreen_handler &&
2503 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2504 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002505 } else if (sym == XKB_KEY_F4 &&
2506 input->modifiers == MOD_ALT_MASK &&
2507 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2508 if (window->close_handler)
2509 window->close_handler(window->parent,
2510 window->user_data);
2511 else
2512 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002513 } else if (window->key_handler) {
2514 (*window->key_handler)(window, input, time, key,
2515 sym, state, window->user_data);
2516 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002517
2518 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2519 key == input->repeat_key) {
2520 its.it_interval.tv_sec = 0;
2521 its.it_interval.tv_nsec = 0;
2522 its.it_value.tv_sec = 0;
2523 its.it_value.tv_nsec = 0;
2524 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2525 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2526 input->repeat_sym = sym;
2527 input->repeat_key = key;
2528 input->repeat_time = time;
2529 its.it_interval.tv_sec = 0;
2530 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2531 its.it_value.tv_sec = 0;
2532 its.it_value.tv_nsec = 400 * 1000 * 1000;
2533 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2534 }
2535}
2536
2537static void
Daniel Stone351eb612012-05-31 15:27:47 -04002538keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2539 uint32_t serial, uint32_t mods_depressed,
2540 uint32_t mods_latched, uint32_t mods_locked,
2541 uint32_t group)
2542{
2543 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002544 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002545
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002546 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2547 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002548 mask = xkb_state_serialize_mods(input->xkb.state,
2549 XKB_STATE_DEPRESSED |
2550 XKB_STATE_LATCHED);
2551 input->modifiers = 0;
2552 if (mask & input->xkb.control_mask)
2553 input->modifiers |= MOD_CONTROL_MASK;
2554 if (mask & input->xkb.alt_mask)
2555 input->modifiers |= MOD_ALT_MASK;
2556 if (mask & input->xkb.shift_mask)
2557 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002558}
2559
Daniel Stone37816df2012-05-16 18:45:18 +01002560static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002561 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002562 keyboard_handle_enter,
2563 keyboard_handle_leave,
2564 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002565 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002566};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002567
2568static void
Daniel Stone37816df2012-05-16 18:45:18 +01002569seat_handle_capabilities(void *data, struct wl_seat *seat,
2570 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002571{
Daniel Stone37816df2012-05-16 18:45:18 +01002572 struct input *input = data;
2573
2574 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2575 input->pointer = wl_seat_get_pointer(seat);
2576 wl_pointer_set_user_data(input->pointer, input);
2577 wl_pointer_add_listener(input->pointer, &pointer_listener,
2578 input);
2579 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2580 wl_pointer_destroy(input->pointer);
2581 input->pointer = NULL;
2582 }
2583
2584 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2585 input->keyboard = wl_seat_get_keyboard(seat);
2586 wl_keyboard_set_user_data(input->keyboard, input);
2587 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2588 input);
2589 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2590 wl_keyboard_destroy(input->keyboard);
2591 input->keyboard = NULL;
2592 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002593}
2594
Daniel Stone37816df2012-05-16 18:45:18 +01002595static const struct wl_seat_listener seat_listener = {
2596 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002597};
2598
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002599void
2600input_get_position(struct input *input, int32_t *x, int32_t *y)
2601{
2602 *x = input->sx;
2603 *y = input->sy;
2604}
2605
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002606struct display *
2607input_get_display(struct input *input)
2608{
2609 return input->display;
2610}
2611
Daniel Stone37816df2012-05-16 18:45:18 +01002612struct wl_seat *
2613input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002614{
Daniel Stone37816df2012-05-16 18:45:18 +01002615 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002616}
2617
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002618uint32_t
2619input_get_modifiers(struct input *input)
2620{
2621 return input->modifiers;
2622}
2623
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002624struct widget *
2625input_get_focus_widget(struct input *input)
2626{
2627 return input->focus_widget;
2628}
2629
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002630struct data_offer {
2631 struct wl_data_offer *offer;
2632 struct input *input;
2633 struct wl_array types;
2634 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002635
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002636 struct task io_task;
2637 int fd;
2638 data_func_t func;
2639 int32_t x, y;
2640 void *user_data;
2641};
2642
2643static void
2644data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2645{
2646 struct data_offer *offer = data;
2647 char **p;
2648
2649 p = wl_array_add(&offer->types, sizeof *p);
2650 *p = strdup(type);
2651}
2652
2653static const struct wl_data_offer_listener data_offer_listener = {
2654 data_offer_offer,
2655};
2656
2657static void
2658data_offer_destroy(struct data_offer *offer)
2659{
2660 char **p;
2661
2662 offer->refcount--;
2663 if (offer->refcount == 0) {
2664 wl_data_offer_destroy(offer->offer);
2665 for (p = offer->types.data; *p; p++)
2666 free(*p);
2667 wl_array_release(&offer->types);
2668 free(offer);
2669 }
2670}
2671
2672static void
2673data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002674 struct wl_data_device *data_device,
2675 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002676{
2677 struct data_offer *offer;
2678
2679 offer = malloc(sizeof *offer);
2680
2681 wl_array_init(&offer->types);
2682 offer->refcount = 1;
2683 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002684 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002685 wl_data_offer_add_listener(offer->offer,
2686 &data_offer_listener, offer);
2687}
2688
2689static void
2690data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002691 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002692 wl_fixed_t x_w, wl_fixed_t y_w,
2693 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002694{
2695 struct input *input = data;
2696 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002697 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002698 float x = wl_fixed_to_double(x_w);
2699 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002700 char **p;
2701
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002702 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002703 window = wl_surface_get_user_data(surface);
2704 input->pointer_focus = window;
2705
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002706 if (offer) {
2707 input->drag_offer = wl_data_offer_get_user_data(offer);
2708
2709 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2710 *p = NULL;
2711
2712 types_data = input->drag_offer->types.data;
2713 } else {
2714 input->drag_offer = NULL;
2715 types_data = NULL;
2716 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002717
2718 window = input->pointer_focus;
2719 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002720 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002721 window->user_data);
2722}
2723
2724static void
2725data_device_leave(void *data, struct wl_data_device *data_device)
2726{
2727 struct input *input = data;
2728
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002729 if (input->drag_offer) {
2730 data_offer_destroy(input->drag_offer);
2731 input->drag_offer = NULL;
2732 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002733}
2734
2735static void
2736data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002737 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002738{
2739 struct input *input = data;
2740 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002741 float x = wl_fixed_to_double(x_w);
2742 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002743 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002744
2745 input->sx = x;
2746 input->sy = y;
2747
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002748 if (input->drag_offer)
2749 types_data = input->drag_offer->types.data;
2750 else
2751 types_data = NULL;
2752
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002753 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002754 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002755 window->user_data);
2756}
2757
2758static void
2759data_device_drop(void *data, struct wl_data_device *data_device)
2760{
2761 struct input *input = data;
2762 struct window *window = input->pointer_focus;
2763
2764 if (window->drop_handler)
2765 window->drop_handler(window, input,
2766 input->sx, input->sy, window->user_data);
2767}
2768
2769static void
2770data_device_selection(void *data,
2771 struct wl_data_device *wl_data_device,
2772 struct wl_data_offer *offer)
2773{
2774 struct input *input = data;
2775 char **p;
2776
2777 if (input->selection_offer)
2778 data_offer_destroy(input->selection_offer);
2779
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002780 if (offer) {
2781 input->selection_offer = wl_data_offer_get_user_data(offer);
2782 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2783 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002784 } else {
2785 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002786 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002787}
2788
2789static const struct wl_data_device_listener data_device_listener = {
2790 data_device_data_offer,
2791 data_device_enter,
2792 data_device_leave,
2793 data_device_motion,
2794 data_device_drop,
2795 data_device_selection
2796};
2797
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002798static void
2799input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002800{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002801 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002802 struct wl_cursor *cursor;
2803 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002804
Daniel Stone80972742012-11-07 17:51:39 +11002805 if (!input->pointer)
2806 return;
2807
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002808 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002809 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002810 return;
2811
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002812 if (index >= (int) cursor->image_count) {
2813 fprintf(stderr, "cursor index out of range\n");
2814 return;
2815 }
2816
2817 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002818 buffer = wl_cursor_image_get_buffer(image);
2819 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002820 return;
2821
Kristian Høgsbergae277372012-08-01 09:41:08 -04002822 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002823 input->pointer_surface,
2824 image->hotspot_x, image->hotspot_y);
2825 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2826 wl_surface_damage(input->pointer_surface, 0, 0,
2827 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002828 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002829}
2830
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002831static const struct wl_callback_listener pointer_surface_listener;
2832
2833static void
2834pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2835 uint32_t time)
2836{
2837 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002838 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002839 int i;
2840
2841 if (callback) {
2842 assert(callback == input->cursor_frame_cb);
2843 wl_callback_destroy(callback);
2844 input->cursor_frame_cb = NULL;
2845 }
2846
Daniel Stone80972742012-11-07 17:51:39 +11002847 if (!input->pointer)
2848 return;
2849
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002850 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002851 wl_pointer_set_cursor(input->pointer,
2852 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002853 NULL, 0, 0);
2854 return;
2855 }
2856
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002857 if (input->current_cursor == CURSOR_UNSET)
2858 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002859 cursor = input->display->cursors[input->current_cursor];
2860 if (!cursor)
2861 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002862
2863 /* FIXME We don't have the current time on the first call so we set
2864 * the animation start to the time of the first frame callback. */
2865 if (time == 0)
2866 input->cursor_anim_start = 0;
2867 else if (input->cursor_anim_start == 0)
2868 input->cursor_anim_start = time;
2869
2870 if (time == 0 || input->cursor_anim_start == 0)
2871 i = 0;
2872 else
2873 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2874
Pekka Paalanenbc106382012-10-10 12:49:31 +03002875 if (cursor->image_count > 1) {
2876 input->cursor_frame_cb =
2877 wl_surface_frame(input->pointer_surface);
2878 wl_callback_add_listener(input->cursor_frame_cb,
2879 &pointer_surface_listener, input);
2880 }
2881
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002882 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002883}
2884
2885static const struct wl_callback_listener pointer_surface_listener = {
2886 pointer_surface_frame_callback
2887};
2888
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002889void
2890input_set_pointer_image(struct input *input, int pointer)
2891{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002892 int force = 0;
2893
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002894 if (!input->pointer)
2895 return;
2896
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002897 if (input->pointer_enter_serial > input->cursor_serial)
2898 force = 1;
2899
2900 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002901 return;
2902
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002903 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002904 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002905 if (!input->cursor_frame_cb)
2906 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002907 else if (force) {
2908 /* The current frame callback may be stuck if, for instance,
2909 * the set cursor request was processed by the server after
2910 * this client lost the focus. In this case the cursor surface
2911 * might not be mapped and the frame callback wouldn't ever
2912 * complete. Send a set_cursor and attach to try to map the
2913 * cursor surface again so that the callback will finish */
2914 input_set_pointer_image_index(input, 0);
2915 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002916}
2917
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002918struct wl_data_device *
2919input_get_data_device(struct input *input)
2920{
2921 return input->data_device;
2922}
2923
2924void
2925input_set_selection(struct input *input,
2926 struct wl_data_source *source, uint32_t time)
2927{
2928 wl_data_device_set_selection(input->data_device, source, time);
2929}
2930
2931void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002932input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002933{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002934 wl_data_offer_accept(input->drag_offer->offer,
2935 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002936}
2937
2938static void
2939offer_io_func(struct task *task, uint32_t events)
2940{
2941 struct data_offer *offer =
2942 container_of(task, struct data_offer, io_task);
2943 unsigned int len;
2944 char buffer[4096];
2945
2946 len = read(offer->fd, buffer, sizeof buffer);
2947 offer->func(buffer, len,
2948 offer->x, offer->y, offer->user_data);
2949
2950 if (len == 0) {
2951 close(offer->fd);
2952 data_offer_destroy(offer);
2953 }
2954}
2955
2956static void
2957data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2958 data_func_t func, void *user_data)
2959{
2960 int p[2];
2961
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002962 if (pipe2(p, O_CLOEXEC) == -1)
2963 return;
2964
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002965 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2966 close(p[1]);
2967
2968 offer->io_task.run = offer_io_func;
2969 offer->fd = p[0];
2970 offer->func = func;
2971 offer->refcount++;
2972 offer->user_data = user_data;
2973
2974 display_watch_fd(offer->input->display,
2975 offer->fd, EPOLLIN, &offer->io_task);
2976}
2977
2978void
2979input_receive_drag_data(struct input *input, const char *mime_type,
2980 data_func_t func, void *data)
2981{
2982 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2983 input->drag_offer->x = input->sx;
2984 input->drag_offer->y = input->sy;
2985}
2986
2987int
2988input_receive_selection_data(struct input *input, const char *mime_type,
2989 data_func_t func, void *data)
2990{
2991 char **p;
2992
2993 if (input->selection_offer == NULL)
2994 return -1;
2995
2996 for (p = input->selection_offer->types.data; *p; p++)
2997 if (strcmp(mime_type, *p) == 0)
2998 break;
2999
3000 if (*p == NULL)
3001 return -1;
3002
3003 data_offer_receive_data(input->selection_offer,
3004 mime_type, func, data);
3005 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003006}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003007
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003008int
3009input_receive_selection_data_to_fd(struct input *input,
3010 const char *mime_type, int fd)
3011{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003012 if (input->selection_offer)
3013 wl_data_offer_receive(input->selection_offer->offer,
3014 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003015
3016 return 0;
3017}
3018
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003019void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003020window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003021{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003022 if (!window->shell_surface)
3023 return;
3024
Daniel Stone37816df2012-05-16 18:45:18 +01003025 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003026}
3027
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003028static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003029idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003030{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003031 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003032
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003033 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003034 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003035 widget_set_allocation(widget,
3036 window->pending_allocation.x,
3037 window->pending_allocation.y,
3038 window->pending_allocation.width,
3039 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003040
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003041 if (window->input_region) {
3042 wl_region_destroy(window->input_region);
3043 window->input_region = NULL;
3044 }
3045
3046 if (window->opaque_region) {
3047 wl_region_destroy(window->opaque_region);
3048 window->opaque_region = NULL;
3049 }
3050
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003051 if (widget->resize_handler)
3052 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003053 widget->allocation.width,
3054 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003055 widget->user_data);
3056
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003057 if (window->allocation.width != widget->allocation.width ||
3058 window->allocation.height != widget->allocation.height) {
3059 window->allocation = widget->allocation;
3060 window_schedule_redraw(window);
3061 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003062}
3063
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003064void
3065window_schedule_resize(struct window *window, int width, int height)
3066{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003067 window->pending_allocation.x = 0;
3068 window->pending_allocation.y = 0;
3069 window->pending_allocation.width = width;
3070 window->pending_allocation.height = height;
3071
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003072 if (window->min_allocation.width == 0)
3073 window->min_allocation = window->pending_allocation;
3074 if (window->pending_allocation.width < window->min_allocation.width)
3075 window->pending_allocation.width = window->min_allocation.width;
3076 if (window->pending_allocation.height < window->min_allocation.height)
3077 window->pending_allocation.height = window->min_allocation.height;
3078
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003079 window->resize_needed = 1;
3080 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003081}
3082
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003083void
3084widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3085{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003086 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003087}
3088
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003089static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003090handle_ping(void *data, struct wl_shell_surface *shell_surface,
3091 uint32_t serial)
3092{
3093 wl_shell_surface_pong(shell_surface, serial);
3094}
3095
3096static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003097handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003098 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003099{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003100 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003101
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003102 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003103 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003104}
3105
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003106static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003107menu_destroy(struct menu *menu)
3108{
3109 widget_destroy(menu->widget);
3110 window_destroy(menu->window);
3111 free(menu);
3112}
3113
3114static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003115handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3116{
3117 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003118 struct menu *menu = window->widget->user_data;
3119
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003120 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003121 * device. Or just use wl_callback. And this really needs to
3122 * be a window vfunc that the menu can set. And we need the
3123 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003124
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003125 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003126 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003127 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003128}
3129
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003130static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003131 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003132 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003133 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003134};
3135
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003136void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003137window_get_allocation(struct window *window,
3138 struct rectangle *allocation)
3139{
3140 *allocation = window->allocation;
3141}
3142
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003143static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003144widget_redraw(struct widget *widget)
3145{
3146 struct widget *child;
3147
3148 if (widget->redraw_handler)
3149 widget->redraw_handler(widget, widget->user_data);
3150 wl_list_for_each(child, &widget->child_list, link)
3151 widget_redraw(child);
3152}
3153
3154static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003155frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3156{
3157 struct window *window = data;
3158
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003159 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003160 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003161 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003162 window->redraw_scheduled = 0;
3163 if (window->redraw_needed)
3164 window_schedule_redraw(window);
3165}
3166
3167static const struct wl_callback_listener listener = {
3168 frame_callback
3169};
3170
3171static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003172idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003173{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003174 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003175
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003176 if (window->resize_needed)
3177 idle_resize(window);
3178
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003179 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003180 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003181 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003182 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003183
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003184 window->frame_cb = wl_surface_frame(window->surface);
3185 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003186 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003187}
3188
3189void
3190window_schedule_redraw(struct window *window)
3191{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003192 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003193 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003194 window->redraw_task.run = idle_redraw;
3195 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003196 window->redraw_scheduled = 1;
3197 }
3198}
3199
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003200int
3201window_is_fullscreen(struct window *window)
3202{
3203 return window->type == TYPE_FULLSCREEN;
3204}
3205
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003206void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003207window_set_fullscreen(struct window *window, int fullscreen)
3208{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003209 if (!window->display->shell)
3210 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003211
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003212 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003213 return;
3214
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003215 if (fullscreen) {
3216 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003217 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003218 wl_shell_surface_set_fullscreen(window->shell_surface,
3219 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3220 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003221 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003222 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003223 wl_shell_surface_set_toplevel(window->shell_surface);
3224 window_schedule_resize(window,
3225 window->saved_allocation.width,
3226 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003227 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003228}
3229
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003230int
3231window_is_maximized(struct window *window)
3232{
3233 return window->type == TYPE_MAXIMIZED;
3234}
3235
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003236void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003237window_set_maximized(struct window *window, int maximized)
3238{
3239 if (!window->display->shell)
3240 return;
3241
3242 if ((window->type == TYPE_MAXIMIZED) == maximized)
3243 return;
3244
3245 if (window->type == TYPE_TOPLEVEL) {
3246 window->saved_allocation = window->allocation;
3247 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3248 window->type = TYPE_MAXIMIZED;
3249 } else {
3250 wl_shell_surface_set_toplevel(window->shell_surface);
3251 window->type = TYPE_TOPLEVEL;
3252 window_schedule_resize(window,
3253 window->saved_allocation.width,
3254 window->saved_allocation.height);
3255 }
3256}
3257
3258void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003259window_set_user_data(struct window *window, void *data)
3260{
3261 window->user_data = data;
3262}
3263
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003264void *
3265window_get_user_data(struct window *window)
3266{
3267 return window->user_data;
3268}
3269
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003270void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003271window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003272 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003273{
3274 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003275}
3276
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003277void
3278window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003279 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003280{
3281 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003282}
3283
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003284void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003285window_set_data_handler(struct window *window, window_data_handler_t handler)
3286{
3287 window->data_handler = handler;
3288}
3289
3290void
3291window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3292{
3293 window->drop_handler = handler;
3294}
3295
3296void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003297window_set_close_handler(struct window *window,
3298 window_close_handler_t handler)
3299{
3300 window->close_handler = handler;
3301}
3302
3303void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003304window_set_fullscreen_handler(struct window *window,
3305 window_fullscreen_handler_t handler)
3306{
3307 window->fullscreen_handler = handler;
3308}
3309
3310void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003311window_set_title(struct window *window, const char *title)
3312{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003313 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003314 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003315 if (window->shell_surface)
3316 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003317}
3318
3319const char *
3320window_get_title(struct window *window)
3321{
3322 return window->title;
3323}
3324
3325void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003326window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3327{
3328 struct text_cursor_position *text_cursor_position =
3329 window->display->text_cursor_position;
3330
Scott Moreau9295ce02012-06-01 12:46:10 -06003331 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003332 return;
3333
3334 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003335 window->surface,
3336 wl_fixed_from_int(x),
3337 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003338}
3339
3340void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003341window_damage(struct window *window, int32_t x, int32_t y,
3342 int32_t width, int32_t height)
3343{
3344 wl_surface_damage(window->surface, x, y, width, height);
3345}
3346
Casey Dahlin9074db52012-04-19 22:50:09 -04003347static void
3348surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003349 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003350{
Rob Bradford7507b572012-05-15 17:55:34 +01003351 struct window *window = data;
3352 struct output *output;
3353 struct output *output_found = NULL;
3354 struct window_output *window_output;
3355
3356 wl_list_for_each(output, &window->display->output_list, link) {
3357 if (output->output == wl_output) {
3358 output_found = output;
3359 break;
3360 }
3361 }
3362
3363 if (!output_found)
3364 return;
3365
3366 window_output = malloc (sizeof *window_output);
3367 window_output->output = output_found;
3368
3369 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003370}
3371
3372static void
3373surface_leave(void *data,
3374 struct wl_surface *wl_surface, struct wl_output *output)
3375{
Rob Bradford7507b572012-05-15 17:55:34 +01003376 struct window *window = data;
3377 struct window_output *window_output;
3378 struct window_output *window_output_found = NULL;
3379
3380 wl_list_for_each(window_output, &window->window_output_list, link) {
3381 if (window_output->output->output == output) {
3382 window_output_found = window_output;
3383 break;
3384 }
3385 }
3386
3387 if (window_output_found) {
3388 wl_list_remove(&window_output_found->link);
3389 free(window_output_found);
3390 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003391}
3392
3393static const struct wl_surface_listener surface_listener = {
3394 surface_enter,
3395 surface_leave
3396};
3397
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003398static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003399window_create_internal(struct display *display,
3400 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003401{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003402 struct window *window;
3403
3404 window = malloc(sizeof *window);
3405 if (window == NULL)
3406 return NULL;
3407
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003408 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003409 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003410 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003411 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003412 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003413 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003414 window->shell_surface =
3415 wl_shell_get_shell_surface(display->shell,
3416 window->surface);
3417 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003418 window->allocation.x = 0;
3419 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003420 window->allocation.width = 0;
3421 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003422 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003423 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003424 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003425 window->input_region = NULL;
3426 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003427
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003428 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003429#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003430 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003431#else
3432 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3433#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003434 else
3435 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003436
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003437 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003438 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003439 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003440
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003441 if (window->shell_surface) {
3442 wl_shell_surface_set_user_data(window->shell_surface, window);
3443 wl_shell_surface_add_listener(window->shell_surface,
3444 &shell_surface_listener, window);
3445 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003446
Rob Bradford7507b572012-05-15 17:55:34 +01003447 wl_list_init (&window->window_output_list);
3448
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003449 return window;
3450}
3451
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003452struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003453window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003454{
3455 struct window *window;
3456
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003457 window = window_create_internal(display, NULL, TYPE_NONE);
3458 if (!window)
3459 return NULL;
3460
3461 return window;
3462}
3463
3464struct window *
3465window_create_custom(struct display *display)
3466{
3467 struct window *window;
3468
3469 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003470 if (!window)
3471 return NULL;
3472
3473 return window;
3474}
3475
3476struct window *
3477window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003478 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003479{
3480 struct window *window;
3481
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003482 window = window_create_internal(parent->display,
3483 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003484 if (!window)
3485 return NULL;
3486
3487 window->x = x;
3488 window->y = y;
3489
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003490 if (display->shell)
3491 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003492 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003493 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003494
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003495 return window;
3496}
3497
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003498static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003499menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003500{
3501 int next;
3502
3503 next = (sy - 8) / 20;
3504 if (menu->current != next) {
3505 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003506 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003507 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003508}
3509
3510static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003511menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003512 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003513 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003514{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003515 struct menu *menu = data;
3516
3517 if (widget == menu->widget)
3518 menu_set_item(data, y);
3519
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003520 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003521}
3522
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003523static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003524menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003525 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003526{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003527 struct menu *menu = data;
3528
3529 if (widget == menu->widget)
3530 menu_set_item(data, y);
3531
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003532 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003533}
3534
3535static void
3536menu_leave_handler(struct widget *widget, struct input *input, void *data)
3537{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003538 struct menu *menu = data;
3539
3540 if (widget == menu->widget)
3541 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003542}
3543
3544static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003545menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003546 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003547 uint32_t button, enum wl_pointer_button_state state,
3548 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003549
3550{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003551 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003552
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003553 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3554 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003555 /* Either relase after press-drag-release or
3556 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003557 menu->func(menu->window->parent,
3558 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003559 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003560 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003561 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003562 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003563 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003564}
3565
3566static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003567menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003568{
3569 cairo_t *cr;
3570 const int32_t r = 3, margin = 3;
3571 struct menu *menu = data;
3572 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003573 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003574
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003575 cr = cairo_create(window->cairo_surface);
3576 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3577 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3578 cairo_paint(cr);
3579
3580 width = window->allocation.width;
3581 height = window->allocation.height;
3582 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003583
3584 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003585 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3586 cairo_fill(cr);
3587
3588 for (i = 0; i < menu->count; i++) {
3589 if (i == menu->current) {
3590 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3591 cairo_rectangle(cr, margin, i * 20 + margin,
3592 width - 2 * margin, 20);
3593 cairo_fill(cr);
3594 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3595 cairo_move_to(cr, 10, i * 20 + 16);
3596 cairo_show_text(cr, menu->entries[i]);
3597 } else {
3598 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3599 cairo_move_to(cr, 10, i * 20 + 16);
3600 cairo_show_text(cr, menu->entries[i]);
3601 }
3602 }
3603
3604 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003605}
3606
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003607void
3608window_show_menu(struct display *display,
3609 struct input *input, uint32_t time, struct window *parent,
3610 int32_t x, int32_t y,
3611 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003612{
3613 struct window *window;
3614 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003615 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003616
3617 menu = malloc(sizeof *menu);
3618 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003619 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003620
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003621 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003622 if (!window) {
3623 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003624 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003625 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003626
3627 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003628 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003629 menu->entries = entries;
3630 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003631 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003632 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003633 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003634 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003635 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003636 window->type = TYPE_MENU;
3637 window->x = x;
3638 window->y = y;
3639
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003640 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003641 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003642 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003643 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003644 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003645
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003646 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003647 widget_set_enter_handler(menu->widget, menu_enter_handler);
3648 widget_set_leave_handler(menu->widget, menu_leave_handler);
3649 widget_set_motion_handler(menu->widget, menu_motion_handler);
3650 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003651
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003652 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003653 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003654}
3655
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003656void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003657window_set_buffer_type(struct window *window, enum window_buffer_type type)
3658{
3659 window->buffer_type = type;
3660}
3661
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003662
3663static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003664display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003665 struct wl_output *wl_output,
3666 int x, int y,
3667 int physical_width,
3668 int physical_height,
3669 int subpixel,
3670 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003671 const char *model,
3672 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003673{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003674 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003675
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003676 output->allocation.x = x;
3677 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003678 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003679}
3680
3681static void
3682display_handle_mode(void *data,
3683 struct wl_output *wl_output,
3684 uint32_t flags,
3685 int width,
3686 int height,
3687 int refresh)
3688{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003689 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003690 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003691
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003692 if (flags & WL_OUTPUT_MODE_CURRENT) {
3693 output->allocation.width = width;
3694 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003695 if (display->output_configure_handler)
3696 (*display->output_configure_handler)(
3697 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003698 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003699}
3700
3701static const struct wl_output_listener output_listener = {
3702 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003703 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003704};
3705
3706static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003707display_add_output(struct display *d, uint32_t id)
3708{
3709 struct output *output;
3710
3711 output = malloc(sizeof *output);
3712 if (output == NULL)
3713 return;
3714
3715 memset(output, 0, sizeof *output);
3716 output->display = d;
3717 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003718 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003719 wl_list_insert(d->output_list.prev, &output->link);
3720
3721 wl_output_add_listener(output->output, &output_listener, output);
3722}
3723
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003724static void
3725output_destroy(struct output *output)
3726{
3727 if (output->destroy_handler)
3728 (*output->destroy_handler)(output, output->user_data);
3729
3730 wl_output_destroy(output->output);
3731 wl_list_remove(&output->link);
3732 free(output);
3733}
3734
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003735void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003736display_set_global_handler(struct display *display,
3737 display_global_handler_t handler)
3738{
3739 struct global *global;
3740
3741 display->global_handler = handler;
3742 if (!handler)
3743 return;
3744
3745 wl_list_for_each(global, &display->global_list, link)
3746 display->global_handler(display,
3747 global->name, global->interface,
3748 global->version, display->user_data);
3749}
3750
3751void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003752display_set_output_configure_handler(struct display *display,
3753 display_output_handler_t handler)
3754{
3755 struct output *output;
3756
3757 display->output_configure_handler = handler;
3758 if (!handler)
3759 return;
3760
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003761 wl_list_for_each(output, &display->output_list, link) {
3762 if (output->allocation.width == 0 &&
3763 output->allocation.height == 0)
3764 continue;
3765
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003766 (*display->output_configure_handler)(output,
3767 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003768 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003769}
3770
3771void
3772output_set_user_data(struct output *output, void *data)
3773{
3774 output->user_data = data;
3775}
3776
3777void *
3778output_get_user_data(struct output *output)
3779{
3780 return output->user_data;
3781}
3782
3783void
3784output_set_destroy_handler(struct output *output,
3785 display_output_handler_t handler)
3786{
3787 output->destroy_handler = handler;
3788 /* FIXME: implement this, once we have way to remove outputs */
3789}
3790
3791void
Scott Moreau4e072362012-09-29 02:03:11 -06003792output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003793{
Scott Moreau4e072362012-09-29 02:03:11 -06003794 struct rectangle allocation = output->allocation;
3795
3796 switch (output->transform) {
3797 case WL_OUTPUT_TRANSFORM_90:
3798 case WL_OUTPUT_TRANSFORM_270:
3799 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3800 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3801 /* Swap width and height */
3802 allocation.width = output->allocation.height;
3803 allocation.height = output->allocation.width;
3804 break;
3805 }
3806
3807 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003808}
3809
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003810struct wl_output *
3811output_get_wl_output(struct output *output)
3812{
3813 return output->output;
3814}
3815
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003816static void
Daniel Stone97f68542012-05-30 16:32:01 +01003817fini_xkb(struct input *input)
3818{
3819 xkb_state_unref(input->xkb.state);
3820 xkb_map_unref(input->xkb.keymap);
3821}
3822
3823static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003824display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003825{
3826 struct input *input;
3827
3828 input = malloc(sizeof *input);
3829 if (input == NULL)
3830 return;
3831
3832 memset(input, 0, sizeof *input);
3833 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003834 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003835 input->pointer_focus = NULL;
3836 input->keyboard_focus = NULL;
3837 wl_list_insert(d->input_list.prev, &input->link);
3838
Daniel Stone37816df2012-05-16 18:45:18 +01003839 wl_seat_add_listener(input->seat, &seat_listener, input);
3840 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003841
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003842 input->data_device =
3843 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003844 input->seat);
3845 wl_data_device_add_listener(input->data_device, &data_device_listener,
3846 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003847
3848 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003849
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003850 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3851 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003852 input->repeat_task.run = keyboard_repeat_func;
3853 display_watch_fd(d, input->repeat_timer_fd,
3854 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003855}
3856
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003857static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003858input_destroy(struct input *input)
3859{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003860 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003861 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003862
3863 if (input->drag_offer)
3864 data_offer_destroy(input->drag_offer);
3865
3866 if (input->selection_offer)
3867 data_offer_destroy(input->selection_offer);
3868
3869 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003870 fini_xkb(input);
3871
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003872 wl_surface_destroy(input->pointer_surface);
3873
Pekka Paalanene1207c72011-12-16 12:02:09 +02003874 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003875 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003876 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003877 free(input);
3878}
3879
3880static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003881init_workspace_manager(struct display *d, uint32_t id)
3882{
3883 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003884 wl_registry_bind(d->registry, id,
3885 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003886 if (d->workspace_manager != NULL)
3887 workspace_manager_add_listener(d->workspace_manager,
3888 &workspace_manager_listener,
3889 d);
3890}
3891
3892static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003893registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3894 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003895{
3896 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003897 struct global *global;
3898
3899 global = malloc(sizeof *global);
3900 global->name = id;
3901 global->interface = strdup(interface);
3902 global->version = version;
3903 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003904
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003905 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003906 d->compositor = wl_registry_bind(registry, id,
3907 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003908 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003909 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003910 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003911 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003912 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003913 d->shell = wl_registry_bind(registry,
3914 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003915 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003916 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003917 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3918 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003919 wl_registry_bind(registry, id,
3920 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003921 } else if (strcmp(interface, "text_cursor_position") == 0) {
3922 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003923 wl_registry_bind(registry, id,
3924 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003925 } else if (strcmp(interface, "workspace_manager") == 0) {
3926 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003927 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003928
3929 if (d->global_handler)
3930 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003931}
3932
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003933void *
3934display_bind(struct display *display, uint32_t name,
3935 const struct wl_interface *interface, uint32_t version)
3936{
3937 return wl_registry_bind(display->registry, name, interface, version);
3938}
3939
3940static const struct wl_registry_listener registry_listener = {
3941 registry_handle_global
3942};
3943
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003944#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003945static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003946init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003947{
3948 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003949 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003950
Rob Clark6396ed32012-03-11 19:48:41 -05003951#ifdef USE_CAIRO_GLESV2
3952# define GL_BIT EGL_OPENGL_ES2_BIT
3953#else
3954# define GL_BIT EGL_OPENGL_BIT
3955#endif
3956
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003957 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04003958 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003959 EGL_RED_SIZE, 1,
3960 EGL_GREEN_SIZE, 1,
3961 EGL_BLUE_SIZE, 1,
3962 EGL_ALPHA_SIZE, 1,
3963 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003964 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003965 EGL_NONE
3966 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003967
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003968#ifdef USE_CAIRO_GLESV2
3969 static const EGLint context_attribs[] = {
3970 EGL_CONTEXT_CLIENT_VERSION, 2,
3971 EGL_NONE
3972 };
3973 EGLint api = EGL_OPENGL_ES_API;
3974#else
3975 EGLint *context_attribs = NULL;
3976 EGLint api = EGL_OPENGL_API;
3977#endif
3978
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003979 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003980 if (!eglInitialize(d->dpy, &major, &minor)) {
3981 fprintf(stderr, "failed to initialize display\n");
3982 return -1;
3983 }
3984
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003985 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003986 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3987 return -1;
3988 }
3989
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003990 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3991 &d->argb_config, 1, &n) || n != 1) {
3992 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003993 return -1;
3994 }
3995
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003996 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003997 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003998 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003999 fprintf(stderr, "failed to create context\n");
4000 return -1;
4001 }
4002
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004003 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01004004 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004005 return -1;
4006 }
4007
Benjamin Franzke0c991632011-09-27 21:57:31 +02004008 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4009 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4010 fprintf(stderr, "failed to get cairo egl argb device\n");
4011 return -1;
4012 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004013
4014 return 0;
4015}
4016
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004017static void
4018fini_egl(struct display *display)
4019{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004020 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004021
4022 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4023 EGL_NO_CONTEXT);
4024
4025 eglTerminate(display->dpy);
4026 eglReleaseThread();
4027}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004028#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004029
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004030static void
4031handle_display_data(struct task *task, uint32_t events)
4032{
4033 struct display *display =
4034 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004035 struct epoll_event ep;
4036 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004037
4038 display->display_fd_events = events;
4039
4040 if (events & EPOLLERR || events & EPOLLHUP) {
4041 display_exit(display);
4042 return;
4043 }
4044
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004045 if (events & EPOLLIN) {
4046 ret = wl_display_dispatch(display->display);
4047 if (ret == -1) {
4048 display_exit(display);
4049 return;
4050 }
4051 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004052
4053 if (events & EPOLLOUT) {
4054 ret = wl_display_flush(display->display);
4055 if (ret == 0) {
4056 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4057 ep.data.ptr = &display->display_task;
4058 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4059 display->display_fd, &ep);
4060 } else if (ret == -1 && errno != EAGAIN) {
4061 display_exit(display);
4062 return;
4063 }
4064 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004065}
4066
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004067struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004068display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004069{
4070 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004071
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004072 d = malloc(sizeof *d);
4073 if (d == NULL)
4074 return NULL;
4075
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004076 memset(d, 0, sizeof *d);
4077
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004078 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004079 if (d->display == NULL) {
4080 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004081 return NULL;
4082 }
4083
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004084 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004085 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004086 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004087 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4088 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004089
4090 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004091 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004092 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004093 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004094
Daniel Stone97f68542012-05-30 16:32:01 +01004095 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004096 if (d->xkb_context == NULL) {
4097 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004098 return NULL;
4099 }
4100
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004101 d->workspace = 0;
4102 d->workspace_count = 1;
4103
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004104 d->registry = wl_display_get_registry(d->display);
4105 wl_registry_add_listener(d->registry, &registry_listener, d);
4106 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004107#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004108 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004109 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004110#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004111
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004112 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004114 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004115
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004116 wl_list_init(&d->window_list);
4117
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004118 return d;
4119}
4120
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004121static void
4122display_destroy_outputs(struct display *display)
4123{
4124 struct output *tmp;
4125 struct output *output;
4126
4127 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4128 output_destroy(output);
4129}
4130
Pekka Paalanene1207c72011-12-16 12:02:09 +02004131static void
4132display_destroy_inputs(struct display *display)
4133{
4134 struct input *tmp;
4135 struct input *input;
4136
4137 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4138 input_destroy(input);
4139}
4140
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004141void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004142display_destroy(struct display *display)
4143{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004144 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004145 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4146 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004147
4148 if (!wl_list_empty(&display->deferred_list))
4149 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4150
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004151 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004152 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004153
Daniel Stone97f68542012-05-30 16:32:01 +01004154 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004155
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004156 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004157 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004158
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004159#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004160 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004161#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004162
Pekka Paalanenc2052982011-12-16 11:41:32 +02004163 if (display->shell)
4164 wl_shell_destroy(display->shell);
4165
4166 if (display->shm)
4167 wl_shm_destroy(display->shm);
4168
4169 if (display->data_device_manager)
4170 wl_data_device_manager_destroy(display->data_device_manager);
4171
4172 wl_compositor_destroy(display->compositor);
4173
4174 close(display->epoll_fd);
4175
U. Artie Eoff44874d92012-10-02 21:12:35 -07004176 if (!(display->display_fd_events & EPOLLERR) &&
4177 !(display->display_fd_events & EPOLLHUP))
4178 wl_display_flush(display->display);
4179
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004180 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004181 free(display);
4182}
4183
4184void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004185display_set_user_data(struct display *display, void *data)
4186{
4187 display->user_data = data;
4188}
4189
4190void *
4191display_get_user_data(struct display *display)
4192{
4193 return display->user_data;
4194}
4195
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004196struct wl_display *
4197display_get_display(struct display *display)
4198{
4199 return display->display;
4200}
4201
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004202struct output *
4203display_get_output(struct display *display)
4204{
4205 return container_of(display->output_list.next, struct output, link);
4206}
4207
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004208struct wl_compositor *
4209display_get_compositor(struct display *display)
4210{
4211 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004212}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004213
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004214uint32_t
4215display_get_serial(struct display *display)
4216{
4217 return display->serial;
4218}
4219
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004220EGLDisplay
4221display_get_egl_display(struct display *d)
4222{
4223 return d->dpy;
4224}
4225
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004226struct wl_data_source *
4227display_create_data_source(struct display *display)
4228{
4229 return wl_data_device_manager_create_data_source(display->data_device_manager);
4230}
4231
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004232EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004233display_get_argb_egl_config(struct display *d)
4234{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004235 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004236}
4237
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004238struct wl_shell *
4239display_get_shell(struct display *display)
4240{
4241 return display->shell;
4242}
4243
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004244int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004245display_acquire_window_surface(struct display *display,
4246 struct window *window,
4247 EGLContext ctx)
4248{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004249 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004250 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004251
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004252 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004253}
4254
4255void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004256display_release_window_surface(struct display *display,
4257 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004258{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004259 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004260 return;
4261
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004262 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004263}
4264
4265void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004266display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004267{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004268 wl_list_insert(&display->deferred_list, &task->link);
4269}
4270
4271void
4272display_watch_fd(struct display *display,
4273 int fd, uint32_t events, struct task *task)
4274{
4275 struct epoll_event ep;
4276
4277 ep.events = events;
4278 ep.data.ptr = task;
4279 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4280}
4281
4282void
4283display_run(struct display *display)
4284{
4285 struct task *task;
4286 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004287 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004288
Pekka Paalanen826d7952011-12-15 10:14:07 +02004289 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004290 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004291 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004292 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004293 struct task, link);
4294 wl_list_remove(&task->link);
4295 task->run(task, 0);
4296 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004297
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004298 wl_display_dispatch_pending(display->display);
4299
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004300 if (!display->running)
4301 break;
4302
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004303 ret = wl_display_flush(display->display);
4304 if (ret < 0 && errno == EAGAIN) {
4305 ep[0].events =
4306 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4307 ep[0].data.ptr = &display->display_task;
4308
4309 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4310 display->display_fd, &ep[0]);
4311 } else if (ret < 0) {
4312 break;
4313 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004314
4315 count = epoll_wait(display->epoll_fd,
4316 ep, ARRAY_LENGTH(ep), -1);
4317 for (i = 0; i < count; i++) {
4318 task = ep[i].data.ptr;
4319 task->run(task, ep[i].events);
4320 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004321 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004322}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004323
4324void
4325display_exit(struct display *display)
4326{
4327 display->running = 0;
4328}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004329
4330void
4331keysym_modifiers_add(struct wl_array *modifiers_map,
4332 const char *name)
4333{
4334 size_t len = strlen(name) + 1;
4335 char *p;
4336
4337 p = wl_array_add(modifiers_map, len);
4338
4339 if (p == NULL)
4340 return;
4341
4342 strncpy(p, name, len);
4343}
4344
4345static xkb_mod_index_t
4346keysym_modifiers_get_index(struct wl_array *modifiers_map,
4347 const char *name)
4348{
4349 xkb_mod_index_t index = 0;
4350 char *p = modifiers_map->data;
4351
4352 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4353 if (strcmp(p, name) == 0)
4354 return index;
4355
4356 index++;
4357 p += strlen(p) + 1;
4358 }
4359
4360 return XKB_MOD_INVALID;
4361}
4362
4363xkb_mod_mask_t
4364keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4365 const char *name)
4366{
4367 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4368
4369 if (index == XKB_MOD_INVALID)
4370 return XKB_MOD_INVALID;
4371
4372 return 1 << index;
4373}