blob: 7f8a3358f951d056965adf6ccd1f9604337f4890 [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
Pekka Paalanenb3627362012-11-19 17:16:00 +0200497 if (display->dpy == EGL_NO_DISPLAY)
498 return NULL;
499
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200500 surface = calloc(1, sizeof *surface);
501 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100502 return NULL;
503
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200504 surface->base.prepare = egl_window_surface_prepare;
505 surface->base.swap = egl_window_surface_swap;
506 surface->base.acquire = egl_window_surface_acquire;
507 surface->base.release = egl_window_surface_release;
508 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100509
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200510 surface->display = display;
511 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400512
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200513 surface->egl_window = wl_egl_window_create(surface->surface,
514 rectangle->width,
515 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100516
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517 surface->egl_surface = eglCreateWindowSurface(display->dpy,
518 display->argb_config,
519 surface->egl_window,
520 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522 surface->cairo_surface =
523 cairo_gl_surface_create_for_egl(display->argb_device,
524 surface->egl_surface,
525 rectangle->width,
526 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100527
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200528 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100529}
530
Pekka Paalanenb3627362012-11-19 17:16:00 +0200531#else
532
533static struct toysurface *
534egl_window_surface_create(struct display *display,
535 struct wl_surface *wl_surface,
536 uint32_t flags,
537 struct rectangle *rectangle)
538{
539 return NULL;
540}
541
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400542#endif
543
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200544struct shm_surface_data {
545 struct wl_buffer *buffer;
546 struct shm_pool *pool;
547};
548
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549struct wl_buffer *
550display_get_buffer_for_surface(struct display *display,
551 cairo_surface_t *surface)
552{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200553 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400554
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200555 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556
557 return data->buffer;
558}
559
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500560static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700561shm_pool_destroy(struct shm_pool *pool);
562
563static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400564shm_surface_data_destroy(void *p)
565{
566 struct shm_surface_data *data = p;
567
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200568 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700569 if (data->pool)
570 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300571
572 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573}
574
Kristian Høgsberg16626282012-04-03 11:21:27 -0400575static struct wl_shm_pool *
576make_shm_pool(struct display *display, int size, void **data)
577{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400578 struct wl_shm_pool *pool;
579 int fd;
580
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300581 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400582 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300583 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
584 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400585 return NULL;
586 }
587
588 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400589 if (*data == MAP_FAILED) {
590 fprintf(stderr, "mmap failed: %m\n");
591 close(fd);
592 return NULL;
593 }
594
595 pool = wl_shm_create_pool(display->shm, fd, size);
596
597 close(fd);
598
599 return pool;
600}
601
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700602static struct shm_pool *
603shm_pool_create(struct display *display, size_t size)
604{
605 struct shm_pool *pool = malloc(sizeof *pool);
606
607 if (!pool)
608 return NULL;
609
610 pool->pool = make_shm_pool(display, size, &pool->data);
611 if (!pool->pool) {
612 free(pool);
613 return NULL;
614 }
615
616 pool->size = size;
617 pool->used = 0;
618
619 return pool;
620}
621
622static void *
623shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
624{
625 if (pool->used + size > pool->size)
626 return NULL;
627
628 *offset = pool->used;
629 pool->used += size;
630
631 return (char *) pool->data + *offset;
632}
633
634/* destroy the pool. this does not unmap the memory though */
635static void
636shm_pool_destroy(struct shm_pool *pool)
637{
638 munmap(pool->data, pool->size);
639 wl_shm_pool_destroy(pool->pool);
640 free(pool);
641}
642
643/* Start allocating from the beginning of the pool again */
644static void
645shm_pool_reset(struct shm_pool *pool)
646{
647 pool->used = 0;
648}
649
650static int
651data_length_for_shm_surface(struct rectangle *rect)
652{
653 int stride;
654
655 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
656 rect->width);
657 return stride * rect->height;
658}
659
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500660static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700661display_create_shm_surface_from_pool(struct display *display,
662 struct rectangle *rectangle,
663 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400664{
665 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400666 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400667 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700668 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400669 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670
671 data = malloc(sizeof *data);
672 if (data == NULL)
673 return NULL;
674
675 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
676 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700677 length = stride * rectangle->height;
678 data->pool = NULL;
679 map = shm_pool_allocate(pool, length, &offset);
680
681 if (!map) {
682 free(data);
683 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400684 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400685
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400686 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400687 CAIRO_FORMAT_ARGB32,
688 rectangle->width,
689 rectangle->height,
690 stride);
691
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200692 cairo_surface_set_user_data(surface, &shm_surface_data_key,
693 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400694
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400695 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500696 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400697 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500698 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400699
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200700 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
701 rectangle->width,
702 rectangle->height,
703 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700705 return surface;
706}
707
708static cairo_surface_t *
709display_create_shm_surface(struct display *display,
710 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200711 struct shm_pool *alternate_pool,
712 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700713{
714 struct shm_surface_data *data;
715 struct shm_pool *pool;
716 cairo_surface_t *surface;
717
Pekka Paalanen99436862012-11-19 17:15:59 +0200718 if (alternate_pool) {
719 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700720 surface = display_create_shm_surface_from_pool(display,
721 rectangle,
722 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200723 alternate_pool);
724 if (surface) {
725 data = cairo_surface_get_user_data(surface,
726 &shm_surface_data_key);
727 goto out;
728 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700729 }
730
731 pool = shm_pool_create(display,
732 data_length_for_shm_surface(rectangle));
733 if (!pool)
734 return NULL;
735
736 surface =
737 display_create_shm_surface_from_pool(display, rectangle,
738 flags, pool);
739
740 if (!surface) {
741 shm_pool_destroy(pool);
742 return NULL;
743 }
744
745 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200746 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700747 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400748
Pekka Paalanen99436862012-11-19 17:15:59 +0200749out:
750 if (data_ret)
751 *data_ret = data;
752
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400753 return surface;
754}
755
nobled7b87cb02011-02-01 18:51:47 +0000756static int
757check_size(struct rectangle *rect)
758{
759 if (rect->width && rect->height)
760 return 0;
761
762 fprintf(stderr, "tried to create surface of "
763 "width: %d, height: %d\n", rect->width, rect->height);
764 return -1;
765}
766
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400767cairo_surface_t *
768display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100769 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400770 struct rectangle *rectangle,
771 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400772{
nobled7b87cb02011-02-01 18:51:47 +0000773 if (check_size(rectangle) < 0)
774 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200775
776 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200777 return display_create_shm_surface(display, rectangle, flags,
778 NULL, NULL);
779}
780
781struct shm_surface {
782 struct toysurface base;
783 struct display *display;
784 struct wl_surface *surface;
785 uint32_t flags;
786 int dx, dy;
787
788 cairo_surface_t *cairo_surface;
789 /* 'data' is automatically destroyed, when 'cairo_surface' is */
790 struct shm_surface_data *data;
791
792 struct shm_pool *resize_pool;
793};
794
795static struct shm_surface *
796to_shm_surface(struct toysurface *base)
797{
798 return container_of(base, struct shm_surface, base);
799}
800
801static cairo_surface_t *
802shm_surface_prepare(struct toysurface *base, int dx, int dy,
803 int width, int height, int resize_hint)
804{
805 struct shm_surface *surface = to_shm_surface(base);
806 struct rectangle rect = { 0, 0, width, height };
807
808 surface->dx = dx;
809 surface->dy = dy;
810
811 if (!resize_hint && surface->resize_pool) {
812 cairo_surface_destroy(surface->cairo_surface);
813 shm_pool_destroy(surface->resize_pool);
814 surface->resize_pool = NULL;
815 surface->cairo_surface = NULL;
816 }
817
818 if (surface->cairo_surface &&
819 cairo_image_surface_get_width(surface->cairo_surface) == width &&
820 cairo_image_surface_get_height(surface->cairo_surface) == height)
821 goto out;
822
823 if (surface->cairo_surface)
824 cairo_surface_destroy(surface->cairo_surface);
825
826 if (resize_hint && !surface->resize_pool) {
827 /* Create a big pool to allocate from, while continuously
828 * resizing. Mmapping a new pool in the server
829 * is relatively expensive, so reusing a pool performs
830 * better, but may temporarily reserve unneeded memory.
831 */
832 /* We should probably base this number on the output size. */
833 surface->resize_pool = shm_pool_create(surface->display,
834 6 * 1024 * 1024);
835 }
836
837 surface->cairo_surface =
838 display_create_shm_surface(surface->display, &rect,
839 surface->flags,
840 surface->resize_pool,
841 &surface->data);
842
843out:
844 return cairo_surface_reference(surface->cairo_surface);
845}
846
847static void
848shm_surface_swap(struct toysurface *base,
849 struct rectangle *server_allocation)
850{
851 struct shm_surface *surface = to_shm_surface(base);
852
853 server_allocation->width =
854 cairo_image_surface_get_width(surface->cairo_surface);
855 server_allocation->height =
856 cairo_image_surface_get_height(surface->cairo_surface);
857
858 wl_surface_attach(surface->surface, surface->data->buffer,
859 surface->dx, surface->dy);
860 wl_surface_damage(surface->surface, 0, 0,
861 server_allocation->width, server_allocation->height);
862 wl_surface_commit(surface->surface);
863}
864
865static int
866shm_surface_acquire(struct toysurface *base, EGLContext ctx)
867{
868 return -1;
869}
870
871static void
872shm_surface_release(struct toysurface *base)
873{
874}
875
876static void
877shm_surface_destroy(struct toysurface *base)
878{
879 struct shm_surface *surface = to_shm_surface(base);
880
881 /* this destroys surface->data, too */
882 cairo_surface_destroy(surface->cairo_surface);
883
884 if (surface->resize_pool)
885 shm_pool_destroy(surface->resize_pool);
886
887 free(surface);
888}
889
890static struct toysurface *
891shm_surface_create(struct display *display, struct wl_surface *wl_surface,
892 uint32_t flags, struct rectangle *rectangle)
893{
894 struct shm_surface *surface;
895
896 surface = calloc(1, sizeof *surface);
897 if (!surface)
898 return NULL;
899
900 surface->base.prepare = shm_surface_prepare;
901 surface->base.swap = shm_surface_swap;
902 surface->base.acquire = shm_surface_acquire;
903 surface->base.release = shm_surface_release;
904 surface->base.destroy = shm_surface_destroy;
905
906 surface->display = display;
907 surface->surface = wl_surface;
908 surface->flags = flags;
909 surface->cairo_surface = display_create_shm_surface(display, rectangle,
910 flags, NULL,
911 &surface->data);
912 if (!surface->cairo_surface) {
913 free(surface);
914 return NULL;
915 }
916
917 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400918}
919
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200920/*
921 * The following correspondences between file names and cursors was copied
922 * from: https://bugs.kde.org/attachment.cgi?id=67313
923 */
924
925static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300926 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200927 "sw-resize"
928};
929
930static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300931 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200932 "se-resize"
933};
934
935static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300936 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200937 "s-resize"
938};
939
940static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300941 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200942 "closedhand",
943 "208530c400c041818281048008011002"
944};
945
946static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300947 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200948 "default",
949 "top_left_arrow",
950 "left-arrow"
951};
952
953static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300954 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200955 "w-resize"
956};
957
958static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300959 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200960 "e-resize"
961};
962
963static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300964 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200965 "nw-resize"
966};
967
968static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300969 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200970 "ne-resize"
971};
972
973static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300974 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200975 "n-resize"
976};
977
978static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300979 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200980 "ibeam",
981 "text"
982};
983
984static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300985 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200986 "pointer",
987 "pointing_hand",
988 "e29285e634086352946a0e7090d73106"
989};
990
991static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400992 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200993 "wait",
994 "0426c94ea35c87780ff01dc239897213"
995};
996
997struct cursor_alternatives {
998 const char **names;
999 size_t count;
1000};
1001
1002static const struct cursor_alternatives cursors[] = {
1003 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1004 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1005 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1006 {grabbings, ARRAY_LENGTH(grabbings)},
1007 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1008 {left_sides, ARRAY_LENGTH(left_sides)},
1009 {right_sides, ARRAY_LENGTH(right_sides)},
1010 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1011 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1012 {top_sides, ARRAY_LENGTH(top_sides)},
1013 {xterms, ARRAY_LENGTH(xterms)},
1014 {hand1s, ARRAY_LENGTH(hand1s)},
1015 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001016};
1017
1018static void
1019create_cursors(struct display *display)
1020{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001021 char *config_file;
1022 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001023 unsigned int i, j;
1024 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001025 struct config_key shell_keys[] = {
1026 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1027 };
1028 struct config_section cs[] = {
1029 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1030 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001031
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001032 config_file = config_file_path("weston.ini");
1033 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1034 free(config_file);
1035
1036 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001037 display->cursors =
1038 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1039
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001040 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001041 cursor = NULL;
1042 for (j = 0; !cursor && j < cursors[i].count; ++j)
1043 cursor = wl_cursor_theme_get_cursor(
1044 display->cursor_theme, cursors[i].names[j]);
1045
1046 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001047 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001048 cursors[i].names[0]);
1049
1050 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001051 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001052}
1053
1054static void
1055destroy_cursors(struct display *display)
1056{
1057 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001058 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001059}
1060
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001061struct wl_cursor_image *
1062display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001063{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001064 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001065
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001066 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001067}
1068
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001069static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001070window_get_resize_dx_dy(struct window *window, int *x, int *y)
1071{
1072 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1073 *x = window->server_allocation.width - window->allocation.width;
1074 else
1075 *x = 0;
1076
1077 if (window->resize_edges & WINDOW_RESIZING_TOP)
1078 *y = window->server_allocation.height -
1079 window->allocation.height;
1080 else
1081 *y = 0;
1082
1083 window->resize_edges = 0;
1084}
1085
1086static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001087window_attach_surface(struct window *window)
1088{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001089 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001090
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001091 if (window->type == TYPE_NONE) {
1092 window->type = TYPE_TOPLEVEL;
1093 if (display->shell)
1094 wl_shell_surface_set_toplevel(window->shell_surface);
1095 }
1096
Pekka Paalanen512dde82012-10-10 12:49:27 +03001097 if (window->opaque_region) {
1098 wl_surface_set_opaque_region(window->surface,
1099 window->opaque_region);
1100 wl_region_destroy(window->opaque_region);
1101 window->opaque_region = NULL;
1102 }
1103
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001104 if (window->input_region) {
1105 wl_surface_set_input_region(window->surface,
1106 window->input_region);
1107 wl_region_destroy(window->input_region);
1108 window->input_region = NULL;
1109 }
1110
Pekka Paalanen99436862012-11-19 17:15:59 +02001111 window->toysurface->swap(window->toysurface,
1112 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001113}
1114
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001115int
1116window_has_focus(struct window *window)
1117{
1118 return window->focus_count > 0;
1119}
1120
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001121static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001122window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001123{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001124 if (!window->cairo_surface)
1125 return;
1126
1127 window_attach_surface(window);
1128 cairo_surface_destroy(window->cairo_surface);
1129 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001130}
1131
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001132struct display *
1133window_get_display(struct window *window)
1134{
1135 return window->display;
1136}
1137
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001138static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001139window_create_surface(struct window *window)
1140{
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001141 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001142 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001143
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001144 if (!window->transparent)
1145 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001146
Pekka Paalanenb3627362012-11-19 17:16:00 +02001147 if (!window->toysurface &&
1148 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1149 window->display->dpy) {
1150 window->toysurface =
1151 egl_window_surface_create(window->display,
1152 window->surface, flags,
1153 &window->allocation);
1154 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001155
Pekka Paalanenb3627362012-11-19 17:16:00 +02001156 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001157 window->toysurface = shm_surface_create(window->display,
1158 window->surface, flags,
1159 &window->allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001160
Pekka Paalanen99436862012-11-19 17:15:59 +02001161 window_get_resize_dx_dy(window, &dx, &dy);
1162 window->cairo_surface =
1163 window->toysurface->prepare(window->toysurface, dx, dy,
1164 window->allocation.width,
1165 window->allocation.height,
1166 window->resizing);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001167}
1168
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001169static void frame_destroy(struct frame *frame);
1170
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001171void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001172window_destroy(struct window *window)
1173{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001174 struct display *display = window->display;
1175 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001176 struct window_output *window_output;
1177 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001178
1179 if (window->redraw_scheduled)
1180 wl_list_remove(&window->redraw_task.link);
1181
1182 wl_list_for_each(input, &display->input_list, link) {
1183 if (input->pointer_focus == window)
1184 input->pointer_focus = NULL;
1185 if (input->keyboard_focus == window)
1186 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001187 if (input->focus_widget &&
1188 input->focus_widget->window == window)
1189 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001190 }
1191
Rob Bradford7507b572012-05-15 17:55:34 +01001192 wl_list_for_each_safe(window_output, window_output_tmp,
1193 &window->window_output_list, link) {
1194 free (window_output);
1195 }
1196
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001197 if (window->input_region)
1198 wl_region_destroy(window->input_region);
1199 if (window->opaque_region)
1200 wl_region_destroy(window->opaque_region);
1201
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001202 if (window->frame)
1203 frame_destroy(window->frame);
1204
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001205 if (window->shell_surface)
1206 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001207 wl_surface_destroy(window->surface);
1208 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001209
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001210 if (window->toysurface)
1211 window->toysurface->destroy(window->toysurface);
1212
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001213 if (window->frame_cb)
1214 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001215 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001216 free(window);
1217}
1218
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001219static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001220widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001221{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001222 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001223
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001224 wl_list_for_each(child, &widget->child_list, link) {
1225 target = widget_find_widget(child, x, y);
1226 if (target)
1227 return target;
1228 }
1229
1230 if (widget->allocation.x <= x &&
1231 x < widget->allocation.x + widget->allocation.width &&
1232 widget->allocation.y <= y &&
1233 y < widget->allocation.y + widget->allocation.height) {
1234 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001235 }
1236
1237 return NULL;
1238}
1239
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001240static struct widget *
1241widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001242{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001243 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001244
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001245 widget = malloc(sizeof *widget);
1246 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001247 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001248 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001249 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001250 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001251 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001252 widget->tooltip = NULL;
1253 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001254
1255 return widget;
1256}
1257
1258struct widget *
1259window_add_widget(struct window *window, void *data)
1260{
1261 window->widget = widget_create(window, data);
1262 wl_list_init(&window->widget->link);
1263
1264 return window->widget;
1265}
1266
1267struct widget *
1268widget_add_widget(struct widget *parent, void *data)
1269{
1270 struct widget *widget;
1271
1272 widget = widget_create(parent->window, data);
1273 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001274
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001275 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001276}
1277
1278void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001279widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001280{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001281 struct display *display = widget->window->display;
1282 struct input *input;
1283
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001284 if (widget->tooltip) {
1285 free(widget->tooltip);
1286 widget->tooltip = NULL;
1287 }
1288
Pekka Paalanene156fb62012-01-19 13:51:38 +02001289 wl_list_for_each(input, &display->input_list, link) {
1290 if (input->focus_widget == widget)
1291 input->focus_widget = NULL;
1292 }
1293
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001294 wl_list_remove(&widget->link);
1295 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001296}
1297
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001298void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001299widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001300{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001301 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001302}
1303
1304void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001305widget_set_size(struct widget *widget, int32_t width, int32_t height)
1306{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001307 widget->allocation.width = width;
1308 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001309}
1310
1311void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001312widget_set_allocation(struct widget *widget,
1313 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001314{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001315 widget->allocation.x = x;
1316 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001317 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001318}
1319
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001320void
1321widget_set_transparent(struct widget *widget, int transparent)
1322{
1323 widget->opaque = !transparent;
1324}
1325
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001326void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001327widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001328{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001329 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001330}
1331
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001332void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001333widget_set_resize_handler(struct widget *widget,
1334 widget_resize_handler_t handler)
1335{
1336 widget->resize_handler = handler;
1337}
1338
1339void
1340widget_set_redraw_handler(struct widget *widget,
1341 widget_redraw_handler_t handler)
1342{
1343 widget->redraw_handler = handler;
1344}
1345
1346void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001347widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001348{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001349 widget->enter_handler = handler;
1350}
1351
1352void
1353widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1354{
1355 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001356}
1357
1358void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001359widget_set_motion_handler(struct widget *widget,
1360 widget_motion_handler_t handler)
1361{
1362 widget->motion_handler = handler;
1363}
1364
1365void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001366widget_set_button_handler(struct widget *widget,
1367 widget_button_handler_t handler)
1368{
1369 widget->button_handler = handler;
1370}
1371
1372void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001373widget_set_axis_handler(struct widget *widget,
1374 widget_axis_handler_t handler)
1375{
1376 widget->axis_handler = handler;
1377}
1378
1379void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001380widget_schedule_redraw(struct widget *widget)
1381{
1382 window_schedule_redraw(widget->window);
1383}
1384
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001385cairo_surface_t *
1386window_get_surface(struct window *window)
1387{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001388 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001389}
1390
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001391struct wl_surface *
1392window_get_wl_surface(struct window *window)
1393{
1394 return window->surface;
1395}
1396
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001397struct wl_shell_surface *
1398window_get_wl_shell_surface(struct window *window)
1399{
1400 return window->shell_surface;
1401}
1402
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001403static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001404tooltip_redraw_handler(struct widget *widget, void *data)
1405{
1406 cairo_t *cr;
1407 const int32_t r = 3;
1408 struct tooltip *tooltip = data;
1409 int32_t width, height;
1410 struct window *window = widget->window;
1411
1412 cr = cairo_create(window->cairo_surface);
1413 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1414 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1415 cairo_paint(cr);
1416
1417 width = window->allocation.width;
1418 height = window->allocation.height;
1419 rounded_rect(cr, 0, 0, width, height, r);
1420
1421 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1422 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1423 cairo_fill(cr);
1424
1425 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1426 cairo_move_to(cr, 10, 16);
1427 cairo_show_text(cr, tooltip->entry);
1428 cairo_destroy(cr);
1429}
1430
1431static cairo_text_extents_t
1432get_text_extents(struct tooltip *tooltip)
1433{
1434 struct window *window;
1435 cairo_t *cr;
1436 cairo_text_extents_t extents;
1437
1438 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1439 * created yet */
1440 window = tooltip->widget->window->parent;
1441 cr = cairo_create(window->cairo_surface);
1442 cairo_text_extents(cr, tooltip->entry, &extents);
1443 cairo_destroy(cr);
1444
1445 return extents;
1446}
1447
1448static int
1449window_create_tooltip(struct tooltip *tooltip)
1450{
1451 struct widget *parent = tooltip->parent;
1452 struct display *display = parent->window->display;
1453 struct window *window;
1454 const int offset_y = 27;
1455 const int margin = 3;
1456 cairo_text_extents_t extents;
1457
1458 if (tooltip->widget)
1459 return 0;
1460
1461 window = window_create_transient(display, parent->window, tooltip->x,
1462 tooltip->y + offset_y,
1463 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1464 if (!window)
1465 return -1;
1466
1467 tooltip->window = window;
1468 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1469
1470 extents = get_text_extents(tooltip);
1471 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1472 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1473
1474 return 0;
1475}
1476
1477void
1478widget_destroy_tooltip(struct widget *parent)
1479{
1480 struct tooltip *tooltip = parent->tooltip;
1481
1482 parent->tooltip_count = 0;
1483 if (!tooltip)
1484 return;
1485
1486 if (tooltip->widget) {
1487 widget_destroy(tooltip->widget);
1488 window_destroy(tooltip->window);
1489 tooltip->widget = NULL;
1490 tooltip->window = NULL;
1491 }
1492
1493 close(tooltip->tooltip_fd);
1494 free(tooltip->entry);
1495 free(tooltip);
1496 parent->tooltip = NULL;
1497}
1498
1499static void
1500tooltip_func(struct task *task, uint32_t events)
1501{
1502 struct tooltip *tooltip =
1503 container_of(task, struct tooltip, tooltip_task);
1504 uint64_t exp;
1505
Martin Olsson8df662a2012-07-08 03:03:47 +02001506 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1507 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001508 window_create_tooltip(tooltip);
1509}
1510
1511#define TOOLTIP_TIMEOUT 500
1512static int
1513tooltip_timer_reset(struct tooltip *tooltip)
1514{
1515 struct itimerspec its;
1516
1517 its.it_interval.tv_sec = 0;
1518 its.it_interval.tv_nsec = 0;
1519 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1520 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1521 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1522 fprintf(stderr, "could not set timerfd\n: %m");
1523 return -1;
1524 }
1525
1526 return 0;
1527}
1528
1529int
1530widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1531{
1532 struct tooltip *tooltip = parent->tooltip;
1533
1534 parent->tooltip_count++;
1535 if (tooltip) {
1536 tooltip->x = x;
1537 tooltip->y = y;
1538 tooltip_timer_reset(tooltip);
1539 return 0;
1540 }
1541
1542 /* the handler might be triggered too fast via input device motion, so
1543 * we need this check here to make sure tooltip is fully initialized */
1544 if (parent->tooltip_count > 1)
1545 return 0;
1546
1547 tooltip = malloc(sizeof *tooltip);
1548 if (!tooltip)
1549 return -1;
1550
1551 parent->tooltip = tooltip;
1552 tooltip->parent = parent;
1553 tooltip->widget = NULL;
1554 tooltip->window = NULL;
1555 tooltip->x = x;
1556 tooltip->y = y;
1557 tooltip->entry = strdup(entry);
1558 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1559 if (tooltip->tooltip_fd < 0) {
1560 fprintf(stderr, "could not create timerfd\n: %m");
1561 return -1;
1562 }
1563
1564 tooltip->tooltip_task.run = tooltip_func;
1565 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1566 EPOLLIN, &tooltip->tooltip_task);
1567 tooltip_timer_reset(tooltip);
1568
1569 return 0;
1570}
1571
1572static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001573workspace_manager_state(void *data,
1574 struct workspace_manager *workspace_manager,
1575 uint32_t current,
1576 uint32_t count)
1577{
1578 struct display *display = data;
1579
1580 display->workspace = current;
1581 display->workspace_count = count;
1582}
1583
1584static const struct workspace_manager_listener workspace_manager_listener = {
1585 workspace_manager_state
1586};
1587
1588static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001589frame_resize_handler(struct widget *widget,
1590 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001591{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001592 struct frame *frame = data;
1593 struct widget *child = frame->child;
1594 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001595 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001596 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001597 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001598 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001599 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001600 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001601
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001602 switch (widget->window->type) {
1603 case TYPE_FULLSCREEN:
1604 decoration_width = 0;
1605 decoration_height = 0;
1606
1607 allocation.x = 0;
1608 allocation.y = 0;
1609 allocation.width = width;
1610 allocation.height = height;
1611 opaque_margin = 0;
1612
1613 wl_list_for_each(button, &frame->buttons_list, link)
1614 button->widget->opaque = 1;
1615 break;
1616 case TYPE_MAXIMIZED:
1617 decoration_width = t->width * 2;
1618 decoration_height = t->width + t->titlebar_height;
1619
1620 allocation.x = t->width;
1621 allocation.y = t->titlebar_height;
1622 allocation.width = width - decoration_width;
1623 allocation.height = height - decoration_height;
1624
1625 opaque_margin = 0;
1626
1627 wl_list_for_each(button, &frame->buttons_list, link)
1628 button->widget->opaque = 0;
1629 break;
1630 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001631 decoration_width = (t->width + t->margin) * 2;
1632 decoration_height = t->width +
1633 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001634
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001635 allocation.x = t->width + t->margin;
1636 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001637 allocation.width = width - decoration_width;
1638 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001639
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001640 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001641
1642 wl_list_for_each(button, &frame->buttons_list, link)
1643 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001644 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001645 }
1646
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001647 widget_set_allocation(child, allocation.x, allocation.y,
1648 allocation.width, allocation.height);
1649
1650 if (child->resize_handler)
1651 child->resize_handler(child,
1652 allocation.width,
1653 allocation.height,
1654 child->user_data);
1655
Scott Moreauf7e498c2012-05-14 11:39:29 -06001656 width = child->allocation.width + decoration_width;
1657 height = child->allocation.height + decoration_height;
1658
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001659 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1660
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001661 widget->window->input_region =
1662 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001663 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001664 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001665 shadow_margin, shadow_margin,
1666 width - 2 * shadow_margin,
1667 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001668 } else {
1669 wl_region_add(widget->window->input_region,
1670 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001671 }
1672
Scott Moreauf7e498c2012-05-14 11:39:29 -06001673 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001674
1675 if (child->opaque) {
1676 widget->window->opaque_region =
1677 wl_compositor_create_region(display->compositor);
1678 wl_region_add(widget->window->opaque_region,
1679 opaque_margin, opaque_margin,
1680 widget->allocation.width - 2 * opaque_margin,
1681 widget->allocation.height - 2 * opaque_margin);
1682 }
Martin Minarik1998b152012-05-10 02:04:35 +02001683
1684 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001685 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1686 x_l = t->width + shadow_margin;
1687 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001688 wl_list_for_each(button, &frame->buttons_list, link) {
1689 const int button_padding = 4;
1690 w = cairo_image_surface_get_width(button->icon);
1691 h = cairo_image_surface_get_height(button->icon);
1692
1693 if (button->decoration == FRAME_BUTTON_FANCY)
1694 w += 10;
1695
1696 if (button->align == FRAME_BUTTON_LEFT) {
1697 widget_set_allocation(button->widget,
1698 x_l, y , w + 1, h + 1);
1699 x_l += w;
1700 x_l += button_padding;
1701 } else {
1702 x_r -= w;
1703 widget_set_allocation(button->widget,
1704 x_r, y , w + 1, h + 1);
1705 x_r -= button_padding;
1706 }
1707 }
1708}
1709
1710static int
1711frame_button_enter_handler(struct widget *widget,
1712 struct input *input, float x, float y, void *data)
1713{
1714 struct frame_button *frame_button = data;
1715
1716 widget_schedule_redraw(frame_button->widget);
1717 frame_button->state = FRAME_BUTTON_OVER;
1718
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001719 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001720}
1721
1722static void
1723frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1724{
1725 struct frame_button *frame_button = data;
1726
1727 widget_schedule_redraw(frame_button->widget);
1728 frame_button->state = FRAME_BUTTON_DEFAULT;
1729}
1730
1731static void
1732frame_button_button_handler(struct widget *widget,
1733 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001734 uint32_t button,
1735 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001736{
1737 struct frame_button *frame_button = data;
1738 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001739 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001740
1741 if (button != BTN_LEFT)
1742 return;
1743
1744 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001745 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001746 frame_button->state = FRAME_BUTTON_ACTIVE;
1747 widget_schedule_redraw(frame_button->widget);
1748
1749 if (frame_button->type == FRAME_BUTTON_ICON)
1750 window_show_frame_menu(window, input, time);
1751 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001752 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001753 frame_button->state = FRAME_BUTTON_DEFAULT;
1754 widget_schedule_redraw(frame_button->widget);
1755 break;
1756 }
1757
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001758 if (!was_pressed)
1759 return;
1760
Martin Minarik1998b152012-05-10 02:04:35 +02001761 switch (frame_button->type) {
1762 case FRAME_BUTTON_CLOSE:
1763 if (window->close_handler)
1764 window->close_handler(window->parent,
1765 window->user_data);
1766 else
1767 display_exit(window->display);
1768 break;
1769 case FRAME_BUTTON_MINIMIZE:
1770 fprintf(stderr,"Minimize stub\n");
1771 break;
1772 case FRAME_BUTTON_MAXIMIZE:
1773 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1774 break;
1775 default:
1776 /* Unknown operation */
1777 break;
1778 }
1779}
1780
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001781static int
1782frame_button_motion_handler(struct widget *widget,
1783 struct input *input, uint32_t time,
1784 float x, float y, void *data)
1785{
1786 struct frame_button *frame_button = data;
1787 enum frame_button_pointer previous_button_state = frame_button->state;
1788
1789 /* only track state for a pressed button */
1790 if (input->grab != widget)
1791 return CURSOR_LEFT_PTR;
1792
1793 if (x > widget->allocation.x &&
1794 x < (widget->allocation.x + widget->allocation.width) &&
1795 y > widget->allocation.y &&
1796 y < (widget->allocation.y + widget->allocation.height)) {
1797 frame_button->state = FRAME_BUTTON_ACTIVE;
1798 } else {
1799 frame_button->state = FRAME_BUTTON_DEFAULT;
1800 }
1801
1802 if (frame_button->state != previous_button_state)
1803 widget_schedule_redraw(frame_button->widget);
1804
1805 return CURSOR_LEFT_PTR;
1806}
1807
Martin Minarik1998b152012-05-10 02:04:35 +02001808static void
1809frame_button_redraw_handler(struct widget *widget, void *data)
1810{
1811 struct frame_button *frame_button = data;
1812 cairo_t *cr;
1813 int width, height, x, y;
1814 struct window *window = widget->window;
1815
1816 x = widget->allocation.x;
1817 y = widget->allocation.y;
1818 width = widget->allocation.width;
1819 height = widget->allocation.height;
1820
1821 if (!width)
1822 return;
1823 if (!height)
1824 return;
1825 if (widget->opaque)
1826 return;
1827
1828 cr = cairo_create(window->cairo_surface);
1829
1830 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1831 cairo_set_line_width(cr, 1);
1832
1833 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1834 cairo_rectangle (cr, x, y, 25, 16);
1835
1836 cairo_stroke_preserve(cr);
1837
1838 switch (frame_button->state) {
1839 case FRAME_BUTTON_DEFAULT:
1840 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1841 break;
1842 case FRAME_BUTTON_OVER:
1843 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1844 break;
1845 case FRAME_BUTTON_ACTIVE:
1846 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1847 break;
1848 }
1849
1850 cairo_fill (cr);
1851
1852 x += 4;
1853 }
1854
1855 cairo_set_source_surface(cr, frame_button->icon, x, y);
1856 cairo_paint(cr);
1857
1858 cairo_destroy(cr);
1859}
1860
1861static struct widget *
1862frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1863 enum frame_button_align align, enum frame_button_decoration style)
1864{
1865 struct frame_button *frame_button;
1866 const char *icon = data;
1867
1868 frame_button = malloc (sizeof *frame_button);
1869 memset(frame_button, 0, sizeof *frame_button);
1870
1871 frame_button->icon = cairo_image_surface_create_from_png(icon);
1872 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1873 frame_button->frame = frame;
1874 frame_button->type = type;
1875 frame_button->align = align;
1876 frame_button->decoration = style;
1877
1878 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1879
1880 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1881 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1882 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1883 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001884 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001885 return frame_button->widget;
1886}
1887
1888static void
1889frame_button_destroy(struct frame_button *frame_button)
1890{
1891 widget_destroy(frame_button->widget);
1892 wl_list_remove(&frame_button->link);
1893 cairo_surface_destroy(frame_button->icon);
1894 free(frame_button);
1895
1896 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001897}
1898
1899static void
1900frame_redraw_handler(struct widget *widget, void *data)
1901{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001902 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001903 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001904 struct theme *t = window->display->theme;
1905 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001906
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001907 if (window->type == TYPE_FULLSCREEN)
1908 return;
1909
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001910 cr = cairo_create(window->cairo_surface);
1911
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001912 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001913 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001914 if (window->type == TYPE_MAXIMIZED)
1915 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001916 theme_render_frame(t, cr, widget->allocation.width,
1917 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001918
1919 cairo_destroy(cr);
1920}
1921
1922static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001923frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001924{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001925 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001926 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001927 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001928
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001929 location = theme_get_location(t, input->sx, input->sy,
1930 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001931 frame->widget->allocation.height,
1932 window->type == TYPE_MAXIMIZED ?
1933 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001934
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001935 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001936 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001937 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001938 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001939 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001940 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001941 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001942 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001943 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001944 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001945 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001946 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001947 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001948 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001949 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001950 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001951 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001952 case THEME_LOCATION_EXTERIOR:
1953 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001954 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001955 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001956 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001957}
1958
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001959static void
1960frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001961{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001962 struct display *display;
1963
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001964 switch (index) {
1965 case 0: /* close */
1966 if (window->close_handler)
1967 window->close_handler(window->parent,
1968 window->user_data);
1969 else
1970 display_exit(window->display);
1971 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001972 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001973 display = window->display;
1974 if (display->workspace > 0)
1975 workspace_manager_move_surface(display->workspace_manager,
1976 window->surface,
1977 display->workspace - 1);
1978 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001979 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001980 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001981 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001982 workspace_manager_move_surface(display->workspace_manager,
1983 window->surface,
1984 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05001985 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001986 case 3: /* fullscreen */
1987 /* we don't have a way to get out of fullscreen for now */
1988 if (window->fullscreen_handler)
1989 window->fullscreen_handler(window, window->user_data);
1990 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001991 }
1992}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001993
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001994void
1995window_show_frame_menu(struct window *window,
1996 struct input *input, uint32_t time)
1997{
1998 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001999 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002000
2001 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002002 "Close",
2003 "Move to workspace above", "Move to workspace below",
2004 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002005 };
2006
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002007 if (window->fullscreen_handler)
2008 count = ARRAY_LENGTH(entries);
2009 else
2010 count = ARRAY_LENGTH(entries) - 1;
2011
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002012 input_get_position(input, &x, &y);
2013 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002014 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002015}
2016
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002017static int
2018frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002019 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002020{
2021 return frame_get_pointer_image_for_location(data, input);
2022}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002023
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002024static int
2025frame_motion_handler(struct widget *widget,
2026 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002027 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002028{
2029 return frame_get_pointer_image_for_location(data, input);
2030}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002031
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002032static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002033frame_button_handler(struct widget *widget,
2034 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002035 uint32_t button, enum wl_pointer_button_state state,
2036 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002037
2038{
2039 struct frame *frame = data;
2040 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002041 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002042 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002043
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002044 location = theme_get_location(display->theme, input->sx, input->sy,
2045 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002046 frame->widget->allocation.height,
2047 window->type == TYPE_MAXIMIZED ?
2048 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002049
Daniel Stone4dbadb12012-05-30 16:31:51 +01002050 if (window->display->shell && button == BTN_LEFT &&
2051 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002052 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002053 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002054 if (!window->shell_surface)
2055 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002056 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002057 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002058 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002059 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002060 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002061 case THEME_LOCATION_RESIZING_TOP:
2062 case THEME_LOCATION_RESIZING_BOTTOM:
2063 case THEME_LOCATION_RESIZING_LEFT:
2064 case THEME_LOCATION_RESIZING_RIGHT:
2065 case THEME_LOCATION_RESIZING_TOP_LEFT:
2066 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2067 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2068 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002069 if (!window->shell_surface)
2070 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002071 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002072
Pekka Paalanen99436862012-11-19 17:15:59 +02002073 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002074 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002075 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002076 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002077 break;
2078 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002079 } else if (button == BTN_RIGHT &&
2080 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002081 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002082 }
2083}
2084
2085struct widget *
2086frame_create(struct window *window, void *data)
2087{
2088 struct frame *frame;
2089
2090 frame = malloc(sizeof *frame);
2091 memset(frame, 0, sizeof *frame);
2092
2093 frame->widget = window_add_widget(window, frame);
2094 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002095
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002096 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2097 widget_set_resize_handler(frame->widget, frame_resize_handler);
2098 widget_set_enter_handler(frame->widget, frame_enter_handler);
2099 widget_set_motion_handler(frame->widget, frame_motion_handler);
2100 widget_set_button_handler(frame->widget, frame_button_handler);
2101
Martin Minarik1998b152012-05-10 02:04:35 +02002102 /* Create empty list for frame buttons */
2103 wl_list_init(&frame->buttons_list);
2104
2105 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2106 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2107
2108 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2109 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2110
2111 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2112 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2113
2114 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2115 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2116
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002117 window->frame = frame;
2118
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002119 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002120}
2121
Kristian Høgsberga1627922012-06-20 17:30:03 -04002122void
2123frame_set_child_size(struct widget *widget, int child_width, int child_height)
2124{
2125 struct display *display = widget->window->display;
2126 struct theme *t = display->theme;
2127 int decoration_width, decoration_height;
2128 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002129 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002130
2131 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002132 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002133 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002134 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002135
2136 width = child_width + decoration_width;
2137 height = child_height + decoration_height;
2138 } else {
2139 width = child_width;
2140 height = child_height;
2141 }
2142
2143 window_schedule_resize(widget->window, width, height);
2144}
2145
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002146static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002147frame_destroy(struct frame *frame)
2148{
Martin Minarik1998b152012-05-10 02:04:35 +02002149 struct frame_button *button, *tmp;
2150
2151 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2152 frame_button_destroy(button);
2153
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002154 /* frame->child must be destroyed by the application */
2155 widget_destroy(frame->widget);
2156 free(frame);
2157}
2158
2159static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002160input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002161 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002162{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002163 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002164 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002165
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002166 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002167 return;
2168
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002169 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002170 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002171 widget = old;
2172 if (input->grab)
2173 widget = input->grab;
2174 if (widget->leave_handler)
2175 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002176 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002177 }
2178
2179 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002180 widget = focus;
2181 if (input->grab)
2182 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002183 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002184 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002185 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002186 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002187
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002188 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002189 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002190}
2191
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002192void
2193input_grab(struct input *input, struct widget *widget, uint32_t button)
2194{
2195 input->grab = widget;
2196 input->grab_button = button;
2197}
2198
2199void
2200input_ungrab(struct input *input)
2201{
2202 struct widget *widget;
2203
2204 input->grab = NULL;
2205 if (input->pointer_focus) {
2206 widget = widget_find_widget(input->pointer_focus->widget,
2207 input->sx, input->sy);
2208 input_set_focus_widget(input, widget, input->sx, input->sy);
2209 }
2210}
2211
2212static void
2213input_remove_pointer_focus(struct input *input)
2214{
2215 struct window *window = input->pointer_focus;
2216
2217 if (!window)
2218 return;
2219
2220 input_set_focus_widget(input, NULL, 0, 0);
2221
2222 input->pointer_focus = NULL;
2223 input->current_cursor = CURSOR_UNSET;
2224}
2225
2226static void
2227pointer_handle_enter(void *data, struct wl_pointer *pointer,
2228 uint32_t serial, struct wl_surface *surface,
2229 wl_fixed_t sx_w, wl_fixed_t sy_w)
2230{
2231 struct input *input = data;
2232 struct window *window;
2233 struct widget *widget;
2234 float sx = wl_fixed_to_double(sx_w);
2235 float sy = wl_fixed_to_double(sy_w);
2236
2237 if (!surface) {
2238 /* enter event for a window we've just destroyed */
2239 return;
2240 }
2241
2242 input->display->serial = serial;
2243 input->pointer_enter_serial = serial;
2244 input->pointer_focus = wl_surface_get_user_data(surface);
2245 window = input->pointer_focus;
2246
Pekka Paalanen99436862012-11-19 17:15:59 +02002247 if (window->resizing) {
2248 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002249 /* Schedule a redraw to free the pool */
2250 window_schedule_redraw(window);
2251 }
2252
2253 input->sx = sx;
2254 input->sy = sy;
2255
2256 widget = widget_find_widget(window->widget, sx, sy);
2257 input_set_focus_widget(input, widget, sx, sy);
2258}
2259
2260static void
2261pointer_handle_leave(void *data, struct wl_pointer *pointer,
2262 uint32_t serial, struct wl_surface *surface)
2263{
2264 struct input *input = data;
2265
2266 input->display->serial = serial;
2267 input_remove_pointer_focus(input);
2268}
2269
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002270static void
Daniel Stone37816df2012-05-16 18:45:18 +01002271pointer_handle_motion(void *data, struct wl_pointer *pointer,
2272 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002273{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002274 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002275 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002276 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002277 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002278 float sx = wl_fixed_to_double(sx_w);
2279 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002280
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002281 input->sx = sx;
2282 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002283
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002284 if (!window)
2285 return;
2286
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002287 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002288 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002289 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002290 }
2291
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002292 if (input->grab)
2293 widget = input->grab;
2294 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002295 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002296 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002297 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002298 input, time, sx, sy,
2299 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002300
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002301 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002302}
2303
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002304static void
Daniel Stone37816df2012-05-16 18:45:18 +01002305pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002306 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002307{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002308 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002309 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002310 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002311
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002312 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002313 if (input->focus_widget && input->grab == NULL &&
2314 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002315 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002316
Neil Roberts6b28aad2012-01-23 19:11:18 +00002317 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002318 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002319 (*widget->button_handler)(widget,
2320 input, time,
2321 button, state,
2322 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002323
Daniel Stone4dbadb12012-05-30 16:31:51 +01002324 if (input->grab && input->grab_button == button &&
2325 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002326 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002327}
2328
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002329static void
Daniel Stone37816df2012-05-16 18:45:18 +01002330pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002331 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002332{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002333 struct input *input = data;
2334 struct widget *widget;
2335
2336 widget = input->focus_widget;
2337 if (input->grab)
2338 widget = input->grab;
2339 if (widget && widget->axis_handler)
2340 (*widget->axis_handler)(widget,
2341 input, time,
2342 axis, value,
2343 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002344}
2345
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002346static const struct wl_pointer_listener pointer_listener = {
2347 pointer_handle_enter,
2348 pointer_handle_leave,
2349 pointer_handle_motion,
2350 pointer_handle_button,
2351 pointer_handle_axis,
2352};
2353
2354static void
2355input_remove_keyboard_focus(struct input *input)
2356{
2357 struct window *window = input->keyboard_focus;
2358 struct itimerspec its;
2359
2360 its.it_interval.tv_sec = 0;
2361 its.it_interval.tv_nsec = 0;
2362 its.it_value.tv_sec = 0;
2363 its.it_value.tv_nsec = 0;
2364 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2365
2366 if (!window)
2367 return;
2368
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002369 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002370 if (window->keyboard_focus_handler)
2371 (*window->keyboard_focus_handler)(window, NULL,
2372 window->user_data);
2373
2374 input->keyboard_focus = NULL;
2375}
2376
2377static void
2378keyboard_repeat_func(struct task *task, uint32_t events)
2379{
2380 struct input *input =
2381 container_of(task, struct input, repeat_task);
2382 struct window *window = input->keyboard_focus;
2383 uint64_t exp;
2384
2385 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2386 /* If we change the timer between the fd becoming
2387 * readable and getting here, there'll be nothing to
2388 * read and we get EAGAIN. */
2389 return;
2390
2391 if (window && window->key_handler) {
2392 (*window->key_handler)(window, input, input->repeat_time,
2393 input->repeat_key, input->repeat_sym,
2394 WL_KEYBOARD_KEY_STATE_PRESSED,
2395 window->user_data);
2396 }
2397}
2398
2399static void
2400keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2401 uint32_t format, int fd, uint32_t size)
2402{
2403 struct input *input = data;
2404 char *map_str;
2405
2406 if (!data) {
2407 close(fd);
2408 return;
2409 }
2410
2411 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2412 close(fd);
2413 return;
2414 }
2415
2416 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2417 if (map_str == MAP_FAILED) {
2418 close(fd);
2419 return;
2420 }
2421
2422 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2423 map_str,
2424 XKB_KEYMAP_FORMAT_TEXT_V1,
2425 0);
2426 munmap(map_str, size);
2427 close(fd);
2428
2429 if (!input->xkb.keymap) {
2430 fprintf(stderr, "failed to compile keymap\n");
2431 return;
2432 }
2433
2434 input->xkb.state = xkb_state_new(input->xkb.keymap);
2435 if (!input->xkb.state) {
2436 fprintf(stderr, "failed to create XKB state\n");
2437 xkb_map_unref(input->xkb.keymap);
2438 input->xkb.keymap = NULL;
2439 return;
2440 }
2441
2442 input->xkb.control_mask =
2443 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2444 input->xkb.alt_mask =
2445 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2446 input->xkb.shift_mask =
2447 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2448}
2449
2450static void
2451keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2452 uint32_t serial, struct wl_surface *surface,
2453 struct wl_array *keys)
2454{
2455 struct input *input = data;
2456 struct window *window;
2457
2458 input->display->serial = serial;
2459 input->keyboard_focus = wl_surface_get_user_data(surface);
2460
2461 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002462 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002463 if (window->keyboard_focus_handler)
2464 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002465 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002466}
2467
2468static void
2469keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2470 uint32_t serial, struct wl_surface *surface)
2471{
2472 struct input *input = data;
2473
2474 input->display->serial = serial;
2475 input_remove_keyboard_focus(input);
2476}
2477
Scott Moreau210d0792012-03-22 10:47:01 -06002478static void
Daniel Stone37816df2012-05-16 18:45:18 +01002479keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002480 uint32_t serial, uint32_t time, uint32_t key,
2481 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002482{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002483 struct input *input = data;
2484 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002485 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002486 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002487 const xkb_keysym_t *syms;
2488 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002489 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002490
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002491 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002492 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002493 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002494 return;
2495
Daniel Stone97f68542012-05-30 16:32:01 +01002496 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002497
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002498 sym = XKB_KEY_NoSymbol;
2499 if (num_syms == 1)
2500 sym = syms[0];
2501
2502 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002503 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002504 window_set_maximized(window,
2505 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002506 } else if (sym == XKB_KEY_F11 &&
2507 window->fullscreen_handler &&
2508 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2509 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002510 } else if (sym == XKB_KEY_F4 &&
2511 input->modifiers == MOD_ALT_MASK &&
2512 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2513 if (window->close_handler)
2514 window->close_handler(window->parent,
2515 window->user_data);
2516 else
2517 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002518 } else if (window->key_handler) {
2519 (*window->key_handler)(window, input, time, key,
2520 sym, state, window->user_data);
2521 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002522
2523 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2524 key == input->repeat_key) {
2525 its.it_interval.tv_sec = 0;
2526 its.it_interval.tv_nsec = 0;
2527 its.it_value.tv_sec = 0;
2528 its.it_value.tv_nsec = 0;
2529 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2530 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2531 input->repeat_sym = sym;
2532 input->repeat_key = key;
2533 input->repeat_time = time;
2534 its.it_interval.tv_sec = 0;
2535 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2536 its.it_value.tv_sec = 0;
2537 its.it_value.tv_nsec = 400 * 1000 * 1000;
2538 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2539 }
2540}
2541
2542static void
Daniel Stone351eb612012-05-31 15:27:47 -04002543keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2544 uint32_t serial, uint32_t mods_depressed,
2545 uint32_t mods_latched, uint32_t mods_locked,
2546 uint32_t group)
2547{
2548 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002549 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002550
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002551 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2552 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002553 mask = xkb_state_serialize_mods(input->xkb.state,
2554 XKB_STATE_DEPRESSED |
2555 XKB_STATE_LATCHED);
2556 input->modifiers = 0;
2557 if (mask & input->xkb.control_mask)
2558 input->modifiers |= MOD_CONTROL_MASK;
2559 if (mask & input->xkb.alt_mask)
2560 input->modifiers |= MOD_ALT_MASK;
2561 if (mask & input->xkb.shift_mask)
2562 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002563}
2564
Daniel Stone37816df2012-05-16 18:45:18 +01002565static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002566 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002567 keyboard_handle_enter,
2568 keyboard_handle_leave,
2569 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002570 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002571};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002572
2573static void
Daniel Stone37816df2012-05-16 18:45:18 +01002574seat_handle_capabilities(void *data, struct wl_seat *seat,
2575 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002576{
Daniel Stone37816df2012-05-16 18:45:18 +01002577 struct input *input = data;
2578
2579 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2580 input->pointer = wl_seat_get_pointer(seat);
2581 wl_pointer_set_user_data(input->pointer, input);
2582 wl_pointer_add_listener(input->pointer, &pointer_listener,
2583 input);
2584 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2585 wl_pointer_destroy(input->pointer);
2586 input->pointer = NULL;
2587 }
2588
2589 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2590 input->keyboard = wl_seat_get_keyboard(seat);
2591 wl_keyboard_set_user_data(input->keyboard, input);
2592 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2593 input);
2594 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2595 wl_keyboard_destroy(input->keyboard);
2596 input->keyboard = NULL;
2597 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002598}
2599
Daniel Stone37816df2012-05-16 18:45:18 +01002600static const struct wl_seat_listener seat_listener = {
2601 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002602};
2603
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002604void
2605input_get_position(struct input *input, int32_t *x, int32_t *y)
2606{
2607 *x = input->sx;
2608 *y = input->sy;
2609}
2610
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002611struct display *
2612input_get_display(struct input *input)
2613{
2614 return input->display;
2615}
2616
Daniel Stone37816df2012-05-16 18:45:18 +01002617struct wl_seat *
2618input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002619{
Daniel Stone37816df2012-05-16 18:45:18 +01002620 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002621}
2622
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002623uint32_t
2624input_get_modifiers(struct input *input)
2625{
2626 return input->modifiers;
2627}
2628
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002629struct widget *
2630input_get_focus_widget(struct input *input)
2631{
2632 return input->focus_widget;
2633}
2634
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002635struct data_offer {
2636 struct wl_data_offer *offer;
2637 struct input *input;
2638 struct wl_array types;
2639 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002640
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002641 struct task io_task;
2642 int fd;
2643 data_func_t func;
2644 int32_t x, y;
2645 void *user_data;
2646};
2647
2648static void
2649data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2650{
2651 struct data_offer *offer = data;
2652 char **p;
2653
2654 p = wl_array_add(&offer->types, sizeof *p);
2655 *p = strdup(type);
2656}
2657
2658static const struct wl_data_offer_listener data_offer_listener = {
2659 data_offer_offer,
2660};
2661
2662static void
2663data_offer_destroy(struct data_offer *offer)
2664{
2665 char **p;
2666
2667 offer->refcount--;
2668 if (offer->refcount == 0) {
2669 wl_data_offer_destroy(offer->offer);
2670 for (p = offer->types.data; *p; p++)
2671 free(*p);
2672 wl_array_release(&offer->types);
2673 free(offer);
2674 }
2675}
2676
2677static void
2678data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002679 struct wl_data_device *data_device,
2680 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002681{
2682 struct data_offer *offer;
2683
2684 offer = malloc(sizeof *offer);
2685
2686 wl_array_init(&offer->types);
2687 offer->refcount = 1;
2688 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002689 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002690 wl_data_offer_add_listener(offer->offer,
2691 &data_offer_listener, offer);
2692}
2693
2694static void
2695data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002696 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002697 wl_fixed_t x_w, wl_fixed_t y_w,
2698 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002699{
2700 struct input *input = data;
2701 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002702 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002703 float x = wl_fixed_to_double(x_w);
2704 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002705 char **p;
2706
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002707 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002708 window = wl_surface_get_user_data(surface);
2709 input->pointer_focus = window;
2710
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002711 if (offer) {
2712 input->drag_offer = wl_data_offer_get_user_data(offer);
2713
2714 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2715 *p = NULL;
2716
2717 types_data = input->drag_offer->types.data;
2718 } else {
2719 input->drag_offer = NULL;
2720 types_data = NULL;
2721 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002722
2723 window = input->pointer_focus;
2724 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002725 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002726 window->user_data);
2727}
2728
2729static void
2730data_device_leave(void *data, struct wl_data_device *data_device)
2731{
2732 struct input *input = data;
2733
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002734 if (input->drag_offer) {
2735 data_offer_destroy(input->drag_offer);
2736 input->drag_offer = NULL;
2737 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002738}
2739
2740static void
2741data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002742 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002743{
2744 struct input *input = data;
2745 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002746 float x = wl_fixed_to_double(x_w);
2747 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002748 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002749
2750 input->sx = x;
2751 input->sy = y;
2752
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002753 if (input->drag_offer)
2754 types_data = input->drag_offer->types.data;
2755 else
2756 types_data = NULL;
2757
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002758 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002759 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002760 window->user_data);
2761}
2762
2763static void
2764data_device_drop(void *data, struct wl_data_device *data_device)
2765{
2766 struct input *input = data;
2767 struct window *window = input->pointer_focus;
2768
2769 if (window->drop_handler)
2770 window->drop_handler(window, input,
2771 input->sx, input->sy, window->user_data);
2772}
2773
2774static void
2775data_device_selection(void *data,
2776 struct wl_data_device *wl_data_device,
2777 struct wl_data_offer *offer)
2778{
2779 struct input *input = data;
2780 char **p;
2781
2782 if (input->selection_offer)
2783 data_offer_destroy(input->selection_offer);
2784
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002785 if (offer) {
2786 input->selection_offer = wl_data_offer_get_user_data(offer);
2787 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2788 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002789 } else {
2790 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002791 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002792}
2793
2794static const struct wl_data_device_listener data_device_listener = {
2795 data_device_data_offer,
2796 data_device_enter,
2797 data_device_leave,
2798 data_device_motion,
2799 data_device_drop,
2800 data_device_selection
2801};
2802
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002803static void
2804input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002805{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002806 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002807 struct wl_cursor *cursor;
2808 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002809
Daniel Stone80972742012-11-07 17:51:39 +11002810 if (!input->pointer)
2811 return;
2812
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002813 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002814 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002815 return;
2816
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002817 if (index >= (int) cursor->image_count) {
2818 fprintf(stderr, "cursor index out of range\n");
2819 return;
2820 }
2821
2822 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002823 buffer = wl_cursor_image_get_buffer(image);
2824 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002825 return;
2826
Kristian Høgsbergae277372012-08-01 09:41:08 -04002827 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002828 input->pointer_surface,
2829 image->hotspot_x, image->hotspot_y);
2830 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2831 wl_surface_damage(input->pointer_surface, 0, 0,
2832 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002833 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002834}
2835
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002836static const struct wl_callback_listener pointer_surface_listener;
2837
2838static void
2839pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2840 uint32_t time)
2841{
2842 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002843 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002844 int i;
2845
2846 if (callback) {
2847 assert(callback == input->cursor_frame_cb);
2848 wl_callback_destroy(callback);
2849 input->cursor_frame_cb = NULL;
2850 }
2851
Daniel Stone80972742012-11-07 17:51:39 +11002852 if (!input->pointer)
2853 return;
2854
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002855 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002856 wl_pointer_set_cursor(input->pointer,
2857 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002858 NULL, 0, 0);
2859 return;
2860 }
2861
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002862 if (input->current_cursor == CURSOR_UNSET)
2863 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002864 cursor = input->display->cursors[input->current_cursor];
2865 if (!cursor)
2866 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002867
2868 /* FIXME We don't have the current time on the first call so we set
2869 * the animation start to the time of the first frame callback. */
2870 if (time == 0)
2871 input->cursor_anim_start = 0;
2872 else if (input->cursor_anim_start == 0)
2873 input->cursor_anim_start = time;
2874
2875 if (time == 0 || input->cursor_anim_start == 0)
2876 i = 0;
2877 else
2878 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2879
Pekka Paalanenbc106382012-10-10 12:49:31 +03002880 if (cursor->image_count > 1) {
2881 input->cursor_frame_cb =
2882 wl_surface_frame(input->pointer_surface);
2883 wl_callback_add_listener(input->cursor_frame_cb,
2884 &pointer_surface_listener, input);
2885 }
2886
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002887 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002888}
2889
2890static const struct wl_callback_listener pointer_surface_listener = {
2891 pointer_surface_frame_callback
2892};
2893
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002894void
2895input_set_pointer_image(struct input *input, int pointer)
2896{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002897 int force = 0;
2898
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002899 if (!input->pointer)
2900 return;
2901
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002902 if (input->pointer_enter_serial > input->cursor_serial)
2903 force = 1;
2904
2905 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002906 return;
2907
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002908 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002909 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002910 if (!input->cursor_frame_cb)
2911 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002912 else if (force) {
2913 /* The current frame callback may be stuck if, for instance,
2914 * the set cursor request was processed by the server after
2915 * this client lost the focus. In this case the cursor surface
2916 * might not be mapped and the frame callback wouldn't ever
2917 * complete. Send a set_cursor and attach to try to map the
2918 * cursor surface again so that the callback will finish */
2919 input_set_pointer_image_index(input, 0);
2920 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002921}
2922
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002923struct wl_data_device *
2924input_get_data_device(struct input *input)
2925{
2926 return input->data_device;
2927}
2928
2929void
2930input_set_selection(struct input *input,
2931 struct wl_data_source *source, uint32_t time)
2932{
2933 wl_data_device_set_selection(input->data_device, source, time);
2934}
2935
2936void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002937input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002938{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002939 wl_data_offer_accept(input->drag_offer->offer,
2940 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002941}
2942
2943static void
2944offer_io_func(struct task *task, uint32_t events)
2945{
2946 struct data_offer *offer =
2947 container_of(task, struct data_offer, io_task);
2948 unsigned int len;
2949 char buffer[4096];
2950
2951 len = read(offer->fd, buffer, sizeof buffer);
2952 offer->func(buffer, len,
2953 offer->x, offer->y, offer->user_data);
2954
2955 if (len == 0) {
2956 close(offer->fd);
2957 data_offer_destroy(offer);
2958 }
2959}
2960
2961static void
2962data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2963 data_func_t func, void *user_data)
2964{
2965 int p[2];
2966
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002967 if (pipe2(p, O_CLOEXEC) == -1)
2968 return;
2969
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002970 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2971 close(p[1]);
2972
2973 offer->io_task.run = offer_io_func;
2974 offer->fd = p[0];
2975 offer->func = func;
2976 offer->refcount++;
2977 offer->user_data = user_data;
2978
2979 display_watch_fd(offer->input->display,
2980 offer->fd, EPOLLIN, &offer->io_task);
2981}
2982
2983void
2984input_receive_drag_data(struct input *input, const char *mime_type,
2985 data_func_t func, void *data)
2986{
2987 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2988 input->drag_offer->x = input->sx;
2989 input->drag_offer->y = input->sy;
2990}
2991
2992int
2993input_receive_selection_data(struct input *input, const char *mime_type,
2994 data_func_t func, void *data)
2995{
2996 char **p;
2997
2998 if (input->selection_offer == NULL)
2999 return -1;
3000
3001 for (p = input->selection_offer->types.data; *p; p++)
3002 if (strcmp(mime_type, *p) == 0)
3003 break;
3004
3005 if (*p == NULL)
3006 return -1;
3007
3008 data_offer_receive_data(input->selection_offer,
3009 mime_type, func, data);
3010 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003011}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003012
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003013int
3014input_receive_selection_data_to_fd(struct input *input,
3015 const char *mime_type, int fd)
3016{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003017 if (input->selection_offer)
3018 wl_data_offer_receive(input->selection_offer->offer,
3019 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003020
3021 return 0;
3022}
3023
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003024void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003025window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003026{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003027 if (!window->shell_surface)
3028 return;
3029
Daniel Stone37816df2012-05-16 18:45:18 +01003030 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003031}
3032
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003033static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003034idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003035{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003036 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003037
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003038 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003039 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003040 widget_set_allocation(widget,
3041 window->pending_allocation.x,
3042 window->pending_allocation.y,
3043 window->pending_allocation.width,
3044 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003045
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003046 if (window->input_region) {
3047 wl_region_destroy(window->input_region);
3048 window->input_region = NULL;
3049 }
3050
3051 if (window->opaque_region) {
3052 wl_region_destroy(window->opaque_region);
3053 window->opaque_region = NULL;
3054 }
3055
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003056 if (widget->resize_handler)
3057 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003058 widget->allocation.width,
3059 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003060 widget->user_data);
3061
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003062 if (window->allocation.width != widget->allocation.width ||
3063 window->allocation.height != widget->allocation.height) {
3064 window->allocation = widget->allocation;
3065 window_schedule_redraw(window);
3066 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003067}
3068
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003069void
3070window_schedule_resize(struct window *window, int width, int height)
3071{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003072 window->pending_allocation.x = 0;
3073 window->pending_allocation.y = 0;
3074 window->pending_allocation.width = width;
3075 window->pending_allocation.height = height;
3076
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003077 if (window->min_allocation.width == 0)
3078 window->min_allocation = window->pending_allocation;
3079 if (window->pending_allocation.width < window->min_allocation.width)
3080 window->pending_allocation.width = window->min_allocation.width;
3081 if (window->pending_allocation.height < window->min_allocation.height)
3082 window->pending_allocation.height = window->min_allocation.height;
3083
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003084 window->resize_needed = 1;
3085 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003086}
3087
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003088void
3089widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3090{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003091 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003092}
3093
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003094static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003095handle_ping(void *data, struct wl_shell_surface *shell_surface,
3096 uint32_t serial)
3097{
3098 wl_shell_surface_pong(shell_surface, serial);
3099}
3100
3101static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003102handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003103 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003104{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003105 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003106
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003107 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003108 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003109}
3110
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003111static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003112menu_destroy(struct menu *menu)
3113{
3114 widget_destroy(menu->widget);
3115 window_destroy(menu->window);
3116 free(menu);
3117}
3118
3119static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003120handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3121{
3122 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003123 struct menu *menu = window->widget->user_data;
3124
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003125 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003126 * device. Or just use wl_callback. And this really needs to
3127 * be a window vfunc that the menu can set. And we need the
3128 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003129
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003130 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003131 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003132 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003133}
3134
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003135static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003136 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003137 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003138 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003139};
3140
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003141void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003142window_get_allocation(struct window *window,
3143 struct rectangle *allocation)
3144{
3145 *allocation = window->allocation;
3146}
3147
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003148static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003149widget_redraw(struct widget *widget)
3150{
3151 struct widget *child;
3152
3153 if (widget->redraw_handler)
3154 widget->redraw_handler(widget, widget->user_data);
3155 wl_list_for_each(child, &widget->child_list, link)
3156 widget_redraw(child);
3157}
3158
3159static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003160frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3161{
3162 struct window *window = data;
3163
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003164 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003165 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003166 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003167 window->redraw_scheduled = 0;
3168 if (window->redraw_needed)
3169 window_schedule_redraw(window);
3170}
3171
3172static const struct wl_callback_listener listener = {
3173 frame_callback
3174};
3175
3176static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003177idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003178{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003179 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003180
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003181 if (window->resize_needed)
3182 idle_resize(window);
3183
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003184 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003185 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003186 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003187 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003188
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003189 window->frame_cb = wl_surface_frame(window->surface);
3190 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003191 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003192}
3193
3194void
3195window_schedule_redraw(struct window *window)
3196{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003197 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003198 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003199 window->redraw_task.run = idle_redraw;
3200 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003201 window->redraw_scheduled = 1;
3202 }
3203}
3204
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003205int
3206window_is_fullscreen(struct window *window)
3207{
3208 return window->type == TYPE_FULLSCREEN;
3209}
3210
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003211void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003212window_set_fullscreen(struct window *window, int fullscreen)
3213{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003214 if (!window->display->shell)
3215 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003216
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003217 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003218 return;
3219
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003220 if (fullscreen) {
3221 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003222 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003223 wl_shell_surface_set_fullscreen(window->shell_surface,
3224 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3225 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003226 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003227 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003228 wl_shell_surface_set_toplevel(window->shell_surface);
3229 window_schedule_resize(window,
3230 window->saved_allocation.width,
3231 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003232 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003233}
3234
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003235int
3236window_is_maximized(struct window *window)
3237{
3238 return window->type == TYPE_MAXIMIZED;
3239}
3240
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003241void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003242window_set_maximized(struct window *window, int maximized)
3243{
3244 if (!window->display->shell)
3245 return;
3246
3247 if ((window->type == TYPE_MAXIMIZED) == maximized)
3248 return;
3249
3250 if (window->type == TYPE_TOPLEVEL) {
3251 window->saved_allocation = window->allocation;
3252 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3253 window->type = TYPE_MAXIMIZED;
3254 } else {
3255 wl_shell_surface_set_toplevel(window->shell_surface);
3256 window->type = TYPE_TOPLEVEL;
3257 window_schedule_resize(window,
3258 window->saved_allocation.width,
3259 window->saved_allocation.height);
3260 }
3261}
3262
3263void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003264window_set_user_data(struct window *window, void *data)
3265{
3266 window->user_data = data;
3267}
3268
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003269void *
3270window_get_user_data(struct window *window)
3271{
3272 return window->user_data;
3273}
3274
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003275void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003276window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003277 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003278{
3279 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003280}
3281
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003282void
3283window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003284 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003285{
3286 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003287}
3288
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003289void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003290window_set_data_handler(struct window *window, window_data_handler_t handler)
3291{
3292 window->data_handler = handler;
3293}
3294
3295void
3296window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3297{
3298 window->drop_handler = handler;
3299}
3300
3301void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003302window_set_close_handler(struct window *window,
3303 window_close_handler_t handler)
3304{
3305 window->close_handler = handler;
3306}
3307
3308void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003309window_set_fullscreen_handler(struct window *window,
3310 window_fullscreen_handler_t handler)
3311{
3312 window->fullscreen_handler = handler;
3313}
3314
3315void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003316window_set_title(struct window *window, const char *title)
3317{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003318 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003319 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003320 if (window->shell_surface)
3321 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003322}
3323
3324const char *
3325window_get_title(struct window *window)
3326{
3327 return window->title;
3328}
3329
3330void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003331window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3332{
3333 struct text_cursor_position *text_cursor_position =
3334 window->display->text_cursor_position;
3335
Scott Moreau9295ce02012-06-01 12:46:10 -06003336 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003337 return;
3338
3339 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003340 window->surface,
3341 wl_fixed_from_int(x),
3342 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003343}
3344
3345void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003346window_damage(struct window *window, int32_t x, int32_t y,
3347 int32_t width, int32_t height)
3348{
3349 wl_surface_damage(window->surface, x, y, width, height);
3350}
3351
Casey Dahlin9074db52012-04-19 22:50:09 -04003352static void
3353surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003354 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003355{
Rob Bradford7507b572012-05-15 17:55:34 +01003356 struct window *window = data;
3357 struct output *output;
3358 struct output *output_found = NULL;
3359 struct window_output *window_output;
3360
3361 wl_list_for_each(output, &window->display->output_list, link) {
3362 if (output->output == wl_output) {
3363 output_found = output;
3364 break;
3365 }
3366 }
3367
3368 if (!output_found)
3369 return;
3370
3371 window_output = malloc (sizeof *window_output);
3372 window_output->output = output_found;
3373
3374 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003375}
3376
3377static void
3378surface_leave(void *data,
3379 struct wl_surface *wl_surface, struct wl_output *output)
3380{
Rob Bradford7507b572012-05-15 17:55:34 +01003381 struct window *window = data;
3382 struct window_output *window_output;
3383 struct window_output *window_output_found = NULL;
3384
3385 wl_list_for_each(window_output, &window->window_output_list, link) {
3386 if (window_output->output->output == output) {
3387 window_output_found = window_output;
3388 break;
3389 }
3390 }
3391
3392 if (window_output_found) {
3393 wl_list_remove(&window_output_found->link);
3394 free(window_output_found);
3395 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003396}
3397
3398static const struct wl_surface_listener surface_listener = {
3399 surface_enter,
3400 surface_leave
3401};
3402
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003403static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003404window_create_internal(struct display *display,
3405 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003406{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003407 struct window *window;
3408
3409 window = malloc(sizeof *window);
3410 if (window == NULL)
3411 return NULL;
3412
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003413 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003414 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003415 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003416 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003417 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003418 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003419 window->shell_surface =
3420 wl_shell_get_shell_surface(display->shell,
3421 window->surface);
3422 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003423 window->allocation.x = 0;
3424 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003425 window->allocation.width = 0;
3426 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003427 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003428 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003429 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003430 window->input_region = NULL;
3431 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003432
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003433 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003434#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003435 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003436#else
3437 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3438#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003439 else
3440 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003441
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003442 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003443 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003444 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003445
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003446 if (window->shell_surface) {
3447 wl_shell_surface_set_user_data(window->shell_surface, window);
3448 wl_shell_surface_add_listener(window->shell_surface,
3449 &shell_surface_listener, window);
3450 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003451
Rob Bradford7507b572012-05-15 17:55:34 +01003452 wl_list_init (&window->window_output_list);
3453
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003454 return window;
3455}
3456
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003457struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003458window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003459{
3460 struct window *window;
3461
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003462 window = window_create_internal(display, NULL, TYPE_NONE);
3463 if (!window)
3464 return NULL;
3465
3466 return window;
3467}
3468
3469struct window *
3470window_create_custom(struct display *display)
3471{
3472 struct window *window;
3473
3474 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003475 if (!window)
3476 return NULL;
3477
3478 return window;
3479}
3480
3481struct window *
3482window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003483 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003484{
3485 struct window *window;
3486
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003487 window = window_create_internal(parent->display,
3488 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003489 if (!window)
3490 return NULL;
3491
3492 window->x = x;
3493 window->y = y;
3494
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003495 if (display->shell)
3496 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003497 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003498 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003499
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003500 return window;
3501}
3502
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003503static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003504menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003505{
3506 int next;
3507
3508 next = (sy - 8) / 20;
3509 if (menu->current != next) {
3510 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003511 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003512 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003513}
3514
3515static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003516menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003517 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003518 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003519{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003520 struct menu *menu = data;
3521
3522 if (widget == menu->widget)
3523 menu_set_item(data, y);
3524
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003525 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003526}
3527
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003528static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003529menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003530 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003531{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003532 struct menu *menu = data;
3533
3534 if (widget == menu->widget)
3535 menu_set_item(data, y);
3536
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003537 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003538}
3539
3540static void
3541menu_leave_handler(struct widget *widget, struct input *input, void *data)
3542{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003543 struct menu *menu = data;
3544
3545 if (widget == menu->widget)
3546 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003547}
3548
3549static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003550menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003551 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003552 uint32_t button, enum wl_pointer_button_state state,
3553 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003554
3555{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003556 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003557
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003558 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3559 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003560 /* Either relase after press-drag-release or
3561 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003562 menu->func(menu->window->parent,
3563 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003564 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003565 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003566 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003567 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003568 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003569}
3570
3571static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003572menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003573{
3574 cairo_t *cr;
3575 const int32_t r = 3, margin = 3;
3576 struct menu *menu = data;
3577 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003578 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003579
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003580 cr = cairo_create(window->cairo_surface);
3581 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3582 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3583 cairo_paint(cr);
3584
3585 width = window->allocation.width;
3586 height = window->allocation.height;
3587 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003588
3589 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003590 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3591 cairo_fill(cr);
3592
3593 for (i = 0; i < menu->count; i++) {
3594 if (i == menu->current) {
3595 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3596 cairo_rectangle(cr, margin, i * 20 + margin,
3597 width - 2 * margin, 20);
3598 cairo_fill(cr);
3599 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3600 cairo_move_to(cr, 10, i * 20 + 16);
3601 cairo_show_text(cr, menu->entries[i]);
3602 } else {
3603 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3604 cairo_move_to(cr, 10, i * 20 + 16);
3605 cairo_show_text(cr, menu->entries[i]);
3606 }
3607 }
3608
3609 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003610}
3611
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003612void
3613window_show_menu(struct display *display,
3614 struct input *input, uint32_t time, struct window *parent,
3615 int32_t x, int32_t y,
3616 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003617{
3618 struct window *window;
3619 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003620 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003621
3622 menu = malloc(sizeof *menu);
3623 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003624 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003625
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003626 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003627 if (!window) {
3628 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003629 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003630 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003631
3632 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003633 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003634 menu->entries = entries;
3635 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003636 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003637 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003638 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003639 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003640 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003641 window->type = TYPE_MENU;
3642 window->x = x;
3643 window->y = y;
3644
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003645 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003646 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003647 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003648 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003649 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003650
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003651 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003652 widget_set_enter_handler(menu->widget, menu_enter_handler);
3653 widget_set_leave_handler(menu->widget, menu_leave_handler);
3654 widget_set_motion_handler(menu->widget, menu_motion_handler);
3655 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003656
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003657 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003658 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003659}
3660
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003661void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003662window_set_buffer_type(struct window *window, enum window_buffer_type type)
3663{
3664 window->buffer_type = type;
3665}
3666
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003667
3668static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003669display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003670 struct wl_output *wl_output,
3671 int x, int y,
3672 int physical_width,
3673 int physical_height,
3674 int subpixel,
3675 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003676 const char *model,
3677 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003678{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003679 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003680
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003681 output->allocation.x = x;
3682 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003683 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003684}
3685
3686static void
3687display_handle_mode(void *data,
3688 struct wl_output *wl_output,
3689 uint32_t flags,
3690 int width,
3691 int height,
3692 int refresh)
3693{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003694 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003695 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003696
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003697 if (flags & WL_OUTPUT_MODE_CURRENT) {
3698 output->allocation.width = width;
3699 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003700 if (display->output_configure_handler)
3701 (*display->output_configure_handler)(
3702 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003703 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003704}
3705
3706static const struct wl_output_listener output_listener = {
3707 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003708 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003709};
3710
3711static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003712display_add_output(struct display *d, uint32_t id)
3713{
3714 struct output *output;
3715
3716 output = malloc(sizeof *output);
3717 if (output == NULL)
3718 return;
3719
3720 memset(output, 0, sizeof *output);
3721 output->display = d;
3722 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003723 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003724 wl_list_insert(d->output_list.prev, &output->link);
3725
3726 wl_output_add_listener(output->output, &output_listener, output);
3727}
3728
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003729static void
3730output_destroy(struct output *output)
3731{
3732 if (output->destroy_handler)
3733 (*output->destroy_handler)(output, output->user_data);
3734
3735 wl_output_destroy(output->output);
3736 wl_list_remove(&output->link);
3737 free(output);
3738}
3739
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003740void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003741display_set_global_handler(struct display *display,
3742 display_global_handler_t handler)
3743{
3744 struct global *global;
3745
3746 display->global_handler = handler;
3747 if (!handler)
3748 return;
3749
3750 wl_list_for_each(global, &display->global_list, link)
3751 display->global_handler(display,
3752 global->name, global->interface,
3753 global->version, display->user_data);
3754}
3755
3756void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003757display_set_output_configure_handler(struct display *display,
3758 display_output_handler_t handler)
3759{
3760 struct output *output;
3761
3762 display->output_configure_handler = handler;
3763 if (!handler)
3764 return;
3765
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003766 wl_list_for_each(output, &display->output_list, link) {
3767 if (output->allocation.width == 0 &&
3768 output->allocation.height == 0)
3769 continue;
3770
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003771 (*display->output_configure_handler)(output,
3772 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003773 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003774}
3775
3776void
3777output_set_user_data(struct output *output, void *data)
3778{
3779 output->user_data = data;
3780}
3781
3782void *
3783output_get_user_data(struct output *output)
3784{
3785 return output->user_data;
3786}
3787
3788void
3789output_set_destroy_handler(struct output *output,
3790 display_output_handler_t handler)
3791{
3792 output->destroy_handler = handler;
3793 /* FIXME: implement this, once we have way to remove outputs */
3794}
3795
3796void
Scott Moreau4e072362012-09-29 02:03:11 -06003797output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003798{
Scott Moreau4e072362012-09-29 02:03:11 -06003799 struct rectangle allocation = output->allocation;
3800
3801 switch (output->transform) {
3802 case WL_OUTPUT_TRANSFORM_90:
3803 case WL_OUTPUT_TRANSFORM_270:
3804 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3805 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3806 /* Swap width and height */
3807 allocation.width = output->allocation.height;
3808 allocation.height = output->allocation.width;
3809 break;
3810 }
3811
3812 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003813}
3814
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003815struct wl_output *
3816output_get_wl_output(struct output *output)
3817{
3818 return output->output;
3819}
3820
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003821static void
Daniel Stone97f68542012-05-30 16:32:01 +01003822fini_xkb(struct input *input)
3823{
3824 xkb_state_unref(input->xkb.state);
3825 xkb_map_unref(input->xkb.keymap);
3826}
3827
3828static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003829display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003830{
3831 struct input *input;
3832
3833 input = malloc(sizeof *input);
3834 if (input == NULL)
3835 return;
3836
3837 memset(input, 0, sizeof *input);
3838 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003839 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003840 input->pointer_focus = NULL;
3841 input->keyboard_focus = NULL;
3842 wl_list_insert(d->input_list.prev, &input->link);
3843
Daniel Stone37816df2012-05-16 18:45:18 +01003844 wl_seat_add_listener(input->seat, &seat_listener, input);
3845 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003846
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003847 input->data_device =
3848 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003849 input->seat);
3850 wl_data_device_add_listener(input->data_device, &data_device_listener,
3851 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003852
3853 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003854
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003855 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3856 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003857 input->repeat_task.run = keyboard_repeat_func;
3858 display_watch_fd(d, input->repeat_timer_fd,
3859 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003860}
3861
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003862static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003863input_destroy(struct input *input)
3864{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003865 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003866 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003867
3868 if (input->drag_offer)
3869 data_offer_destroy(input->drag_offer);
3870
3871 if (input->selection_offer)
3872 data_offer_destroy(input->selection_offer);
3873
3874 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003875 fini_xkb(input);
3876
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003877 wl_surface_destroy(input->pointer_surface);
3878
Pekka Paalanene1207c72011-12-16 12:02:09 +02003879 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003880 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003881 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003882 free(input);
3883}
3884
3885static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003886init_workspace_manager(struct display *d, uint32_t id)
3887{
3888 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003889 wl_registry_bind(d->registry, id,
3890 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003891 if (d->workspace_manager != NULL)
3892 workspace_manager_add_listener(d->workspace_manager,
3893 &workspace_manager_listener,
3894 d);
3895}
3896
3897static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003898registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3899 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003900{
3901 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003902 struct global *global;
3903
3904 global = malloc(sizeof *global);
3905 global->name = id;
3906 global->interface = strdup(interface);
3907 global->version = version;
3908 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003909
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003910 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003911 d->compositor = wl_registry_bind(registry, id,
3912 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003913 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003914 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003915 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003916 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003917 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003918 d->shell = wl_registry_bind(registry,
3919 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003920 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003921 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003922 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3923 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003924 wl_registry_bind(registry, id,
3925 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003926 } else if (strcmp(interface, "text_cursor_position") == 0) {
3927 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003928 wl_registry_bind(registry, id,
3929 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003930 } else if (strcmp(interface, "workspace_manager") == 0) {
3931 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003932 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003933
3934 if (d->global_handler)
3935 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003936}
3937
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003938void *
3939display_bind(struct display *display, uint32_t name,
3940 const struct wl_interface *interface, uint32_t version)
3941{
3942 return wl_registry_bind(display->registry, name, interface, version);
3943}
3944
3945static const struct wl_registry_listener registry_listener = {
3946 registry_handle_global
3947};
3948
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003949#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003950static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003951init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003952{
3953 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003954 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003955
Rob Clark6396ed32012-03-11 19:48:41 -05003956#ifdef USE_CAIRO_GLESV2
3957# define GL_BIT EGL_OPENGL_ES2_BIT
3958#else
3959# define GL_BIT EGL_OPENGL_BIT
3960#endif
3961
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003962 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04003963 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003964 EGL_RED_SIZE, 1,
3965 EGL_GREEN_SIZE, 1,
3966 EGL_BLUE_SIZE, 1,
3967 EGL_ALPHA_SIZE, 1,
3968 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003969 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003970 EGL_NONE
3971 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003972
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003973#ifdef USE_CAIRO_GLESV2
3974 static const EGLint context_attribs[] = {
3975 EGL_CONTEXT_CLIENT_VERSION, 2,
3976 EGL_NONE
3977 };
3978 EGLint api = EGL_OPENGL_ES_API;
3979#else
3980 EGLint *context_attribs = NULL;
3981 EGLint api = EGL_OPENGL_API;
3982#endif
3983
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003984 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003985 if (!eglInitialize(d->dpy, &major, &minor)) {
3986 fprintf(stderr, "failed to initialize display\n");
3987 return -1;
3988 }
3989
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003990 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003991 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3992 return -1;
3993 }
3994
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003995 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3996 &d->argb_config, 1, &n) || n != 1) {
3997 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003998 return -1;
3999 }
4000
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004001 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004002 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004003 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004004 fprintf(stderr, "failed to create context\n");
4005 return -1;
4006 }
4007
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004008 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01004009 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004010 return -1;
4011 }
4012
Benjamin Franzke0c991632011-09-27 21:57:31 +02004013 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4014 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4015 fprintf(stderr, "failed to get cairo egl argb device\n");
4016 return -1;
4017 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004018
4019 return 0;
4020}
4021
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004022static void
4023fini_egl(struct display *display)
4024{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004025 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004026
4027 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4028 EGL_NO_CONTEXT);
4029
4030 eglTerminate(display->dpy);
4031 eglReleaseThread();
4032}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004033#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004034
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004035static void
4036handle_display_data(struct task *task, uint32_t events)
4037{
4038 struct display *display =
4039 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004040 struct epoll_event ep;
4041 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004042
4043 display->display_fd_events = events;
4044
4045 if (events & EPOLLERR || events & EPOLLHUP) {
4046 display_exit(display);
4047 return;
4048 }
4049
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004050 if (events & EPOLLIN) {
4051 ret = wl_display_dispatch(display->display);
4052 if (ret == -1) {
4053 display_exit(display);
4054 return;
4055 }
4056 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004057
4058 if (events & EPOLLOUT) {
4059 ret = wl_display_flush(display->display);
4060 if (ret == 0) {
4061 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4062 ep.data.ptr = &display->display_task;
4063 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4064 display->display_fd, &ep);
4065 } else if (ret == -1 && errno != EAGAIN) {
4066 display_exit(display);
4067 return;
4068 }
4069 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004070}
4071
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004072struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004073display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004074{
4075 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004076
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004077 d = malloc(sizeof *d);
4078 if (d == NULL)
4079 return NULL;
4080
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004081 memset(d, 0, sizeof *d);
4082
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004083 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004084 if (d->display == NULL) {
4085 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004086 return NULL;
4087 }
4088
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004089 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004090 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004091 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004092 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4093 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004094
4095 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004096 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004097 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004098 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004099
Daniel Stone97f68542012-05-30 16:32:01 +01004100 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004101 if (d->xkb_context == NULL) {
4102 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004103 return NULL;
4104 }
4105
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004106 d->workspace = 0;
4107 d->workspace_count = 1;
4108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004109 d->registry = wl_display_get_registry(d->display);
4110 wl_registry_add_listener(d->registry, &registry_listener, d);
4111 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004112#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004113 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004114 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004115#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004116
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004117 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004118
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004119 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004120
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004121 wl_list_init(&d->window_list);
4122
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004123 return d;
4124}
4125
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004126static void
4127display_destroy_outputs(struct display *display)
4128{
4129 struct output *tmp;
4130 struct output *output;
4131
4132 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4133 output_destroy(output);
4134}
4135
Pekka Paalanene1207c72011-12-16 12:02:09 +02004136static void
4137display_destroy_inputs(struct display *display)
4138{
4139 struct input *tmp;
4140 struct input *input;
4141
4142 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4143 input_destroy(input);
4144}
4145
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004146void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004147display_destroy(struct display *display)
4148{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004149 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004150 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4151 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004152
4153 if (!wl_list_empty(&display->deferred_list))
4154 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4155
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004156 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004157 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004158
Daniel Stone97f68542012-05-30 16:32:01 +01004159 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004160
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004161 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004162 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004163
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004164#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004165 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004166#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004167
Pekka Paalanenc2052982011-12-16 11:41:32 +02004168 if (display->shell)
4169 wl_shell_destroy(display->shell);
4170
4171 if (display->shm)
4172 wl_shm_destroy(display->shm);
4173
4174 if (display->data_device_manager)
4175 wl_data_device_manager_destroy(display->data_device_manager);
4176
4177 wl_compositor_destroy(display->compositor);
4178
4179 close(display->epoll_fd);
4180
U. Artie Eoff44874d92012-10-02 21:12:35 -07004181 if (!(display->display_fd_events & EPOLLERR) &&
4182 !(display->display_fd_events & EPOLLHUP))
4183 wl_display_flush(display->display);
4184
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004185 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004186 free(display);
4187}
4188
4189void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004190display_set_user_data(struct display *display, void *data)
4191{
4192 display->user_data = data;
4193}
4194
4195void *
4196display_get_user_data(struct display *display)
4197{
4198 return display->user_data;
4199}
4200
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004201struct wl_display *
4202display_get_display(struct display *display)
4203{
4204 return display->display;
4205}
4206
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004207struct output *
4208display_get_output(struct display *display)
4209{
4210 return container_of(display->output_list.next, struct output, link);
4211}
4212
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004213struct wl_compositor *
4214display_get_compositor(struct display *display)
4215{
4216 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004217}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004218
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004219uint32_t
4220display_get_serial(struct display *display)
4221{
4222 return display->serial;
4223}
4224
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004225EGLDisplay
4226display_get_egl_display(struct display *d)
4227{
4228 return d->dpy;
4229}
4230
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004231struct wl_data_source *
4232display_create_data_source(struct display *display)
4233{
4234 return wl_data_device_manager_create_data_source(display->data_device_manager);
4235}
4236
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004237EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004238display_get_argb_egl_config(struct display *d)
4239{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004240 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004241}
4242
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004243struct wl_shell *
4244display_get_shell(struct display *display)
4245{
4246 return display->shell;
4247}
4248
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004249int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004250display_acquire_window_surface(struct display *display,
4251 struct window *window,
4252 EGLContext ctx)
4253{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004254 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004255 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004256
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004257 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004258}
4259
4260void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004261display_release_window_surface(struct display *display,
4262 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004263{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004264 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004265 return;
4266
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004267 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004268}
4269
4270void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004271display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004272{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004273 wl_list_insert(&display->deferred_list, &task->link);
4274}
4275
4276void
4277display_watch_fd(struct display *display,
4278 int fd, uint32_t events, struct task *task)
4279{
4280 struct epoll_event ep;
4281
4282 ep.events = events;
4283 ep.data.ptr = task;
4284 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4285}
4286
4287void
4288display_run(struct display *display)
4289{
4290 struct task *task;
4291 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004292 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004293
Pekka Paalanen826d7952011-12-15 10:14:07 +02004294 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004295 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004296 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004297 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004298 struct task, link);
4299 wl_list_remove(&task->link);
4300 task->run(task, 0);
4301 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004302
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004303 wl_display_dispatch_pending(display->display);
4304
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004305 if (!display->running)
4306 break;
4307
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004308 ret = wl_display_flush(display->display);
4309 if (ret < 0 && errno == EAGAIN) {
4310 ep[0].events =
4311 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4312 ep[0].data.ptr = &display->display_task;
4313
4314 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4315 display->display_fd, &ep[0]);
4316 } else if (ret < 0) {
4317 break;
4318 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004319
4320 count = epoll_wait(display->epoll_fd,
4321 ep, ARRAY_LENGTH(ep), -1);
4322 for (i = 0; i < count; i++) {
4323 task = ep[i].data.ptr;
4324 task->run(task, ep[i].events);
4325 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004326 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004327}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004328
4329void
4330display_exit(struct display *display)
4331{
4332 display->running = 0;
4333}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004334
4335void
4336keysym_modifiers_add(struct wl_array *modifiers_map,
4337 const char *name)
4338{
4339 size_t len = strlen(name) + 1;
4340 char *p;
4341
4342 p = wl_array_add(modifiers_map, len);
4343
4344 if (p == NULL)
4345 return;
4346
4347 strncpy(p, name, len);
4348}
4349
4350static xkb_mod_index_t
4351keysym_modifiers_get_index(struct wl_array *modifiers_map,
4352 const char *name)
4353{
4354 xkb_mod_index_t index = 0;
4355 char *p = modifiers_map->data;
4356
4357 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4358 if (strcmp(p, name) == 0)
4359 return index;
4360
4361 index++;
4362 p += strlen(p) + 1;
4363 }
4364
4365 return XKB_MOD_INVALID;
4366}
4367
4368xkb_mod_mask_t
4369keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4370 const char *name)
4371{
4372 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4373
4374 if (index == XKB_MOD_INVALID)
4375 return XKB_MOD_INVALID;
4376
4377 return 1 << index;
4378}