blob: fa247ab471f08511fe6557e98a016b51c37e44ea [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
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001718 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001719 wl_region_add(widget->window->opaque_region,
1720 opaque_margin, opaque_margin,
1721 widget->allocation.width - 2 * opaque_margin,
1722 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001723
1724 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001725 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1726 x_l = t->width + shadow_margin;
1727 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001728 wl_list_for_each(button, &frame->buttons_list, link) {
1729 const int button_padding = 4;
1730 w = cairo_image_surface_get_width(button->icon);
1731 h = cairo_image_surface_get_height(button->icon);
1732
1733 if (button->decoration == FRAME_BUTTON_FANCY)
1734 w += 10;
1735
1736 if (button->align == FRAME_BUTTON_LEFT) {
1737 widget_set_allocation(button->widget,
1738 x_l, y , w + 1, h + 1);
1739 x_l += w;
1740 x_l += button_padding;
1741 } else {
1742 x_r -= w;
1743 widget_set_allocation(button->widget,
1744 x_r, y , w + 1, h + 1);
1745 x_r -= button_padding;
1746 }
1747 }
1748}
1749
1750static int
1751frame_button_enter_handler(struct widget *widget,
1752 struct input *input, float x, float y, void *data)
1753{
1754 struct frame_button *frame_button = data;
1755
1756 widget_schedule_redraw(frame_button->widget);
1757 frame_button->state = FRAME_BUTTON_OVER;
1758
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001759 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001760}
1761
1762static void
1763frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1764{
1765 struct frame_button *frame_button = data;
1766
1767 widget_schedule_redraw(frame_button->widget);
1768 frame_button->state = FRAME_BUTTON_DEFAULT;
1769}
1770
1771static void
1772frame_button_button_handler(struct widget *widget,
1773 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001774 uint32_t button,
1775 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001776{
1777 struct frame_button *frame_button = data;
1778 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001779 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001780
1781 if (button != BTN_LEFT)
1782 return;
1783
1784 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001785 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001786 frame_button->state = FRAME_BUTTON_ACTIVE;
1787 widget_schedule_redraw(frame_button->widget);
1788
1789 if (frame_button->type == FRAME_BUTTON_ICON)
1790 window_show_frame_menu(window, input, time);
1791 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001792 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001793 frame_button->state = FRAME_BUTTON_DEFAULT;
1794 widget_schedule_redraw(frame_button->widget);
1795 break;
1796 }
1797
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001798 if (!was_pressed)
1799 return;
1800
Martin Minarik1998b152012-05-10 02:04:35 +02001801 switch (frame_button->type) {
1802 case FRAME_BUTTON_CLOSE:
1803 if (window->close_handler)
1804 window->close_handler(window->parent,
1805 window->user_data);
1806 else
1807 display_exit(window->display);
1808 break;
1809 case FRAME_BUTTON_MINIMIZE:
1810 fprintf(stderr,"Minimize stub\n");
1811 break;
1812 case FRAME_BUTTON_MAXIMIZE:
1813 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1814 break;
1815 default:
1816 /* Unknown operation */
1817 break;
1818 }
1819}
1820
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001821static int
1822frame_button_motion_handler(struct widget *widget,
1823 struct input *input, uint32_t time,
1824 float x, float y, void *data)
1825{
1826 struct frame_button *frame_button = data;
1827 enum frame_button_pointer previous_button_state = frame_button->state;
1828
1829 /* only track state for a pressed button */
1830 if (input->grab != widget)
1831 return CURSOR_LEFT_PTR;
1832
1833 if (x > widget->allocation.x &&
1834 x < (widget->allocation.x + widget->allocation.width) &&
1835 y > widget->allocation.y &&
1836 y < (widget->allocation.y + widget->allocation.height)) {
1837 frame_button->state = FRAME_BUTTON_ACTIVE;
1838 } else {
1839 frame_button->state = FRAME_BUTTON_DEFAULT;
1840 }
1841
1842 if (frame_button->state != previous_button_state)
1843 widget_schedule_redraw(frame_button->widget);
1844
1845 return CURSOR_LEFT_PTR;
1846}
1847
Martin Minarik1998b152012-05-10 02:04:35 +02001848static void
1849frame_button_redraw_handler(struct widget *widget, void *data)
1850{
1851 struct frame_button *frame_button = data;
1852 cairo_t *cr;
1853 int width, height, x, y;
1854 struct window *window = widget->window;
1855
1856 x = widget->allocation.x;
1857 y = widget->allocation.y;
1858 width = widget->allocation.width;
1859 height = widget->allocation.height;
1860
1861 if (!width)
1862 return;
1863 if (!height)
1864 return;
1865 if (widget->opaque)
1866 return;
1867
1868 cr = cairo_create(window->cairo_surface);
1869
1870 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1871 cairo_set_line_width(cr, 1);
1872
1873 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1874 cairo_rectangle (cr, x, y, 25, 16);
1875
1876 cairo_stroke_preserve(cr);
1877
1878 switch (frame_button->state) {
1879 case FRAME_BUTTON_DEFAULT:
1880 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1881 break;
1882 case FRAME_BUTTON_OVER:
1883 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1884 break;
1885 case FRAME_BUTTON_ACTIVE:
1886 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1887 break;
1888 }
1889
1890 cairo_fill (cr);
1891
1892 x += 4;
1893 }
1894
1895 cairo_set_source_surface(cr, frame_button->icon, x, y);
1896 cairo_paint(cr);
1897
1898 cairo_destroy(cr);
1899}
1900
1901static struct widget *
1902frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1903 enum frame_button_align align, enum frame_button_decoration style)
1904{
1905 struct frame_button *frame_button;
1906 const char *icon = data;
1907
1908 frame_button = malloc (sizeof *frame_button);
1909 memset(frame_button, 0, sizeof *frame_button);
1910
1911 frame_button->icon = cairo_image_surface_create_from_png(icon);
1912 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1913 frame_button->frame = frame;
1914 frame_button->type = type;
1915 frame_button->align = align;
1916 frame_button->decoration = style;
1917
1918 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1919
1920 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1921 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1922 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1923 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001924 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001925 return frame_button->widget;
1926}
1927
1928static void
1929frame_button_destroy(struct frame_button *frame_button)
1930{
1931 widget_destroy(frame_button->widget);
1932 wl_list_remove(&frame_button->link);
1933 cairo_surface_destroy(frame_button->icon);
1934 free(frame_button);
1935
1936 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001937}
1938
1939static void
1940frame_redraw_handler(struct widget *widget, void *data)
1941{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001942 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001943 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001944 struct theme *t = window->display->theme;
1945 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001946
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001947 if (window->type == TYPE_FULLSCREEN)
1948 return;
1949
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001950 cr = cairo_create(window->cairo_surface);
1951
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001952 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001953 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001954 if (window->type == TYPE_MAXIMIZED)
1955 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001956 theme_render_frame(t, cr, widget->allocation.width,
1957 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001958
1959 cairo_destroy(cr);
1960}
1961
1962static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001963frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001964{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001965 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001966 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001967 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001968
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001969 location = theme_get_location(t, input->sx, input->sy,
1970 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001971 frame->widget->allocation.height,
1972 window->type == TYPE_MAXIMIZED ?
1973 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001974
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001975 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001976 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001977 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001978 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001979 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001980 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001981 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001982 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001983 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001984 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001985 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001986 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001987 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001988 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001989 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001990 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001991 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001992 case THEME_LOCATION_EXTERIOR:
1993 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001994 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001995 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001996 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001997}
1998
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001999static void
2000frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002001{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002002 struct display *display;
2003
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002004 switch (index) {
2005 case 0: /* close */
2006 if (window->close_handler)
2007 window->close_handler(window->parent,
2008 window->user_data);
2009 else
2010 display_exit(window->display);
2011 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002012 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002013 display = window->display;
2014 if (display->workspace > 0)
2015 workspace_manager_move_surface(display->workspace_manager,
2016 window->surface,
2017 display->workspace - 1);
2018 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002019 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002020 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002021 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002022 workspace_manager_move_surface(display->workspace_manager,
2023 window->surface,
2024 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002025 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002026 case 3: /* fullscreen */
2027 /* we don't have a way to get out of fullscreen for now */
2028 if (window->fullscreen_handler)
2029 window->fullscreen_handler(window, window->user_data);
2030 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002031 }
2032}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002033
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002034void
2035window_show_frame_menu(struct window *window,
2036 struct input *input, uint32_t time)
2037{
2038 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002039 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002040
2041 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002042 "Close",
2043 "Move to workspace above", "Move to workspace below",
2044 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002045 };
2046
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002047 if (window->fullscreen_handler)
2048 count = ARRAY_LENGTH(entries);
2049 else
2050 count = ARRAY_LENGTH(entries) - 1;
2051
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002052 input_get_position(input, &x, &y);
2053 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002054 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002055}
2056
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002057static int
2058frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002059 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002060{
2061 return frame_get_pointer_image_for_location(data, input);
2062}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002063
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002064static int
2065frame_motion_handler(struct widget *widget,
2066 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002067 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002068{
2069 return frame_get_pointer_image_for_location(data, input);
2070}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002071
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002072static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002073frame_button_handler(struct widget *widget,
2074 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002075 uint32_t button, enum wl_pointer_button_state state,
2076 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002077
2078{
2079 struct frame *frame = data;
2080 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002081 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002082 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002083
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002084 location = theme_get_location(display->theme, input->sx, input->sy,
2085 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002086 frame->widget->allocation.height,
2087 window->type == TYPE_MAXIMIZED ?
2088 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002089
Daniel Stone4dbadb12012-05-30 16:31:51 +01002090 if (window->display->shell && button == BTN_LEFT &&
2091 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002092 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002093 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002094 if (!window->shell_surface)
2095 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002096 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002097 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002098 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002099 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002100 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002101 case THEME_LOCATION_RESIZING_TOP:
2102 case THEME_LOCATION_RESIZING_BOTTOM:
2103 case THEME_LOCATION_RESIZING_LEFT:
2104 case THEME_LOCATION_RESIZING_RIGHT:
2105 case THEME_LOCATION_RESIZING_TOP_LEFT:
2106 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2107 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2108 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002109 if (!window->shell_surface)
2110 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002111 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002112
Pekka Paalanen99436862012-11-19 17:15:59 +02002113 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002114 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002115 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002116 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002117 break;
2118 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002119 } else if (button == BTN_RIGHT &&
2120 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002121 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002122 }
2123}
2124
2125struct widget *
2126frame_create(struct window *window, void *data)
2127{
2128 struct frame *frame;
2129
2130 frame = malloc(sizeof *frame);
2131 memset(frame, 0, sizeof *frame);
2132
2133 frame->widget = window_add_widget(window, frame);
2134 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002135
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002136 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2137 widget_set_resize_handler(frame->widget, frame_resize_handler);
2138 widget_set_enter_handler(frame->widget, frame_enter_handler);
2139 widget_set_motion_handler(frame->widget, frame_motion_handler);
2140 widget_set_button_handler(frame->widget, frame_button_handler);
2141
Martin Minarik1998b152012-05-10 02:04:35 +02002142 /* Create empty list for frame buttons */
2143 wl_list_init(&frame->buttons_list);
2144
2145 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2146 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2147
2148 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2149 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2150
2151 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2152 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2153
2154 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2155 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2156
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002157 window->frame = frame;
2158
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002159 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002160}
2161
Kristian Høgsberga1627922012-06-20 17:30:03 -04002162void
2163frame_set_child_size(struct widget *widget, int child_width, int child_height)
2164{
2165 struct display *display = widget->window->display;
2166 struct theme *t = display->theme;
2167 int decoration_width, decoration_height;
2168 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002169 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002170
2171 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002172 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002173 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002174 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002175
2176 width = child_width + decoration_width;
2177 height = child_height + decoration_height;
2178 } else {
2179 width = child_width;
2180 height = child_height;
2181 }
2182
2183 window_schedule_resize(widget->window, width, height);
2184}
2185
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002186static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002187frame_destroy(struct frame *frame)
2188{
Martin Minarik1998b152012-05-10 02:04:35 +02002189 struct frame_button *button, *tmp;
2190
2191 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2192 frame_button_destroy(button);
2193
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002194 /* frame->child must be destroyed by the application */
2195 widget_destroy(frame->widget);
2196 free(frame);
2197}
2198
2199static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002200input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002201 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002202{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002203 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002204 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002205
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002206 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002207 return;
2208
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002209 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002210 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002211 widget = old;
2212 if (input->grab)
2213 widget = input->grab;
2214 if (widget->leave_handler)
2215 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002216 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002217 }
2218
2219 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002220 widget = focus;
2221 if (input->grab)
2222 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002223 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002224 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002225 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002226 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002227
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002228 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002229 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002230}
2231
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002232void
2233input_grab(struct input *input, struct widget *widget, uint32_t button)
2234{
2235 input->grab = widget;
2236 input->grab_button = button;
2237}
2238
2239void
2240input_ungrab(struct input *input)
2241{
2242 struct widget *widget;
2243
2244 input->grab = NULL;
2245 if (input->pointer_focus) {
2246 widget = widget_find_widget(input->pointer_focus->widget,
2247 input->sx, input->sy);
2248 input_set_focus_widget(input, widget, input->sx, input->sy);
2249 }
2250}
2251
2252static void
2253input_remove_pointer_focus(struct input *input)
2254{
2255 struct window *window = input->pointer_focus;
2256
2257 if (!window)
2258 return;
2259
2260 input_set_focus_widget(input, NULL, 0, 0);
2261
2262 input->pointer_focus = NULL;
2263 input->current_cursor = CURSOR_UNSET;
2264}
2265
2266static void
2267pointer_handle_enter(void *data, struct wl_pointer *pointer,
2268 uint32_t serial, struct wl_surface *surface,
2269 wl_fixed_t sx_w, wl_fixed_t sy_w)
2270{
2271 struct input *input = data;
2272 struct window *window;
2273 struct widget *widget;
2274 float sx = wl_fixed_to_double(sx_w);
2275 float sy = wl_fixed_to_double(sy_w);
2276
2277 if (!surface) {
2278 /* enter event for a window we've just destroyed */
2279 return;
2280 }
2281
2282 input->display->serial = serial;
2283 input->pointer_enter_serial = serial;
2284 input->pointer_focus = wl_surface_get_user_data(surface);
2285 window = input->pointer_focus;
2286
Pekka Paalanen99436862012-11-19 17:15:59 +02002287 if (window->resizing) {
2288 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002289 /* Schedule a redraw to free the pool */
2290 window_schedule_redraw(window);
2291 }
2292
2293 input->sx = sx;
2294 input->sy = sy;
2295
2296 widget = widget_find_widget(window->widget, sx, sy);
2297 input_set_focus_widget(input, widget, sx, sy);
2298}
2299
2300static void
2301pointer_handle_leave(void *data, struct wl_pointer *pointer,
2302 uint32_t serial, struct wl_surface *surface)
2303{
2304 struct input *input = data;
2305
2306 input->display->serial = serial;
2307 input_remove_pointer_focus(input);
2308}
2309
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002310static void
Daniel Stone37816df2012-05-16 18:45:18 +01002311pointer_handle_motion(void *data, struct wl_pointer *pointer,
2312 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002313{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002314 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002315 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002316 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002317 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002318 float sx = wl_fixed_to_double(sx_w);
2319 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002320
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002321 input->sx = sx;
2322 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002323
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002324 if (!window)
2325 return;
2326
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002327 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002328 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002329 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002330 }
2331
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002332 if (input->grab)
2333 widget = input->grab;
2334 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002335 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002336 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002337 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002338 input, time, sx, sy,
2339 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002340
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002341 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002342}
2343
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002344static void
Daniel Stone37816df2012-05-16 18:45:18 +01002345pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002346 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002347{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002348 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002349 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002350 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002351
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002352 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002353 if (input->focus_widget && input->grab == NULL &&
2354 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002355 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002356
Neil Roberts6b28aad2012-01-23 19:11:18 +00002357 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002358 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002359 (*widget->button_handler)(widget,
2360 input, time,
2361 button, state,
2362 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002363
Daniel Stone4dbadb12012-05-30 16:31:51 +01002364 if (input->grab && input->grab_button == button &&
2365 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002366 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002367}
2368
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002369static void
Daniel Stone37816df2012-05-16 18:45:18 +01002370pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002371 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002372{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002373 struct input *input = data;
2374 struct widget *widget;
2375
2376 widget = input->focus_widget;
2377 if (input->grab)
2378 widget = input->grab;
2379 if (widget && widget->axis_handler)
2380 (*widget->axis_handler)(widget,
2381 input, time,
2382 axis, value,
2383 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002384}
2385
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002386static const struct wl_pointer_listener pointer_listener = {
2387 pointer_handle_enter,
2388 pointer_handle_leave,
2389 pointer_handle_motion,
2390 pointer_handle_button,
2391 pointer_handle_axis,
2392};
2393
2394static void
2395input_remove_keyboard_focus(struct input *input)
2396{
2397 struct window *window = input->keyboard_focus;
2398 struct itimerspec its;
2399
2400 its.it_interval.tv_sec = 0;
2401 its.it_interval.tv_nsec = 0;
2402 its.it_value.tv_sec = 0;
2403 its.it_value.tv_nsec = 0;
2404 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2405
2406 if (!window)
2407 return;
2408
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002409 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002410 if (window->keyboard_focus_handler)
2411 (*window->keyboard_focus_handler)(window, NULL,
2412 window->user_data);
2413
2414 input->keyboard_focus = NULL;
2415}
2416
2417static void
2418keyboard_repeat_func(struct task *task, uint32_t events)
2419{
2420 struct input *input =
2421 container_of(task, struct input, repeat_task);
2422 struct window *window = input->keyboard_focus;
2423 uint64_t exp;
2424
2425 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2426 /* If we change the timer between the fd becoming
2427 * readable and getting here, there'll be nothing to
2428 * read and we get EAGAIN. */
2429 return;
2430
2431 if (window && window->key_handler) {
2432 (*window->key_handler)(window, input, input->repeat_time,
2433 input->repeat_key, input->repeat_sym,
2434 WL_KEYBOARD_KEY_STATE_PRESSED,
2435 window->user_data);
2436 }
2437}
2438
2439static void
2440keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2441 uint32_t format, int fd, uint32_t size)
2442{
2443 struct input *input = data;
2444 char *map_str;
2445
2446 if (!data) {
2447 close(fd);
2448 return;
2449 }
2450
2451 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2452 close(fd);
2453 return;
2454 }
2455
2456 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2457 if (map_str == MAP_FAILED) {
2458 close(fd);
2459 return;
2460 }
2461
2462 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2463 map_str,
2464 XKB_KEYMAP_FORMAT_TEXT_V1,
2465 0);
2466 munmap(map_str, size);
2467 close(fd);
2468
2469 if (!input->xkb.keymap) {
2470 fprintf(stderr, "failed to compile keymap\n");
2471 return;
2472 }
2473
2474 input->xkb.state = xkb_state_new(input->xkb.keymap);
2475 if (!input->xkb.state) {
2476 fprintf(stderr, "failed to create XKB state\n");
2477 xkb_map_unref(input->xkb.keymap);
2478 input->xkb.keymap = NULL;
2479 return;
2480 }
2481
2482 input->xkb.control_mask =
2483 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2484 input->xkb.alt_mask =
2485 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2486 input->xkb.shift_mask =
2487 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2488}
2489
2490static void
2491keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2492 uint32_t serial, struct wl_surface *surface,
2493 struct wl_array *keys)
2494{
2495 struct input *input = data;
2496 struct window *window;
2497
2498 input->display->serial = serial;
2499 input->keyboard_focus = wl_surface_get_user_data(surface);
2500
2501 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002502 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002503 if (window->keyboard_focus_handler)
2504 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002505 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002506}
2507
2508static void
2509keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2510 uint32_t serial, struct wl_surface *surface)
2511{
2512 struct input *input = data;
2513
2514 input->display->serial = serial;
2515 input_remove_keyboard_focus(input);
2516}
2517
Scott Moreau210d0792012-03-22 10:47:01 -06002518static void
Daniel Stone37816df2012-05-16 18:45:18 +01002519keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002520 uint32_t serial, uint32_t time, uint32_t key,
2521 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002522{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002523 struct input *input = data;
2524 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002525 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002526 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002527 const xkb_keysym_t *syms;
2528 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002529 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002530
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002531 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002532 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002533 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002534 return;
2535
Daniel Stone97f68542012-05-30 16:32:01 +01002536 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002537
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002538 sym = XKB_KEY_NoSymbol;
2539 if (num_syms == 1)
2540 sym = syms[0];
2541
2542 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002543 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002544 window_set_maximized(window,
2545 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002546 } else if (sym == XKB_KEY_F11 &&
2547 window->fullscreen_handler &&
2548 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2549 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002550 } else if (sym == XKB_KEY_F4 &&
2551 input->modifiers == MOD_ALT_MASK &&
2552 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2553 if (window->close_handler)
2554 window->close_handler(window->parent,
2555 window->user_data);
2556 else
2557 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002558 } else if (window->key_handler) {
2559 (*window->key_handler)(window, input, time, key,
2560 sym, state, window->user_data);
2561 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002562
2563 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2564 key == input->repeat_key) {
2565 its.it_interval.tv_sec = 0;
2566 its.it_interval.tv_nsec = 0;
2567 its.it_value.tv_sec = 0;
2568 its.it_value.tv_nsec = 0;
2569 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2570 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2571 input->repeat_sym = sym;
2572 input->repeat_key = key;
2573 input->repeat_time = time;
2574 its.it_interval.tv_sec = 0;
2575 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2576 its.it_value.tv_sec = 0;
2577 its.it_value.tv_nsec = 400 * 1000 * 1000;
2578 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2579 }
2580}
2581
2582static void
Daniel Stone351eb612012-05-31 15:27:47 -04002583keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2584 uint32_t serial, uint32_t mods_depressed,
2585 uint32_t mods_latched, uint32_t mods_locked,
2586 uint32_t group)
2587{
2588 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002589 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002590
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002591 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2592 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002593 mask = xkb_state_serialize_mods(input->xkb.state,
2594 XKB_STATE_DEPRESSED |
2595 XKB_STATE_LATCHED);
2596 input->modifiers = 0;
2597 if (mask & input->xkb.control_mask)
2598 input->modifiers |= MOD_CONTROL_MASK;
2599 if (mask & input->xkb.alt_mask)
2600 input->modifiers |= MOD_ALT_MASK;
2601 if (mask & input->xkb.shift_mask)
2602 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002603}
2604
Daniel Stone37816df2012-05-16 18:45:18 +01002605static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002606 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002607 keyboard_handle_enter,
2608 keyboard_handle_leave,
2609 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002610 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002611};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002612
2613static void
Daniel Stone37816df2012-05-16 18:45:18 +01002614seat_handle_capabilities(void *data, struct wl_seat *seat,
2615 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002616{
Daniel Stone37816df2012-05-16 18:45:18 +01002617 struct input *input = data;
2618
2619 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2620 input->pointer = wl_seat_get_pointer(seat);
2621 wl_pointer_set_user_data(input->pointer, input);
2622 wl_pointer_add_listener(input->pointer, &pointer_listener,
2623 input);
2624 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2625 wl_pointer_destroy(input->pointer);
2626 input->pointer = NULL;
2627 }
2628
2629 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2630 input->keyboard = wl_seat_get_keyboard(seat);
2631 wl_keyboard_set_user_data(input->keyboard, input);
2632 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2633 input);
2634 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2635 wl_keyboard_destroy(input->keyboard);
2636 input->keyboard = NULL;
2637 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002638}
2639
Daniel Stone37816df2012-05-16 18:45:18 +01002640static const struct wl_seat_listener seat_listener = {
2641 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002642};
2643
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002644void
2645input_get_position(struct input *input, int32_t *x, int32_t *y)
2646{
2647 *x = input->sx;
2648 *y = input->sy;
2649}
2650
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002651struct display *
2652input_get_display(struct input *input)
2653{
2654 return input->display;
2655}
2656
Daniel Stone37816df2012-05-16 18:45:18 +01002657struct wl_seat *
2658input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002659{
Daniel Stone37816df2012-05-16 18:45:18 +01002660 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002661}
2662
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002663uint32_t
2664input_get_modifiers(struct input *input)
2665{
2666 return input->modifiers;
2667}
2668
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002669struct widget *
2670input_get_focus_widget(struct input *input)
2671{
2672 return input->focus_widget;
2673}
2674
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002675struct data_offer {
2676 struct wl_data_offer *offer;
2677 struct input *input;
2678 struct wl_array types;
2679 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002680
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002681 struct task io_task;
2682 int fd;
2683 data_func_t func;
2684 int32_t x, y;
2685 void *user_data;
2686};
2687
2688static void
2689data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2690{
2691 struct data_offer *offer = data;
2692 char **p;
2693
2694 p = wl_array_add(&offer->types, sizeof *p);
2695 *p = strdup(type);
2696}
2697
2698static const struct wl_data_offer_listener data_offer_listener = {
2699 data_offer_offer,
2700};
2701
2702static void
2703data_offer_destroy(struct data_offer *offer)
2704{
2705 char **p;
2706
2707 offer->refcount--;
2708 if (offer->refcount == 0) {
2709 wl_data_offer_destroy(offer->offer);
2710 for (p = offer->types.data; *p; p++)
2711 free(*p);
2712 wl_array_release(&offer->types);
2713 free(offer);
2714 }
2715}
2716
2717static void
2718data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002719 struct wl_data_device *data_device,
2720 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002721{
2722 struct data_offer *offer;
2723
2724 offer = malloc(sizeof *offer);
2725
2726 wl_array_init(&offer->types);
2727 offer->refcount = 1;
2728 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002729 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002730 wl_data_offer_add_listener(offer->offer,
2731 &data_offer_listener, offer);
2732}
2733
2734static void
2735data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002736 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002737 wl_fixed_t x_w, wl_fixed_t y_w,
2738 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002739{
2740 struct input *input = data;
2741 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002742 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002743 float x = wl_fixed_to_double(x_w);
2744 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002745 char **p;
2746
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002747 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002748 window = wl_surface_get_user_data(surface);
2749 input->pointer_focus = window;
2750
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002751 if (offer) {
2752 input->drag_offer = wl_data_offer_get_user_data(offer);
2753
2754 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2755 *p = NULL;
2756
2757 types_data = input->drag_offer->types.data;
2758 } else {
2759 input->drag_offer = NULL;
2760 types_data = NULL;
2761 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002762
2763 window = input->pointer_focus;
2764 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002765 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002766 window->user_data);
2767}
2768
2769static void
2770data_device_leave(void *data, struct wl_data_device *data_device)
2771{
2772 struct input *input = data;
2773
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002774 if (input->drag_offer) {
2775 data_offer_destroy(input->drag_offer);
2776 input->drag_offer = NULL;
2777 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002778}
2779
2780static void
2781data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002782 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002783{
2784 struct input *input = data;
2785 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002786 float x = wl_fixed_to_double(x_w);
2787 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002788 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002789
2790 input->sx = x;
2791 input->sy = y;
2792
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002793 if (input->drag_offer)
2794 types_data = input->drag_offer->types.data;
2795 else
2796 types_data = NULL;
2797
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002798 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002799 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002800 window->user_data);
2801}
2802
2803static void
2804data_device_drop(void *data, struct wl_data_device *data_device)
2805{
2806 struct input *input = data;
2807 struct window *window = input->pointer_focus;
2808
2809 if (window->drop_handler)
2810 window->drop_handler(window, input,
2811 input->sx, input->sy, window->user_data);
2812}
2813
2814static void
2815data_device_selection(void *data,
2816 struct wl_data_device *wl_data_device,
2817 struct wl_data_offer *offer)
2818{
2819 struct input *input = data;
2820 char **p;
2821
2822 if (input->selection_offer)
2823 data_offer_destroy(input->selection_offer);
2824
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002825 if (offer) {
2826 input->selection_offer = wl_data_offer_get_user_data(offer);
2827 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2828 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002829 } else {
2830 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002831 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002832}
2833
2834static const struct wl_data_device_listener data_device_listener = {
2835 data_device_data_offer,
2836 data_device_enter,
2837 data_device_leave,
2838 data_device_motion,
2839 data_device_drop,
2840 data_device_selection
2841};
2842
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002843static void
2844input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002845{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002846 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002847 struct wl_cursor *cursor;
2848 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002849
Daniel Stone80972742012-11-07 17:51:39 +11002850 if (!input->pointer)
2851 return;
2852
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002853 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002854 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002855 return;
2856
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002857 if (index >= (int) cursor->image_count) {
2858 fprintf(stderr, "cursor index out of range\n");
2859 return;
2860 }
2861
2862 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002863 buffer = wl_cursor_image_get_buffer(image);
2864 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002865 return;
2866
Kristian Høgsbergae277372012-08-01 09:41:08 -04002867 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002868 input->pointer_surface,
2869 image->hotspot_x, image->hotspot_y);
2870 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2871 wl_surface_damage(input->pointer_surface, 0, 0,
2872 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002873 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002874}
2875
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002876static const struct wl_callback_listener pointer_surface_listener;
2877
2878static void
2879pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2880 uint32_t time)
2881{
2882 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002883 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002884 int i;
2885
2886 if (callback) {
2887 assert(callback == input->cursor_frame_cb);
2888 wl_callback_destroy(callback);
2889 input->cursor_frame_cb = NULL;
2890 }
2891
Daniel Stone80972742012-11-07 17:51:39 +11002892 if (!input->pointer)
2893 return;
2894
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002895 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002896 wl_pointer_set_cursor(input->pointer,
2897 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002898 NULL, 0, 0);
2899 return;
2900 }
2901
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002902 if (input->current_cursor == CURSOR_UNSET)
2903 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002904 cursor = input->display->cursors[input->current_cursor];
2905 if (!cursor)
2906 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002907
2908 /* FIXME We don't have the current time on the first call so we set
2909 * the animation start to the time of the first frame callback. */
2910 if (time == 0)
2911 input->cursor_anim_start = 0;
2912 else if (input->cursor_anim_start == 0)
2913 input->cursor_anim_start = time;
2914
2915 if (time == 0 || input->cursor_anim_start == 0)
2916 i = 0;
2917 else
2918 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2919
Pekka Paalanenbc106382012-10-10 12:49:31 +03002920 if (cursor->image_count > 1) {
2921 input->cursor_frame_cb =
2922 wl_surface_frame(input->pointer_surface);
2923 wl_callback_add_listener(input->cursor_frame_cb,
2924 &pointer_surface_listener, input);
2925 }
2926
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002927 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002928}
2929
2930static const struct wl_callback_listener pointer_surface_listener = {
2931 pointer_surface_frame_callback
2932};
2933
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002934void
2935input_set_pointer_image(struct input *input, int pointer)
2936{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002937 int force = 0;
2938
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002939 if (!input->pointer)
2940 return;
2941
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002942 if (input->pointer_enter_serial > input->cursor_serial)
2943 force = 1;
2944
2945 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002946 return;
2947
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002948 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002949 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002950 if (!input->cursor_frame_cb)
2951 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002952 else if (force) {
2953 /* The current frame callback may be stuck if, for instance,
2954 * the set cursor request was processed by the server after
2955 * this client lost the focus. In this case the cursor surface
2956 * might not be mapped and the frame callback wouldn't ever
2957 * complete. Send a set_cursor and attach to try to map the
2958 * cursor surface again so that the callback will finish */
2959 input_set_pointer_image_index(input, 0);
2960 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002961}
2962
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002963struct wl_data_device *
2964input_get_data_device(struct input *input)
2965{
2966 return input->data_device;
2967}
2968
2969void
2970input_set_selection(struct input *input,
2971 struct wl_data_source *source, uint32_t time)
2972{
2973 wl_data_device_set_selection(input->data_device, source, time);
2974}
2975
2976void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002977input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002978{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002979 wl_data_offer_accept(input->drag_offer->offer,
2980 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002981}
2982
2983static void
2984offer_io_func(struct task *task, uint32_t events)
2985{
2986 struct data_offer *offer =
2987 container_of(task, struct data_offer, io_task);
2988 unsigned int len;
2989 char buffer[4096];
2990
2991 len = read(offer->fd, buffer, sizeof buffer);
2992 offer->func(buffer, len,
2993 offer->x, offer->y, offer->user_data);
2994
2995 if (len == 0) {
2996 close(offer->fd);
2997 data_offer_destroy(offer);
2998 }
2999}
3000
3001static void
3002data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3003 data_func_t func, void *user_data)
3004{
3005 int p[2];
3006
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003007 if (pipe2(p, O_CLOEXEC) == -1)
3008 return;
3009
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003010 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3011 close(p[1]);
3012
3013 offer->io_task.run = offer_io_func;
3014 offer->fd = p[0];
3015 offer->func = func;
3016 offer->refcount++;
3017 offer->user_data = user_data;
3018
3019 display_watch_fd(offer->input->display,
3020 offer->fd, EPOLLIN, &offer->io_task);
3021}
3022
3023void
3024input_receive_drag_data(struct input *input, const char *mime_type,
3025 data_func_t func, void *data)
3026{
3027 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3028 input->drag_offer->x = input->sx;
3029 input->drag_offer->y = input->sy;
3030}
3031
3032int
3033input_receive_selection_data(struct input *input, const char *mime_type,
3034 data_func_t func, void *data)
3035{
3036 char **p;
3037
3038 if (input->selection_offer == NULL)
3039 return -1;
3040
3041 for (p = input->selection_offer->types.data; *p; p++)
3042 if (strcmp(mime_type, *p) == 0)
3043 break;
3044
3045 if (*p == NULL)
3046 return -1;
3047
3048 data_offer_receive_data(input->selection_offer,
3049 mime_type, func, data);
3050 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003051}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003052
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003053int
3054input_receive_selection_data_to_fd(struct input *input,
3055 const char *mime_type, int fd)
3056{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003057 if (input->selection_offer)
3058 wl_data_offer_receive(input->selection_offer->offer,
3059 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003060
3061 return 0;
3062}
3063
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003064void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003065window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003066{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003067 if (!window->shell_surface)
3068 return;
3069
Daniel Stone37816df2012-05-16 18:45:18 +01003070 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003071}
3072
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003073static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003074idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003075{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003076 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003077 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003078
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003079 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003080 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003081 widget_set_allocation(widget,
3082 window->pending_allocation.x,
3083 window->pending_allocation.y,
3084 window->pending_allocation.width,
3085 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003086
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003087 if (window->input_region) {
3088 wl_region_destroy(window->input_region);
3089 window->input_region = NULL;
3090 }
3091
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003092 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003093 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003094
3095 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003096
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003097 if (widget->resize_handler)
3098 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003099 widget->allocation.width,
3100 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003101 widget->user_data);
3102
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003103 if (window->allocation.width != widget->allocation.width ||
3104 window->allocation.height != widget->allocation.height) {
3105 window->allocation = widget->allocation;
3106 window_schedule_redraw(window);
3107 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003108
3109 if (widget->opaque)
3110 wl_region_add(window->opaque_region, 0, 0,
3111 widget->allocation.width,
3112 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003113}
3114
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003115void
3116window_schedule_resize(struct window *window, int width, int height)
3117{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003118 window->pending_allocation.x = 0;
3119 window->pending_allocation.y = 0;
3120 window->pending_allocation.width = width;
3121 window->pending_allocation.height = height;
3122
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003123 if (window->min_allocation.width == 0)
3124 window->min_allocation = window->pending_allocation;
3125 if (window->pending_allocation.width < window->min_allocation.width)
3126 window->pending_allocation.width = window->min_allocation.width;
3127 if (window->pending_allocation.height < window->min_allocation.height)
3128 window->pending_allocation.height = window->min_allocation.height;
3129
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003130 window->resize_needed = 1;
3131 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003132}
3133
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003134void
3135widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3136{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003137 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003138}
3139
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003140static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003141handle_ping(void *data, struct wl_shell_surface *shell_surface,
3142 uint32_t serial)
3143{
3144 wl_shell_surface_pong(shell_surface, serial);
3145}
3146
3147static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003148handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003149 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003150{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003151 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003152
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003153 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003154 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003155}
3156
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003157static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003158menu_destroy(struct menu *menu)
3159{
3160 widget_destroy(menu->widget);
3161 window_destroy(menu->window);
3162 free(menu);
3163}
3164
3165static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003166handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3167{
3168 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003169 struct menu *menu = window->widget->user_data;
3170
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003171 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003172 * device. Or just use wl_callback. And this really needs to
3173 * be a window vfunc that the menu can set. And we need the
3174 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003175
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003176 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003177 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003178 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003179}
3180
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003181static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003182 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003183 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003184 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003185};
3186
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003187void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003188window_get_allocation(struct window *window,
3189 struct rectangle *allocation)
3190{
3191 *allocation = window->allocation;
3192}
3193
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003194static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003195widget_redraw(struct widget *widget)
3196{
3197 struct widget *child;
3198
3199 if (widget->redraw_handler)
3200 widget->redraw_handler(widget, widget->user_data);
3201 wl_list_for_each(child, &widget->child_list, link)
3202 widget_redraw(child);
3203}
3204
3205static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003206frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3207{
3208 struct window *window = data;
3209
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003210 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003211 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003212 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003213 window->redraw_scheduled = 0;
3214 if (window->redraw_needed)
3215 window_schedule_redraw(window);
3216}
3217
3218static const struct wl_callback_listener listener = {
3219 frame_callback
3220};
3221
3222static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003223idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003224{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003225 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003226
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003227 if (window->resize_needed)
3228 idle_resize(window);
3229
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003230 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003231 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003232 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003233 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003234
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003235 window->frame_cb = wl_surface_frame(window->surface);
3236 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003237 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003238}
3239
3240void
3241window_schedule_redraw(struct window *window)
3242{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003243 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003244 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003245 window->redraw_task.run = idle_redraw;
3246 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003247 window->redraw_scheduled = 1;
3248 }
3249}
3250
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003251int
3252window_is_fullscreen(struct window *window)
3253{
3254 return window->type == TYPE_FULLSCREEN;
3255}
3256
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003257void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003258window_set_fullscreen(struct window *window, int fullscreen)
3259{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003260 if (!window->display->shell)
3261 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003262
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003263 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003264 return;
3265
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003266 if (fullscreen) {
3267 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003268 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003269 wl_shell_surface_set_fullscreen(window->shell_surface,
3270 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3271 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003272 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003273 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003274 wl_shell_surface_set_toplevel(window->shell_surface);
3275 window_schedule_resize(window,
3276 window->saved_allocation.width,
3277 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003278 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003279}
3280
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003281int
3282window_is_maximized(struct window *window)
3283{
3284 return window->type == TYPE_MAXIMIZED;
3285}
3286
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003287void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003288window_set_maximized(struct window *window, int maximized)
3289{
3290 if (!window->display->shell)
3291 return;
3292
3293 if ((window->type == TYPE_MAXIMIZED) == maximized)
3294 return;
3295
3296 if (window->type == TYPE_TOPLEVEL) {
3297 window->saved_allocation = window->allocation;
3298 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3299 window->type = TYPE_MAXIMIZED;
3300 } else {
3301 wl_shell_surface_set_toplevel(window->shell_surface);
3302 window->type = TYPE_TOPLEVEL;
3303 window_schedule_resize(window,
3304 window->saved_allocation.width,
3305 window->saved_allocation.height);
3306 }
3307}
3308
3309void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003310window_set_user_data(struct window *window, void *data)
3311{
3312 window->user_data = data;
3313}
3314
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003315void *
3316window_get_user_data(struct window *window)
3317{
3318 return window->user_data;
3319}
3320
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003321void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003322window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003323 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003324{
3325 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003326}
3327
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003328void
3329window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003330 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003331{
3332 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003333}
3334
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003335void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003336window_set_data_handler(struct window *window, window_data_handler_t handler)
3337{
3338 window->data_handler = handler;
3339}
3340
3341void
3342window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3343{
3344 window->drop_handler = handler;
3345}
3346
3347void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003348window_set_close_handler(struct window *window,
3349 window_close_handler_t handler)
3350{
3351 window->close_handler = handler;
3352}
3353
3354void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003355window_set_fullscreen_handler(struct window *window,
3356 window_fullscreen_handler_t handler)
3357{
3358 window->fullscreen_handler = handler;
3359}
3360
3361void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003362window_set_title(struct window *window, const char *title)
3363{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003364 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003365 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003366 if (window->shell_surface)
3367 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003368}
3369
3370const char *
3371window_get_title(struct window *window)
3372{
3373 return window->title;
3374}
3375
3376void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003377window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3378{
3379 struct text_cursor_position *text_cursor_position =
3380 window->display->text_cursor_position;
3381
Scott Moreau9295ce02012-06-01 12:46:10 -06003382 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003383 return;
3384
3385 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003386 window->surface,
3387 wl_fixed_from_int(x),
3388 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003389}
3390
3391void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003392window_damage(struct window *window, int32_t x, int32_t y,
3393 int32_t width, int32_t height)
3394{
3395 wl_surface_damage(window->surface, x, y, width, height);
3396}
3397
Casey Dahlin9074db52012-04-19 22:50:09 -04003398static void
3399surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003400 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003401{
Rob Bradford7507b572012-05-15 17:55:34 +01003402 struct window *window = data;
3403 struct output *output;
3404 struct output *output_found = NULL;
3405 struct window_output *window_output;
3406
3407 wl_list_for_each(output, &window->display->output_list, link) {
3408 if (output->output == wl_output) {
3409 output_found = output;
3410 break;
3411 }
3412 }
3413
3414 if (!output_found)
3415 return;
3416
3417 window_output = malloc (sizeof *window_output);
3418 window_output->output = output_found;
3419
3420 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003421}
3422
3423static void
3424surface_leave(void *data,
3425 struct wl_surface *wl_surface, struct wl_output *output)
3426{
Rob Bradford7507b572012-05-15 17:55:34 +01003427 struct window *window = data;
3428 struct window_output *window_output;
3429 struct window_output *window_output_found = NULL;
3430
3431 wl_list_for_each(window_output, &window->window_output_list, link) {
3432 if (window_output->output->output == output) {
3433 window_output_found = window_output;
3434 break;
3435 }
3436 }
3437
3438 if (window_output_found) {
3439 wl_list_remove(&window_output_found->link);
3440 free(window_output_found);
3441 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003442}
3443
3444static const struct wl_surface_listener surface_listener = {
3445 surface_enter,
3446 surface_leave
3447};
3448
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003449static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003450window_create_internal(struct display *display,
3451 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003452{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003453 struct window *window;
3454
3455 window = malloc(sizeof *window);
3456 if (window == NULL)
3457 return NULL;
3458
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003459 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003460 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003461 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003462 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003463 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003464 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003465 window->shell_surface =
3466 wl_shell_get_shell_surface(display->shell,
3467 window->surface);
3468 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003469 window->allocation.x = 0;
3470 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003471 window->allocation.width = 0;
3472 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003473 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003474 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003475 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003476 window->input_region = NULL;
3477 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003478
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003479 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003480#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003481 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003482#else
3483 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3484#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003485 else
3486 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003487
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003488 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003489 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003490 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003491
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003492 if (window->shell_surface) {
3493 wl_shell_surface_set_user_data(window->shell_surface, window);
3494 wl_shell_surface_add_listener(window->shell_surface,
3495 &shell_surface_listener, window);
3496 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003497
Rob Bradford7507b572012-05-15 17:55:34 +01003498 wl_list_init (&window->window_output_list);
3499
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003500 return window;
3501}
3502
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003503struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003504window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003505{
3506 struct window *window;
3507
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003508 window = window_create_internal(display, NULL, TYPE_NONE);
3509 if (!window)
3510 return NULL;
3511
3512 return window;
3513}
3514
3515struct window *
3516window_create_custom(struct display *display)
3517{
3518 struct window *window;
3519
3520 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003521 if (!window)
3522 return NULL;
3523
3524 return window;
3525}
3526
3527struct window *
3528window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003529 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003530{
3531 struct window *window;
3532
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003533 window = window_create_internal(parent->display,
3534 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003535 if (!window)
3536 return NULL;
3537
3538 window->x = x;
3539 window->y = y;
3540
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003541 if (display->shell)
3542 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003543 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003544 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003545
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003546 return window;
3547}
3548
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003549static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003550menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003551{
3552 int next;
3553
3554 next = (sy - 8) / 20;
3555 if (menu->current != next) {
3556 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003557 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003558 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003559}
3560
3561static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003562menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003563 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003564 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003565{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003566 struct menu *menu = data;
3567
3568 if (widget == menu->widget)
3569 menu_set_item(data, y);
3570
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003571 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003572}
3573
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003574static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003575menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003576 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003577{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003578 struct menu *menu = data;
3579
3580 if (widget == menu->widget)
3581 menu_set_item(data, y);
3582
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003583 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003584}
3585
3586static void
3587menu_leave_handler(struct widget *widget, struct input *input, void *data)
3588{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003589 struct menu *menu = data;
3590
3591 if (widget == menu->widget)
3592 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003593}
3594
3595static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003596menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003597 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003598 uint32_t button, enum wl_pointer_button_state state,
3599 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003600
3601{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003602 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003603
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003604 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3605 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003606 /* Either relase after press-drag-release or
3607 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003608 menu->func(menu->window->parent,
3609 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003610 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003611 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003612 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003613 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003614 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003615}
3616
3617static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003618menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003619{
3620 cairo_t *cr;
3621 const int32_t r = 3, margin = 3;
3622 struct menu *menu = data;
3623 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003624 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003625
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003626 cr = cairo_create(window->cairo_surface);
3627 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3628 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3629 cairo_paint(cr);
3630
3631 width = window->allocation.width;
3632 height = window->allocation.height;
3633 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003634
3635 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003636 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3637 cairo_fill(cr);
3638
3639 for (i = 0; i < menu->count; i++) {
3640 if (i == menu->current) {
3641 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3642 cairo_rectangle(cr, margin, i * 20 + margin,
3643 width - 2 * margin, 20);
3644 cairo_fill(cr);
3645 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3646 cairo_move_to(cr, 10, i * 20 + 16);
3647 cairo_show_text(cr, menu->entries[i]);
3648 } else {
3649 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3650 cairo_move_to(cr, 10, i * 20 + 16);
3651 cairo_show_text(cr, menu->entries[i]);
3652 }
3653 }
3654
3655 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003656}
3657
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003658void
3659window_show_menu(struct display *display,
3660 struct input *input, uint32_t time, struct window *parent,
3661 int32_t x, int32_t y,
3662 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003663{
3664 struct window *window;
3665 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003666 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003667
3668 menu = malloc(sizeof *menu);
3669 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003670 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003671
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003672 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003673 if (!window) {
3674 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003675 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003676 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003677
3678 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003679 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003680 menu->entries = entries;
3681 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003682 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003683 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003684 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003685 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003686 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003687 window->type = TYPE_MENU;
3688 window->x = x;
3689 window->y = y;
3690
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003691 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003692 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003693 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003694 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003695 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003696
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003697 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003698 widget_set_enter_handler(menu->widget, menu_enter_handler);
3699 widget_set_leave_handler(menu->widget, menu_leave_handler);
3700 widget_set_motion_handler(menu->widget, menu_motion_handler);
3701 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003702
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003703 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003704 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003705}
3706
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003707void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003708window_set_buffer_type(struct window *window, enum window_buffer_type type)
3709{
3710 window->buffer_type = type;
3711}
3712
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003713
3714static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003715display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003716 struct wl_output *wl_output,
3717 int x, int y,
3718 int physical_width,
3719 int physical_height,
3720 int subpixel,
3721 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003722 const char *model,
3723 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003724{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003725 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003726
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003727 output->allocation.x = x;
3728 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003729 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003730}
3731
3732static void
3733display_handle_mode(void *data,
3734 struct wl_output *wl_output,
3735 uint32_t flags,
3736 int width,
3737 int height,
3738 int refresh)
3739{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003740 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003741 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003742
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003743 if (flags & WL_OUTPUT_MODE_CURRENT) {
3744 output->allocation.width = width;
3745 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003746 if (display->output_configure_handler)
3747 (*display->output_configure_handler)(
3748 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003749 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003750}
3751
3752static const struct wl_output_listener output_listener = {
3753 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003754 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003755};
3756
3757static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003758display_add_output(struct display *d, uint32_t id)
3759{
3760 struct output *output;
3761
3762 output = malloc(sizeof *output);
3763 if (output == NULL)
3764 return;
3765
3766 memset(output, 0, sizeof *output);
3767 output->display = d;
3768 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003769 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003770 wl_list_insert(d->output_list.prev, &output->link);
3771
3772 wl_output_add_listener(output->output, &output_listener, output);
3773}
3774
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003775static void
3776output_destroy(struct output *output)
3777{
3778 if (output->destroy_handler)
3779 (*output->destroy_handler)(output, output->user_data);
3780
3781 wl_output_destroy(output->output);
3782 wl_list_remove(&output->link);
3783 free(output);
3784}
3785
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003786void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003787display_set_global_handler(struct display *display,
3788 display_global_handler_t handler)
3789{
3790 struct global *global;
3791
3792 display->global_handler = handler;
3793 if (!handler)
3794 return;
3795
3796 wl_list_for_each(global, &display->global_list, link)
3797 display->global_handler(display,
3798 global->name, global->interface,
3799 global->version, display->user_data);
3800}
3801
3802void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003803display_set_output_configure_handler(struct display *display,
3804 display_output_handler_t handler)
3805{
3806 struct output *output;
3807
3808 display->output_configure_handler = handler;
3809 if (!handler)
3810 return;
3811
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003812 wl_list_for_each(output, &display->output_list, link) {
3813 if (output->allocation.width == 0 &&
3814 output->allocation.height == 0)
3815 continue;
3816
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003817 (*display->output_configure_handler)(output,
3818 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003819 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003820}
3821
3822void
3823output_set_user_data(struct output *output, void *data)
3824{
3825 output->user_data = data;
3826}
3827
3828void *
3829output_get_user_data(struct output *output)
3830{
3831 return output->user_data;
3832}
3833
3834void
3835output_set_destroy_handler(struct output *output,
3836 display_output_handler_t handler)
3837{
3838 output->destroy_handler = handler;
3839 /* FIXME: implement this, once we have way to remove outputs */
3840}
3841
3842void
Scott Moreau4e072362012-09-29 02:03:11 -06003843output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003844{
Scott Moreau4e072362012-09-29 02:03:11 -06003845 struct rectangle allocation = output->allocation;
3846
3847 switch (output->transform) {
3848 case WL_OUTPUT_TRANSFORM_90:
3849 case WL_OUTPUT_TRANSFORM_270:
3850 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3851 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3852 /* Swap width and height */
3853 allocation.width = output->allocation.height;
3854 allocation.height = output->allocation.width;
3855 break;
3856 }
3857
3858 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003859}
3860
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003861struct wl_output *
3862output_get_wl_output(struct output *output)
3863{
3864 return output->output;
3865}
3866
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003867static void
Daniel Stone97f68542012-05-30 16:32:01 +01003868fini_xkb(struct input *input)
3869{
3870 xkb_state_unref(input->xkb.state);
3871 xkb_map_unref(input->xkb.keymap);
3872}
3873
3874static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003875display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003876{
3877 struct input *input;
3878
3879 input = malloc(sizeof *input);
3880 if (input == NULL)
3881 return;
3882
3883 memset(input, 0, sizeof *input);
3884 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003885 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003886 input->pointer_focus = NULL;
3887 input->keyboard_focus = NULL;
3888 wl_list_insert(d->input_list.prev, &input->link);
3889
Daniel Stone37816df2012-05-16 18:45:18 +01003890 wl_seat_add_listener(input->seat, &seat_listener, input);
3891 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003892
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003893 input->data_device =
3894 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003895 input->seat);
3896 wl_data_device_add_listener(input->data_device, &data_device_listener,
3897 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003898
3899 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003900
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003901 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3902 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003903 input->repeat_task.run = keyboard_repeat_func;
3904 display_watch_fd(d, input->repeat_timer_fd,
3905 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003906}
3907
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003908static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003909input_destroy(struct input *input)
3910{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003911 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003912 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003913
3914 if (input->drag_offer)
3915 data_offer_destroy(input->drag_offer);
3916
3917 if (input->selection_offer)
3918 data_offer_destroy(input->selection_offer);
3919
3920 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003921 fini_xkb(input);
3922
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003923 wl_surface_destroy(input->pointer_surface);
3924
Pekka Paalanene1207c72011-12-16 12:02:09 +02003925 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003926 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003927 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003928 free(input);
3929}
3930
3931static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003932init_workspace_manager(struct display *d, uint32_t id)
3933{
3934 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003935 wl_registry_bind(d->registry, id,
3936 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003937 if (d->workspace_manager != NULL)
3938 workspace_manager_add_listener(d->workspace_manager,
3939 &workspace_manager_listener,
3940 d);
3941}
3942
3943static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003944registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3945 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003946{
3947 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003948 struct global *global;
3949
3950 global = malloc(sizeof *global);
3951 global->name = id;
3952 global->interface = strdup(interface);
3953 global->version = version;
3954 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003955
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003956 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003957 d->compositor = wl_registry_bind(registry, id,
3958 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003959 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003960 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003961 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003962 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003963 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003964 d->shell = wl_registry_bind(registry,
3965 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003966 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003967 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003968 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3969 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003970 wl_registry_bind(registry, id,
3971 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003972 } else if (strcmp(interface, "text_cursor_position") == 0) {
3973 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003974 wl_registry_bind(registry, id,
3975 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003976 } else if (strcmp(interface, "workspace_manager") == 0) {
3977 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003978 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003979
3980 if (d->global_handler)
3981 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003982}
3983
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003984void *
3985display_bind(struct display *display, uint32_t name,
3986 const struct wl_interface *interface, uint32_t version)
3987{
3988 return wl_registry_bind(display->registry, name, interface, version);
3989}
3990
3991static const struct wl_registry_listener registry_listener = {
3992 registry_handle_global
3993};
3994
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003995#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003996static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003997init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003998{
3999 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004000 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004001
Rob Clark6396ed32012-03-11 19:48:41 -05004002#ifdef USE_CAIRO_GLESV2
4003# define GL_BIT EGL_OPENGL_ES2_BIT
4004#else
4005# define GL_BIT EGL_OPENGL_BIT
4006#endif
4007
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004008 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004009 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004010 EGL_RED_SIZE, 1,
4011 EGL_GREEN_SIZE, 1,
4012 EGL_BLUE_SIZE, 1,
4013 EGL_ALPHA_SIZE, 1,
4014 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004015 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004016 EGL_NONE
4017 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004018
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004019#ifdef USE_CAIRO_GLESV2
4020 static const EGLint context_attribs[] = {
4021 EGL_CONTEXT_CLIENT_VERSION, 2,
4022 EGL_NONE
4023 };
4024 EGLint api = EGL_OPENGL_ES_API;
4025#else
4026 EGLint *context_attribs = NULL;
4027 EGLint api = EGL_OPENGL_API;
4028#endif
4029
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004030 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004031 if (!eglInitialize(d->dpy, &major, &minor)) {
4032 fprintf(stderr, "failed to initialize display\n");
4033 return -1;
4034 }
4035
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004036 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004037 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
4038 return -1;
4039 }
4040
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004041 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4042 &d->argb_config, 1, &n) || n != 1) {
4043 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004044 return -1;
4045 }
4046
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004047 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004048 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004049 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004050 fprintf(stderr, "failed to create context\n");
4051 return -1;
4052 }
4053
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004054 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01004055 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004056 return -1;
4057 }
4058
Benjamin Franzke0c991632011-09-27 21:57:31 +02004059 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4060 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4061 fprintf(stderr, "failed to get cairo egl argb device\n");
4062 return -1;
4063 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004064
4065 return 0;
4066}
4067
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004068static void
4069fini_egl(struct display *display)
4070{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004071 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004072
4073 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4074 EGL_NO_CONTEXT);
4075
4076 eglTerminate(display->dpy);
4077 eglReleaseThread();
4078}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004079#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004080
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004081static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004082init_dummy_surface(struct display *display)
4083{
4084 int len;
4085 void *data;
4086
4087 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4088 data = malloc(len);
4089 display->dummy_surface =
4090 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4091 1, 1, len);
4092 display->dummy_surface_data = data;
4093}
4094
4095static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004096handle_display_data(struct task *task, uint32_t events)
4097{
4098 struct display *display =
4099 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004100 struct epoll_event ep;
4101 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004102
4103 display->display_fd_events = events;
4104
4105 if (events & EPOLLERR || events & EPOLLHUP) {
4106 display_exit(display);
4107 return;
4108 }
4109
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004110 if (events & EPOLLIN) {
4111 ret = wl_display_dispatch(display->display);
4112 if (ret == -1) {
4113 display_exit(display);
4114 return;
4115 }
4116 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004117
4118 if (events & EPOLLOUT) {
4119 ret = wl_display_flush(display->display);
4120 if (ret == 0) {
4121 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4122 ep.data.ptr = &display->display_task;
4123 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4124 display->display_fd, &ep);
4125 } else if (ret == -1 && errno != EAGAIN) {
4126 display_exit(display);
4127 return;
4128 }
4129 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004130}
4131
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004132struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004133display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004134{
4135 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004136
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004137 d = malloc(sizeof *d);
4138 if (d == NULL)
4139 return NULL;
4140
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004141 memset(d, 0, sizeof *d);
4142
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004143 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004144 if (d->display == NULL) {
4145 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004146 return NULL;
4147 }
4148
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004149 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004150 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004151 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004152 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4153 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004154
4155 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004156 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004157 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004158 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004159
Daniel Stone97f68542012-05-30 16:32:01 +01004160 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004161 if (d->xkb_context == NULL) {
4162 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004163 return NULL;
4164 }
4165
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004166 d->workspace = 0;
4167 d->workspace_count = 1;
4168
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004169 d->registry = wl_display_get_registry(d->display);
4170 wl_registry_add_listener(d->registry, &registry_listener, d);
4171 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004172#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004173 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004174 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004175#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004176
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004177 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004178
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004179 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004180
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004181 wl_list_init(&d->window_list);
4182
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004183 init_dummy_surface(d);
4184
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004185 return d;
4186}
4187
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004188static void
4189display_destroy_outputs(struct display *display)
4190{
4191 struct output *tmp;
4192 struct output *output;
4193
4194 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4195 output_destroy(output);
4196}
4197
Pekka Paalanene1207c72011-12-16 12:02:09 +02004198static void
4199display_destroy_inputs(struct display *display)
4200{
4201 struct input *tmp;
4202 struct input *input;
4203
4204 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4205 input_destroy(input);
4206}
4207
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004208void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004209display_destroy(struct display *display)
4210{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004211 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004212 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4213 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004214
4215 if (!wl_list_empty(&display->deferred_list))
4216 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4217
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004218 cairo_surface_destroy(display->dummy_surface);
4219 free(display->dummy_surface_data);
4220
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004221 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004222 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004223
Daniel Stone97f68542012-05-30 16:32:01 +01004224 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004225
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004226 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004227 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004228
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004229#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004230 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004231#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004232
Pekka Paalanenc2052982011-12-16 11:41:32 +02004233 if (display->shell)
4234 wl_shell_destroy(display->shell);
4235
4236 if (display->shm)
4237 wl_shm_destroy(display->shm);
4238
4239 if (display->data_device_manager)
4240 wl_data_device_manager_destroy(display->data_device_manager);
4241
4242 wl_compositor_destroy(display->compositor);
4243
4244 close(display->epoll_fd);
4245
U. Artie Eoff44874d92012-10-02 21:12:35 -07004246 if (!(display->display_fd_events & EPOLLERR) &&
4247 !(display->display_fd_events & EPOLLHUP))
4248 wl_display_flush(display->display);
4249
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004250 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004251 free(display);
4252}
4253
4254void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004255display_set_user_data(struct display *display, void *data)
4256{
4257 display->user_data = data;
4258}
4259
4260void *
4261display_get_user_data(struct display *display)
4262{
4263 return display->user_data;
4264}
4265
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004266struct wl_display *
4267display_get_display(struct display *display)
4268{
4269 return display->display;
4270}
4271
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004272struct output *
4273display_get_output(struct display *display)
4274{
4275 return container_of(display->output_list.next, struct output, link);
4276}
4277
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004278struct wl_compositor *
4279display_get_compositor(struct display *display)
4280{
4281 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004282}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004283
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004284uint32_t
4285display_get_serial(struct display *display)
4286{
4287 return display->serial;
4288}
4289
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004290EGLDisplay
4291display_get_egl_display(struct display *d)
4292{
4293 return d->dpy;
4294}
4295
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004296struct wl_data_source *
4297display_create_data_source(struct display *display)
4298{
4299 return wl_data_device_manager_create_data_source(display->data_device_manager);
4300}
4301
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004302EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004303display_get_argb_egl_config(struct display *d)
4304{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004305 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004306}
4307
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004308struct wl_shell *
4309display_get_shell(struct display *display)
4310{
4311 return display->shell;
4312}
4313
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004314int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004315display_acquire_window_surface(struct display *display,
4316 struct window *window,
4317 EGLContext ctx)
4318{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004319 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004320 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004321
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004322 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004323}
4324
4325void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004326display_release_window_surface(struct display *display,
4327 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004328{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004329 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004330 return;
4331
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004332 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004333}
4334
4335void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004336display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004337{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004338 wl_list_insert(&display->deferred_list, &task->link);
4339}
4340
4341void
4342display_watch_fd(struct display *display,
4343 int fd, uint32_t events, struct task *task)
4344{
4345 struct epoll_event ep;
4346
4347 ep.events = events;
4348 ep.data.ptr = task;
4349 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4350}
4351
4352void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004353display_unwatch_fd(struct display *display, int fd)
4354{
4355 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4356}
4357
4358void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004359display_run(struct display *display)
4360{
4361 struct task *task;
4362 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004363 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004364
Pekka Paalanen826d7952011-12-15 10:14:07 +02004365 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004366 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004367 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004368 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004369 struct task, link);
4370 wl_list_remove(&task->link);
4371 task->run(task, 0);
4372 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004373
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004374 wl_display_dispatch_pending(display->display);
4375
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004376 if (!display->running)
4377 break;
4378
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004379 ret = wl_display_flush(display->display);
4380 if (ret < 0 && errno == EAGAIN) {
4381 ep[0].events =
4382 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4383 ep[0].data.ptr = &display->display_task;
4384
4385 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4386 display->display_fd, &ep[0]);
4387 } else if (ret < 0) {
4388 break;
4389 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004390
4391 count = epoll_wait(display->epoll_fd,
4392 ep, ARRAY_LENGTH(ep), -1);
4393 for (i = 0; i < count; i++) {
4394 task = ep[i].data.ptr;
4395 task->run(task, ep[i].events);
4396 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004397 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004398}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004399
4400void
4401display_exit(struct display *display)
4402{
4403 display->running = 0;
4404}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004405
4406void
4407keysym_modifiers_add(struct wl_array *modifiers_map,
4408 const char *name)
4409{
4410 size_t len = strlen(name) + 1;
4411 char *p;
4412
4413 p = wl_array_add(modifiers_map, len);
4414
4415 if (p == NULL)
4416 return;
4417
4418 strncpy(p, name, len);
4419}
4420
4421static xkb_mod_index_t
4422keysym_modifiers_get_index(struct wl_array *modifiers_map,
4423 const char *name)
4424{
4425 xkb_mod_index_t index = 0;
4426 char *p = modifiers_map->data;
4427
4428 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4429 if (strcmp(p, name) == 0)
4430 return index;
4431
4432 index++;
4433 p += strlen(p) + 1;
4434 }
4435
4436 return XKB_MOD_INVALID;
4437}
4438
4439xkb_mod_mask_t
4440keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4441 const char *name)
4442{
4443 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4444
4445 if (index == XKB_MOD_INVALID)
4446 return XKB_MOD_INVALID;
4447
4448 return 1 << index;
4449}