blob: 3469fe64810c49d8ee6fb9fadfe888c54f95656d [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
Pekka Paalanena4eda732012-11-19 17:16:02 +0200785struct shm_surface_leaf {
786 cairo_surface_t *cairo_surface;
787 /* 'data' is automatically destroyed, when 'cairo_surface' is */
788 struct shm_surface_data *data;
789
790 struct shm_pool *resize_pool;
791 int busy;
792};
793
794static void
795shm_surface_leaf_release(struct shm_surface_leaf *leaf)
796{
797 if (leaf->cairo_surface)
798 cairo_surface_destroy(leaf->cairo_surface);
799 /* leaf->data already destroyed via cairo private */
800
801 if (leaf->resize_pool)
802 shm_pool_destroy(leaf->resize_pool);
803}
804
Pekka Paalanen99436862012-11-19 17:15:59 +0200805struct shm_surface {
806 struct toysurface base;
807 struct display *display;
808 struct wl_surface *surface;
809 uint32_t flags;
810 int dx, dy;
811
Pekka Paalanena4eda732012-11-19 17:16:02 +0200812 struct shm_surface_leaf leaf[2];
813 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200814};
815
816static struct shm_surface *
817to_shm_surface(struct toysurface *base)
818{
819 return container_of(base, struct shm_surface, base);
820}
821
Pekka Paalanena4eda732012-11-19 17:16:02 +0200822static void
823shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
824{
825 struct shm_surface_leaf *leaf = data;
826
827 leaf->busy = 0;
828}
829
830static const struct wl_buffer_listener shm_surface_buffer_listener = {
831 shm_surface_buffer_release
832};
833
Pekka Paalanen99436862012-11-19 17:15:59 +0200834static cairo_surface_t *
835shm_surface_prepare(struct toysurface *base, int dx, int dy,
836 int width, int height, int resize_hint)
837{
838 struct shm_surface *surface = to_shm_surface(base);
839 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200840 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200841
842 surface->dx = dx;
843 surface->dy = dy;
844
Pekka Paalanena4eda732012-11-19 17:16:02 +0200845 /* pick a free buffer from the two */
846 if (!surface->leaf[0].busy)
847 leaf = &surface->leaf[0];
848 else if (!surface->leaf[1].busy)
849 leaf = &surface->leaf[1];
850 else {
851 fprintf(stderr, "%s: both buffers are held by the server.\n",
852 __func__);
853 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200854 }
855
Pekka Paalanena4eda732012-11-19 17:16:02 +0200856 if (!resize_hint && leaf->resize_pool) {
857 cairo_surface_destroy(leaf->cairo_surface);
858 leaf->cairo_surface = NULL;
859 shm_pool_destroy(leaf->resize_pool);
860 leaf->resize_pool = NULL;
861 }
862
863 if (leaf->cairo_surface &&
864 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
865 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200866 goto out;
867
Pekka Paalanena4eda732012-11-19 17:16:02 +0200868 if (leaf->cairo_surface)
869 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200870
Pekka Paalanena4eda732012-11-19 17:16:02 +0200871 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200872 /* Create a big pool to allocate from, while continuously
873 * resizing. Mmapping a new pool in the server
874 * is relatively expensive, so reusing a pool performs
875 * better, but may temporarily reserve unneeded memory.
876 */
877 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200878 leaf->resize_pool = shm_pool_create(surface->display,
879 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200880 }
881
Pekka Paalanena4eda732012-11-19 17:16:02 +0200882 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200883 display_create_shm_surface(surface->display, &rect,
884 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200885 leaf->resize_pool,
886 &leaf->data);
887 wl_buffer_add_listener(leaf->data->buffer,
888 &shm_surface_buffer_listener, leaf);
Pekka Paalanen99436862012-11-19 17:15:59 +0200889
890out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200891 surface->current = leaf;
892
893 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200894}
895
896static void
897shm_surface_swap(struct toysurface *base,
898 struct rectangle *server_allocation)
899{
900 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200901 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200902
903 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200904 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200905 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200906 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200907
Pekka Paalanena4eda732012-11-19 17:16:02 +0200908 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200909 surface->dx, surface->dy);
910 wl_surface_damage(surface->surface, 0, 0,
911 server_allocation->width, server_allocation->height);
912 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200913
914 leaf->busy = 1;
915 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200916}
917
918static int
919shm_surface_acquire(struct toysurface *base, EGLContext ctx)
920{
921 return -1;
922}
923
924static void
925shm_surface_release(struct toysurface *base)
926{
927}
928
929static void
930shm_surface_destroy(struct toysurface *base)
931{
932 struct shm_surface *surface = to_shm_surface(base);
933
Pekka Paalanena4eda732012-11-19 17:16:02 +0200934 shm_surface_leaf_release(&surface->leaf[0]);
935 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200936
937 free(surface);
938}
939
940static struct toysurface *
941shm_surface_create(struct display *display, struct wl_surface *wl_surface,
942 uint32_t flags, struct rectangle *rectangle)
943{
944 struct shm_surface *surface;
945
946 surface = calloc(1, sizeof *surface);
947 if (!surface)
948 return NULL;
949
950 surface->base.prepare = shm_surface_prepare;
951 surface->base.swap = shm_surface_swap;
952 surface->base.acquire = shm_surface_acquire;
953 surface->base.release = shm_surface_release;
954 surface->base.destroy = shm_surface_destroy;
955
956 surface->display = display;
957 surface->surface = wl_surface;
958 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200959
960 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400961}
962
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200963/*
964 * The following correspondences between file names and cursors was copied
965 * from: https://bugs.kde.org/attachment.cgi?id=67313
966 */
967
968static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300969 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200970 "sw-resize"
971};
972
973static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300974 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200975 "se-resize"
976};
977
978static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300979 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200980 "s-resize"
981};
982
983static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300984 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200985 "closedhand",
986 "208530c400c041818281048008011002"
987};
988
989static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300990 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200991 "default",
992 "top_left_arrow",
993 "left-arrow"
994};
995
996static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300997 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200998 "w-resize"
999};
1000
1001static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001002 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001003 "e-resize"
1004};
1005
1006static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001007 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001008 "nw-resize"
1009};
1010
1011static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001012 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001013 "ne-resize"
1014};
1015
1016static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001017 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001018 "n-resize"
1019};
1020
1021static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001022 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001023 "ibeam",
1024 "text"
1025};
1026
1027static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001028 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001029 "pointer",
1030 "pointing_hand",
1031 "e29285e634086352946a0e7090d73106"
1032};
1033
1034static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001035 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001036 "wait",
1037 "0426c94ea35c87780ff01dc239897213"
1038};
1039
1040struct cursor_alternatives {
1041 const char **names;
1042 size_t count;
1043};
1044
1045static const struct cursor_alternatives cursors[] = {
1046 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1047 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1048 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1049 {grabbings, ARRAY_LENGTH(grabbings)},
1050 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1051 {left_sides, ARRAY_LENGTH(left_sides)},
1052 {right_sides, ARRAY_LENGTH(right_sides)},
1053 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1054 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1055 {top_sides, ARRAY_LENGTH(top_sides)},
1056 {xterms, ARRAY_LENGTH(xterms)},
1057 {hand1s, ARRAY_LENGTH(hand1s)},
1058 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001059};
1060
1061static void
1062create_cursors(struct display *display)
1063{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001064 char *config_file;
1065 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001066 unsigned int i, j;
1067 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001068 struct config_key shell_keys[] = {
1069 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1070 };
1071 struct config_section cs[] = {
1072 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1073 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001074
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001075 config_file = config_file_path("weston.ini");
1076 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1077 free(config_file);
1078
1079 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001080 display->cursors =
1081 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1082
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001083 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001084 cursor = NULL;
1085 for (j = 0; !cursor && j < cursors[i].count; ++j)
1086 cursor = wl_cursor_theme_get_cursor(
1087 display->cursor_theme, cursors[i].names[j]);
1088
1089 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001090 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001091 cursors[i].names[0]);
1092
1093 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001094 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001095}
1096
1097static void
1098destroy_cursors(struct display *display)
1099{
1100 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001101 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001102}
1103
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001104struct wl_cursor_image *
1105display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001106{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001107 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001108
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001109 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001110}
1111
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001112static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001113window_get_resize_dx_dy(struct window *window, int *x, int *y)
1114{
1115 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1116 *x = window->server_allocation.width - window->allocation.width;
1117 else
1118 *x = 0;
1119
1120 if (window->resize_edges & WINDOW_RESIZING_TOP)
1121 *y = window->server_allocation.height -
1122 window->allocation.height;
1123 else
1124 *y = 0;
1125
1126 window->resize_edges = 0;
1127}
1128
1129static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001130window_attach_surface(struct window *window)
1131{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001132 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001133
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001134 if (window->type == TYPE_NONE) {
1135 window->type = TYPE_TOPLEVEL;
1136 if (display->shell)
1137 wl_shell_surface_set_toplevel(window->shell_surface);
1138 }
1139
Pekka Paalanen512dde82012-10-10 12:49:27 +03001140 if (window->opaque_region) {
1141 wl_surface_set_opaque_region(window->surface,
1142 window->opaque_region);
1143 wl_region_destroy(window->opaque_region);
1144 window->opaque_region = NULL;
1145 }
1146
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001147 if (window->input_region) {
1148 wl_surface_set_input_region(window->surface,
1149 window->input_region);
1150 wl_region_destroy(window->input_region);
1151 window->input_region = NULL;
1152 }
1153
Pekka Paalanen99436862012-11-19 17:15:59 +02001154 window->toysurface->swap(window->toysurface,
1155 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001156}
1157
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001158int
1159window_has_focus(struct window *window)
1160{
1161 return window->focus_count > 0;
1162}
1163
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001164static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001165window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001166{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001167 if (!window->cairo_surface)
1168 return;
1169
1170 window_attach_surface(window);
1171 cairo_surface_destroy(window->cairo_surface);
1172 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001173}
1174
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001175struct display *
1176window_get_display(struct window *window)
1177{
1178 return window->display;
1179}
1180
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001181static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001182window_create_surface(struct window *window)
1183{
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001184 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001185 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001186
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001187 if (!window->transparent)
1188 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001189
Pekka Paalanenb3627362012-11-19 17:16:00 +02001190 if (!window->toysurface &&
1191 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1192 window->display->dpy) {
1193 window->toysurface =
1194 egl_window_surface_create(window->display,
1195 window->surface, flags,
1196 &window->allocation);
1197 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001198
Pekka Paalanenb3627362012-11-19 17:16:00 +02001199 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001200 window->toysurface = shm_surface_create(window->display,
1201 window->surface, flags,
1202 &window->allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001203
Pekka Paalanen99436862012-11-19 17:15:59 +02001204 window_get_resize_dx_dy(window, &dx, &dy);
1205 window->cairo_surface =
1206 window->toysurface->prepare(window->toysurface, dx, dy,
1207 window->allocation.width,
1208 window->allocation.height,
1209 window->resizing);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001210}
1211
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001212static void frame_destroy(struct frame *frame);
1213
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001214void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001215window_destroy(struct window *window)
1216{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001217 struct display *display = window->display;
1218 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001219 struct window_output *window_output;
1220 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001221
1222 if (window->redraw_scheduled)
1223 wl_list_remove(&window->redraw_task.link);
1224
1225 wl_list_for_each(input, &display->input_list, link) {
1226 if (input->pointer_focus == window)
1227 input->pointer_focus = NULL;
1228 if (input->keyboard_focus == window)
1229 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001230 if (input->focus_widget &&
1231 input->focus_widget->window == window)
1232 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001233 }
1234
Rob Bradford7507b572012-05-15 17:55:34 +01001235 wl_list_for_each_safe(window_output, window_output_tmp,
1236 &window->window_output_list, link) {
1237 free (window_output);
1238 }
1239
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001240 if (window->input_region)
1241 wl_region_destroy(window->input_region);
1242 if (window->opaque_region)
1243 wl_region_destroy(window->opaque_region);
1244
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001245 if (window->frame)
1246 frame_destroy(window->frame);
1247
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001248 if (window->shell_surface)
1249 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001250 wl_surface_destroy(window->surface);
1251 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001252
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001253 if (window->toysurface)
1254 window->toysurface->destroy(window->toysurface);
1255
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001256 if (window->frame_cb)
1257 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001258 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001259 free(window);
1260}
1261
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001262static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001263widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001264{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001265 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001266
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001267 wl_list_for_each(child, &widget->child_list, link) {
1268 target = widget_find_widget(child, x, y);
1269 if (target)
1270 return target;
1271 }
1272
1273 if (widget->allocation.x <= x &&
1274 x < widget->allocation.x + widget->allocation.width &&
1275 widget->allocation.y <= y &&
1276 y < widget->allocation.y + widget->allocation.height) {
1277 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001278 }
1279
1280 return NULL;
1281}
1282
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001283static struct widget *
1284widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001285{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001286 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001287
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001288 widget = malloc(sizeof *widget);
1289 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001290 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001291 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001292 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001293 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001294 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001295 widget->tooltip = NULL;
1296 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001297
1298 return widget;
1299}
1300
1301struct widget *
1302window_add_widget(struct window *window, void *data)
1303{
1304 window->widget = widget_create(window, data);
1305 wl_list_init(&window->widget->link);
1306
1307 return window->widget;
1308}
1309
1310struct widget *
1311widget_add_widget(struct widget *parent, void *data)
1312{
1313 struct widget *widget;
1314
1315 widget = widget_create(parent->window, data);
1316 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001317
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001318 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001319}
1320
1321void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001322widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001323{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001324 struct display *display = widget->window->display;
1325 struct input *input;
1326
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001327 if (widget->tooltip) {
1328 free(widget->tooltip);
1329 widget->tooltip = NULL;
1330 }
1331
Pekka Paalanene156fb62012-01-19 13:51:38 +02001332 wl_list_for_each(input, &display->input_list, link) {
1333 if (input->focus_widget == widget)
1334 input->focus_widget = NULL;
1335 }
1336
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001337 wl_list_remove(&widget->link);
1338 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001339}
1340
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001341void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001342widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001343{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001344 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001345}
1346
1347void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001348widget_set_size(struct widget *widget, int32_t width, int32_t height)
1349{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001350 widget->allocation.width = width;
1351 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001352}
1353
1354void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001355widget_set_allocation(struct widget *widget,
1356 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001357{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001358 widget->allocation.x = x;
1359 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001360 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001361}
1362
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001363void
1364widget_set_transparent(struct widget *widget, int transparent)
1365{
1366 widget->opaque = !transparent;
1367}
1368
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001369void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001370widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001371{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001372 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001373}
1374
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001375void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001376widget_set_resize_handler(struct widget *widget,
1377 widget_resize_handler_t handler)
1378{
1379 widget->resize_handler = handler;
1380}
1381
1382void
1383widget_set_redraw_handler(struct widget *widget,
1384 widget_redraw_handler_t handler)
1385{
1386 widget->redraw_handler = handler;
1387}
1388
1389void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001390widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001391{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001392 widget->enter_handler = handler;
1393}
1394
1395void
1396widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1397{
1398 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001399}
1400
1401void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001402widget_set_motion_handler(struct widget *widget,
1403 widget_motion_handler_t handler)
1404{
1405 widget->motion_handler = handler;
1406}
1407
1408void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001409widget_set_button_handler(struct widget *widget,
1410 widget_button_handler_t handler)
1411{
1412 widget->button_handler = handler;
1413}
1414
1415void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001416widget_set_axis_handler(struct widget *widget,
1417 widget_axis_handler_t handler)
1418{
1419 widget->axis_handler = handler;
1420}
1421
1422void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001423widget_schedule_redraw(struct widget *widget)
1424{
1425 window_schedule_redraw(widget->window);
1426}
1427
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001428cairo_surface_t *
1429window_get_surface(struct window *window)
1430{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001431 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001432}
1433
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001434struct wl_surface *
1435window_get_wl_surface(struct window *window)
1436{
1437 return window->surface;
1438}
1439
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001440struct wl_shell_surface *
1441window_get_wl_shell_surface(struct window *window)
1442{
1443 return window->shell_surface;
1444}
1445
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001446static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001447tooltip_redraw_handler(struct widget *widget, void *data)
1448{
1449 cairo_t *cr;
1450 const int32_t r = 3;
1451 struct tooltip *tooltip = data;
1452 int32_t width, height;
1453 struct window *window = widget->window;
1454
1455 cr = cairo_create(window->cairo_surface);
1456 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1457 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1458 cairo_paint(cr);
1459
1460 width = window->allocation.width;
1461 height = window->allocation.height;
1462 rounded_rect(cr, 0, 0, width, height, r);
1463
1464 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1465 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1466 cairo_fill(cr);
1467
1468 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1469 cairo_move_to(cr, 10, 16);
1470 cairo_show_text(cr, tooltip->entry);
1471 cairo_destroy(cr);
1472}
1473
1474static cairo_text_extents_t
1475get_text_extents(struct tooltip *tooltip)
1476{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001477 cairo_t *cr;
1478 cairo_text_extents_t extents;
1479
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001480 /* Use the dummy_surface because tooltip's surface was not
1481 * created yet, and parent does not have a valid surface
1482 * outside repaint, either.
1483 */
1484 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001485 cairo_text_extents(cr, tooltip->entry, &extents);
1486 cairo_destroy(cr);
1487
1488 return extents;
1489}
1490
1491static int
1492window_create_tooltip(struct tooltip *tooltip)
1493{
1494 struct widget *parent = tooltip->parent;
1495 struct display *display = parent->window->display;
1496 struct window *window;
1497 const int offset_y = 27;
1498 const int margin = 3;
1499 cairo_text_extents_t extents;
1500
1501 if (tooltip->widget)
1502 return 0;
1503
1504 window = window_create_transient(display, parent->window, tooltip->x,
1505 tooltip->y + offset_y,
1506 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1507 if (!window)
1508 return -1;
1509
1510 tooltip->window = window;
1511 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1512
1513 extents = get_text_extents(tooltip);
1514 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1515 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1516
1517 return 0;
1518}
1519
1520void
1521widget_destroy_tooltip(struct widget *parent)
1522{
1523 struct tooltip *tooltip = parent->tooltip;
1524
1525 parent->tooltip_count = 0;
1526 if (!tooltip)
1527 return;
1528
1529 if (tooltip->widget) {
1530 widget_destroy(tooltip->widget);
1531 window_destroy(tooltip->window);
1532 tooltip->widget = NULL;
1533 tooltip->window = NULL;
1534 }
1535
1536 close(tooltip->tooltip_fd);
1537 free(tooltip->entry);
1538 free(tooltip);
1539 parent->tooltip = NULL;
1540}
1541
1542static void
1543tooltip_func(struct task *task, uint32_t events)
1544{
1545 struct tooltip *tooltip =
1546 container_of(task, struct tooltip, tooltip_task);
1547 uint64_t exp;
1548
Martin Olsson8df662a2012-07-08 03:03:47 +02001549 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1550 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001551 window_create_tooltip(tooltip);
1552}
1553
1554#define TOOLTIP_TIMEOUT 500
1555static int
1556tooltip_timer_reset(struct tooltip *tooltip)
1557{
1558 struct itimerspec its;
1559
1560 its.it_interval.tv_sec = 0;
1561 its.it_interval.tv_nsec = 0;
1562 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1563 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1564 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1565 fprintf(stderr, "could not set timerfd\n: %m");
1566 return -1;
1567 }
1568
1569 return 0;
1570}
1571
1572int
1573widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1574{
1575 struct tooltip *tooltip = parent->tooltip;
1576
1577 parent->tooltip_count++;
1578 if (tooltip) {
1579 tooltip->x = x;
1580 tooltip->y = y;
1581 tooltip_timer_reset(tooltip);
1582 return 0;
1583 }
1584
1585 /* the handler might be triggered too fast via input device motion, so
1586 * we need this check here to make sure tooltip is fully initialized */
1587 if (parent->tooltip_count > 1)
1588 return 0;
1589
1590 tooltip = malloc(sizeof *tooltip);
1591 if (!tooltip)
1592 return -1;
1593
1594 parent->tooltip = tooltip;
1595 tooltip->parent = parent;
1596 tooltip->widget = NULL;
1597 tooltip->window = NULL;
1598 tooltip->x = x;
1599 tooltip->y = y;
1600 tooltip->entry = strdup(entry);
1601 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1602 if (tooltip->tooltip_fd < 0) {
1603 fprintf(stderr, "could not create timerfd\n: %m");
1604 return -1;
1605 }
1606
1607 tooltip->tooltip_task.run = tooltip_func;
1608 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1609 EPOLLIN, &tooltip->tooltip_task);
1610 tooltip_timer_reset(tooltip);
1611
1612 return 0;
1613}
1614
1615static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001616workspace_manager_state(void *data,
1617 struct workspace_manager *workspace_manager,
1618 uint32_t current,
1619 uint32_t count)
1620{
1621 struct display *display = data;
1622
1623 display->workspace = current;
1624 display->workspace_count = count;
1625}
1626
1627static const struct workspace_manager_listener workspace_manager_listener = {
1628 workspace_manager_state
1629};
1630
1631static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001632frame_resize_handler(struct widget *widget,
1633 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001634{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001635 struct frame *frame = data;
1636 struct widget *child = frame->child;
1637 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001638 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001639 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001640 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001641 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001643 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001644
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001645 switch (widget->window->type) {
1646 case TYPE_FULLSCREEN:
1647 decoration_width = 0;
1648 decoration_height = 0;
1649
1650 allocation.x = 0;
1651 allocation.y = 0;
1652 allocation.width = width;
1653 allocation.height = height;
1654 opaque_margin = 0;
1655
1656 wl_list_for_each(button, &frame->buttons_list, link)
1657 button->widget->opaque = 1;
1658 break;
1659 case TYPE_MAXIMIZED:
1660 decoration_width = t->width * 2;
1661 decoration_height = t->width + t->titlebar_height;
1662
1663 allocation.x = t->width;
1664 allocation.y = t->titlebar_height;
1665 allocation.width = width - decoration_width;
1666 allocation.height = height - decoration_height;
1667
1668 opaque_margin = 0;
1669
1670 wl_list_for_each(button, &frame->buttons_list, link)
1671 button->widget->opaque = 0;
1672 break;
1673 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001674 decoration_width = (t->width + t->margin) * 2;
1675 decoration_height = t->width +
1676 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001677
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001678 allocation.x = t->width + t->margin;
1679 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001680 allocation.width = width - decoration_width;
1681 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001682
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001683 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001684
1685 wl_list_for_each(button, &frame->buttons_list, link)
1686 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001687 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001688 }
1689
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001690 widget_set_allocation(child, allocation.x, allocation.y,
1691 allocation.width, allocation.height);
1692
1693 if (child->resize_handler)
1694 child->resize_handler(child,
1695 allocation.width,
1696 allocation.height,
1697 child->user_data);
1698
Scott Moreauf7e498c2012-05-14 11:39:29 -06001699 width = child->allocation.width + decoration_width;
1700 height = child->allocation.height + decoration_height;
1701
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001702 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1703
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001704 widget->window->input_region =
1705 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001706 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001707 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001708 shadow_margin, shadow_margin,
1709 width - 2 * shadow_margin,
1710 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001711 } else {
1712 wl_region_add(widget->window->input_region,
1713 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001714 }
1715
Scott Moreauf7e498c2012-05-14 11:39:29 -06001716 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001717
1718 if (child->opaque) {
1719 widget->window->opaque_region =
1720 wl_compositor_create_region(display->compositor);
1721 wl_region_add(widget->window->opaque_region,
1722 opaque_margin, opaque_margin,
1723 widget->allocation.width - 2 * opaque_margin,
1724 widget->allocation.height - 2 * opaque_margin);
1725 }
Martin Minarik1998b152012-05-10 02:04:35 +02001726
1727 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001728 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1729 x_l = t->width + shadow_margin;
1730 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001731 wl_list_for_each(button, &frame->buttons_list, link) {
1732 const int button_padding = 4;
1733 w = cairo_image_surface_get_width(button->icon);
1734 h = cairo_image_surface_get_height(button->icon);
1735
1736 if (button->decoration == FRAME_BUTTON_FANCY)
1737 w += 10;
1738
1739 if (button->align == FRAME_BUTTON_LEFT) {
1740 widget_set_allocation(button->widget,
1741 x_l, y , w + 1, h + 1);
1742 x_l += w;
1743 x_l += button_padding;
1744 } else {
1745 x_r -= w;
1746 widget_set_allocation(button->widget,
1747 x_r, y , w + 1, h + 1);
1748 x_r -= button_padding;
1749 }
1750 }
1751}
1752
1753static int
1754frame_button_enter_handler(struct widget *widget,
1755 struct input *input, float x, float y, void *data)
1756{
1757 struct frame_button *frame_button = data;
1758
1759 widget_schedule_redraw(frame_button->widget);
1760 frame_button->state = FRAME_BUTTON_OVER;
1761
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001762 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001763}
1764
1765static void
1766frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1767{
1768 struct frame_button *frame_button = data;
1769
1770 widget_schedule_redraw(frame_button->widget);
1771 frame_button->state = FRAME_BUTTON_DEFAULT;
1772}
1773
1774static void
1775frame_button_button_handler(struct widget *widget,
1776 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001777 uint32_t button,
1778 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001779{
1780 struct frame_button *frame_button = data;
1781 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001782 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001783
1784 if (button != BTN_LEFT)
1785 return;
1786
1787 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001788 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001789 frame_button->state = FRAME_BUTTON_ACTIVE;
1790 widget_schedule_redraw(frame_button->widget);
1791
1792 if (frame_button->type == FRAME_BUTTON_ICON)
1793 window_show_frame_menu(window, input, time);
1794 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001795 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001796 frame_button->state = FRAME_BUTTON_DEFAULT;
1797 widget_schedule_redraw(frame_button->widget);
1798 break;
1799 }
1800
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001801 if (!was_pressed)
1802 return;
1803
Martin Minarik1998b152012-05-10 02:04:35 +02001804 switch (frame_button->type) {
1805 case FRAME_BUTTON_CLOSE:
1806 if (window->close_handler)
1807 window->close_handler(window->parent,
1808 window->user_data);
1809 else
1810 display_exit(window->display);
1811 break;
1812 case FRAME_BUTTON_MINIMIZE:
1813 fprintf(stderr,"Minimize stub\n");
1814 break;
1815 case FRAME_BUTTON_MAXIMIZE:
1816 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1817 break;
1818 default:
1819 /* Unknown operation */
1820 break;
1821 }
1822}
1823
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001824static int
1825frame_button_motion_handler(struct widget *widget,
1826 struct input *input, uint32_t time,
1827 float x, float y, void *data)
1828{
1829 struct frame_button *frame_button = data;
1830 enum frame_button_pointer previous_button_state = frame_button->state;
1831
1832 /* only track state for a pressed button */
1833 if (input->grab != widget)
1834 return CURSOR_LEFT_PTR;
1835
1836 if (x > widget->allocation.x &&
1837 x < (widget->allocation.x + widget->allocation.width) &&
1838 y > widget->allocation.y &&
1839 y < (widget->allocation.y + widget->allocation.height)) {
1840 frame_button->state = FRAME_BUTTON_ACTIVE;
1841 } else {
1842 frame_button->state = FRAME_BUTTON_DEFAULT;
1843 }
1844
1845 if (frame_button->state != previous_button_state)
1846 widget_schedule_redraw(frame_button->widget);
1847
1848 return CURSOR_LEFT_PTR;
1849}
1850
Martin Minarik1998b152012-05-10 02:04:35 +02001851static void
1852frame_button_redraw_handler(struct widget *widget, void *data)
1853{
1854 struct frame_button *frame_button = data;
1855 cairo_t *cr;
1856 int width, height, x, y;
1857 struct window *window = widget->window;
1858
1859 x = widget->allocation.x;
1860 y = widget->allocation.y;
1861 width = widget->allocation.width;
1862 height = widget->allocation.height;
1863
1864 if (!width)
1865 return;
1866 if (!height)
1867 return;
1868 if (widget->opaque)
1869 return;
1870
1871 cr = cairo_create(window->cairo_surface);
1872
1873 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1874 cairo_set_line_width(cr, 1);
1875
1876 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1877 cairo_rectangle (cr, x, y, 25, 16);
1878
1879 cairo_stroke_preserve(cr);
1880
1881 switch (frame_button->state) {
1882 case FRAME_BUTTON_DEFAULT:
1883 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1884 break;
1885 case FRAME_BUTTON_OVER:
1886 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1887 break;
1888 case FRAME_BUTTON_ACTIVE:
1889 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1890 break;
1891 }
1892
1893 cairo_fill (cr);
1894
1895 x += 4;
1896 }
1897
1898 cairo_set_source_surface(cr, frame_button->icon, x, y);
1899 cairo_paint(cr);
1900
1901 cairo_destroy(cr);
1902}
1903
1904static struct widget *
1905frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1906 enum frame_button_align align, enum frame_button_decoration style)
1907{
1908 struct frame_button *frame_button;
1909 const char *icon = data;
1910
1911 frame_button = malloc (sizeof *frame_button);
1912 memset(frame_button, 0, sizeof *frame_button);
1913
1914 frame_button->icon = cairo_image_surface_create_from_png(icon);
1915 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1916 frame_button->frame = frame;
1917 frame_button->type = type;
1918 frame_button->align = align;
1919 frame_button->decoration = style;
1920
1921 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1922
1923 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1924 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1925 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1926 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001927 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001928 return frame_button->widget;
1929}
1930
1931static void
1932frame_button_destroy(struct frame_button *frame_button)
1933{
1934 widget_destroy(frame_button->widget);
1935 wl_list_remove(&frame_button->link);
1936 cairo_surface_destroy(frame_button->icon);
1937 free(frame_button);
1938
1939 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001940}
1941
1942static void
1943frame_redraw_handler(struct widget *widget, void *data)
1944{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001945 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001946 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001947 struct theme *t = window->display->theme;
1948 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001949
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001950 if (window->type == TYPE_FULLSCREEN)
1951 return;
1952
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001953 cr = cairo_create(window->cairo_surface);
1954
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001955 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001956 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001957 if (window->type == TYPE_MAXIMIZED)
1958 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001959 theme_render_frame(t, cr, widget->allocation.width,
1960 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001961
1962 cairo_destroy(cr);
1963}
1964
1965static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001966frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001967{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001968 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001969 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001970 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001971
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001972 location = theme_get_location(t, input->sx, input->sy,
1973 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001974 frame->widget->allocation.height,
1975 window->type == TYPE_MAXIMIZED ?
1976 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001977
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001978 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001979 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001980 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001981 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001982 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001983 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001984 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001985 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001986 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001987 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001988 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001989 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001990 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001991 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001992 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001993 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001994 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001995 case THEME_LOCATION_EXTERIOR:
1996 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001997 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001998 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001999 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002000}
2001
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002002static void
2003frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002004{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002005 struct display *display;
2006
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002007 switch (index) {
2008 case 0: /* close */
2009 if (window->close_handler)
2010 window->close_handler(window->parent,
2011 window->user_data);
2012 else
2013 display_exit(window->display);
2014 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002015 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002016 display = window->display;
2017 if (display->workspace > 0)
2018 workspace_manager_move_surface(display->workspace_manager,
2019 window->surface,
2020 display->workspace - 1);
2021 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002022 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002023 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002024 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002025 workspace_manager_move_surface(display->workspace_manager,
2026 window->surface,
2027 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002028 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002029 case 3: /* fullscreen */
2030 /* we don't have a way to get out of fullscreen for now */
2031 if (window->fullscreen_handler)
2032 window->fullscreen_handler(window, window->user_data);
2033 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002034 }
2035}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002036
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002037void
2038window_show_frame_menu(struct window *window,
2039 struct input *input, uint32_t time)
2040{
2041 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002042 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002043
2044 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002045 "Close",
2046 "Move to workspace above", "Move to workspace below",
2047 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002048 };
2049
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002050 if (window->fullscreen_handler)
2051 count = ARRAY_LENGTH(entries);
2052 else
2053 count = ARRAY_LENGTH(entries) - 1;
2054
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002055 input_get_position(input, &x, &y);
2056 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002057 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002058}
2059
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002060static int
2061frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002062 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002063{
2064 return frame_get_pointer_image_for_location(data, input);
2065}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002066
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002067static int
2068frame_motion_handler(struct widget *widget,
2069 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002070 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002071{
2072 return frame_get_pointer_image_for_location(data, input);
2073}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002074
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002075static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002076frame_button_handler(struct widget *widget,
2077 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002078 uint32_t button, enum wl_pointer_button_state state,
2079 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002080
2081{
2082 struct frame *frame = data;
2083 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002084 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002085 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002086
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002087 location = theme_get_location(display->theme, input->sx, input->sy,
2088 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002089 frame->widget->allocation.height,
2090 window->type == TYPE_MAXIMIZED ?
2091 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002092
Daniel Stone4dbadb12012-05-30 16:31:51 +01002093 if (window->display->shell && button == BTN_LEFT &&
2094 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002095 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002096 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002097 if (!window->shell_surface)
2098 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002099 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002100 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002101 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002102 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002103 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002104 case THEME_LOCATION_RESIZING_TOP:
2105 case THEME_LOCATION_RESIZING_BOTTOM:
2106 case THEME_LOCATION_RESIZING_LEFT:
2107 case THEME_LOCATION_RESIZING_RIGHT:
2108 case THEME_LOCATION_RESIZING_TOP_LEFT:
2109 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2110 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2111 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002112 if (!window->shell_surface)
2113 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002114 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002115
Pekka Paalanen99436862012-11-19 17:15:59 +02002116 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002117 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002118 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002119 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002120 break;
2121 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002122 } else if (button == BTN_RIGHT &&
2123 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002124 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002125 }
2126}
2127
2128struct widget *
2129frame_create(struct window *window, void *data)
2130{
2131 struct frame *frame;
2132
2133 frame = malloc(sizeof *frame);
2134 memset(frame, 0, sizeof *frame);
2135
2136 frame->widget = window_add_widget(window, frame);
2137 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002138
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002139 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2140 widget_set_resize_handler(frame->widget, frame_resize_handler);
2141 widget_set_enter_handler(frame->widget, frame_enter_handler);
2142 widget_set_motion_handler(frame->widget, frame_motion_handler);
2143 widget_set_button_handler(frame->widget, frame_button_handler);
2144
Martin Minarik1998b152012-05-10 02:04:35 +02002145 /* Create empty list for frame buttons */
2146 wl_list_init(&frame->buttons_list);
2147
2148 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2149 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2150
2151 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2152 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2153
2154 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2155 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2156
2157 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2158 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2159
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002160 window->frame = frame;
2161
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002162 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002163}
2164
Kristian Høgsberga1627922012-06-20 17:30:03 -04002165void
2166frame_set_child_size(struct widget *widget, int child_width, int child_height)
2167{
2168 struct display *display = widget->window->display;
2169 struct theme *t = display->theme;
2170 int decoration_width, decoration_height;
2171 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002172 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002173
2174 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002175 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002176 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002177 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002178
2179 width = child_width + decoration_width;
2180 height = child_height + decoration_height;
2181 } else {
2182 width = child_width;
2183 height = child_height;
2184 }
2185
2186 window_schedule_resize(widget->window, width, height);
2187}
2188
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002189static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002190frame_destroy(struct frame *frame)
2191{
Martin Minarik1998b152012-05-10 02:04:35 +02002192 struct frame_button *button, *tmp;
2193
2194 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2195 frame_button_destroy(button);
2196
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002197 /* frame->child must be destroyed by the application */
2198 widget_destroy(frame->widget);
2199 free(frame);
2200}
2201
2202static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002203input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002204 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002205{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002206 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002207 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002208
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002209 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002210 return;
2211
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002212 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002213 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002214 widget = old;
2215 if (input->grab)
2216 widget = input->grab;
2217 if (widget->leave_handler)
2218 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002219 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002220 }
2221
2222 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002223 widget = focus;
2224 if (input->grab)
2225 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002226 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002227 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002228 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002229 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002230
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002231 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002232 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002233}
2234
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002235void
2236input_grab(struct input *input, struct widget *widget, uint32_t button)
2237{
2238 input->grab = widget;
2239 input->grab_button = button;
2240}
2241
2242void
2243input_ungrab(struct input *input)
2244{
2245 struct widget *widget;
2246
2247 input->grab = NULL;
2248 if (input->pointer_focus) {
2249 widget = widget_find_widget(input->pointer_focus->widget,
2250 input->sx, input->sy);
2251 input_set_focus_widget(input, widget, input->sx, input->sy);
2252 }
2253}
2254
2255static void
2256input_remove_pointer_focus(struct input *input)
2257{
2258 struct window *window = input->pointer_focus;
2259
2260 if (!window)
2261 return;
2262
2263 input_set_focus_widget(input, NULL, 0, 0);
2264
2265 input->pointer_focus = NULL;
2266 input->current_cursor = CURSOR_UNSET;
2267}
2268
2269static void
2270pointer_handle_enter(void *data, struct wl_pointer *pointer,
2271 uint32_t serial, struct wl_surface *surface,
2272 wl_fixed_t sx_w, wl_fixed_t sy_w)
2273{
2274 struct input *input = data;
2275 struct window *window;
2276 struct widget *widget;
2277 float sx = wl_fixed_to_double(sx_w);
2278 float sy = wl_fixed_to_double(sy_w);
2279
2280 if (!surface) {
2281 /* enter event for a window we've just destroyed */
2282 return;
2283 }
2284
2285 input->display->serial = serial;
2286 input->pointer_enter_serial = serial;
2287 input->pointer_focus = wl_surface_get_user_data(surface);
2288 window = input->pointer_focus;
2289
Pekka Paalanen99436862012-11-19 17:15:59 +02002290 if (window->resizing) {
2291 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002292 /* Schedule a redraw to free the pool */
2293 window_schedule_redraw(window);
2294 }
2295
2296 input->sx = sx;
2297 input->sy = sy;
2298
2299 widget = widget_find_widget(window->widget, sx, sy);
2300 input_set_focus_widget(input, widget, sx, sy);
2301}
2302
2303static void
2304pointer_handle_leave(void *data, struct wl_pointer *pointer,
2305 uint32_t serial, struct wl_surface *surface)
2306{
2307 struct input *input = data;
2308
2309 input->display->serial = serial;
2310 input_remove_pointer_focus(input);
2311}
2312
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002313static void
Daniel Stone37816df2012-05-16 18:45:18 +01002314pointer_handle_motion(void *data, struct wl_pointer *pointer,
2315 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002316{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002317 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002318 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002319 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002320 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002321 float sx = wl_fixed_to_double(sx_w);
2322 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002323
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002324 input->sx = sx;
2325 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002326
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002327 if (!window)
2328 return;
2329
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002330 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002331 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002332 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002333 }
2334
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002335 if (input->grab)
2336 widget = input->grab;
2337 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002338 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002339 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002340 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002341 input, time, sx, sy,
2342 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002343
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002344 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002345}
2346
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002347static void
Daniel Stone37816df2012-05-16 18:45:18 +01002348pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002349 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002350{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002351 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002352 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002353 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002354
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002355 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002356 if (input->focus_widget && input->grab == NULL &&
2357 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002358 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002359
Neil Roberts6b28aad2012-01-23 19:11:18 +00002360 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002361 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002362 (*widget->button_handler)(widget,
2363 input, time,
2364 button, state,
2365 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002366
Daniel Stone4dbadb12012-05-30 16:31:51 +01002367 if (input->grab && input->grab_button == button &&
2368 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002369 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002370}
2371
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002372static void
Daniel Stone37816df2012-05-16 18:45:18 +01002373pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002374 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002375{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002376 struct input *input = data;
2377 struct widget *widget;
2378
2379 widget = input->focus_widget;
2380 if (input->grab)
2381 widget = input->grab;
2382 if (widget && widget->axis_handler)
2383 (*widget->axis_handler)(widget,
2384 input, time,
2385 axis, value,
2386 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002387}
2388
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002389static const struct wl_pointer_listener pointer_listener = {
2390 pointer_handle_enter,
2391 pointer_handle_leave,
2392 pointer_handle_motion,
2393 pointer_handle_button,
2394 pointer_handle_axis,
2395};
2396
2397static void
2398input_remove_keyboard_focus(struct input *input)
2399{
2400 struct window *window = input->keyboard_focus;
2401 struct itimerspec its;
2402
2403 its.it_interval.tv_sec = 0;
2404 its.it_interval.tv_nsec = 0;
2405 its.it_value.tv_sec = 0;
2406 its.it_value.tv_nsec = 0;
2407 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2408
2409 if (!window)
2410 return;
2411
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002412 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002413 if (window->keyboard_focus_handler)
2414 (*window->keyboard_focus_handler)(window, NULL,
2415 window->user_data);
2416
2417 input->keyboard_focus = NULL;
2418}
2419
2420static void
2421keyboard_repeat_func(struct task *task, uint32_t events)
2422{
2423 struct input *input =
2424 container_of(task, struct input, repeat_task);
2425 struct window *window = input->keyboard_focus;
2426 uint64_t exp;
2427
2428 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2429 /* If we change the timer between the fd becoming
2430 * readable and getting here, there'll be nothing to
2431 * read and we get EAGAIN. */
2432 return;
2433
2434 if (window && window->key_handler) {
2435 (*window->key_handler)(window, input, input->repeat_time,
2436 input->repeat_key, input->repeat_sym,
2437 WL_KEYBOARD_KEY_STATE_PRESSED,
2438 window->user_data);
2439 }
2440}
2441
2442static void
2443keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2444 uint32_t format, int fd, uint32_t size)
2445{
2446 struct input *input = data;
2447 char *map_str;
2448
2449 if (!data) {
2450 close(fd);
2451 return;
2452 }
2453
2454 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2455 close(fd);
2456 return;
2457 }
2458
2459 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2460 if (map_str == MAP_FAILED) {
2461 close(fd);
2462 return;
2463 }
2464
2465 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2466 map_str,
2467 XKB_KEYMAP_FORMAT_TEXT_V1,
2468 0);
2469 munmap(map_str, size);
2470 close(fd);
2471
2472 if (!input->xkb.keymap) {
2473 fprintf(stderr, "failed to compile keymap\n");
2474 return;
2475 }
2476
2477 input->xkb.state = xkb_state_new(input->xkb.keymap);
2478 if (!input->xkb.state) {
2479 fprintf(stderr, "failed to create XKB state\n");
2480 xkb_map_unref(input->xkb.keymap);
2481 input->xkb.keymap = NULL;
2482 return;
2483 }
2484
2485 input->xkb.control_mask =
2486 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2487 input->xkb.alt_mask =
2488 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2489 input->xkb.shift_mask =
2490 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2491}
2492
2493static void
2494keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2495 uint32_t serial, struct wl_surface *surface,
2496 struct wl_array *keys)
2497{
2498 struct input *input = data;
2499 struct window *window;
2500
2501 input->display->serial = serial;
2502 input->keyboard_focus = wl_surface_get_user_data(surface);
2503
2504 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002505 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002506 if (window->keyboard_focus_handler)
2507 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002508 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002509}
2510
2511static void
2512keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2513 uint32_t serial, struct wl_surface *surface)
2514{
2515 struct input *input = data;
2516
2517 input->display->serial = serial;
2518 input_remove_keyboard_focus(input);
2519}
2520
Scott Moreau210d0792012-03-22 10:47:01 -06002521static void
Daniel Stone37816df2012-05-16 18:45:18 +01002522keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002523 uint32_t serial, uint32_t time, uint32_t key,
2524 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002525{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002526 struct input *input = data;
2527 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002528 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002529 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002530 const xkb_keysym_t *syms;
2531 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002532 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002533
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002534 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002535 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002536 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002537 return;
2538
Daniel Stone97f68542012-05-30 16:32:01 +01002539 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002540
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002541 sym = XKB_KEY_NoSymbol;
2542 if (num_syms == 1)
2543 sym = syms[0];
2544
2545 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002546 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002547 window_set_maximized(window,
2548 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002549 } else if (sym == XKB_KEY_F11 &&
2550 window->fullscreen_handler &&
2551 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2552 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002553 } else if (sym == XKB_KEY_F4 &&
2554 input->modifiers == MOD_ALT_MASK &&
2555 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2556 if (window->close_handler)
2557 window->close_handler(window->parent,
2558 window->user_data);
2559 else
2560 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002561 } else if (window->key_handler) {
2562 (*window->key_handler)(window, input, time, key,
2563 sym, state, window->user_data);
2564 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002565
2566 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2567 key == input->repeat_key) {
2568 its.it_interval.tv_sec = 0;
2569 its.it_interval.tv_nsec = 0;
2570 its.it_value.tv_sec = 0;
2571 its.it_value.tv_nsec = 0;
2572 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2573 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2574 input->repeat_sym = sym;
2575 input->repeat_key = key;
2576 input->repeat_time = time;
2577 its.it_interval.tv_sec = 0;
2578 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2579 its.it_value.tv_sec = 0;
2580 its.it_value.tv_nsec = 400 * 1000 * 1000;
2581 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2582 }
2583}
2584
2585static void
Daniel Stone351eb612012-05-31 15:27:47 -04002586keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2587 uint32_t serial, uint32_t mods_depressed,
2588 uint32_t mods_latched, uint32_t mods_locked,
2589 uint32_t group)
2590{
2591 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002592 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002593
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002594 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2595 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002596 mask = xkb_state_serialize_mods(input->xkb.state,
2597 XKB_STATE_DEPRESSED |
2598 XKB_STATE_LATCHED);
2599 input->modifiers = 0;
2600 if (mask & input->xkb.control_mask)
2601 input->modifiers |= MOD_CONTROL_MASK;
2602 if (mask & input->xkb.alt_mask)
2603 input->modifiers |= MOD_ALT_MASK;
2604 if (mask & input->xkb.shift_mask)
2605 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002606}
2607
Daniel Stone37816df2012-05-16 18:45:18 +01002608static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002609 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002610 keyboard_handle_enter,
2611 keyboard_handle_leave,
2612 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002613 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002614};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002615
2616static void
Daniel Stone37816df2012-05-16 18:45:18 +01002617seat_handle_capabilities(void *data, struct wl_seat *seat,
2618 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002619{
Daniel Stone37816df2012-05-16 18:45:18 +01002620 struct input *input = data;
2621
2622 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2623 input->pointer = wl_seat_get_pointer(seat);
2624 wl_pointer_set_user_data(input->pointer, input);
2625 wl_pointer_add_listener(input->pointer, &pointer_listener,
2626 input);
2627 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2628 wl_pointer_destroy(input->pointer);
2629 input->pointer = NULL;
2630 }
2631
2632 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2633 input->keyboard = wl_seat_get_keyboard(seat);
2634 wl_keyboard_set_user_data(input->keyboard, input);
2635 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2636 input);
2637 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2638 wl_keyboard_destroy(input->keyboard);
2639 input->keyboard = NULL;
2640 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002641}
2642
Daniel Stone37816df2012-05-16 18:45:18 +01002643static const struct wl_seat_listener seat_listener = {
2644 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002645};
2646
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002647void
2648input_get_position(struct input *input, int32_t *x, int32_t *y)
2649{
2650 *x = input->sx;
2651 *y = input->sy;
2652}
2653
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002654struct display *
2655input_get_display(struct input *input)
2656{
2657 return input->display;
2658}
2659
Daniel Stone37816df2012-05-16 18:45:18 +01002660struct wl_seat *
2661input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002662{
Daniel Stone37816df2012-05-16 18:45:18 +01002663 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002664}
2665
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002666uint32_t
2667input_get_modifiers(struct input *input)
2668{
2669 return input->modifiers;
2670}
2671
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002672struct widget *
2673input_get_focus_widget(struct input *input)
2674{
2675 return input->focus_widget;
2676}
2677
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002678struct data_offer {
2679 struct wl_data_offer *offer;
2680 struct input *input;
2681 struct wl_array types;
2682 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002683
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002684 struct task io_task;
2685 int fd;
2686 data_func_t func;
2687 int32_t x, y;
2688 void *user_data;
2689};
2690
2691static void
2692data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2693{
2694 struct data_offer *offer = data;
2695 char **p;
2696
2697 p = wl_array_add(&offer->types, sizeof *p);
2698 *p = strdup(type);
2699}
2700
2701static const struct wl_data_offer_listener data_offer_listener = {
2702 data_offer_offer,
2703};
2704
2705static void
2706data_offer_destroy(struct data_offer *offer)
2707{
2708 char **p;
2709
2710 offer->refcount--;
2711 if (offer->refcount == 0) {
2712 wl_data_offer_destroy(offer->offer);
2713 for (p = offer->types.data; *p; p++)
2714 free(*p);
2715 wl_array_release(&offer->types);
2716 free(offer);
2717 }
2718}
2719
2720static void
2721data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002722 struct wl_data_device *data_device,
2723 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002724{
2725 struct data_offer *offer;
2726
2727 offer = malloc(sizeof *offer);
2728
2729 wl_array_init(&offer->types);
2730 offer->refcount = 1;
2731 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002732 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002733 wl_data_offer_add_listener(offer->offer,
2734 &data_offer_listener, offer);
2735}
2736
2737static void
2738data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002739 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002740 wl_fixed_t x_w, wl_fixed_t y_w,
2741 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002742{
2743 struct input *input = data;
2744 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002745 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002746 float x = wl_fixed_to_double(x_w);
2747 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002748 char **p;
2749
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002750 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002751 window = wl_surface_get_user_data(surface);
2752 input->pointer_focus = window;
2753
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002754 if (offer) {
2755 input->drag_offer = wl_data_offer_get_user_data(offer);
2756
2757 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2758 *p = NULL;
2759
2760 types_data = input->drag_offer->types.data;
2761 } else {
2762 input->drag_offer = NULL;
2763 types_data = NULL;
2764 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002765
2766 window = input->pointer_focus;
2767 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002768 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002769 window->user_data);
2770}
2771
2772static void
2773data_device_leave(void *data, struct wl_data_device *data_device)
2774{
2775 struct input *input = data;
2776
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002777 if (input->drag_offer) {
2778 data_offer_destroy(input->drag_offer);
2779 input->drag_offer = NULL;
2780 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002781}
2782
2783static void
2784data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002785 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002786{
2787 struct input *input = data;
2788 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002789 float x = wl_fixed_to_double(x_w);
2790 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002791 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002792
2793 input->sx = x;
2794 input->sy = y;
2795
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002796 if (input->drag_offer)
2797 types_data = input->drag_offer->types.data;
2798 else
2799 types_data = NULL;
2800
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002801 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002802 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002803 window->user_data);
2804}
2805
2806static void
2807data_device_drop(void *data, struct wl_data_device *data_device)
2808{
2809 struct input *input = data;
2810 struct window *window = input->pointer_focus;
2811
2812 if (window->drop_handler)
2813 window->drop_handler(window, input,
2814 input->sx, input->sy, window->user_data);
2815}
2816
2817static void
2818data_device_selection(void *data,
2819 struct wl_data_device *wl_data_device,
2820 struct wl_data_offer *offer)
2821{
2822 struct input *input = data;
2823 char **p;
2824
2825 if (input->selection_offer)
2826 data_offer_destroy(input->selection_offer);
2827
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002828 if (offer) {
2829 input->selection_offer = wl_data_offer_get_user_data(offer);
2830 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2831 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002832 } else {
2833 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002834 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002835}
2836
2837static const struct wl_data_device_listener data_device_listener = {
2838 data_device_data_offer,
2839 data_device_enter,
2840 data_device_leave,
2841 data_device_motion,
2842 data_device_drop,
2843 data_device_selection
2844};
2845
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002846static void
2847input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002848{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002849 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002850 struct wl_cursor *cursor;
2851 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002852
Daniel Stone80972742012-11-07 17:51:39 +11002853 if (!input->pointer)
2854 return;
2855
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002856 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002857 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002858 return;
2859
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002860 if (index >= (int) cursor->image_count) {
2861 fprintf(stderr, "cursor index out of range\n");
2862 return;
2863 }
2864
2865 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002866 buffer = wl_cursor_image_get_buffer(image);
2867 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002868 return;
2869
Kristian Høgsbergae277372012-08-01 09:41:08 -04002870 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002871 input->pointer_surface,
2872 image->hotspot_x, image->hotspot_y);
2873 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2874 wl_surface_damage(input->pointer_surface, 0, 0,
2875 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002876 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002877}
2878
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002879static const struct wl_callback_listener pointer_surface_listener;
2880
2881static void
2882pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2883 uint32_t time)
2884{
2885 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002886 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002887 int i;
2888
2889 if (callback) {
2890 assert(callback == input->cursor_frame_cb);
2891 wl_callback_destroy(callback);
2892 input->cursor_frame_cb = NULL;
2893 }
2894
Daniel Stone80972742012-11-07 17:51:39 +11002895 if (!input->pointer)
2896 return;
2897
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002898 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002899 wl_pointer_set_cursor(input->pointer,
2900 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002901 NULL, 0, 0);
2902 return;
2903 }
2904
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002905 if (input->current_cursor == CURSOR_UNSET)
2906 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002907 cursor = input->display->cursors[input->current_cursor];
2908 if (!cursor)
2909 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002910
2911 /* FIXME We don't have the current time on the first call so we set
2912 * the animation start to the time of the first frame callback. */
2913 if (time == 0)
2914 input->cursor_anim_start = 0;
2915 else if (input->cursor_anim_start == 0)
2916 input->cursor_anim_start = time;
2917
2918 if (time == 0 || input->cursor_anim_start == 0)
2919 i = 0;
2920 else
2921 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2922
Pekka Paalanenbc106382012-10-10 12:49:31 +03002923 if (cursor->image_count > 1) {
2924 input->cursor_frame_cb =
2925 wl_surface_frame(input->pointer_surface);
2926 wl_callback_add_listener(input->cursor_frame_cb,
2927 &pointer_surface_listener, input);
2928 }
2929
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002930 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002931}
2932
2933static const struct wl_callback_listener pointer_surface_listener = {
2934 pointer_surface_frame_callback
2935};
2936
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002937void
2938input_set_pointer_image(struct input *input, int pointer)
2939{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002940 int force = 0;
2941
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002942 if (!input->pointer)
2943 return;
2944
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002945 if (input->pointer_enter_serial > input->cursor_serial)
2946 force = 1;
2947
2948 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002949 return;
2950
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002951 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002952 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002953 if (!input->cursor_frame_cb)
2954 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002955 else if (force) {
2956 /* The current frame callback may be stuck if, for instance,
2957 * the set cursor request was processed by the server after
2958 * this client lost the focus. In this case the cursor surface
2959 * might not be mapped and the frame callback wouldn't ever
2960 * complete. Send a set_cursor and attach to try to map the
2961 * cursor surface again so that the callback will finish */
2962 input_set_pointer_image_index(input, 0);
2963 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002964}
2965
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002966struct wl_data_device *
2967input_get_data_device(struct input *input)
2968{
2969 return input->data_device;
2970}
2971
2972void
2973input_set_selection(struct input *input,
2974 struct wl_data_source *source, uint32_t time)
2975{
2976 wl_data_device_set_selection(input->data_device, source, time);
2977}
2978
2979void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002980input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002981{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002982 wl_data_offer_accept(input->drag_offer->offer,
2983 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002984}
2985
2986static void
2987offer_io_func(struct task *task, uint32_t events)
2988{
2989 struct data_offer *offer =
2990 container_of(task, struct data_offer, io_task);
2991 unsigned int len;
2992 char buffer[4096];
2993
2994 len = read(offer->fd, buffer, sizeof buffer);
2995 offer->func(buffer, len,
2996 offer->x, offer->y, offer->user_data);
2997
2998 if (len == 0) {
2999 close(offer->fd);
3000 data_offer_destroy(offer);
3001 }
3002}
3003
3004static void
3005data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3006 data_func_t func, void *user_data)
3007{
3008 int p[2];
3009
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003010 if (pipe2(p, O_CLOEXEC) == -1)
3011 return;
3012
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003013 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3014 close(p[1]);
3015
3016 offer->io_task.run = offer_io_func;
3017 offer->fd = p[0];
3018 offer->func = func;
3019 offer->refcount++;
3020 offer->user_data = user_data;
3021
3022 display_watch_fd(offer->input->display,
3023 offer->fd, EPOLLIN, &offer->io_task);
3024}
3025
3026void
3027input_receive_drag_data(struct input *input, const char *mime_type,
3028 data_func_t func, void *data)
3029{
3030 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3031 input->drag_offer->x = input->sx;
3032 input->drag_offer->y = input->sy;
3033}
3034
3035int
3036input_receive_selection_data(struct input *input, const char *mime_type,
3037 data_func_t func, void *data)
3038{
3039 char **p;
3040
3041 if (input->selection_offer == NULL)
3042 return -1;
3043
3044 for (p = input->selection_offer->types.data; *p; p++)
3045 if (strcmp(mime_type, *p) == 0)
3046 break;
3047
3048 if (*p == NULL)
3049 return -1;
3050
3051 data_offer_receive_data(input->selection_offer,
3052 mime_type, func, data);
3053 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003054}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003055
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003056int
3057input_receive_selection_data_to_fd(struct input *input,
3058 const char *mime_type, int fd)
3059{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003060 if (input->selection_offer)
3061 wl_data_offer_receive(input->selection_offer->offer,
3062 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003063
3064 return 0;
3065}
3066
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003067void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003068window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003069{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003070 if (!window->shell_surface)
3071 return;
3072
Daniel Stone37816df2012-05-16 18:45:18 +01003073 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003074}
3075
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003076static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003077idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003078{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003079 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003080
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003081 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003082 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003083 widget_set_allocation(widget,
3084 window->pending_allocation.x,
3085 window->pending_allocation.y,
3086 window->pending_allocation.width,
3087 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003088
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003089 if (window->input_region) {
3090 wl_region_destroy(window->input_region);
3091 window->input_region = NULL;
3092 }
3093
3094 if (window->opaque_region) {
3095 wl_region_destroy(window->opaque_region);
3096 window->opaque_region = NULL;
3097 }
3098
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003099 if (widget->resize_handler)
3100 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003101 widget->allocation.width,
3102 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003103 widget->user_data);
3104
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003105 if (window->allocation.width != widget->allocation.width ||
3106 window->allocation.height != widget->allocation.height) {
3107 window->allocation = widget->allocation;
3108 window_schedule_redraw(window);
3109 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003110}
3111
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003112void
3113window_schedule_resize(struct window *window, int width, int height)
3114{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003115 window->pending_allocation.x = 0;
3116 window->pending_allocation.y = 0;
3117 window->pending_allocation.width = width;
3118 window->pending_allocation.height = height;
3119
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003120 if (window->min_allocation.width == 0)
3121 window->min_allocation = window->pending_allocation;
3122 if (window->pending_allocation.width < window->min_allocation.width)
3123 window->pending_allocation.width = window->min_allocation.width;
3124 if (window->pending_allocation.height < window->min_allocation.height)
3125 window->pending_allocation.height = window->min_allocation.height;
3126
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003127 window->resize_needed = 1;
3128 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003129}
3130
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003131void
3132widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3133{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003134 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003135}
3136
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003137static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003138handle_ping(void *data, struct wl_shell_surface *shell_surface,
3139 uint32_t serial)
3140{
3141 wl_shell_surface_pong(shell_surface, serial);
3142}
3143
3144static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003145handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003146 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003147{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003148 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003149
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003150 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003151 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003152}
3153
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003154static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003155menu_destroy(struct menu *menu)
3156{
3157 widget_destroy(menu->widget);
3158 window_destroy(menu->window);
3159 free(menu);
3160}
3161
3162static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003163handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3164{
3165 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003166 struct menu *menu = window->widget->user_data;
3167
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003168 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003169 * device. Or just use wl_callback. And this really needs to
3170 * be a window vfunc that the menu can set. And we need the
3171 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003172
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003173 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003174 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003175 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003176}
3177
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003178static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003179 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003180 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003181 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003182};
3183
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003184void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003185window_get_allocation(struct window *window,
3186 struct rectangle *allocation)
3187{
3188 *allocation = window->allocation;
3189}
3190
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003191static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003192widget_redraw(struct widget *widget)
3193{
3194 struct widget *child;
3195
3196 if (widget->redraw_handler)
3197 widget->redraw_handler(widget, widget->user_data);
3198 wl_list_for_each(child, &widget->child_list, link)
3199 widget_redraw(child);
3200}
3201
3202static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003203frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3204{
3205 struct window *window = data;
3206
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003207 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003208 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003209 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003210 window->redraw_scheduled = 0;
3211 if (window->redraw_needed)
3212 window_schedule_redraw(window);
3213}
3214
3215static const struct wl_callback_listener listener = {
3216 frame_callback
3217};
3218
3219static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003220idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003221{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003222 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003223
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003224 if (window->resize_needed)
3225 idle_resize(window);
3226
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003227 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003228 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003229 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003230 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003231
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003232 window->frame_cb = wl_surface_frame(window->surface);
3233 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003234 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003235}
3236
3237void
3238window_schedule_redraw(struct window *window)
3239{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003240 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003241 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003242 window->redraw_task.run = idle_redraw;
3243 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003244 window->redraw_scheduled = 1;
3245 }
3246}
3247
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003248int
3249window_is_fullscreen(struct window *window)
3250{
3251 return window->type == TYPE_FULLSCREEN;
3252}
3253
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003254void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003255window_set_fullscreen(struct window *window, int fullscreen)
3256{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003257 if (!window->display->shell)
3258 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003259
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003260 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003261 return;
3262
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003263 if (fullscreen) {
3264 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003265 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003266 wl_shell_surface_set_fullscreen(window->shell_surface,
3267 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3268 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003269 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003270 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003271 wl_shell_surface_set_toplevel(window->shell_surface);
3272 window_schedule_resize(window,
3273 window->saved_allocation.width,
3274 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003275 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003276}
3277
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003278int
3279window_is_maximized(struct window *window)
3280{
3281 return window->type == TYPE_MAXIMIZED;
3282}
3283
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003284void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003285window_set_maximized(struct window *window, int maximized)
3286{
3287 if (!window->display->shell)
3288 return;
3289
3290 if ((window->type == TYPE_MAXIMIZED) == maximized)
3291 return;
3292
3293 if (window->type == TYPE_TOPLEVEL) {
3294 window->saved_allocation = window->allocation;
3295 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3296 window->type = TYPE_MAXIMIZED;
3297 } else {
3298 wl_shell_surface_set_toplevel(window->shell_surface);
3299 window->type = TYPE_TOPLEVEL;
3300 window_schedule_resize(window,
3301 window->saved_allocation.width,
3302 window->saved_allocation.height);
3303 }
3304}
3305
3306void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003307window_set_user_data(struct window *window, void *data)
3308{
3309 window->user_data = data;
3310}
3311
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003312void *
3313window_get_user_data(struct window *window)
3314{
3315 return window->user_data;
3316}
3317
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003318void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003319window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003320 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003321{
3322 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003323}
3324
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003325void
3326window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003327 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003328{
3329 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003330}
3331
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003332void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003333window_set_data_handler(struct window *window, window_data_handler_t handler)
3334{
3335 window->data_handler = handler;
3336}
3337
3338void
3339window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3340{
3341 window->drop_handler = handler;
3342}
3343
3344void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003345window_set_close_handler(struct window *window,
3346 window_close_handler_t handler)
3347{
3348 window->close_handler = handler;
3349}
3350
3351void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003352window_set_fullscreen_handler(struct window *window,
3353 window_fullscreen_handler_t handler)
3354{
3355 window->fullscreen_handler = handler;
3356}
3357
3358void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003359window_set_title(struct window *window, const char *title)
3360{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003361 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003362 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003363 if (window->shell_surface)
3364 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003365}
3366
3367const char *
3368window_get_title(struct window *window)
3369{
3370 return window->title;
3371}
3372
3373void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003374window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3375{
3376 struct text_cursor_position *text_cursor_position =
3377 window->display->text_cursor_position;
3378
Scott Moreau9295ce02012-06-01 12:46:10 -06003379 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003380 return;
3381
3382 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003383 window->surface,
3384 wl_fixed_from_int(x),
3385 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003386}
3387
3388void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003389window_damage(struct window *window, int32_t x, int32_t y,
3390 int32_t width, int32_t height)
3391{
3392 wl_surface_damage(window->surface, x, y, width, height);
3393}
3394
Casey Dahlin9074db52012-04-19 22:50:09 -04003395static void
3396surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003397 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003398{
Rob Bradford7507b572012-05-15 17:55:34 +01003399 struct window *window = data;
3400 struct output *output;
3401 struct output *output_found = NULL;
3402 struct window_output *window_output;
3403
3404 wl_list_for_each(output, &window->display->output_list, link) {
3405 if (output->output == wl_output) {
3406 output_found = output;
3407 break;
3408 }
3409 }
3410
3411 if (!output_found)
3412 return;
3413
3414 window_output = malloc (sizeof *window_output);
3415 window_output->output = output_found;
3416
3417 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003418}
3419
3420static void
3421surface_leave(void *data,
3422 struct wl_surface *wl_surface, struct wl_output *output)
3423{
Rob Bradford7507b572012-05-15 17:55:34 +01003424 struct window *window = data;
3425 struct window_output *window_output;
3426 struct window_output *window_output_found = NULL;
3427
3428 wl_list_for_each(window_output, &window->window_output_list, link) {
3429 if (window_output->output->output == output) {
3430 window_output_found = window_output;
3431 break;
3432 }
3433 }
3434
3435 if (window_output_found) {
3436 wl_list_remove(&window_output_found->link);
3437 free(window_output_found);
3438 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003439}
3440
3441static const struct wl_surface_listener surface_listener = {
3442 surface_enter,
3443 surface_leave
3444};
3445
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003446static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003447window_create_internal(struct display *display,
3448 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003449{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003450 struct window *window;
3451
3452 window = malloc(sizeof *window);
3453 if (window == NULL)
3454 return NULL;
3455
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003456 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003457 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003458 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003459 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003460 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003461 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003462 window->shell_surface =
3463 wl_shell_get_shell_surface(display->shell,
3464 window->surface);
3465 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003466 window->allocation.x = 0;
3467 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003468 window->allocation.width = 0;
3469 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003470 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003471 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003472 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003473 window->input_region = NULL;
3474 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003475
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003476 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003477#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003478 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003479#else
3480 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3481#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003482 else
3483 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003484
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003485 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003486 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003487 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003488
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003489 if (window->shell_surface) {
3490 wl_shell_surface_set_user_data(window->shell_surface, window);
3491 wl_shell_surface_add_listener(window->shell_surface,
3492 &shell_surface_listener, window);
3493 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003494
Rob Bradford7507b572012-05-15 17:55:34 +01003495 wl_list_init (&window->window_output_list);
3496
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003497 return window;
3498}
3499
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003500struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003501window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003502{
3503 struct window *window;
3504
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003505 window = window_create_internal(display, NULL, TYPE_NONE);
3506 if (!window)
3507 return NULL;
3508
3509 return window;
3510}
3511
3512struct window *
3513window_create_custom(struct display *display)
3514{
3515 struct window *window;
3516
3517 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003518 if (!window)
3519 return NULL;
3520
3521 return window;
3522}
3523
3524struct window *
3525window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003526 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003527{
3528 struct window *window;
3529
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003530 window = window_create_internal(parent->display,
3531 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003532 if (!window)
3533 return NULL;
3534
3535 window->x = x;
3536 window->y = y;
3537
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003538 if (display->shell)
3539 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003540 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003541 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003542
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003543 return window;
3544}
3545
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003546static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003547menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003548{
3549 int next;
3550
3551 next = (sy - 8) / 20;
3552 if (menu->current != next) {
3553 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003554 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003555 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003556}
3557
3558static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003559menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003560 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003561 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003562{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003563 struct menu *menu = data;
3564
3565 if (widget == menu->widget)
3566 menu_set_item(data, y);
3567
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003568 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003569}
3570
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003571static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003572menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003573 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003574{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003575 struct menu *menu = data;
3576
3577 if (widget == menu->widget)
3578 menu_set_item(data, y);
3579
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003580 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003581}
3582
3583static void
3584menu_leave_handler(struct widget *widget, struct input *input, void *data)
3585{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003586 struct menu *menu = data;
3587
3588 if (widget == menu->widget)
3589 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003590}
3591
3592static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003593menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003594 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003595 uint32_t button, enum wl_pointer_button_state state,
3596 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003597
3598{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003599 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003600
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003601 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3602 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003603 /* Either relase after press-drag-release or
3604 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003605 menu->func(menu->window->parent,
3606 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003607 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003608 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003609 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003610 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003611 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003612}
3613
3614static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003615menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003616{
3617 cairo_t *cr;
3618 const int32_t r = 3, margin = 3;
3619 struct menu *menu = data;
3620 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003621 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003622
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003623 cr = cairo_create(window->cairo_surface);
3624 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3625 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3626 cairo_paint(cr);
3627
3628 width = window->allocation.width;
3629 height = window->allocation.height;
3630 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003631
3632 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003633 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3634 cairo_fill(cr);
3635
3636 for (i = 0; i < menu->count; i++) {
3637 if (i == menu->current) {
3638 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3639 cairo_rectangle(cr, margin, i * 20 + margin,
3640 width - 2 * margin, 20);
3641 cairo_fill(cr);
3642 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3643 cairo_move_to(cr, 10, i * 20 + 16);
3644 cairo_show_text(cr, menu->entries[i]);
3645 } else {
3646 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3647 cairo_move_to(cr, 10, i * 20 + 16);
3648 cairo_show_text(cr, menu->entries[i]);
3649 }
3650 }
3651
3652 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003653}
3654
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003655void
3656window_show_menu(struct display *display,
3657 struct input *input, uint32_t time, struct window *parent,
3658 int32_t x, int32_t y,
3659 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003660{
3661 struct window *window;
3662 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003663 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003664
3665 menu = malloc(sizeof *menu);
3666 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003667 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003668
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003669 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003670 if (!window) {
3671 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003672 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003673 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003674
3675 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003676 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003677 menu->entries = entries;
3678 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003679 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003680 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003681 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003682 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003683 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003684 window->type = TYPE_MENU;
3685 window->x = x;
3686 window->y = y;
3687
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003688 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003689 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003690 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003691 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003692 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003693
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003694 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003695 widget_set_enter_handler(menu->widget, menu_enter_handler);
3696 widget_set_leave_handler(menu->widget, menu_leave_handler);
3697 widget_set_motion_handler(menu->widget, menu_motion_handler);
3698 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003699
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003700 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003701 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003702}
3703
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003704void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003705window_set_buffer_type(struct window *window, enum window_buffer_type type)
3706{
3707 window->buffer_type = type;
3708}
3709
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003710
3711static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003712display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003713 struct wl_output *wl_output,
3714 int x, int y,
3715 int physical_width,
3716 int physical_height,
3717 int subpixel,
3718 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003719 const char *model,
3720 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003721{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003722 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003723
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003724 output->allocation.x = x;
3725 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003726 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003727}
3728
3729static void
3730display_handle_mode(void *data,
3731 struct wl_output *wl_output,
3732 uint32_t flags,
3733 int width,
3734 int height,
3735 int refresh)
3736{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003737 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003738 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003739
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003740 if (flags & WL_OUTPUT_MODE_CURRENT) {
3741 output->allocation.width = width;
3742 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003743 if (display->output_configure_handler)
3744 (*display->output_configure_handler)(
3745 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003746 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003747}
3748
3749static const struct wl_output_listener output_listener = {
3750 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003751 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003752};
3753
3754static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003755display_add_output(struct display *d, uint32_t id)
3756{
3757 struct output *output;
3758
3759 output = malloc(sizeof *output);
3760 if (output == NULL)
3761 return;
3762
3763 memset(output, 0, sizeof *output);
3764 output->display = d;
3765 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003766 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003767 wl_list_insert(d->output_list.prev, &output->link);
3768
3769 wl_output_add_listener(output->output, &output_listener, output);
3770}
3771
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003772static void
3773output_destroy(struct output *output)
3774{
3775 if (output->destroy_handler)
3776 (*output->destroy_handler)(output, output->user_data);
3777
3778 wl_output_destroy(output->output);
3779 wl_list_remove(&output->link);
3780 free(output);
3781}
3782
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003783void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003784display_set_global_handler(struct display *display,
3785 display_global_handler_t handler)
3786{
3787 struct global *global;
3788
3789 display->global_handler = handler;
3790 if (!handler)
3791 return;
3792
3793 wl_list_for_each(global, &display->global_list, link)
3794 display->global_handler(display,
3795 global->name, global->interface,
3796 global->version, display->user_data);
3797}
3798
3799void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003800display_set_output_configure_handler(struct display *display,
3801 display_output_handler_t handler)
3802{
3803 struct output *output;
3804
3805 display->output_configure_handler = handler;
3806 if (!handler)
3807 return;
3808
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003809 wl_list_for_each(output, &display->output_list, link) {
3810 if (output->allocation.width == 0 &&
3811 output->allocation.height == 0)
3812 continue;
3813
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003814 (*display->output_configure_handler)(output,
3815 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003816 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003817}
3818
3819void
3820output_set_user_data(struct output *output, void *data)
3821{
3822 output->user_data = data;
3823}
3824
3825void *
3826output_get_user_data(struct output *output)
3827{
3828 return output->user_data;
3829}
3830
3831void
3832output_set_destroy_handler(struct output *output,
3833 display_output_handler_t handler)
3834{
3835 output->destroy_handler = handler;
3836 /* FIXME: implement this, once we have way to remove outputs */
3837}
3838
3839void
Scott Moreau4e072362012-09-29 02:03:11 -06003840output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003841{
Scott Moreau4e072362012-09-29 02:03:11 -06003842 struct rectangle allocation = output->allocation;
3843
3844 switch (output->transform) {
3845 case WL_OUTPUT_TRANSFORM_90:
3846 case WL_OUTPUT_TRANSFORM_270:
3847 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3848 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3849 /* Swap width and height */
3850 allocation.width = output->allocation.height;
3851 allocation.height = output->allocation.width;
3852 break;
3853 }
3854
3855 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003856}
3857
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003858struct wl_output *
3859output_get_wl_output(struct output *output)
3860{
3861 return output->output;
3862}
3863
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003864static void
Daniel Stone97f68542012-05-30 16:32:01 +01003865fini_xkb(struct input *input)
3866{
3867 xkb_state_unref(input->xkb.state);
3868 xkb_map_unref(input->xkb.keymap);
3869}
3870
3871static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003872display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003873{
3874 struct input *input;
3875
3876 input = malloc(sizeof *input);
3877 if (input == NULL)
3878 return;
3879
3880 memset(input, 0, sizeof *input);
3881 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003882 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003883 input->pointer_focus = NULL;
3884 input->keyboard_focus = NULL;
3885 wl_list_insert(d->input_list.prev, &input->link);
3886
Daniel Stone37816df2012-05-16 18:45:18 +01003887 wl_seat_add_listener(input->seat, &seat_listener, input);
3888 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003889
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003890 input->data_device =
3891 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003892 input->seat);
3893 wl_data_device_add_listener(input->data_device, &data_device_listener,
3894 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003895
3896 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003897
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003898 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3899 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003900 input->repeat_task.run = keyboard_repeat_func;
3901 display_watch_fd(d, input->repeat_timer_fd,
3902 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003903}
3904
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003905static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003906input_destroy(struct input *input)
3907{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003908 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003909 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003910
3911 if (input->drag_offer)
3912 data_offer_destroy(input->drag_offer);
3913
3914 if (input->selection_offer)
3915 data_offer_destroy(input->selection_offer);
3916
3917 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003918 fini_xkb(input);
3919
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003920 wl_surface_destroy(input->pointer_surface);
3921
Pekka Paalanene1207c72011-12-16 12:02:09 +02003922 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003923 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003924 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003925 free(input);
3926}
3927
3928static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003929init_workspace_manager(struct display *d, uint32_t id)
3930{
3931 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003932 wl_registry_bind(d->registry, id,
3933 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003934 if (d->workspace_manager != NULL)
3935 workspace_manager_add_listener(d->workspace_manager,
3936 &workspace_manager_listener,
3937 d);
3938}
3939
3940static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003941registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3942 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003943{
3944 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003945 struct global *global;
3946
3947 global = malloc(sizeof *global);
3948 global->name = id;
3949 global->interface = strdup(interface);
3950 global->version = version;
3951 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003952
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003953 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003954 d->compositor = wl_registry_bind(registry, id,
3955 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003956 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003957 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003958 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003959 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003960 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003961 d->shell = wl_registry_bind(registry,
3962 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003963 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003964 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003965 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3966 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003967 wl_registry_bind(registry, id,
3968 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003969 } else if (strcmp(interface, "text_cursor_position") == 0) {
3970 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003971 wl_registry_bind(registry, id,
3972 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003973 } else if (strcmp(interface, "workspace_manager") == 0) {
3974 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003975 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003976
3977 if (d->global_handler)
3978 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003979}
3980
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003981void *
3982display_bind(struct display *display, uint32_t name,
3983 const struct wl_interface *interface, uint32_t version)
3984{
3985 return wl_registry_bind(display->registry, name, interface, version);
3986}
3987
3988static const struct wl_registry_listener registry_listener = {
3989 registry_handle_global
3990};
3991
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003992#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003993static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003994init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003995{
3996 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003997 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003998
Rob Clark6396ed32012-03-11 19:48:41 -05003999#ifdef USE_CAIRO_GLESV2
4000# define GL_BIT EGL_OPENGL_ES2_BIT
4001#else
4002# define GL_BIT EGL_OPENGL_BIT
4003#endif
4004
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004005 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004006 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004007 EGL_RED_SIZE, 1,
4008 EGL_GREEN_SIZE, 1,
4009 EGL_BLUE_SIZE, 1,
4010 EGL_ALPHA_SIZE, 1,
4011 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004012 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004013 EGL_NONE
4014 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004015
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004016#ifdef USE_CAIRO_GLESV2
4017 static const EGLint context_attribs[] = {
4018 EGL_CONTEXT_CLIENT_VERSION, 2,
4019 EGL_NONE
4020 };
4021 EGLint api = EGL_OPENGL_ES_API;
4022#else
4023 EGLint *context_attribs = NULL;
4024 EGLint api = EGL_OPENGL_API;
4025#endif
4026
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004027 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004028 if (!eglInitialize(d->dpy, &major, &minor)) {
4029 fprintf(stderr, "failed to initialize display\n");
4030 return -1;
4031 }
4032
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004033 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004034 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
4035 return -1;
4036 }
4037
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004038 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4039 &d->argb_config, 1, &n) || n != 1) {
4040 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004041 return -1;
4042 }
4043
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004044 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004045 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004046 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004047 fprintf(stderr, "failed to create context\n");
4048 return -1;
4049 }
4050
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004051 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01004052 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004053 return -1;
4054 }
4055
Benjamin Franzke0c991632011-09-27 21:57:31 +02004056 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4057 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4058 fprintf(stderr, "failed to get cairo egl argb device\n");
4059 return -1;
4060 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004061
4062 return 0;
4063}
4064
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004065static void
4066fini_egl(struct display *display)
4067{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004068 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004069
4070 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4071 EGL_NO_CONTEXT);
4072
4073 eglTerminate(display->dpy);
4074 eglReleaseThread();
4075}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004076#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004077
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004078static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004079init_dummy_surface(struct display *display)
4080{
4081 int len;
4082 void *data;
4083
4084 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4085 data = malloc(len);
4086 display->dummy_surface =
4087 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4088 1, 1, len);
4089 display->dummy_surface_data = data;
4090}
4091
4092static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004093handle_display_data(struct task *task, uint32_t events)
4094{
4095 struct display *display =
4096 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004097 struct epoll_event ep;
4098 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004099
4100 display->display_fd_events = events;
4101
4102 if (events & EPOLLERR || events & EPOLLHUP) {
4103 display_exit(display);
4104 return;
4105 }
4106
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004107 if (events & EPOLLIN) {
4108 ret = wl_display_dispatch(display->display);
4109 if (ret == -1) {
4110 display_exit(display);
4111 return;
4112 }
4113 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004114
4115 if (events & EPOLLOUT) {
4116 ret = wl_display_flush(display->display);
4117 if (ret == 0) {
4118 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4119 ep.data.ptr = &display->display_task;
4120 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4121 display->display_fd, &ep);
4122 } else if (ret == -1 && errno != EAGAIN) {
4123 display_exit(display);
4124 return;
4125 }
4126 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004127}
4128
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004129struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004130display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004131{
4132 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004133
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004134 d = malloc(sizeof *d);
4135 if (d == NULL)
4136 return NULL;
4137
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004138 memset(d, 0, sizeof *d);
4139
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004140 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004141 if (d->display == NULL) {
4142 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004143 return NULL;
4144 }
4145
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004146 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004147 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004148 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004149 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4150 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004151
4152 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004153 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004154 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004155 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004156
Daniel Stone97f68542012-05-30 16:32:01 +01004157 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004158 if (d->xkb_context == NULL) {
4159 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004160 return NULL;
4161 }
4162
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004163 d->workspace = 0;
4164 d->workspace_count = 1;
4165
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004166 d->registry = wl_display_get_registry(d->display);
4167 wl_registry_add_listener(d->registry, &registry_listener, d);
4168 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004169#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004170 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004171 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004172#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004173
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004174 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004175
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004176 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004177
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004178 wl_list_init(&d->window_list);
4179
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004180 init_dummy_surface(d);
4181
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004182 return d;
4183}
4184
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004185static void
4186display_destroy_outputs(struct display *display)
4187{
4188 struct output *tmp;
4189 struct output *output;
4190
4191 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4192 output_destroy(output);
4193}
4194
Pekka Paalanene1207c72011-12-16 12:02:09 +02004195static void
4196display_destroy_inputs(struct display *display)
4197{
4198 struct input *tmp;
4199 struct input *input;
4200
4201 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4202 input_destroy(input);
4203}
4204
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004205void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004206display_destroy(struct display *display)
4207{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004208 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004209 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4210 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004211
4212 if (!wl_list_empty(&display->deferred_list))
4213 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4214
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004215 cairo_surface_destroy(display->dummy_surface);
4216 free(display->dummy_surface_data);
4217
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004218 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004219 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004220
Daniel Stone97f68542012-05-30 16:32:01 +01004221 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004222
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004223 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004224 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004225
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004226#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004227 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004228#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004229
Pekka Paalanenc2052982011-12-16 11:41:32 +02004230 if (display->shell)
4231 wl_shell_destroy(display->shell);
4232
4233 if (display->shm)
4234 wl_shm_destroy(display->shm);
4235
4236 if (display->data_device_manager)
4237 wl_data_device_manager_destroy(display->data_device_manager);
4238
4239 wl_compositor_destroy(display->compositor);
4240
4241 close(display->epoll_fd);
4242
U. Artie Eoff44874d92012-10-02 21:12:35 -07004243 if (!(display->display_fd_events & EPOLLERR) &&
4244 !(display->display_fd_events & EPOLLHUP))
4245 wl_display_flush(display->display);
4246
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004247 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004248 free(display);
4249}
4250
4251void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004252display_set_user_data(struct display *display, void *data)
4253{
4254 display->user_data = data;
4255}
4256
4257void *
4258display_get_user_data(struct display *display)
4259{
4260 return display->user_data;
4261}
4262
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004263struct wl_display *
4264display_get_display(struct display *display)
4265{
4266 return display->display;
4267}
4268
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004269struct output *
4270display_get_output(struct display *display)
4271{
4272 return container_of(display->output_list.next, struct output, link);
4273}
4274
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004275struct wl_compositor *
4276display_get_compositor(struct display *display)
4277{
4278 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004279}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004280
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004281uint32_t
4282display_get_serial(struct display *display)
4283{
4284 return display->serial;
4285}
4286
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004287EGLDisplay
4288display_get_egl_display(struct display *d)
4289{
4290 return d->dpy;
4291}
4292
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004293struct wl_data_source *
4294display_create_data_source(struct display *display)
4295{
4296 return wl_data_device_manager_create_data_source(display->data_device_manager);
4297}
4298
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004299EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004300display_get_argb_egl_config(struct display *d)
4301{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004302 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004303}
4304
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004305struct wl_shell *
4306display_get_shell(struct display *display)
4307{
4308 return display->shell;
4309}
4310
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004311int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004312display_acquire_window_surface(struct display *display,
4313 struct window *window,
4314 EGLContext ctx)
4315{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004316 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004317 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004318
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004319 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004320}
4321
4322void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004323display_release_window_surface(struct display *display,
4324 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004325{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004326 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004327 return;
4328
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004329 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004330}
4331
4332void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004333display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004334{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004335 wl_list_insert(&display->deferred_list, &task->link);
4336}
4337
4338void
4339display_watch_fd(struct display *display,
4340 int fd, uint32_t events, struct task *task)
4341{
4342 struct epoll_event ep;
4343
4344 ep.events = events;
4345 ep.data.ptr = task;
4346 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4347}
4348
4349void
4350display_run(struct display *display)
4351{
4352 struct task *task;
4353 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004354 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004355
Pekka Paalanen826d7952011-12-15 10:14:07 +02004356 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004357 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004358 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004359 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004360 struct task, link);
4361 wl_list_remove(&task->link);
4362 task->run(task, 0);
4363 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004364
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004365 wl_display_dispatch_pending(display->display);
4366
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004367 if (!display->running)
4368 break;
4369
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004370 ret = wl_display_flush(display->display);
4371 if (ret < 0 && errno == EAGAIN) {
4372 ep[0].events =
4373 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4374 ep[0].data.ptr = &display->display_task;
4375
4376 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4377 display->display_fd, &ep[0]);
4378 } else if (ret < 0) {
4379 break;
4380 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004381
4382 count = epoll_wait(display->epoll_fd,
4383 ep, ARRAY_LENGTH(ep), -1);
4384 for (i = 0; i < count; i++) {
4385 task = ep[i].data.ptr;
4386 task->run(task, ep[i].events);
4387 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004388 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004389}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004390
4391void
4392display_exit(struct display *display)
4393{
4394 display->running = 0;
4395}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004396
4397void
4398keysym_modifiers_add(struct wl_array *modifiers_map,
4399 const char *name)
4400{
4401 size_t len = strlen(name) + 1;
4402 char *p;
4403
4404 p = wl_array_add(modifiers_map, len);
4405
4406 if (p == NULL)
4407 return;
4408
4409 strncpy(p, name, len);
4410}
4411
4412static xkb_mod_index_t
4413keysym_modifiers_get_index(struct wl_array *modifiers_map,
4414 const char *name)
4415{
4416 xkb_mod_index_t index = 0;
4417 char *p = modifiers_map->data;
4418
4419 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4420 if (strcmp(p, name) == 0)
4421 return index;
4422
4423 index++;
4424 p += strlen(p) + 1;
4425 }
4426
4427 return XKB_MOD_INVALID;
4428}
4429
4430xkb_mod_mask_t
4431keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4432 const char *name)
4433{
4434 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4435
4436 if (index == XKB_MOD_INVALID)
4437 return XKB_MOD_INVALID;
4438
4439 return 1 << index;
4440}