blob: c51b20d0fd8a294173570641f95939cea1cc5457 [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;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200129
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133};
134
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400136 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400138 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500139 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500141 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142 TYPE_CUSTOM
143};
Rob Bradford7507b572012-05-15 17:55:34 +0100144
145struct window_output {
146 struct output *output;
147 struct wl_list link;
148};
149
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200150struct toysurface {
151 /*
152 * Prepare the surface for drawing. Makes sure there is a surface
153 * of the right size available for rendering, and returns it.
154 * dx,dy are the x,y of wl_surface.attach.
155 * width,height are the new surface size.
Pekka Paalanen99436862012-11-19 17:15:59 +0200156 * If resize_hint is non-zero, the user is doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157 * Returns the Cairo surface to draw to.
158 */
159 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanen99436862012-11-19 17:15:59 +0200160 int width, int height, int resize_hint);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161
162 /*
163 * Post the surface to the server, returning the server allocation
164 * rectangle. The Cairo surface from prepare() must be destroyed
165 * after calling this.
166 */
167 void (*swap)(struct toysurface *base,
168 struct rectangle *server_allocation);
169
170 /*
171 * Make the toysurface current with the given EGL context.
172 * Returns 0 on success, and negative of failure.
173 */
174 int (*acquire)(struct toysurface *base, EGLContext ctx);
175
176 /*
177 * Release the toysurface from the EGL context, returning control
178 * to Cairo.
179 */
180 void (*release)(struct toysurface *base);
181
182 /*
183 * Destroy the toysurface, including the Cairo surface, any
184 * backing storage, and the Wayland protocol objects.
185 */
186 void (*destroy)(struct toysurface *base);
187};
188
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500189struct window {
190 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500191 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100192 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500193 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200194 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500195 struct wl_region *input_region;
196 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500197 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500198 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400199 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500200 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500201 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400202 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400203 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400204 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400205 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400206 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400207 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400208 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400209 int focus_count;
210
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400211 enum window_buffer_type buffer_type;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200212 struct toysurface *toysurface;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400213 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500214
Pekka Paalanen99436862012-11-19 17:15:59 +0200215 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400216
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500217 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500218 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400219 window_data_handler_t data_handler;
220 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500221 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400222 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400223
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300224 struct wl_callback *frame_cb;
225
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200226 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500227 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500228
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500229 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400230 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500231};
232
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500233struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500234 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300235 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500236 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400237 struct wl_list link;
238 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500239 widget_resize_handler_t resize_handler;
240 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500241 widget_enter_handler_t enter_handler;
242 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500243 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500244 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200245 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400246 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500247 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300248 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400249};
250
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400251struct input {
252 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100253 struct wl_seat *seat;
254 struct wl_pointer *pointer;
255 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400256 struct window *pointer_focus;
257 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300258 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300259 uint32_t cursor_anim_start;
260 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300261 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400262 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400263 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400264 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400265 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400266 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400267
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500268 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500269 struct widget *grab;
270 uint32_t grab_button;
271
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400272 struct wl_data_device *data_device;
273 struct data_offer *drag_offer;
274 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100275
276 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100277 struct xkb_keymap *keymap;
278 struct xkb_state *state;
279 xkb_mod_mask_t control_mask;
280 xkb_mod_mask_t alt_mask;
281 xkb_mod_mask_t shift_mask;
282 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400283
284 struct task repeat_task;
285 int repeat_timer_fd;
286 uint32_t repeat_sym;
287 uint32_t repeat_key;
288 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400289};
290
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500291struct output {
292 struct display *display;
293 struct wl_output *output;
294 struct rectangle allocation;
295 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600296 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200297
298 display_output_handler_t destroy_handler;
299 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500300};
301
Martin Minarik1998b152012-05-10 02:04:35 +0200302enum frame_button_action {
303 FRAME_BUTTON_NULL = 0,
304 FRAME_BUTTON_ICON = 1,
305 FRAME_BUTTON_CLOSE = 2,
306 FRAME_BUTTON_MINIMIZE = 3,
307 FRAME_BUTTON_MAXIMIZE = 4,
308};
309
310enum frame_button_pointer {
311 FRAME_BUTTON_DEFAULT = 0,
312 FRAME_BUTTON_OVER = 1,
313 FRAME_BUTTON_ACTIVE = 2,
314};
315
316enum frame_button_align {
317 FRAME_BUTTON_RIGHT = 0,
318 FRAME_BUTTON_LEFT = 1,
319};
320
321enum frame_button_decoration {
322 FRAME_BUTTON_NONE = 0,
323 FRAME_BUTTON_FANCY = 1,
324};
325
326struct frame_button {
327 struct widget *widget;
328 struct frame *frame;
329 cairo_surface_t *icon;
330 enum frame_button_action type;
331 enum frame_button_pointer state;
332 struct wl_list link; /* buttons_list */
333 enum frame_button_align align;
334 enum frame_button_decoration decoration;
335};
336
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500337struct frame {
338 struct widget *widget;
339 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200340 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500341};
342
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500343struct menu {
344 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500345 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500346 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500347 const char **entries;
348 uint32_t time;
349 int current;
350 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400351 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500352 menu_func_t func;
353};
354
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300355struct tooltip {
356 struct widget *parent;
357 struct window *window;
358 struct widget *widget;
359 char *entry;
360 struct task tooltip_task;
361 int tooltip_fd;
362 float x, y;
363};
364
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700365struct shm_pool {
366 struct wl_shm_pool *pool;
367 size_t size;
368 size_t used;
369 void *data;
370};
371
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400372enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300373 CURSOR_DEFAULT = 100,
374 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400375};
376
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500377enum window_location {
378 WINDOW_INTERIOR = 0,
379 WINDOW_RESIZING_TOP = 1,
380 WINDOW_RESIZING_BOTTOM = 2,
381 WINDOW_RESIZING_LEFT = 4,
382 WINDOW_RESIZING_TOP_LEFT = 5,
383 WINDOW_RESIZING_BOTTOM_LEFT = 6,
384 WINDOW_RESIZING_RIGHT = 8,
385 WINDOW_RESIZING_TOP_RIGHT = 9,
386 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
387 WINDOW_RESIZING_MASK = 15,
388 WINDOW_EXTERIOR = 16,
389 WINDOW_TITLEBAR = 17,
390 WINDOW_CLIENT_AREA = 18,
391};
392
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200393static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500395#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200397struct egl_window_surface {
398 struct toysurface base;
399 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100400 struct display *display;
401 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200402 struct wl_egl_window *egl_window;
403 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100404};
405
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200406static struct egl_window_surface *
407to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100408{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200409 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100410}
411
412static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200413egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanen99436862012-11-19 17:15:59 +0200414 int width, int height, int resize_hint)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100415{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200416 struct egl_window_surface *surface = to_egl_window_surface(base);
417
418 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
419 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
420
421 return cairo_surface_reference(surface->cairo_surface);
422}
423
424static void
425egl_window_surface_swap(struct toysurface *base,
426 struct rectangle *server_allocation)
427{
428 struct egl_window_surface *surface = to_egl_window_surface(base);
429
430 cairo_gl_surface_swapbuffers(surface->cairo_surface);
431 wl_egl_window_get_attached_size(surface->egl_window,
432 &server_allocation->width,
433 &server_allocation->height);
434}
435
436static int
437egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
438{
439 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200440 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100441
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200442 device = cairo_surface_get_device(surface->cairo_surface);
443 if (!device)
444 return -1;
445
446 if (!ctx) {
447 if (device == surface->display->argb_device)
448 ctx = surface->display->argb_ctx;
449 else
450 assert(0);
451 }
452
453 cairo_device_flush(device);
454 cairo_device_acquire(device);
455 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
456 surface->egl_surface, ctx))
457 fprintf(stderr, "failed to make surface current\n");
458
459 return 0;
460}
461
462static void
463egl_window_surface_release(struct toysurface *base)
464{
465 struct egl_window_surface *surface = to_egl_window_surface(base);
466 cairo_device_t *device;
467
468 device = cairo_surface_get_device(surface->cairo_surface);
469 if (!device)
470 return;
471
472 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
473 surface->display->argb_ctx))
474 fprintf(stderr, "failed to make context current\n");
475
476 cairo_device_release(device);
477}
478
479static void
480egl_window_surface_destroy(struct toysurface *base)
481{
482 struct egl_window_surface *surface = to_egl_window_surface(base);
483 struct display *d = surface->display;
484
485 cairo_surface_destroy(surface->cairo_surface);
486 eglDestroySurface(d->dpy, surface->egl_surface);
487 wl_egl_window_destroy(surface->egl_window);
488 surface->surface = NULL;
489
490 free(surface);
491}
492
493static struct toysurface *
494egl_window_surface_create(struct display *display,
495 struct wl_surface *wl_surface,
496 uint32_t flags,
497 struct rectangle *rectangle)
498{
499 struct egl_window_surface *surface;
500
Pekka Paalanenb3627362012-11-19 17:16:00 +0200501 if (display->dpy == EGL_NO_DISPLAY)
502 return NULL;
503
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200504 surface = calloc(1, sizeof *surface);
505 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100506 return NULL;
507
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200508 surface->base.prepare = egl_window_surface_prepare;
509 surface->base.swap = egl_window_surface_swap;
510 surface->base.acquire = egl_window_surface_acquire;
511 surface->base.release = egl_window_surface_release;
512 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100513
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200514 surface->display = display;
515 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400516
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517 surface->egl_window = wl_egl_window_create(surface->surface,
518 rectangle->width,
519 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200521 surface->egl_surface = eglCreateWindowSurface(display->dpy,
522 display->argb_config,
523 surface->egl_window,
524 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200526 surface->cairo_surface =
527 cairo_gl_surface_create_for_egl(display->argb_device,
528 surface->egl_surface,
529 rectangle->width,
530 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100531
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200532 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100533}
534
Pekka Paalanenb3627362012-11-19 17:16:00 +0200535#else
536
537static struct toysurface *
538egl_window_surface_create(struct display *display,
539 struct wl_surface *wl_surface,
540 uint32_t flags,
541 struct rectangle *rectangle)
542{
543 return NULL;
544}
545
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546#endif
547
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200548struct shm_surface_data {
549 struct wl_buffer *buffer;
550 struct shm_pool *pool;
551};
552
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400553struct wl_buffer *
554display_get_buffer_for_surface(struct display *display,
555 cairo_surface_t *surface)
556{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200557 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400558
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200559 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560
561 return data->buffer;
562}
563
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500564static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700565shm_pool_destroy(struct shm_pool *pool);
566
567static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400568shm_surface_data_destroy(void *p)
569{
570 struct shm_surface_data *data = p;
571
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200572 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700573 if (data->pool)
574 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300575
576 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400577}
578
Kristian Høgsberg16626282012-04-03 11:21:27 -0400579static struct wl_shm_pool *
580make_shm_pool(struct display *display, int size, void **data)
581{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400582 struct wl_shm_pool *pool;
583 int fd;
584
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300585 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400586 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300587 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
588 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400589 return NULL;
590 }
591
592 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400593 if (*data == MAP_FAILED) {
594 fprintf(stderr, "mmap failed: %m\n");
595 close(fd);
596 return NULL;
597 }
598
599 pool = wl_shm_create_pool(display->shm, fd, size);
600
601 close(fd);
602
603 return pool;
604}
605
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700606static struct shm_pool *
607shm_pool_create(struct display *display, size_t size)
608{
609 struct shm_pool *pool = malloc(sizeof *pool);
610
611 if (!pool)
612 return NULL;
613
614 pool->pool = make_shm_pool(display, size, &pool->data);
615 if (!pool->pool) {
616 free(pool);
617 return NULL;
618 }
619
620 pool->size = size;
621 pool->used = 0;
622
623 return pool;
624}
625
626static void *
627shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
628{
629 if (pool->used + size > pool->size)
630 return NULL;
631
632 *offset = pool->used;
633 pool->used += size;
634
635 return (char *) pool->data + *offset;
636}
637
638/* destroy the pool. this does not unmap the memory though */
639static void
640shm_pool_destroy(struct shm_pool *pool)
641{
642 munmap(pool->data, pool->size);
643 wl_shm_pool_destroy(pool->pool);
644 free(pool);
645}
646
647/* Start allocating from the beginning of the pool again */
648static void
649shm_pool_reset(struct shm_pool *pool)
650{
651 pool->used = 0;
652}
653
654static int
655data_length_for_shm_surface(struct rectangle *rect)
656{
657 int stride;
658
659 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
660 rect->width);
661 return stride * rect->height;
662}
663
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500664static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700665display_create_shm_surface_from_pool(struct display *display,
666 struct rectangle *rectangle,
667 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400668{
669 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400670 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400671 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700672 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400673 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400674
675 data = malloc(sizeof *data);
676 if (data == NULL)
677 return NULL;
678
679 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
680 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700681 length = stride * rectangle->height;
682 data->pool = NULL;
683 map = shm_pool_allocate(pool, length, &offset);
684
685 if (!map) {
686 free(data);
687 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400688 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400690 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400691 CAIRO_FORMAT_ARGB32,
692 rectangle->width,
693 rectangle->height,
694 stride);
695
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200696 cairo_surface_set_user_data(surface, &shm_surface_data_key,
697 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400698
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400699 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500700 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400701 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500702 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400703
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200704 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
705 rectangle->width,
706 rectangle->height,
707 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400708
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700709 return surface;
710}
711
712static cairo_surface_t *
713display_create_shm_surface(struct display *display,
714 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200715 struct shm_pool *alternate_pool,
716 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700717{
718 struct shm_surface_data *data;
719 struct shm_pool *pool;
720 cairo_surface_t *surface;
721
Pekka Paalanen99436862012-11-19 17:15:59 +0200722 if (alternate_pool) {
723 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700724 surface = display_create_shm_surface_from_pool(display,
725 rectangle,
726 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200727 alternate_pool);
728 if (surface) {
729 data = cairo_surface_get_user_data(surface,
730 &shm_surface_data_key);
731 goto out;
732 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700733 }
734
735 pool = shm_pool_create(display,
736 data_length_for_shm_surface(rectangle));
737 if (!pool)
738 return NULL;
739
740 surface =
741 display_create_shm_surface_from_pool(display, rectangle,
742 flags, pool);
743
744 if (!surface) {
745 shm_pool_destroy(pool);
746 return NULL;
747 }
748
749 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200750 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700751 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400752
Pekka Paalanen99436862012-11-19 17:15:59 +0200753out:
754 if (data_ret)
755 *data_ret = data;
756
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400757 return surface;
758}
759
nobled7b87cb02011-02-01 18:51:47 +0000760static int
761check_size(struct rectangle *rect)
762{
763 if (rect->width && rect->height)
764 return 0;
765
766 fprintf(stderr, "tried to create surface of "
767 "width: %d, height: %d\n", rect->width, rect->height);
768 return -1;
769}
770
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400771cairo_surface_t *
772display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100773 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400774 struct rectangle *rectangle,
775 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400776{
nobled7b87cb02011-02-01 18:51:47 +0000777 if (check_size(rectangle) < 0)
778 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200779
780 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200781 return display_create_shm_surface(display, rectangle, flags,
782 NULL, NULL);
783}
784
785struct shm_surface {
786 struct toysurface base;
787 struct display *display;
788 struct wl_surface *surface;
789 uint32_t flags;
790 int dx, dy;
791
792 cairo_surface_t *cairo_surface;
793 /* 'data' is automatically destroyed, when 'cairo_surface' is */
794 struct shm_surface_data *data;
795
796 struct shm_pool *resize_pool;
797};
798
799static struct shm_surface *
800to_shm_surface(struct toysurface *base)
801{
802 return container_of(base, struct shm_surface, base);
803}
804
805static cairo_surface_t *
806shm_surface_prepare(struct toysurface *base, int dx, int dy,
807 int width, int height, int resize_hint)
808{
809 struct shm_surface *surface = to_shm_surface(base);
810 struct rectangle rect = { 0, 0, width, height };
811
812 surface->dx = dx;
813 surface->dy = dy;
814
815 if (!resize_hint && surface->resize_pool) {
816 cairo_surface_destroy(surface->cairo_surface);
817 shm_pool_destroy(surface->resize_pool);
818 surface->resize_pool = NULL;
819 surface->cairo_surface = NULL;
820 }
821
822 if (surface->cairo_surface &&
823 cairo_image_surface_get_width(surface->cairo_surface) == width &&
824 cairo_image_surface_get_height(surface->cairo_surface) == height)
825 goto out;
826
827 if (surface->cairo_surface)
828 cairo_surface_destroy(surface->cairo_surface);
829
830 if (resize_hint && !surface->resize_pool) {
831 /* Create a big pool to allocate from, while continuously
832 * resizing. Mmapping a new pool in the server
833 * is relatively expensive, so reusing a pool performs
834 * better, but may temporarily reserve unneeded memory.
835 */
836 /* We should probably base this number on the output size. */
837 surface->resize_pool = shm_pool_create(surface->display,
838 6 * 1024 * 1024);
839 }
840
841 surface->cairo_surface =
842 display_create_shm_surface(surface->display, &rect,
843 surface->flags,
844 surface->resize_pool,
845 &surface->data);
846
847out:
848 return cairo_surface_reference(surface->cairo_surface);
849}
850
851static void
852shm_surface_swap(struct toysurface *base,
853 struct rectangle *server_allocation)
854{
855 struct shm_surface *surface = to_shm_surface(base);
856
857 server_allocation->width =
858 cairo_image_surface_get_width(surface->cairo_surface);
859 server_allocation->height =
860 cairo_image_surface_get_height(surface->cairo_surface);
861
862 wl_surface_attach(surface->surface, surface->data->buffer,
863 surface->dx, surface->dy);
864 wl_surface_damage(surface->surface, 0, 0,
865 server_allocation->width, server_allocation->height);
866 wl_surface_commit(surface->surface);
867}
868
869static int
870shm_surface_acquire(struct toysurface *base, EGLContext ctx)
871{
872 return -1;
873}
874
875static void
876shm_surface_release(struct toysurface *base)
877{
878}
879
880static void
881shm_surface_destroy(struct toysurface *base)
882{
883 struct shm_surface *surface = to_shm_surface(base);
884
885 /* this destroys surface->data, too */
886 cairo_surface_destroy(surface->cairo_surface);
887
888 if (surface->resize_pool)
889 shm_pool_destroy(surface->resize_pool);
890
891 free(surface);
892}
893
894static struct toysurface *
895shm_surface_create(struct display *display, struct wl_surface *wl_surface,
896 uint32_t flags, struct rectangle *rectangle)
897{
898 struct shm_surface *surface;
899
900 surface = calloc(1, sizeof *surface);
901 if (!surface)
902 return NULL;
903
904 surface->base.prepare = shm_surface_prepare;
905 surface->base.swap = shm_surface_swap;
906 surface->base.acquire = shm_surface_acquire;
907 surface->base.release = shm_surface_release;
908 surface->base.destroy = shm_surface_destroy;
909
910 surface->display = display;
911 surface->surface = wl_surface;
912 surface->flags = flags;
913 surface->cairo_surface = display_create_shm_surface(display, rectangle,
914 flags, NULL,
915 &surface->data);
916 if (!surface->cairo_surface) {
917 free(surface);
918 return NULL;
919 }
920
921 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400922}
923
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200924/*
925 * The following correspondences between file names and cursors was copied
926 * from: https://bugs.kde.org/attachment.cgi?id=67313
927 */
928
929static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300930 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200931 "sw-resize"
932};
933
934static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300935 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200936 "se-resize"
937};
938
939static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300940 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200941 "s-resize"
942};
943
944static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300945 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200946 "closedhand",
947 "208530c400c041818281048008011002"
948};
949
950static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300951 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200952 "default",
953 "top_left_arrow",
954 "left-arrow"
955};
956
957static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300958 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200959 "w-resize"
960};
961
962static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300963 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200964 "e-resize"
965};
966
967static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300968 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200969 "nw-resize"
970};
971
972static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300973 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200974 "ne-resize"
975};
976
977static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300978 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200979 "n-resize"
980};
981
982static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300983 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200984 "ibeam",
985 "text"
986};
987
988static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300989 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200990 "pointer",
991 "pointing_hand",
992 "e29285e634086352946a0e7090d73106"
993};
994
995static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400996 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200997 "wait",
998 "0426c94ea35c87780ff01dc239897213"
999};
1000
1001struct cursor_alternatives {
1002 const char **names;
1003 size_t count;
1004};
1005
1006static const struct cursor_alternatives cursors[] = {
1007 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1008 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1009 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1010 {grabbings, ARRAY_LENGTH(grabbings)},
1011 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1012 {left_sides, ARRAY_LENGTH(left_sides)},
1013 {right_sides, ARRAY_LENGTH(right_sides)},
1014 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1015 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1016 {top_sides, ARRAY_LENGTH(top_sides)},
1017 {xterms, ARRAY_LENGTH(xterms)},
1018 {hand1s, ARRAY_LENGTH(hand1s)},
1019 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001020};
1021
1022static void
1023create_cursors(struct display *display)
1024{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001025 char *config_file;
1026 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001027 unsigned int i, j;
1028 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001029 struct config_key shell_keys[] = {
1030 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1031 };
1032 struct config_section cs[] = {
1033 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1034 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001035
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001036 config_file = config_file_path("weston.ini");
1037 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1038 free(config_file);
1039
1040 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001041 display->cursors =
1042 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1043
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001044 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001045 cursor = NULL;
1046 for (j = 0; !cursor && j < cursors[i].count; ++j)
1047 cursor = wl_cursor_theme_get_cursor(
1048 display->cursor_theme, cursors[i].names[j]);
1049
1050 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001051 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001052 cursors[i].names[0]);
1053
1054 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001055 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001056}
1057
1058static void
1059destroy_cursors(struct display *display)
1060{
1061 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001062 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001063}
1064
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001065struct wl_cursor_image *
1066display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001067{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001068 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001069
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001070 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001071}
1072
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001073static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001074window_get_resize_dx_dy(struct window *window, int *x, int *y)
1075{
1076 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1077 *x = window->server_allocation.width - window->allocation.width;
1078 else
1079 *x = 0;
1080
1081 if (window->resize_edges & WINDOW_RESIZING_TOP)
1082 *y = window->server_allocation.height -
1083 window->allocation.height;
1084 else
1085 *y = 0;
1086
1087 window->resize_edges = 0;
1088}
1089
1090static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001091window_attach_surface(struct window *window)
1092{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001093 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001094
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001095 if (window->type == TYPE_NONE) {
1096 window->type = TYPE_TOPLEVEL;
1097 if (display->shell)
1098 wl_shell_surface_set_toplevel(window->shell_surface);
1099 }
1100
Pekka Paalanen512dde82012-10-10 12:49:27 +03001101 if (window->opaque_region) {
1102 wl_surface_set_opaque_region(window->surface,
1103 window->opaque_region);
1104 wl_region_destroy(window->opaque_region);
1105 window->opaque_region = NULL;
1106 }
1107
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001108 if (window->input_region) {
1109 wl_surface_set_input_region(window->surface,
1110 window->input_region);
1111 wl_region_destroy(window->input_region);
1112 window->input_region = NULL;
1113 }
1114
Pekka Paalanen99436862012-11-19 17:15:59 +02001115 window->toysurface->swap(window->toysurface,
1116 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001117}
1118
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001119int
1120window_has_focus(struct window *window)
1121{
1122 return window->focus_count > 0;
1123}
1124
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001125static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001126window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001127{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001128 if (!window->cairo_surface)
1129 return;
1130
1131 window_attach_surface(window);
1132 cairo_surface_destroy(window->cairo_surface);
1133 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001134}
1135
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001136struct display *
1137window_get_display(struct window *window)
1138{
1139 return window->display;
1140}
1141
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001142static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001143window_create_surface(struct window *window)
1144{
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001145 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001146 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001147
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001148 if (!window->transparent)
1149 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001150
Pekka Paalanenb3627362012-11-19 17:16:00 +02001151 if (!window->toysurface &&
1152 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1153 window->display->dpy) {
1154 window->toysurface =
1155 egl_window_surface_create(window->display,
1156 window->surface, flags,
1157 &window->allocation);
1158 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001159
Pekka Paalanenb3627362012-11-19 17:16:00 +02001160 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001161 window->toysurface = shm_surface_create(window->display,
1162 window->surface, flags,
1163 &window->allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001164
Pekka Paalanen99436862012-11-19 17:15:59 +02001165 window_get_resize_dx_dy(window, &dx, &dy);
1166 window->cairo_surface =
1167 window->toysurface->prepare(window->toysurface, dx, dy,
1168 window->allocation.width,
1169 window->allocation.height,
1170 window->resizing);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001171}
1172
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001173static void frame_destroy(struct frame *frame);
1174
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001175void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001176window_destroy(struct window *window)
1177{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001178 struct display *display = window->display;
1179 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001180 struct window_output *window_output;
1181 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001182
1183 if (window->redraw_scheduled)
1184 wl_list_remove(&window->redraw_task.link);
1185
1186 wl_list_for_each(input, &display->input_list, link) {
1187 if (input->pointer_focus == window)
1188 input->pointer_focus = NULL;
1189 if (input->keyboard_focus == window)
1190 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001191 if (input->focus_widget &&
1192 input->focus_widget->window == window)
1193 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001194 }
1195
Rob Bradford7507b572012-05-15 17:55:34 +01001196 wl_list_for_each_safe(window_output, window_output_tmp,
1197 &window->window_output_list, link) {
1198 free (window_output);
1199 }
1200
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001201 if (window->input_region)
1202 wl_region_destroy(window->input_region);
1203 if (window->opaque_region)
1204 wl_region_destroy(window->opaque_region);
1205
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001206 if (window->frame)
1207 frame_destroy(window->frame);
1208
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001209 if (window->shell_surface)
1210 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001211 wl_surface_destroy(window->surface);
1212 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001213
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001214 if (window->toysurface)
1215 window->toysurface->destroy(window->toysurface);
1216
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001217 if (window->frame_cb)
1218 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001219 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001220 free(window);
1221}
1222
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001223static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001224widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001225{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001226 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001227
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001228 wl_list_for_each(child, &widget->child_list, link) {
1229 target = widget_find_widget(child, x, y);
1230 if (target)
1231 return target;
1232 }
1233
1234 if (widget->allocation.x <= x &&
1235 x < widget->allocation.x + widget->allocation.width &&
1236 widget->allocation.y <= y &&
1237 y < widget->allocation.y + widget->allocation.height) {
1238 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001239 }
1240
1241 return NULL;
1242}
1243
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001244static struct widget *
1245widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001246{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001247 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001248
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001249 widget = malloc(sizeof *widget);
1250 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001251 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001252 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001253 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001254 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001255 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001256 widget->tooltip = NULL;
1257 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001258
1259 return widget;
1260}
1261
1262struct widget *
1263window_add_widget(struct window *window, void *data)
1264{
1265 window->widget = widget_create(window, data);
1266 wl_list_init(&window->widget->link);
1267
1268 return window->widget;
1269}
1270
1271struct widget *
1272widget_add_widget(struct widget *parent, void *data)
1273{
1274 struct widget *widget;
1275
1276 widget = widget_create(parent->window, data);
1277 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001278
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001279 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001280}
1281
1282void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001283widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001284{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001285 struct display *display = widget->window->display;
1286 struct input *input;
1287
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001288 if (widget->tooltip) {
1289 free(widget->tooltip);
1290 widget->tooltip = NULL;
1291 }
1292
Pekka Paalanene156fb62012-01-19 13:51:38 +02001293 wl_list_for_each(input, &display->input_list, link) {
1294 if (input->focus_widget == widget)
1295 input->focus_widget = NULL;
1296 }
1297
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001298 wl_list_remove(&widget->link);
1299 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001300}
1301
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001302void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001303widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001304{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001305 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001306}
1307
1308void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001309widget_set_size(struct widget *widget, int32_t width, int32_t height)
1310{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001311 widget->allocation.width = width;
1312 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001313}
1314
1315void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001316widget_set_allocation(struct widget *widget,
1317 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001318{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001319 widget->allocation.x = x;
1320 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001321 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001322}
1323
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001324void
1325widget_set_transparent(struct widget *widget, int transparent)
1326{
1327 widget->opaque = !transparent;
1328}
1329
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001330void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001331widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001332{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001333 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001334}
1335
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001336void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001337widget_set_resize_handler(struct widget *widget,
1338 widget_resize_handler_t handler)
1339{
1340 widget->resize_handler = handler;
1341}
1342
1343void
1344widget_set_redraw_handler(struct widget *widget,
1345 widget_redraw_handler_t handler)
1346{
1347 widget->redraw_handler = handler;
1348}
1349
1350void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001351widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001352{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001353 widget->enter_handler = handler;
1354}
1355
1356void
1357widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1358{
1359 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001360}
1361
1362void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001363widget_set_motion_handler(struct widget *widget,
1364 widget_motion_handler_t handler)
1365{
1366 widget->motion_handler = handler;
1367}
1368
1369void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001370widget_set_button_handler(struct widget *widget,
1371 widget_button_handler_t handler)
1372{
1373 widget->button_handler = handler;
1374}
1375
1376void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001377widget_set_axis_handler(struct widget *widget,
1378 widget_axis_handler_t handler)
1379{
1380 widget->axis_handler = handler;
1381}
1382
1383void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001384widget_schedule_redraw(struct widget *widget)
1385{
1386 window_schedule_redraw(widget->window);
1387}
1388
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001389cairo_surface_t *
1390window_get_surface(struct window *window)
1391{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001392 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001393}
1394
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001395struct wl_surface *
1396window_get_wl_surface(struct window *window)
1397{
1398 return window->surface;
1399}
1400
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001401struct wl_shell_surface *
1402window_get_wl_shell_surface(struct window *window)
1403{
1404 return window->shell_surface;
1405}
1406
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001407static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001408tooltip_redraw_handler(struct widget *widget, void *data)
1409{
1410 cairo_t *cr;
1411 const int32_t r = 3;
1412 struct tooltip *tooltip = data;
1413 int32_t width, height;
1414 struct window *window = widget->window;
1415
1416 cr = cairo_create(window->cairo_surface);
1417 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1418 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1419 cairo_paint(cr);
1420
1421 width = window->allocation.width;
1422 height = window->allocation.height;
1423 rounded_rect(cr, 0, 0, width, height, r);
1424
1425 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1426 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1427 cairo_fill(cr);
1428
1429 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1430 cairo_move_to(cr, 10, 16);
1431 cairo_show_text(cr, tooltip->entry);
1432 cairo_destroy(cr);
1433}
1434
1435static cairo_text_extents_t
1436get_text_extents(struct tooltip *tooltip)
1437{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001438 cairo_t *cr;
1439 cairo_text_extents_t extents;
1440
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001441 /* Use the dummy_surface because tooltip's surface was not
1442 * created yet, and parent does not have a valid surface
1443 * outside repaint, either.
1444 */
1445 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001446 cairo_text_extents(cr, tooltip->entry, &extents);
1447 cairo_destroy(cr);
1448
1449 return extents;
1450}
1451
1452static int
1453window_create_tooltip(struct tooltip *tooltip)
1454{
1455 struct widget *parent = tooltip->parent;
1456 struct display *display = parent->window->display;
1457 struct window *window;
1458 const int offset_y = 27;
1459 const int margin = 3;
1460 cairo_text_extents_t extents;
1461
1462 if (tooltip->widget)
1463 return 0;
1464
1465 window = window_create_transient(display, parent->window, tooltip->x,
1466 tooltip->y + offset_y,
1467 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1468 if (!window)
1469 return -1;
1470
1471 tooltip->window = window;
1472 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1473
1474 extents = get_text_extents(tooltip);
1475 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1476 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1477
1478 return 0;
1479}
1480
1481void
1482widget_destroy_tooltip(struct widget *parent)
1483{
1484 struct tooltip *tooltip = parent->tooltip;
1485
1486 parent->tooltip_count = 0;
1487 if (!tooltip)
1488 return;
1489
1490 if (tooltip->widget) {
1491 widget_destroy(tooltip->widget);
1492 window_destroy(tooltip->window);
1493 tooltip->widget = NULL;
1494 tooltip->window = NULL;
1495 }
1496
1497 close(tooltip->tooltip_fd);
1498 free(tooltip->entry);
1499 free(tooltip);
1500 parent->tooltip = NULL;
1501}
1502
1503static void
1504tooltip_func(struct task *task, uint32_t events)
1505{
1506 struct tooltip *tooltip =
1507 container_of(task, struct tooltip, tooltip_task);
1508 uint64_t exp;
1509
Martin Olsson8df662a2012-07-08 03:03:47 +02001510 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1511 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001512 window_create_tooltip(tooltip);
1513}
1514
1515#define TOOLTIP_TIMEOUT 500
1516static int
1517tooltip_timer_reset(struct tooltip *tooltip)
1518{
1519 struct itimerspec its;
1520
1521 its.it_interval.tv_sec = 0;
1522 its.it_interval.tv_nsec = 0;
1523 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1524 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1525 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1526 fprintf(stderr, "could not set timerfd\n: %m");
1527 return -1;
1528 }
1529
1530 return 0;
1531}
1532
1533int
1534widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1535{
1536 struct tooltip *tooltip = parent->tooltip;
1537
1538 parent->tooltip_count++;
1539 if (tooltip) {
1540 tooltip->x = x;
1541 tooltip->y = y;
1542 tooltip_timer_reset(tooltip);
1543 return 0;
1544 }
1545
1546 /* the handler might be triggered too fast via input device motion, so
1547 * we need this check here to make sure tooltip is fully initialized */
1548 if (parent->tooltip_count > 1)
1549 return 0;
1550
1551 tooltip = malloc(sizeof *tooltip);
1552 if (!tooltip)
1553 return -1;
1554
1555 parent->tooltip = tooltip;
1556 tooltip->parent = parent;
1557 tooltip->widget = NULL;
1558 tooltip->window = NULL;
1559 tooltip->x = x;
1560 tooltip->y = y;
1561 tooltip->entry = strdup(entry);
1562 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1563 if (tooltip->tooltip_fd < 0) {
1564 fprintf(stderr, "could not create timerfd\n: %m");
1565 return -1;
1566 }
1567
1568 tooltip->tooltip_task.run = tooltip_func;
1569 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1570 EPOLLIN, &tooltip->tooltip_task);
1571 tooltip_timer_reset(tooltip);
1572
1573 return 0;
1574}
1575
1576static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001577workspace_manager_state(void *data,
1578 struct workspace_manager *workspace_manager,
1579 uint32_t current,
1580 uint32_t count)
1581{
1582 struct display *display = data;
1583
1584 display->workspace = current;
1585 display->workspace_count = count;
1586}
1587
1588static const struct workspace_manager_listener workspace_manager_listener = {
1589 workspace_manager_state
1590};
1591
1592static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593frame_resize_handler(struct widget *widget,
1594 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001595{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001596 struct frame *frame = data;
1597 struct widget *child = frame->child;
1598 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001599 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001600 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001601 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001602 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001604 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001605
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001606 switch (widget->window->type) {
1607 case TYPE_FULLSCREEN:
1608 decoration_width = 0;
1609 decoration_height = 0;
1610
1611 allocation.x = 0;
1612 allocation.y = 0;
1613 allocation.width = width;
1614 allocation.height = height;
1615 opaque_margin = 0;
1616
1617 wl_list_for_each(button, &frame->buttons_list, link)
1618 button->widget->opaque = 1;
1619 break;
1620 case TYPE_MAXIMIZED:
1621 decoration_width = t->width * 2;
1622 decoration_height = t->width + t->titlebar_height;
1623
1624 allocation.x = t->width;
1625 allocation.y = t->titlebar_height;
1626 allocation.width = width - decoration_width;
1627 allocation.height = height - decoration_height;
1628
1629 opaque_margin = 0;
1630
1631 wl_list_for_each(button, &frame->buttons_list, link)
1632 button->widget->opaque = 0;
1633 break;
1634 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001635 decoration_width = (t->width + t->margin) * 2;
1636 decoration_height = t->width +
1637 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001638
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001639 allocation.x = t->width + t->margin;
1640 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001641 allocation.width = width - decoration_width;
1642 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001643
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001644 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001645
1646 wl_list_for_each(button, &frame->buttons_list, link)
1647 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001648 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001649 }
1650
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001651 widget_set_allocation(child, allocation.x, allocation.y,
1652 allocation.width, allocation.height);
1653
1654 if (child->resize_handler)
1655 child->resize_handler(child,
1656 allocation.width,
1657 allocation.height,
1658 child->user_data);
1659
Scott Moreauf7e498c2012-05-14 11:39:29 -06001660 width = child->allocation.width + decoration_width;
1661 height = child->allocation.height + decoration_height;
1662
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001663 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1664
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001665 widget->window->input_region =
1666 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001667 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001668 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001669 shadow_margin, shadow_margin,
1670 width - 2 * shadow_margin,
1671 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001672 } else {
1673 wl_region_add(widget->window->input_region,
1674 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001675 }
1676
Scott Moreauf7e498c2012-05-14 11:39:29 -06001677 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001678
1679 if (child->opaque) {
1680 widget->window->opaque_region =
1681 wl_compositor_create_region(display->compositor);
1682 wl_region_add(widget->window->opaque_region,
1683 opaque_margin, opaque_margin,
1684 widget->allocation.width - 2 * opaque_margin,
1685 widget->allocation.height - 2 * opaque_margin);
1686 }
Martin Minarik1998b152012-05-10 02:04:35 +02001687
1688 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001689 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1690 x_l = t->width + shadow_margin;
1691 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001692 wl_list_for_each(button, &frame->buttons_list, link) {
1693 const int button_padding = 4;
1694 w = cairo_image_surface_get_width(button->icon);
1695 h = cairo_image_surface_get_height(button->icon);
1696
1697 if (button->decoration == FRAME_BUTTON_FANCY)
1698 w += 10;
1699
1700 if (button->align == FRAME_BUTTON_LEFT) {
1701 widget_set_allocation(button->widget,
1702 x_l, y , w + 1, h + 1);
1703 x_l += w;
1704 x_l += button_padding;
1705 } else {
1706 x_r -= w;
1707 widget_set_allocation(button->widget,
1708 x_r, y , w + 1, h + 1);
1709 x_r -= button_padding;
1710 }
1711 }
1712}
1713
1714static int
1715frame_button_enter_handler(struct widget *widget,
1716 struct input *input, float x, float y, void *data)
1717{
1718 struct frame_button *frame_button = data;
1719
1720 widget_schedule_redraw(frame_button->widget);
1721 frame_button->state = FRAME_BUTTON_OVER;
1722
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001723 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001724}
1725
1726static void
1727frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1728{
1729 struct frame_button *frame_button = data;
1730
1731 widget_schedule_redraw(frame_button->widget);
1732 frame_button->state = FRAME_BUTTON_DEFAULT;
1733}
1734
1735static void
1736frame_button_button_handler(struct widget *widget,
1737 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001738 uint32_t button,
1739 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001740{
1741 struct frame_button *frame_button = data;
1742 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001743 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001744
1745 if (button != BTN_LEFT)
1746 return;
1747
1748 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001749 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001750 frame_button->state = FRAME_BUTTON_ACTIVE;
1751 widget_schedule_redraw(frame_button->widget);
1752
1753 if (frame_button->type == FRAME_BUTTON_ICON)
1754 window_show_frame_menu(window, input, time);
1755 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001756 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001757 frame_button->state = FRAME_BUTTON_DEFAULT;
1758 widget_schedule_redraw(frame_button->widget);
1759 break;
1760 }
1761
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001762 if (!was_pressed)
1763 return;
1764
Martin Minarik1998b152012-05-10 02:04:35 +02001765 switch (frame_button->type) {
1766 case FRAME_BUTTON_CLOSE:
1767 if (window->close_handler)
1768 window->close_handler(window->parent,
1769 window->user_data);
1770 else
1771 display_exit(window->display);
1772 break;
1773 case FRAME_BUTTON_MINIMIZE:
1774 fprintf(stderr,"Minimize stub\n");
1775 break;
1776 case FRAME_BUTTON_MAXIMIZE:
1777 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1778 break;
1779 default:
1780 /* Unknown operation */
1781 break;
1782 }
1783}
1784
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001785static int
1786frame_button_motion_handler(struct widget *widget,
1787 struct input *input, uint32_t time,
1788 float x, float y, void *data)
1789{
1790 struct frame_button *frame_button = data;
1791 enum frame_button_pointer previous_button_state = frame_button->state;
1792
1793 /* only track state for a pressed button */
1794 if (input->grab != widget)
1795 return CURSOR_LEFT_PTR;
1796
1797 if (x > widget->allocation.x &&
1798 x < (widget->allocation.x + widget->allocation.width) &&
1799 y > widget->allocation.y &&
1800 y < (widget->allocation.y + widget->allocation.height)) {
1801 frame_button->state = FRAME_BUTTON_ACTIVE;
1802 } else {
1803 frame_button->state = FRAME_BUTTON_DEFAULT;
1804 }
1805
1806 if (frame_button->state != previous_button_state)
1807 widget_schedule_redraw(frame_button->widget);
1808
1809 return CURSOR_LEFT_PTR;
1810}
1811
Martin Minarik1998b152012-05-10 02:04:35 +02001812static void
1813frame_button_redraw_handler(struct widget *widget, void *data)
1814{
1815 struct frame_button *frame_button = data;
1816 cairo_t *cr;
1817 int width, height, x, y;
1818 struct window *window = widget->window;
1819
1820 x = widget->allocation.x;
1821 y = widget->allocation.y;
1822 width = widget->allocation.width;
1823 height = widget->allocation.height;
1824
1825 if (!width)
1826 return;
1827 if (!height)
1828 return;
1829 if (widget->opaque)
1830 return;
1831
1832 cr = cairo_create(window->cairo_surface);
1833
1834 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1835 cairo_set_line_width(cr, 1);
1836
1837 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1838 cairo_rectangle (cr, x, y, 25, 16);
1839
1840 cairo_stroke_preserve(cr);
1841
1842 switch (frame_button->state) {
1843 case FRAME_BUTTON_DEFAULT:
1844 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1845 break;
1846 case FRAME_BUTTON_OVER:
1847 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1848 break;
1849 case FRAME_BUTTON_ACTIVE:
1850 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1851 break;
1852 }
1853
1854 cairo_fill (cr);
1855
1856 x += 4;
1857 }
1858
1859 cairo_set_source_surface(cr, frame_button->icon, x, y);
1860 cairo_paint(cr);
1861
1862 cairo_destroy(cr);
1863}
1864
1865static struct widget *
1866frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1867 enum frame_button_align align, enum frame_button_decoration style)
1868{
1869 struct frame_button *frame_button;
1870 const char *icon = data;
1871
1872 frame_button = malloc (sizeof *frame_button);
1873 memset(frame_button, 0, sizeof *frame_button);
1874
1875 frame_button->icon = cairo_image_surface_create_from_png(icon);
1876 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1877 frame_button->frame = frame;
1878 frame_button->type = type;
1879 frame_button->align = align;
1880 frame_button->decoration = style;
1881
1882 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1883
1884 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1885 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1886 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1887 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001888 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001889 return frame_button->widget;
1890}
1891
1892static void
1893frame_button_destroy(struct frame_button *frame_button)
1894{
1895 widget_destroy(frame_button->widget);
1896 wl_list_remove(&frame_button->link);
1897 cairo_surface_destroy(frame_button->icon);
1898 free(frame_button);
1899
1900 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001901}
1902
1903static void
1904frame_redraw_handler(struct widget *widget, void *data)
1905{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001906 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001907 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001908 struct theme *t = window->display->theme;
1909 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001910
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001911 if (window->type == TYPE_FULLSCREEN)
1912 return;
1913
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001914 cr = cairo_create(window->cairo_surface);
1915
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001916 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001917 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001918 if (window->type == TYPE_MAXIMIZED)
1919 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001920 theme_render_frame(t, cr, widget->allocation.width,
1921 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001922
1923 cairo_destroy(cr);
1924}
1925
1926static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001927frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001928{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001929 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001930 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001931 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001932
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001933 location = theme_get_location(t, input->sx, input->sy,
1934 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001935 frame->widget->allocation.height,
1936 window->type == TYPE_MAXIMIZED ?
1937 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001938
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001939 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001940 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001941 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001942 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001943 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001944 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001945 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001946 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001947 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001948 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001949 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001950 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001951 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001952 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001953 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001954 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001955 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001956 case THEME_LOCATION_EXTERIOR:
1957 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001958 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001959 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001960 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001961}
1962
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001963static void
1964frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001965{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001966 struct display *display;
1967
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001968 switch (index) {
1969 case 0: /* close */
1970 if (window->close_handler)
1971 window->close_handler(window->parent,
1972 window->user_data);
1973 else
1974 display_exit(window->display);
1975 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001976 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001977 display = window->display;
1978 if (display->workspace > 0)
1979 workspace_manager_move_surface(display->workspace_manager,
1980 window->surface,
1981 display->workspace - 1);
1982 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001983 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001984 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001985 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001986 workspace_manager_move_surface(display->workspace_manager,
1987 window->surface,
1988 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05001989 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001990 case 3: /* fullscreen */
1991 /* we don't have a way to get out of fullscreen for now */
1992 if (window->fullscreen_handler)
1993 window->fullscreen_handler(window, window->user_data);
1994 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001995 }
1996}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001997
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001998void
1999window_show_frame_menu(struct window *window,
2000 struct input *input, uint32_t time)
2001{
2002 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002003 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002004
2005 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002006 "Close",
2007 "Move to workspace above", "Move to workspace below",
2008 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002009 };
2010
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002011 if (window->fullscreen_handler)
2012 count = ARRAY_LENGTH(entries);
2013 else
2014 count = ARRAY_LENGTH(entries) - 1;
2015
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002016 input_get_position(input, &x, &y);
2017 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002018 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002019}
2020
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002021static int
2022frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002023 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002024{
2025 return frame_get_pointer_image_for_location(data, input);
2026}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002027
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002028static int
2029frame_motion_handler(struct widget *widget,
2030 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002031 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002032{
2033 return frame_get_pointer_image_for_location(data, input);
2034}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002035
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002036static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002037frame_button_handler(struct widget *widget,
2038 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002039 uint32_t button, enum wl_pointer_button_state state,
2040 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002041
2042{
2043 struct frame *frame = data;
2044 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002045 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002046 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002047
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002048 location = theme_get_location(display->theme, input->sx, input->sy,
2049 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002050 frame->widget->allocation.height,
2051 window->type == TYPE_MAXIMIZED ?
2052 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002053
Daniel Stone4dbadb12012-05-30 16:31:51 +01002054 if (window->display->shell && button == BTN_LEFT &&
2055 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002056 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002057 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002058 if (!window->shell_surface)
2059 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002060 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002061 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002062 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002063 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002064 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002065 case THEME_LOCATION_RESIZING_TOP:
2066 case THEME_LOCATION_RESIZING_BOTTOM:
2067 case THEME_LOCATION_RESIZING_LEFT:
2068 case THEME_LOCATION_RESIZING_RIGHT:
2069 case THEME_LOCATION_RESIZING_TOP_LEFT:
2070 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2071 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2072 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002073 if (!window->shell_surface)
2074 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002075 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002076
Pekka Paalanen99436862012-11-19 17:15:59 +02002077 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002078 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002079 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002080 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002081 break;
2082 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002083 } else if (button == BTN_RIGHT &&
2084 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002085 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002086 }
2087}
2088
2089struct widget *
2090frame_create(struct window *window, void *data)
2091{
2092 struct frame *frame;
2093
2094 frame = malloc(sizeof *frame);
2095 memset(frame, 0, sizeof *frame);
2096
2097 frame->widget = window_add_widget(window, frame);
2098 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002099
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002100 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2101 widget_set_resize_handler(frame->widget, frame_resize_handler);
2102 widget_set_enter_handler(frame->widget, frame_enter_handler);
2103 widget_set_motion_handler(frame->widget, frame_motion_handler);
2104 widget_set_button_handler(frame->widget, frame_button_handler);
2105
Martin Minarik1998b152012-05-10 02:04:35 +02002106 /* Create empty list for frame buttons */
2107 wl_list_init(&frame->buttons_list);
2108
2109 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2110 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2111
2112 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2113 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2114
2115 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2116 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2117
2118 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2119 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2120
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002121 window->frame = frame;
2122
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002123 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002124}
2125
Kristian Høgsberga1627922012-06-20 17:30:03 -04002126void
2127frame_set_child_size(struct widget *widget, int child_width, int child_height)
2128{
2129 struct display *display = widget->window->display;
2130 struct theme *t = display->theme;
2131 int decoration_width, decoration_height;
2132 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002133 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002134
2135 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002136 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002137 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002138 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002139
2140 width = child_width + decoration_width;
2141 height = child_height + decoration_height;
2142 } else {
2143 width = child_width;
2144 height = child_height;
2145 }
2146
2147 window_schedule_resize(widget->window, width, height);
2148}
2149
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002150static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002151frame_destroy(struct frame *frame)
2152{
Martin Minarik1998b152012-05-10 02:04:35 +02002153 struct frame_button *button, *tmp;
2154
2155 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2156 frame_button_destroy(button);
2157
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002158 /* frame->child must be destroyed by the application */
2159 widget_destroy(frame->widget);
2160 free(frame);
2161}
2162
2163static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002164input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002165 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002166{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002167 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002168 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002169
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002170 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002171 return;
2172
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002173 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002174 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002175 widget = old;
2176 if (input->grab)
2177 widget = input->grab;
2178 if (widget->leave_handler)
2179 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002180 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002181 }
2182
2183 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002184 widget = focus;
2185 if (input->grab)
2186 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002187 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002188 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002189 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002190 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002191
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002192 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002193 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002194}
2195
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002196void
2197input_grab(struct input *input, struct widget *widget, uint32_t button)
2198{
2199 input->grab = widget;
2200 input->grab_button = button;
2201}
2202
2203void
2204input_ungrab(struct input *input)
2205{
2206 struct widget *widget;
2207
2208 input->grab = NULL;
2209 if (input->pointer_focus) {
2210 widget = widget_find_widget(input->pointer_focus->widget,
2211 input->sx, input->sy);
2212 input_set_focus_widget(input, widget, input->sx, input->sy);
2213 }
2214}
2215
2216static void
2217input_remove_pointer_focus(struct input *input)
2218{
2219 struct window *window = input->pointer_focus;
2220
2221 if (!window)
2222 return;
2223
2224 input_set_focus_widget(input, NULL, 0, 0);
2225
2226 input->pointer_focus = NULL;
2227 input->current_cursor = CURSOR_UNSET;
2228}
2229
2230static void
2231pointer_handle_enter(void *data, struct wl_pointer *pointer,
2232 uint32_t serial, struct wl_surface *surface,
2233 wl_fixed_t sx_w, wl_fixed_t sy_w)
2234{
2235 struct input *input = data;
2236 struct window *window;
2237 struct widget *widget;
2238 float sx = wl_fixed_to_double(sx_w);
2239 float sy = wl_fixed_to_double(sy_w);
2240
2241 if (!surface) {
2242 /* enter event for a window we've just destroyed */
2243 return;
2244 }
2245
2246 input->display->serial = serial;
2247 input->pointer_enter_serial = serial;
2248 input->pointer_focus = wl_surface_get_user_data(surface);
2249 window = input->pointer_focus;
2250
Pekka Paalanen99436862012-11-19 17:15:59 +02002251 if (window->resizing) {
2252 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002253 /* Schedule a redraw to free the pool */
2254 window_schedule_redraw(window);
2255 }
2256
2257 input->sx = sx;
2258 input->sy = sy;
2259
2260 widget = widget_find_widget(window->widget, sx, sy);
2261 input_set_focus_widget(input, widget, sx, sy);
2262}
2263
2264static void
2265pointer_handle_leave(void *data, struct wl_pointer *pointer,
2266 uint32_t serial, struct wl_surface *surface)
2267{
2268 struct input *input = data;
2269
2270 input->display->serial = serial;
2271 input_remove_pointer_focus(input);
2272}
2273
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002274static void
Daniel Stone37816df2012-05-16 18:45:18 +01002275pointer_handle_motion(void *data, struct wl_pointer *pointer,
2276 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002277{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002278 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002279 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002280 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002281 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002282 float sx = wl_fixed_to_double(sx_w);
2283 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002284
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002285 input->sx = sx;
2286 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002287
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002288 if (!window)
2289 return;
2290
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002291 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002292 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002293 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002294 }
2295
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002296 if (input->grab)
2297 widget = input->grab;
2298 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002299 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002300 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002301 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002302 input, time, sx, sy,
2303 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002304
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002305 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002306}
2307
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002308static void
Daniel Stone37816df2012-05-16 18:45:18 +01002309pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002310 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002311{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002312 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002313 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002314 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002315
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002316 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002317 if (input->focus_widget && input->grab == NULL &&
2318 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002319 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002320
Neil Roberts6b28aad2012-01-23 19:11:18 +00002321 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002322 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002323 (*widget->button_handler)(widget,
2324 input, time,
2325 button, state,
2326 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002327
Daniel Stone4dbadb12012-05-30 16:31:51 +01002328 if (input->grab && input->grab_button == button &&
2329 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002330 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002331}
2332
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002333static void
Daniel Stone37816df2012-05-16 18:45:18 +01002334pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002335 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002336{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002337 struct input *input = data;
2338 struct widget *widget;
2339
2340 widget = input->focus_widget;
2341 if (input->grab)
2342 widget = input->grab;
2343 if (widget && widget->axis_handler)
2344 (*widget->axis_handler)(widget,
2345 input, time,
2346 axis, value,
2347 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002348}
2349
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002350static const struct wl_pointer_listener pointer_listener = {
2351 pointer_handle_enter,
2352 pointer_handle_leave,
2353 pointer_handle_motion,
2354 pointer_handle_button,
2355 pointer_handle_axis,
2356};
2357
2358static void
2359input_remove_keyboard_focus(struct input *input)
2360{
2361 struct window *window = input->keyboard_focus;
2362 struct itimerspec its;
2363
2364 its.it_interval.tv_sec = 0;
2365 its.it_interval.tv_nsec = 0;
2366 its.it_value.tv_sec = 0;
2367 its.it_value.tv_nsec = 0;
2368 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2369
2370 if (!window)
2371 return;
2372
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002373 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002374 if (window->keyboard_focus_handler)
2375 (*window->keyboard_focus_handler)(window, NULL,
2376 window->user_data);
2377
2378 input->keyboard_focus = NULL;
2379}
2380
2381static void
2382keyboard_repeat_func(struct task *task, uint32_t events)
2383{
2384 struct input *input =
2385 container_of(task, struct input, repeat_task);
2386 struct window *window = input->keyboard_focus;
2387 uint64_t exp;
2388
2389 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2390 /* If we change the timer between the fd becoming
2391 * readable and getting here, there'll be nothing to
2392 * read and we get EAGAIN. */
2393 return;
2394
2395 if (window && window->key_handler) {
2396 (*window->key_handler)(window, input, input->repeat_time,
2397 input->repeat_key, input->repeat_sym,
2398 WL_KEYBOARD_KEY_STATE_PRESSED,
2399 window->user_data);
2400 }
2401}
2402
2403static void
2404keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2405 uint32_t format, int fd, uint32_t size)
2406{
2407 struct input *input = data;
2408 char *map_str;
2409
2410 if (!data) {
2411 close(fd);
2412 return;
2413 }
2414
2415 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2416 close(fd);
2417 return;
2418 }
2419
2420 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2421 if (map_str == MAP_FAILED) {
2422 close(fd);
2423 return;
2424 }
2425
2426 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2427 map_str,
2428 XKB_KEYMAP_FORMAT_TEXT_V1,
2429 0);
2430 munmap(map_str, size);
2431 close(fd);
2432
2433 if (!input->xkb.keymap) {
2434 fprintf(stderr, "failed to compile keymap\n");
2435 return;
2436 }
2437
2438 input->xkb.state = xkb_state_new(input->xkb.keymap);
2439 if (!input->xkb.state) {
2440 fprintf(stderr, "failed to create XKB state\n");
2441 xkb_map_unref(input->xkb.keymap);
2442 input->xkb.keymap = NULL;
2443 return;
2444 }
2445
2446 input->xkb.control_mask =
2447 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2448 input->xkb.alt_mask =
2449 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2450 input->xkb.shift_mask =
2451 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2452}
2453
2454static void
2455keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2456 uint32_t serial, struct wl_surface *surface,
2457 struct wl_array *keys)
2458{
2459 struct input *input = data;
2460 struct window *window;
2461
2462 input->display->serial = serial;
2463 input->keyboard_focus = wl_surface_get_user_data(surface);
2464
2465 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002466 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002467 if (window->keyboard_focus_handler)
2468 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002469 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002470}
2471
2472static void
2473keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2474 uint32_t serial, struct wl_surface *surface)
2475{
2476 struct input *input = data;
2477
2478 input->display->serial = serial;
2479 input_remove_keyboard_focus(input);
2480}
2481
Scott Moreau210d0792012-03-22 10:47:01 -06002482static void
Daniel Stone37816df2012-05-16 18:45:18 +01002483keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002484 uint32_t serial, uint32_t time, uint32_t key,
2485 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002486{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002487 struct input *input = data;
2488 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002489 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002490 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002491 const xkb_keysym_t *syms;
2492 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002493 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002494
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002495 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002496 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002497 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002498 return;
2499
Daniel Stone97f68542012-05-30 16:32:01 +01002500 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002501
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002502 sym = XKB_KEY_NoSymbol;
2503 if (num_syms == 1)
2504 sym = syms[0];
2505
2506 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002507 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002508 window_set_maximized(window,
2509 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002510 } else if (sym == XKB_KEY_F11 &&
2511 window->fullscreen_handler &&
2512 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2513 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002514 } else if (sym == XKB_KEY_F4 &&
2515 input->modifiers == MOD_ALT_MASK &&
2516 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2517 if (window->close_handler)
2518 window->close_handler(window->parent,
2519 window->user_data);
2520 else
2521 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002522 } else if (window->key_handler) {
2523 (*window->key_handler)(window, input, time, key,
2524 sym, state, window->user_data);
2525 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002526
2527 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2528 key == input->repeat_key) {
2529 its.it_interval.tv_sec = 0;
2530 its.it_interval.tv_nsec = 0;
2531 its.it_value.tv_sec = 0;
2532 its.it_value.tv_nsec = 0;
2533 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2534 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2535 input->repeat_sym = sym;
2536 input->repeat_key = key;
2537 input->repeat_time = time;
2538 its.it_interval.tv_sec = 0;
2539 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2540 its.it_value.tv_sec = 0;
2541 its.it_value.tv_nsec = 400 * 1000 * 1000;
2542 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2543 }
2544}
2545
2546static void
Daniel Stone351eb612012-05-31 15:27:47 -04002547keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2548 uint32_t serial, uint32_t mods_depressed,
2549 uint32_t mods_latched, uint32_t mods_locked,
2550 uint32_t group)
2551{
2552 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002553 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002554
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002555 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2556 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002557 mask = xkb_state_serialize_mods(input->xkb.state,
2558 XKB_STATE_DEPRESSED |
2559 XKB_STATE_LATCHED);
2560 input->modifiers = 0;
2561 if (mask & input->xkb.control_mask)
2562 input->modifiers |= MOD_CONTROL_MASK;
2563 if (mask & input->xkb.alt_mask)
2564 input->modifiers |= MOD_ALT_MASK;
2565 if (mask & input->xkb.shift_mask)
2566 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002567}
2568
Daniel Stone37816df2012-05-16 18:45:18 +01002569static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002570 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002571 keyboard_handle_enter,
2572 keyboard_handle_leave,
2573 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002574 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002575};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002576
2577static void
Daniel Stone37816df2012-05-16 18:45:18 +01002578seat_handle_capabilities(void *data, struct wl_seat *seat,
2579 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002580{
Daniel Stone37816df2012-05-16 18:45:18 +01002581 struct input *input = data;
2582
2583 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2584 input->pointer = wl_seat_get_pointer(seat);
2585 wl_pointer_set_user_data(input->pointer, input);
2586 wl_pointer_add_listener(input->pointer, &pointer_listener,
2587 input);
2588 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2589 wl_pointer_destroy(input->pointer);
2590 input->pointer = NULL;
2591 }
2592
2593 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2594 input->keyboard = wl_seat_get_keyboard(seat);
2595 wl_keyboard_set_user_data(input->keyboard, input);
2596 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2597 input);
2598 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2599 wl_keyboard_destroy(input->keyboard);
2600 input->keyboard = NULL;
2601 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002602}
2603
Daniel Stone37816df2012-05-16 18:45:18 +01002604static const struct wl_seat_listener seat_listener = {
2605 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002606};
2607
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002608void
2609input_get_position(struct input *input, int32_t *x, int32_t *y)
2610{
2611 *x = input->sx;
2612 *y = input->sy;
2613}
2614
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002615struct display *
2616input_get_display(struct input *input)
2617{
2618 return input->display;
2619}
2620
Daniel Stone37816df2012-05-16 18:45:18 +01002621struct wl_seat *
2622input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002623{
Daniel Stone37816df2012-05-16 18:45:18 +01002624 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002625}
2626
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002627uint32_t
2628input_get_modifiers(struct input *input)
2629{
2630 return input->modifiers;
2631}
2632
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002633struct widget *
2634input_get_focus_widget(struct input *input)
2635{
2636 return input->focus_widget;
2637}
2638
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002639struct data_offer {
2640 struct wl_data_offer *offer;
2641 struct input *input;
2642 struct wl_array types;
2643 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002644
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002645 struct task io_task;
2646 int fd;
2647 data_func_t func;
2648 int32_t x, y;
2649 void *user_data;
2650};
2651
2652static void
2653data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2654{
2655 struct data_offer *offer = data;
2656 char **p;
2657
2658 p = wl_array_add(&offer->types, sizeof *p);
2659 *p = strdup(type);
2660}
2661
2662static const struct wl_data_offer_listener data_offer_listener = {
2663 data_offer_offer,
2664};
2665
2666static void
2667data_offer_destroy(struct data_offer *offer)
2668{
2669 char **p;
2670
2671 offer->refcount--;
2672 if (offer->refcount == 0) {
2673 wl_data_offer_destroy(offer->offer);
2674 for (p = offer->types.data; *p; p++)
2675 free(*p);
2676 wl_array_release(&offer->types);
2677 free(offer);
2678 }
2679}
2680
2681static void
2682data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002683 struct wl_data_device *data_device,
2684 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002685{
2686 struct data_offer *offer;
2687
2688 offer = malloc(sizeof *offer);
2689
2690 wl_array_init(&offer->types);
2691 offer->refcount = 1;
2692 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002693 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002694 wl_data_offer_add_listener(offer->offer,
2695 &data_offer_listener, offer);
2696}
2697
2698static void
2699data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002700 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002701 wl_fixed_t x_w, wl_fixed_t y_w,
2702 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002703{
2704 struct input *input = data;
2705 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002706 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002707 float x = wl_fixed_to_double(x_w);
2708 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002709 char **p;
2710
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002711 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002712 window = wl_surface_get_user_data(surface);
2713 input->pointer_focus = window;
2714
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002715 if (offer) {
2716 input->drag_offer = wl_data_offer_get_user_data(offer);
2717
2718 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2719 *p = NULL;
2720
2721 types_data = input->drag_offer->types.data;
2722 } else {
2723 input->drag_offer = NULL;
2724 types_data = NULL;
2725 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002726
2727 window = input->pointer_focus;
2728 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002729 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002730 window->user_data);
2731}
2732
2733static void
2734data_device_leave(void *data, struct wl_data_device *data_device)
2735{
2736 struct input *input = data;
2737
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002738 if (input->drag_offer) {
2739 data_offer_destroy(input->drag_offer);
2740 input->drag_offer = NULL;
2741 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002742}
2743
2744static void
2745data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002746 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002747{
2748 struct input *input = data;
2749 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002750 float x = wl_fixed_to_double(x_w);
2751 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002752 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002753
2754 input->sx = x;
2755 input->sy = y;
2756
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002757 if (input->drag_offer)
2758 types_data = input->drag_offer->types.data;
2759 else
2760 types_data = NULL;
2761
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002762 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002763 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002764 window->user_data);
2765}
2766
2767static void
2768data_device_drop(void *data, struct wl_data_device *data_device)
2769{
2770 struct input *input = data;
2771 struct window *window = input->pointer_focus;
2772
2773 if (window->drop_handler)
2774 window->drop_handler(window, input,
2775 input->sx, input->sy, window->user_data);
2776}
2777
2778static void
2779data_device_selection(void *data,
2780 struct wl_data_device *wl_data_device,
2781 struct wl_data_offer *offer)
2782{
2783 struct input *input = data;
2784 char **p;
2785
2786 if (input->selection_offer)
2787 data_offer_destroy(input->selection_offer);
2788
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002789 if (offer) {
2790 input->selection_offer = wl_data_offer_get_user_data(offer);
2791 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2792 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002793 } else {
2794 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002795 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002796}
2797
2798static const struct wl_data_device_listener data_device_listener = {
2799 data_device_data_offer,
2800 data_device_enter,
2801 data_device_leave,
2802 data_device_motion,
2803 data_device_drop,
2804 data_device_selection
2805};
2806
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002807static void
2808input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002809{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002810 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002811 struct wl_cursor *cursor;
2812 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002813
Daniel Stone80972742012-11-07 17:51:39 +11002814 if (!input->pointer)
2815 return;
2816
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002817 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002818 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002819 return;
2820
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002821 if (index >= (int) cursor->image_count) {
2822 fprintf(stderr, "cursor index out of range\n");
2823 return;
2824 }
2825
2826 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002827 buffer = wl_cursor_image_get_buffer(image);
2828 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002829 return;
2830
Kristian Høgsbergae277372012-08-01 09:41:08 -04002831 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002832 input->pointer_surface,
2833 image->hotspot_x, image->hotspot_y);
2834 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2835 wl_surface_damage(input->pointer_surface, 0, 0,
2836 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002837 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002838}
2839
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002840static const struct wl_callback_listener pointer_surface_listener;
2841
2842static void
2843pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2844 uint32_t time)
2845{
2846 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002847 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002848 int i;
2849
2850 if (callback) {
2851 assert(callback == input->cursor_frame_cb);
2852 wl_callback_destroy(callback);
2853 input->cursor_frame_cb = NULL;
2854 }
2855
Daniel Stone80972742012-11-07 17:51:39 +11002856 if (!input->pointer)
2857 return;
2858
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002859 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002860 wl_pointer_set_cursor(input->pointer,
2861 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002862 NULL, 0, 0);
2863 return;
2864 }
2865
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002866 if (input->current_cursor == CURSOR_UNSET)
2867 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002868 cursor = input->display->cursors[input->current_cursor];
2869 if (!cursor)
2870 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002871
2872 /* FIXME We don't have the current time on the first call so we set
2873 * the animation start to the time of the first frame callback. */
2874 if (time == 0)
2875 input->cursor_anim_start = 0;
2876 else if (input->cursor_anim_start == 0)
2877 input->cursor_anim_start = time;
2878
2879 if (time == 0 || input->cursor_anim_start == 0)
2880 i = 0;
2881 else
2882 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2883
Pekka Paalanenbc106382012-10-10 12:49:31 +03002884 if (cursor->image_count > 1) {
2885 input->cursor_frame_cb =
2886 wl_surface_frame(input->pointer_surface);
2887 wl_callback_add_listener(input->cursor_frame_cb,
2888 &pointer_surface_listener, input);
2889 }
2890
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002891 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002892}
2893
2894static const struct wl_callback_listener pointer_surface_listener = {
2895 pointer_surface_frame_callback
2896};
2897
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002898void
2899input_set_pointer_image(struct input *input, int pointer)
2900{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002901 int force = 0;
2902
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002903 if (!input->pointer)
2904 return;
2905
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002906 if (input->pointer_enter_serial > input->cursor_serial)
2907 force = 1;
2908
2909 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002910 return;
2911
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002912 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002913 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002914 if (!input->cursor_frame_cb)
2915 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002916 else if (force) {
2917 /* The current frame callback may be stuck if, for instance,
2918 * the set cursor request was processed by the server after
2919 * this client lost the focus. In this case the cursor surface
2920 * might not be mapped and the frame callback wouldn't ever
2921 * complete. Send a set_cursor and attach to try to map the
2922 * cursor surface again so that the callback will finish */
2923 input_set_pointer_image_index(input, 0);
2924 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002925}
2926
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002927struct wl_data_device *
2928input_get_data_device(struct input *input)
2929{
2930 return input->data_device;
2931}
2932
2933void
2934input_set_selection(struct input *input,
2935 struct wl_data_source *source, uint32_t time)
2936{
2937 wl_data_device_set_selection(input->data_device, source, time);
2938}
2939
2940void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002941input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002942{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002943 wl_data_offer_accept(input->drag_offer->offer,
2944 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002945}
2946
2947static void
2948offer_io_func(struct task *task, uint32_t events)
2949{
2950 struct data_offer *offer =
2951 container_of(task, struct data_offer, io_task);
2952 unsigned int len;
2953 char buffer[4096];
2954
2955 len = read(offer->fd, buffer, sizeof buffer);
2956 offer->func(buffer, len,
2957 offer->x, offer->y, offer->user_data);
2958
2959 if (len == 0) {
2960 close(offer->fd);
2961 data_offer_destroy(offer);
2962 }
2963}
2964
2965static void
2966data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2967 data_func_t func, void *user_data)
2968{
2969 int p[2];
2970
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002971 if (pipe2(p, O_CLOEXEC) == -1)
2972 return;
2973
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002974 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2975 close(p[1]);
2976
2977 offer->io_task.run = offer_io_func;
2978 offer->fd = p[0];
2979 offer->func = func;
2980 offer->refcount++;
2981 offer->user_data = user_data;
2982
2983 display_watch_fd(offer->input->display,
2984 offer->fd, EPOLLIN, &offer->io_task);
2985}
2986
2987void
2988input_receive_drag_data(struct input *input, const char *mime_type,
2989 data_func_t func, void *data)
2990{
2991 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2992 input->drag_offer->x = input->sx;
2993 input->drag_offer->y = input->sy;
2994}
2995
2996int
2997input_receive_selection_data(struct input *input, const char *mime_type,
2998 data_func_t func, void *data)
2999{
3000 char **p;
3001
3002 if (input->selection_offer == NULL)
3003 return -1;
3004
3005 for (p = input->selection_offer->types.data; *p; p++)
3006 if (strcmp(mime_type, *p) == 0)
3007 break;
3008
3009 if (*p == NULL)
3010 return -1;
3011
3012 data_offer_receive_data(input->selection_offer,
3013 mime_type, func, data);
3014 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003015}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003016
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003017int
3018input_receive_selection_data_to_fd(struct input *input,
3019 const char *mime_type, int fd)
3020{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003021 if (input->selection_offer)
3022 wl_data_offer_receive(input->selection_offer->offer,
3023 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003024
3025 return 0;
3026}
3027
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003028void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003029window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003030{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003031 if (!window->shell_surface)
3032 return;
3033
Daniel Stone37816df2012-05-16 18:45:18 +01003034 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003035}
3036
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003037static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003038idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003039{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003040 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003041
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003042 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003043 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003044 widget_set_allocation(widget,
3045 window->pending_allocation.x,
3046 window->pending_allocation.y,
3047 window->pending_allocation.width,
3048 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003049
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003050 if (window->input_region) {
3051 wl_region_destroy(window->input_region);
3052 window->input_region = NULL;
3053 }
3054
3055 if (window->opaque_region) {
3056 wl_region_destroy(window->opaque_region);
3057 window->opaque_region = NULL;
3058 }
3059
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003060 if (widget->resize_handler)
3061 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003062 widget->allocation.width,
3063 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003064 widget->user_data);
3065
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003066 if (window->allocation.width != widget->allocation.width ||
3067 window->allocation.height != widget->allocation.height) {
3068 window->allocation = widget->allocation;
3069 window_schedule_redraw(window);
3070 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003071}
3072
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003073void
3074window_schedule_resize(struct window *window, int width, int height)
3075{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003076 window->pending_allocation.x = 0;
3077 window->pending_allocation.y = 0;
3078 window->pending_allocation.width = width;
3079 window->pending_allocation.height = height;
3080
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003081 if (window->min_allocation.width == 0)
3082 window->min_allocation = window->pending_allocation;
3083 if (window->pending_allocation.width < window->min_allocation.width)
3084 window->pending_allocation.width = window->min_allocation.width;
3085 if (window->pending_allocation.height < window->min_allocation.height)
3086 window->pending_allocation.height = window->min_allocation.height;
3087
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003088 window->resize_needed = 1;
3089 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003090}
3091
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003092void
3093widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3094{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003095 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003096}
3097
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003098static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003099handle_ping(void *data, struct wl_shell_surface *shell_surface,
3100 uint32_t serial)
3101{
3102 wl_shell_surface_pong(shell_surface, serial);
3103}
3104
3105static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003106handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003107 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003108{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003109 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003110
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003111 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003112 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003113}
3114
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003115static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003116menu_destroy(struct menu *menu)
3117{
3118 widget_destroy(menu->widget);
3119 window_destroy(menu->window);
3120 free(menu);
3121}
3122
3123static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003124handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3125{
3126 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003127 struct menu *menu = window->widget->user_data;
3128
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003129 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003130 * device. Or just use wl_callback. And this really needs to
3131 * be a window vfunc that the menu can set. And we need the
3132 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003133
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003134 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003135 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003136 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003137}
3138
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003139static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003140 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003141 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003142 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003143};
3144
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003145void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003146window_get_allocation(struct window *window,
3147 struct rectangle *allocation)
3148{
3149 *allocation = window->allocation;
3150}
3151
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003152static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003153widget_redraw(struct widget *widget)
3154{
3155 struct widget *child;
3156
3157 if (widget->redraw_handler)
3158 widget->redraw_handler(widget, widget->user_data);
3159 wl_list_for_each(child, &widget->child_list, link)
3160 widget_redraw(child);
3161}
3162
3163static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003164frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3165{
3166 struct window *window = data;
3167
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003168 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003169 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003170 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003171 window->redraw_scheduled = 0;
3172 if (window->redraw_needed)
3173 window_schedule_redraw(window);
3174}
3175
3176static const struct wl_callback_listener listener = {
3177 frame_callback
3178};
3179
3180static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003181idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003182{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003183 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003184
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003185 if (window->resize_needed)
3186 idle_resize(window);
3187
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003188 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003189 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003190 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003191 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003192
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003193 window->frame_cb = wl_surface_frame(window->surface);
3194 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003195 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003196}
3197
3198void
3199window_schedule_redraw(struct window *window)
3200{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003201 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003202 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003203 window->redraw_task.run = idle_redraw;
3204 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003205 window->redraw_scheduled = 1;
3206 }
3207}
3208
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003209int
3210window_is_fullscreen(struct window *window)
3211{
3212 return window->type == TYPE_FULLSCREEN;
3213}
3214
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003215void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003216window_set_fullscreen(struct window *window, int fullscreen)
3217{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003218 if (!window->display->shell)
3219 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003220
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003221 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003222 return;
3223
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003224 if (fullscreen) {
3225 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003226 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003227 wl_shell_surface_set_fullscreen(window->shell_surface,
3228 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3229 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003230 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003231 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003232 wl_shell_surface_set_toplevel(window->shell_surface);
3233 window_schedule_resize(window,
3234 window->saved_allocation.width,
3235 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003236 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003237}
3238
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003239int
3240window_is_maximized(struct window *window)
3241{
3242 return window->type == TYPE_MAXIMIZED;
3243}
3244
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003245void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003246window_set_maximized(struct window *window, int maximized)
3247{
3248 if (!window->display->shell)
3249 return;
3250
3251 if ((window->type == TYPE_MAXIMIZED) == maximized)
3252 return;
3253
3254 if (window->type == TYPE_TOPLEVEL) {
3255 window->saved_allocation = window->allocation;
3256 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3257 window->type = TYPE_MAXIMIZED;
3258 } else {
3259 wl_shell_surface_set_toplevel(window->shell_surface);
3260 window->type = TYPE_TOPLEVEL;
3261 window_schedule_resize(window,
3262 window->saved_allocation.width,
3263 window->saved_allocation.height);
3264 }
3265}
3266
3267void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003268window_set_user_data(struct window *window, void *data)
3269{
3270 window->user_data = data;
3271}
3272
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003273void *
3274window_get_user_data(struct window *window)
3275{
3276 return window->user_data;
3277}
3278
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003279void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003280window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003281 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003282{
3283 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003284}
3285
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003286void
3287window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003288 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003289{
3290 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003291}
3292
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003293void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003294window_set_data_handler(struct window *window, window_data_handler_t handler)
3295{
3296 window->data_handler = handler;
3297}
3298
3299void
3300window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3301{
3302 window->drop_handler = handler;
3303}
3304
3305void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003306window_set_close_handler(struct window *window,
3307 window_close_handler_t handler)
3308{
3309 window->close_handler = handler;
3310}
3311
3312void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003313window_set_fullscreen_handler(struct window *window,
3314 window_fullscreen_handler_t handler)
3315{
3316 window->fullscreen_handler = handler;
3317}
3318
3319void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003320window_set_title(struct window *window, const char *title)
3321{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003322 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003323 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003324 if (window->shell_surface)
3325 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003326}
3327
3328const char *
3329window_get_title(struct window *window)
3330{
3331 return window->title;
3332}
3333
3334void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003335window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3336{
3337 struct text_cursor_position *text_cursor_position =
3338 window->display->text_cursor_position;
3339
Scott Moreau9295ce02012-06-01 12:46:10 -06003340 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003341 return;
3342
3343 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003344 window->surface,
3345 wl_fixed_from_int(x),
3346 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003347}
3348
3349void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003350window_damage(struct window *window, int32_t x, int32_t y,
3351 int32_t width, int32_t height)
3352{
3353 wl_surface_damage(window->surface, x, y, width, height);
3354}
3355
Casey Dahlin9074db52012-04-19 22:50:09 -04003356static void
3357surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003358 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003359{
Rob Bradford7507b572012-05-15 17:55:34 +01003360 struct window *window = data;
3361 struct output *output;
3362 struct output *output_found = NULL;
3363 struct window_output *window_output;
3364
3365 wl_list_for_each(output, &window->display->output_list, link) {
3366 if (output->output == wl_output) {
3367 output_found = output;
3368 break;
3369 }
3370 }
3371
3372 if (!output_found)
3373 return;
3374
3375 window_output = malloc (sizeof *window_output);
3376 window_output->output = output_found;
3377
3378 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003379}
3380
3381static void
3382surface_leave(void *data,
3383 struct wl_surface *wl_surface, struct wl_output *output)
3384{
Rob Bradford7507b572012-05-15 17:55:34 +01003385 struct window *window = data;
3386 struct window_output *window_output;
3387 struct window_output *window_output_found = NULL;
3388
3389 wl_list_for_each(window_output, &window->window_output_list, link) {
3390 if (window_output->output->output == output) {
3391 window_output_found = window_output;
3392 break;
3393 }
3394 }
3395
3396 if (window_output_found) {
3397 wl_list_remove(&window_output_found->link);
3398 free(window_output_found);
3399 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003400}
3401
3402static const struct wl_surface_listener surface_listener = {
3403 surface_enter,
3404 surface_leave
3405};
3406
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003407static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003408window_create_internal(struct display *display,
3409 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003410{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003411 struct window *window;
3412
3413 window = malloc(sizeof *window);
3414 if (window == NULL)
3415 return NULL;
3416
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003417 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003418 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003419 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003420 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003421 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003422 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003423 window->shell_surface =
3424 wl_shell_get_shell_surface(display->shell,
3425 window->surface);
3426 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003427 window->allocation.x = 0;
3428 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003429 window->allocation.width = 0;
3430 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003431 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003432 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003433 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003434 window->input_region = NULL;
3435 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003436
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003437 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003438#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003439 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003440#else
3441 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3442#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003443 else
3444 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003445
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003446 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003447 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003448 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003449
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003450 if (window->shell_surface) {
3451 wl_shell_surface_set_user_data(window->shell_surface, window);
3452 wl_shell_surface_add_listener(window->shell_surface,
3453 &shell_surface_listener, window);
3454 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003455
Rob Bradford7507b572012-05-15 17:55:34 +01003456 wl_list_init (&window->window_output_list);
3457
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003458 return window;
3459}
3460
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003461struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003462window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003463{
3464 struct window *window;
3465
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003466 window = window_create_internal(display, NULL, TYPE_NONE);
3467 if (!window)
3468 return NULL;
3469
3470 return window;
3471}
3472
3473struct window *
3474window_create_custom(struct display *display)
3475{
3476 struct window *window;
3477
3478 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003479 if (!window)
3480 return NULL;
3481
3482 return window;
3483}
3484
3485struct window *
3486window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003487 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003488{
3489 struct window *window;
3490
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003491 window = window_create_internal(parent->display,
3492 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003493 if (!window)
3494 return NULL;
3495
3496 window->x = x;
3497 window->y = y;
3498
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003499 if (display->shell)
3500 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003501 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003502 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003503
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003504 return window;
3505}
3506
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003507static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003508menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003509{
3510 int next;
3511
3512 next = (sy - 8) / 20;
3513 if (menu->current != next) {
3514 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003515 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003516 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003517}
3518
3519static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003520menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003521 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003522 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003523{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003524 struct menu *menu = data;
3525
3526 if (widget == menu->widget)
3527 menu_set_item(data, y);
3528
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003529 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003530}
3531
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003532static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003533menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003534 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003535{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003536 struct menu *menu = data;
3537
3538 if (widget == menu->widget)
3539 menu_set_item(data, y);
3540
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003541 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003542}
3543
3544static void
3545menu_leave_handler(struct widget *widget, struct input *input, void *data)
3546{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003547 struct menu *menu = data;
3548
3549 if (widget == menu->widget)
3550 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003551}
3552
3553static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003554menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003555 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003556 uint32_t button, enum wl_pointer_button_state state,
3557 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003558
3559{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003560 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003561
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003562 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3563 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003564 /* Either relase after press-drag-release or
3565 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003566 menu->func(menu->window->parent,
3567 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003568 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003569 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003570 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003571 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003572 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003573}
3574
3575static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003576menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003577{
3578 cairo_t *cr;
3579 const int32_t r = 3, margin = 3;
3580 struct menu *menu = data;
3581 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003582 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003583
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003584 cr = cairo_create(window->cairo_surface);
3585 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3586 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3587 cairo_paint(cr);
3588
3589 width = window->allocation.width;
3590 height = window->allocation.height;
3591 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003592
3593 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003594 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3595 cairo_fill(cr);
3596
3597 for (i = 0; i < menu->count; i++) {
3598 if (i == menu->current) {
3599 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3600 cairo_rectangle(cr, margin, i * 20 + margin,
3601 width - 2 * margin, 20);
3602 cairo_fill(cr);
3603 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3604 cairo_move_to(cr, 10, i * 20 + 16);
3605 cairo_show_text(cr, menu->entries[i]);
3606 } else {
3607 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3608 cairo_move_to(cr, 10, i * 20 + 16);
3609 cairo_show_text(cr, menu->entries[i]);
3610 }
3611 }
3612
3613 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003614}
3615
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003616void
3617window_show_menu(struct display *display,
3618 struct input *input, uint32_t time, struct window *parent,
3619 int32_t x, int32_t y,
3620 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003621{
3622 struct window *window;
3623 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003624 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003625
3626 menu = malloc(sizeof *menu);
3627 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003628 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003629
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003630 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003631 if (!window) {
3632 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003633 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003634 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003635
3636 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003637 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003638 menu->entries = entries;
3639 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003640 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003641 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003642 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003643 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003644 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003645 window->type = TYPE_MENU;
3646 window->x = x;
3647 window->y = y;
3648
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003649 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003650 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003651 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003652 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003653 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003654
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003655 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003656 widget_set_enter_handler(menu->widget, menu_enter_handler);
3657 widget_set_leave_handler(menu->widget, menu_leave_handler);
3658 widget_set_motion_handler(menu->widget, menu_motion_handler);
3659 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003660
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003661 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003662 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003663}
3664
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003665void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003666window_set_buffer_type(struct window *window, enum window_buffer_type type)
3667{
3668 window->buffer_type = type;
3669}
3670
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003671
3672static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003673display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003674 struct wl_output *wl_output,
3675 int x, int y,
3676 int physical_width,
3677 int physical_height,
3678 int subpixel,
3679 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003680 const char *model,
3681 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003682{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003683 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003684
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003685 output->allocation.x = x;
3686 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003687 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003688}
3689
3690static void
3691display_handle_mode(void *data,
3692 struct wl_output *wl_output,
3693 uint32_t flags,
3694 int width,
3695 int height,
3696 int refresh)
3697{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003698 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003699 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003700
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003701 if (flags & WL_OUTPUT_MODE_CURRENT) {
3702 output->allocation.width = width;
3703 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003704 if (display->output_configure_handler)
3705 (*display->output_configure_handler)(
3706 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003707 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003708}
3709
3710static const struct wl_output_listener output_listener = {
3711 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003712 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003713};
3714
3715static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003716display_add_output(struct display *d, uint32_t id)
3717{
3718 struct output *output;
3719
3720 output = malloc(sizeof *output);
3721 if (output == NULL)
3722 return;
3723
3724 memset(output, 0, sizeof *output);
3725 output->display = d;
3726 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003727 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003728 wl_list_insert(d->output_list.prev, &output->link);
3729
3730 wl_output_add_listener(output->output, &output_listener, output);
3731}
3732
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003733static void
3734output_destroy(struct output *output)
3735{
3736 if (output->destroy_handler)
3737 (*output->destroy_handler)(output, output->user_data);
3738
3739 wl_output_destroy(output->output);
3740 wl_list_remove(&output->link);
3741 free(output);
3742}
3743
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003744void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003745display_set_global_handler(struct display *display,
3746 display_global_handler_t handler)
3747{
3748 struct global *global;
3749
3750 display->global_handler = handler;
3751 if (!handler)
3752 return;
3753
3754 wl_list_for_each(global, &display->global_list, link)
3755 display->global_handler(display,
3756 global->name, global->interface,
3757 global->version, display->user_data);
3758}
3759
3760void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003761display_set_output_configure_handler(struct display *display,
3762 display_output_handler_t handler)
3763{
3764 struct output *output;
3765
3766 display->output_configure_handler = handler;
3767 if (!handler)
3768 return;
3769
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003770 wl_list_for_each(output, &display->output_list, link) {
3771 if (output->allocation.width == 0 &&
3772 output->allocation.height == 0)
3773 continue;
3774
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003775 (*display->output_configure_handler)(output,
3776 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003777 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003778}
3779
3780void
3781output_set_user_data(struct output *output, void *data)
3782{
3783 output->user_data = data;
3784}
3785
3786void *
3787output_get_user_data(struct output *output)
3788{
3789 return output->user_data;
3790}
3791
3792void
3793output_set_destroy_handler(struct output *output,
3794 display_output_handler_t handler)
3795{
3796 output->destroy_handler = handler;
3797 /* FIXME: implement this, once we have way to remove outputs */
3798}
3799
3800void
Scott Moreau4e072362012-09-29 02:03:11 -06003801output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003802{
Scott Moreau4e072362012-09-29 02:03:11 -06003803 struct rectangle allocation = output->allocation;
3804
3805 switch (output->transform) {
3806 case WL_OUTPUT_TRANSFORM_90:
3807 case WL_OUTPUT_TRANSFORM_270:
3808 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3809 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3810 /* Swap width and height */
3811 allocation.width = output->allocation.height;
3812 allocation.height = output->allocation.width;
3813 break;
3814 }
3815
3816 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003817}
3818
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003819struct wl_output *
3820output_get_wl_output(struct output *output)
3821{
3822 return output->output;
3823}
3824
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003825static void
Daniel Stone97f68542012-05-30 16:32:01 +01003826fini_xkb(struct input *input)
3827{
3828 xkb_state_unref(input->xkb.state);
3829 xkb_map_unref(input->xkb.keymap);
3830}
3831
3832static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003833display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003834{
3835 struct input *input;
3836
3837 input = malloc(sizeof *input);
3838 if (input == NULL)
3839 return;
3840
3841 memset(input, 0, sizeof *input);
3842 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003843 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003844 input->pointer_focus = NULL;
3845 input->keyboard_focus = NULL;
3846 wl_list_insert(d->input_list.prev, &input->link);
3847
Daniel Stone37816df2012-05-16 18:45:18 +01003848 wl_seat_add_listener(input->seat, &seat_listener, input);
3849 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003850
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003851 input->data_device =
3852 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003853 input->seat);
3854 wl_data_device_add_listener(input->data_device, &data_device_listener,
3855 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003856
3857 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003858
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003859 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3860 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003861 input->repeat_task.run = keyboard_repeat_func;
3862 display_watch_fd(d, input->repeat_timer_fd,
3863 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003864}
3865
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003866static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003867input_destroy(struct input *input)
3868{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003869 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003870 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003871
3872 if (input->drag_offer)
3873 data_offer_destroy(input->drag_offer);
3874
3875 if (input->selection_offer)
3876 data_offer_destroy(input->selection_offer);
3877
3878 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003879 fini_xkb(input);
3880
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003881 wl_surface_destroy(input->pointer_surface);
3882
Pekka Paalanene1207c72011-12-16 12:02:09 +02003883 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003884 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003885 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003886 free(input);
3887}
3888
3889static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003890init_workspace_manager(struct display *d, uint32_t id)
3891{
3892 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003893 wl_registry_bind(d->registry, id,
3894 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003895 if (d->workspace_manager != NULL)
3896 workspace_manager_add_listener(d->workspace_manager,
3897 &workspace_manager_listener,
3898 d);
3899}
3900
3901static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003902registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3903 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003904{
3905 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003906 struct global *global;
3907
3908 global = malloc(sizeof *global);
3909 global->name = id;
3910 global->interface = strdup(interface);
3911 global->version = version;
3912 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003913
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003914 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003915 d->compositor = wl_registry_bind(registry, id,
3916 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003917 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003918 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003919 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003920 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003921 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003922 d->shell = wl_registry_bind(registry,
3923 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003924 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003925 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003926 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3927 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003928 wl_registry_bind(registry, id,
3929 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003930 } else if (strcmp(interface, "text_cursor_position") == 0) {
3931 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003932 wl_registry_bind(registry, id,
3933 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003934 } else if (strcmp(interface, "workspace_manager") == 0) {
3935 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003936 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003937
3938 if (d->global_handler)
3939 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003940}
3941
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003942void *
3943display_bind(struct display *display, uint32_t name,
3944 const struct wl_interface *interface, uint32_t version)
3945{
3946 return wl_registry_bind(display->registry, name, interface, version);
3947}
3948
3949static const struct wl_registry_listener registry_listener = {
3950 registry_handle_global
3951};
3952
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003953#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003954static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003955init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003956{
3957 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003958 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003959
Rob Clark6396ed32012-03-11 19:48:41 -05003960#ifdef USE_CAIRO_GLESV2
3961# define GL_BIT EGL_OPENGL_ES2_BIT
3962#else
3963# define GL_BIT EGL_OPENGL_BIT
3964#endif
3965
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003966 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04003967 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003968 EGL_RED_SIZE, 1,
3969 EGL_GREEN_SIZE, 1,
3970 EGL_BLUE_SIZE, 1,
3971 EGL_ALPHA_SIZE, 1,
3972 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003973 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003974 EGL_NONE
3975 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003976
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003977#ifdef USE_CAIRO_GLESV2
3978 static const EGLint context_attribs[] = {
3979 EGL_CONTEXT_CLIENT_VERSION, 2,
3980 EGL_NONE
3981 };
3982 EGLint api = EGL_OPENGL_ES_API;
3983#else
3984 EGLint *context_attribs = NULL;
3985 EGLint api = EGL_OPENGL_API;
3986#endif
3987
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003988 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003989 if (!eglInitialize(d->dpy, &major, &minor)) {
3990 fprintf(stderr, "failed to initialize display\n");
3991 return -1;
3992 }
3993
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003994 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003995 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3996 return -1;
3997 }
3998
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003999 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4000 &d->argb_config, 1, &n) || n != 1) {
4001 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004002 return -1;
4003 }
4004
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004005 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004006 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004007 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004008 fprintf(stderr, "failed to create context\n");
4009 return -1;
4010 }
4011
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004012 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01004013 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004014 return -1;
4015 }
4016
Benjamin Franzke0c991632011-09-27 21:57:31 +02004017 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4018 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4019 fprintf(stderr, "failed to get cairo egl argb device\n");
4020 return -1;
4021 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004022
4023 return 0;
4024}
4025
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004026static void
4027fini_egl(struct display *display)
4028{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004029 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004030
4031 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4032 EGL_NO_CONTEXT);
4033
4034 eglTerminate(display->dpy);
4035 eglReleaseThread();
4036}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004037#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004038
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004039static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004040init_dummy_surface(struct display *display)
4041{
4042 int len;
4043 void *data;
4044
4045 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4046 data = malloc(len);
4047 display->dummy_surface =
4048 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4049 1, 1, len);
4050 display->dummy_surface_data = data;
4051}
4052
4053static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004054handle_display_data(struct task *task, uint32_t events)
4055{
4056 struct display *display =
4057 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004058 struct epoll_event ep;
4059 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004060
4061 display->display_fd_events = events;
4062
4063 if (events & EPOLLERR || events & EPOLLHUP) {
4064 display_exit(display);
4065 return;
4066 }
4067
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004068 if (events & EPOLLIN) {
4069 ret = wl_display_dispatch(display->display);
4070 if (ret == -1) {
4071 display_exit(display);
4072 return;
4073 }
4074 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004075
4076 if (events & EPOLLOUT) {
4077 ret = wl_display_flush(display->display);
4078 if (ret == 0) {
4079 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4080 ep.data.ptr = &display->display_task;
4081 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4082 display->display_fd, &ep);
4083 } else if (ret == -1 && errno != EAGAIN) {
4084 display_exit(display);
4085 return;
4086 }
4087 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004088}
4089
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004090struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004091display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004092{
4093 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004094
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004095 d = malloc(sizeof *d);
4096 if (d == NULL)
4097 return NULL;
4098
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004099 memset(d, 0, sizeof *d);
4100
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004101 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004102 if (d->display == NULL) {
4103 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004104 return NULL;
4105 }
4106
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004107 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004108 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004109 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004110 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4111 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004112
4113 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004114 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004115 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004116 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004117
Daniel Stone97f68542012-05-30 16:32:01 +01004118 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004119 if (d->xkb_context == NULL) {
4120 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004121 return NULL;
4122 }
4123
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004124 d->workspace = 0;
4125 d->workspace_count = 1;
4126
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004127 d->registry = wl_display_get_registry(d->display);
4128 wl_registry_add_listener(d->registry, &registry_listener, d);
4129 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004130#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004131 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004132 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004133#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004134
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004135 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004136
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004137 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004138
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004139 wl_list_init(&d->window_list);
4140
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004141 init_dummy_surface(d);
4142
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004143 return d;
4144}
4145
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004146static void
4147display_destroy_outputs(struct display *display)
4148{
4149 struct output *tmp;
4150 struct output *output;
4151
4152 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4153 output_destroy(output);
4154}
4155
Pekka Paalanene1207c72011-12-16 12:02:09 +02004156static void
4157display_destroy_inputs(struct display *display)
4158{
4159 struct input *tmp;
4160 struct input *input;
4161
4162 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4163 input_destroy(input);
4164}
4165
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004166void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004167display_destroy(struct display *display)
4168{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004169 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004170 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4171 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004172
4173 if (!wl_list_empty(&display->deferred_list))
4174 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4175
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004176 cairo_surface_destroy(display->dummy_surface);
4177 free(display->dummy_surface_data);
4178
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004179 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004180 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004181
Daniel Stone97f68542012-05-30 16:32:01 +01004182 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004183
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004184 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004185 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004186
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004187#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004188 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004189#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004190
Pekka Paalanenc2052982011-12-16 11:41:32 +02004191 if (display->shell)
4192 wl_shell_destroy(display->shell);
4193
4194 if (display->shm)
4195 wl_shm_destroy(display->shm);
4196
4197 if (display->data_device_manager)
4198 wl_data_device_manager_destroy(display->data_device_manager);
4199
4200 wl_compositor_destroy(display->compositor);
4201
4202 close(display->epoll_fd);
4203
U. Artie Eoff44874d92012-10-02 21:12:35 -07004204 if (!(display->display_fd_events & EPOLLERR) &&
4205 !(display->display_fd_events & EPOLLHUP))
4206 wl_display_flush(display->display);
4207
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004208 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004209 free(display);
4210}
4211
4212void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004213display_set_user_data(struct display *display, void *data)
4214{
4215 display->user_data = data;
4216}
4217
4218void *
4219display_get_user_data(struct display *display)
4220{
4221 return display->user_data;
4222}
4223
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004224struct wl_display *
4225display_get_display(struct display *display)
4226{
4227 return display->display;
4228}
4229
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004230struct output *
4231display_get_output(struct display *display)
4232{
4233 return container_of(display->output_list.next, struct output, link);
4234}
4235
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004236struct wl_compositor *
4237display_get_compositor(struct display *display)
4238{
4239 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004240}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004241
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004242uint32_t
4243display_get_serial(struct display *display)
4244{
4245 return display->serial;
4246}
4247
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004248EGLDisplay
4249display_get_egl_display(struct display *d)
4250{
4251 return d->dpy;
4252}
4253
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004254struct wl_data_source *
4255display_create_data_source(struct display *display)
4256{
4257 return wl_data_device_manager_create_data_source(display->data_device_manager);
4258}
4259
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004260EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004261display_get_argb_egl_config(struct display *d)
4262{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004263 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004264}
4265
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004266struct wl_shell *
4267display_get_shell(struct display *display)
4268{
4269 return display->shell;
4270}
4271
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004272int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004273display_acquire_window_surface(struct display *display,
4274 struct window *window,
4275 EGLContext ctx)
4276{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004277 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004278 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004279
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004280 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004281}
4282
4283void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004284display_release_window_surface(struct display *display,
4285 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004286{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004287 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004288 return;
4289
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004290 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004291}
4292
4293void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004294display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004295{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004296 wl_list_insert(&display->deferred_list, &task->link);
4297}
4298
4299void
4300display_watch_fd(struct display *display,
4301 int fd, uint32_t events, struct task *task)
4302{
4303 struct epoll_event ep;
4304
4305 ep.events = events;
4306 ep.data.ptr = task;
4307 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4308}
4309
4310void
4311display_run(struct display *display)
4312{
4313 struct task *task;
4314 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004315 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004316
Pekka Paalanen826d7952011-12-15 10:14:07 +02004317 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004318 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004319 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004320 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004321 struct task, link);
4322 wl_list_remove(&task->link);
4323 task->run(task, 0);
4324 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004325
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004326 wl_display_dispatch_pending(display->display);
4327
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004328 if (!display->running)
4329 break;
4330
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004331 ret = wl_display_flush(display->display);
4332 if (ret < 0 && errno == EAGAIN) {
4333 ep[0].events =
4334 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4335 ep[0].data.ptr = &display->display_task;
4336
4337 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4338 display->display_fd, &ep[0]);
4339 } else if (ret < 0) {
4340 break;
4341 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004342
4343 count = epoll_wait(display->epoll_fd,
4344 ep, ARRAY_LENGTH(ep), -1);
4345 for (i = 0; i < count; i++) {
4346 task = ep[i].data.ptr;
4347 task->run(task, ep[i].events);
4348 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004349 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004350}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004351
4352void
4353display_exit(struct display *display)
4354{
4355 display->running = 0;
4356}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004357
4358void
4359keysym_modifiers_add(struct wl_array *modifiers_map,
4360 const char *name)
4361{
4362 size_t len = strlen(name) + 1;
4363 char *p;
4364
4365 p = wl_array_add(modifiers_map, len);
4366
4367 if (p == NULL)
4368 return;
4369
4370 strncpy(p, name, len);
4371}
4372
4373static xkb_mod_index_t
4374keysym_modifiers_get_index(struct wl_array *modifiers_map,
4375 const char *name)
4376{
4377 xkb_mod_index_t index = 0;
4378 char *p = modifiers_map->data;
4379
4380 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4381 if (strcmp(p, name) == 0)
4382 return index;
4383
4384 index++;
4385 p += strlen(p) + 1;
4386 }
4387
4388 return XKB_MOD_INVALID;
4389}
4390
4391xkb_mod_mask_t
4392keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4393 const char *name)
4394{
4395 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4396
4397 if (index == XKB_MOD_INVALID)
4398 return XKB_MOD_INVALID;
4399
4400 return 1 << index;
4401}