blob: f24f42ca20cce72112e8d38b06982389a8af498b [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 Paalanenec076692012-11-30 13:37:27 +0200150enum toysurface_prepare_flags {
151 SURFACE_HINT_RESIZE = 0x01,
152};
153
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200154struct toysurface {
155 /*
156 * Prepare the surface for drawing. Makes sure there is a surface
157 * of the right size available for rendering, and returns it.
158 * dx,dy are the x,y of wl_surface.attach.
159 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200160 * If flags has SURFACE_HINT_RESIZE set, the user is
161 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200162 * Returns the Cairo surface to draw to.
163 */
164 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200165 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200166
167 /*
168 * Post the surface to the server, returning the server allocation
169 * rectangle. The Cairo surface from prepare() must be destroyed
170 * after calling this.
171 */
172 void (*swap)(struct toysurface *base,
173 struct rectangle *server_allocation);
174
175 /*
176 * Make the toysurface current with the given EGL context.
177 * Returns 0 on success, and negative of failure.
178 */
179 int (*acquire)(struct toysurface *base, EGLContext ctx);
180
181 /*
182 * Release the toysurface from the EGL context, returning control
183 * to Cairo.
184 */
185 void (*release)(struct toysurface *base);
186
187 /*
188 * Destroy the toysurface, including the Cairo surface, any
189 * backing storage, and the Wayland protocol objects.
190 */
191 void (*destroy)(struct toysurface *base);
192};
193
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500194struct window {
195 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500196 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100197 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500198 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200199 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500200 struct wl_region *input_region;
201 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500202 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500203 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400204 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500205 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500206 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400207 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400208 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400209 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400210 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400211 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400212 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400213 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400214 int focus_count;
215
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400216 enum window_buffer_type buffer_type;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +0200217 enum wl_output_transform buffer_transform;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200218 struct toysurface *toysurface;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400219 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500220
Pekka Paalanen99436862012-11-19 17:15:59 +0200221 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400222
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500223 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500224 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400225 window_data_handler_t data_handler;
226 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500227 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400228 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200229 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400230
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300231 struct wl_callback *frame_cb;
232
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200233 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500234 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500235
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500236 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400237 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500238};
239
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500240struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500241 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300242 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500243 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400244 struct wl_list link;
245 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500246 widget_resize_handler_t resize_handler;
247 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500248 widget_enter_handler_t enter_handler;
249 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500250 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500251 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200252 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400253 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500254 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300255 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400256};
257
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400258struct input {
259 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100260 struct wl_seat *seat;
261 struct wl_pointer *pointer;
262 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400263 struct window *pointer_focus;
264 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300265 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300266 uint32_t cursor_anim_start;
267 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300268 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400269 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400270 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400271 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400272 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400273 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400274
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500275 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500276 struct widget *grab;
277 uint32_t grab_button;
278
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400279 struct wl_data_device *data_device;
280 struct data_offer *drag_offer;
281 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100282
283 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100284 struct xkb_keymap *keymap;
285 struct xkb_state *state;
286 xkb_mod_mask_t control_mask;
287 xkb_mod_mask_t alt_mask;
288 xkb_mod_mask_t shift_mask;
289 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400290
291 struct task repeat_task;
292 int repeat_timer_fd;
293 uint32_t repeat_sym;
294 uint32_t repeat_key;
295 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400296};
297
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500298struct output {
299 struct display *display;
300 struct wl_output *output;
301 struct rectangle allocation;
302 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600303 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200304
305 display_output_handler_t destroy_handler;
306 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500307};
308
Martin Minarik1998b152012-05-10 02:04:35 +0200309enum frame_button_action {
310 FRAME_BUTTON_NULL = 0,
311 FRAME_BUTTON_ICON = 1,
312 FRAME_BUTTON_CLOSE = 2,
313 FRAME_BUTTON_MINIMIZE = 3,
314 FRAME_BUTTON_MAXIMIZE = 4,
315};
316
317enum frame_button_pointer {
318 FRAME_BUTTON_DEFAULT = 0,
319 FRAME_BUTTON_OVER = 1,
320 FRAME_BUTTON_ACTIVE = 2,
321};
322
323enum frame_button_align {
324 FRAME_BUTTON_RIGHT = 0,
325 FRAME_BUTTON_LEFT = 1,
326};
327
328enum frame_button_decoration {
329 FRAME_BUTTON_NONE = 0,
330 FRAME_BUTTON_FANCY = 1,
331};
332
333struct frame_button {
334 struct widget *widget;
335 struct frame *frame;
336 cairo_surface_t *icon;
337 enum frame_button_action type;
338 enum frame_button_pointer state;
339 struct wl_list link; /* buttons_list */
340 enum frame_button_align align;
341 enum frame_button_decoration decoration;
342};
343
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500344struct frame {
345 struct widget *widget;
346 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200347 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500348};
349
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500350struct menu {
351 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500352 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500353 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500354 const char **entries;
355 uint32_t time;
356 int current;
357 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400358 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500359 menu_func_t func;
360};
361
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300362struct tooltip {
363 struct widget *parent;
364 struct window *window;
365 struct widget *widget;
366 char *entry;
367 struct task tooltip_task;
368 int tooltip_fd;
369 float x, y;
370};
371
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700372struct shm_pool {
373 struct wl_shm_pool *pool;
374 size_t size;
375 size_t used;
376 void *data;
377};
378
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400379enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300380 CURSOR_DEFAULT = 100,
381 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400382};
383
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500384enum window_location {
385 WINDOW_INTERIOR = 0,
386 WINDOW_RESIZING_TOP = 1,
387 WINDOW_RESIZING_BOTTOM = 2,
388 WINDOW_RESIZING_LEFT = 4,
389 WINDOW_RESIZING_TOP_LEFT = 5,
390 WINDOW_RESIZING_BOTTOM_LEFT = 6,
391 WINDOW_RESIZING_RIGHT = 8,
392 WINDOW_RESIZING_TOP_RIGHT = 9,
393 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
394 WINDOW_RESIZING_MASK = 15,
395 WINDOW_EXTERIOR = 16,
396 WINDOW_TITLEBAR = 17,
397 WINDOW_CLIENT_AREA = 18,
398};
399
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200400static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500402#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400403
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200404struct egl_window_surface {
405 struct toysurface base;
406 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100407 struct display *display;
408 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200409 struct wl_egl_window *egl_window;
410 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100411};
412
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200413static struct egl_window_surface *
414to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100415{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200416 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100417}
418
419static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200420egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200421 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100422{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200423 struct egl_window_surface *surface = to_egl_window_surface(base);
424
425 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
426 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
427
428 return cairo_surface_reference(surface->cairo_surface);
429}
430
431static void
432egl_window_surface_swap(struct toysurface *base,
433 struct rectangle *server_allocation)
434{
435 struct egl_window_surface *surface = to_egl_window_surface(base);
436
437 cairo_gl_surface_swapbuffers(surface->cairo_surface);
438 wl_egl_window_get_attached_size(surface->egl_window,
439 &server_allocation->width,
440 &server_allocation->height);
441}
442
443static int
444egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
445{
446 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200447 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100448
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200449 device = cairo_surface_get_device(surface->cairo_surface);
450 if (!device)
451 return -1;
452
453 if (!ctx) {
454 if (device == surface->display->argb_device)
455 ctx = surface->display->argb_ctx;
456 else
457 assert(0);
458 }
459
460 cairo_device_flush(device);
461 cairo_device_acquire(device);
462 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
463 surface->egl_surface, ctx))
464 fprintf(stderr, "failed to make surface current\n");
465
466 return 0;
467}
468
469static void
470egl_window_surface_release(struct toysurface *base)
471{
472 struct egl_window_surface *surface = to_egl_window_surface(base);
473 cairo_device_t *device;
474
475 device = cairo_surface_get_device(surface->cairo_surface);
476 if (!device)
477 return;
478
479 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
480 surface->display->argb_ctx))
481 fprintf(stderr, "failed to make context current\n");
482
483 cairo_device_release(device);
484}
485
486static void
487egl_window_surface_destroy(struct toysurface *base)
488{
489 struct egl_window_surface *surface = to_egl_window_surface(base);
490 struct display *d = surface->display;
491
492 cairo_surface_destroy(surface->cairo_surface);
493 eglDestroySurface(d->dpy, surface->egl_surface);
494 wl_egl_window_destroy(surface->egl_window);
495 surface->surface = NULL;
496
497 free(surface);
498}
499
500static struct toysurface *
501egl_window_surface_create(struct display *display,
502 struct wl_surface *wl_surface,
503 uint32_t flags,
504 struct rectangle *rectangle)
505{
506 struct egl_window_surface *surface;
507
Pekka Paalanenb3627362012-11-19 17:16:00 +0200508 if (display->dpy == EGL_NO_DISPLAY)
509 return NULL;
510
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200511 surface = calloc(1, sizeof *surface);
512 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100513 return NULL;
514
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200515 surface->base.prepare = egl_window_surface_prepare;
516 surface->base.swap = egl_window_surface_swap;
517 surface->base.acquire = egl_window_surface_acquire;
518 surface->base.release = egl_window_surface_release;
519 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200521 surface->display = display;
522 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400523
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200524 surface->egl_window = wl_egl_window_create(surface->surface,
525 rectangle->width,
526 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100527
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200528 surface->egl_surface = eglCreateWindowSurface(display->dpy,
529 display->argb_config,
530 surface->egl_window,
531 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100532
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200533 surface->cairo_surface =
534 cairo_gl_surface_create_for_egl(display->argb_device,
535 surface->egl_surface,
536 rectangle->width,
537 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100538
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200539 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100540}
541
Pekka Paalanenb3627362012-11-19 17:16:00 +0200542#else
543
544static struct toysurface *
545egl_window_surface_create(struct display *display,
546 struct wl_surface *wl_surface,
547 uint32_t flags,
548 struct rectangle *rectangle)
549{
550 return NULL;
551}
552
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400553#endif
554
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200555struct shm_surface_data {
556 struct wl_buffer *buffer;
557 struct shm_pool *pool;
558};
559
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560struct wl_buffer *
561display_get_buffer_for_surface(struct display *display,
562 cairo_surface_t *surface)
563{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200564 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400565
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200566 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400567
568 return data->buffer;
569}
570
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500571static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700572shm_pool_destroy(struct shm_pool *pool);
573
574static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400575shm_surface_data_destroy(void *p)
576{
577 struct shm_surface_data *data = p;
578
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200579 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700580 if (data->pool)
581 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300582
583 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584}
585
Kristian Høgsberg16626282012-04-03 11:21:27 -0400586static struct wl_shm_pool *
587make_shm_pool(struct display *display, int size, void **data)
588{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400589 struct wl_shm_pool *pool;
590 int fd;
591
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300592 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400593 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300594 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
595 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400596 return NULL;
597 }
598
599 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400600 if (*data == MAP_FAILED) {
601 fprintf(stderr, "mmap failed: %m\n");
602 close(fd);
603 return NULL;
604 }
605
606 pool = wl_shm_create_pool(display->shm, fd, size);
607
608 close(fd);
609
610 return pool;
611}
612
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700613static struct shm_pool *
614shm_pool_create(struct display *display, size_t size)
615{
616 struct shm_pool *pool = malloc(sizeof *pool);
617
618 if (!pool)
619 return NULL;
620
621 pool->pool = make_shm_pool(display, size, &pool->data);
622 if (!pool->pool) {
623 free(pool);
624 return NULL;
625 }
626
627 pool->size = size;
628 pool->used = 0;
629
630 return pool;
631}
632
633static void *
634shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
635{
636 if (pool->used + size > pool->size)
637 return NULL;
638
639 *offset = pool->used;
640 pool->used += size;
641
642 return (char *) pool->data + *offset;
643}
644
645/* destroy the pool. this does not unmap the memory though */
646static void
647shm_pool_destroy(struct shm_pool *pool)
648{
649 munmap(pool->data, pool->size);
650 wl_shm_pool_destroy(pool->pool);
651 free(pool);
652}
653
654/* Start allocating from the beginning of the pool again */
655static void
656shm_pool_reset(struct shm_pool *pool)
657{
658 pool->used = 0;
659}
660
661static int
662data_length_for_shm_surface(struct rectangle *rect)
663{
664 int stride;
665
666 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
667 rect->width);
668 return stride * rect->height;
669}
670
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500671static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700672display_create_shm_surface_from_pool(struct display *display,
673 struct rectangle *rectangle,
674 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675{
676 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400677 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400678 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700679 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400680 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681
682 data = malloc(sizeof *data);
683 if (data == NULL)
684 return NULL;
685
686 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
687 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700688 length = stride * rectangle->height;
689 data->pool = NULL;
690 map = shm_pool_allocate(pool, length, &offset);
691
692 if (!map) {
693 free(data);
694 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400695 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400696
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400697 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400698 CAIRO_FORMAT_ARGB32,
699 rectangle->width,
700 rectangle->height,
701 stride);
702
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200703 cairo_surface_set_user_data(surface, &shm_surface_data_key,
704 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400705
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400706 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500707 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400708 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500709 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400710
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200711 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
712 rectangle->width,
713 rectangle->height,
714 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400715
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700716 return surface;
717}
718
719static cairo_surface_t *
720display_create_shm_surface(struct display *display,
721 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200722 struct shm_pool *alternate_pool,
723 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700724{
725 struct shm_surface_data *data;
726 struct shm_pool *pool;
727 cairo_surface_t *surface;
728
Pekka Paalanen99436862012-11-19 17:15:59 +0200729 if (alternate_pool) {
730 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700731 surface = display_create_shm_surface_from_pool(display,
732 rectangle,
733 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200734 alternate_pool);
735 if (surface) {
736 data = cairo_surface_get_user_data(surface,
737 &shm_surface_data_key);
738 goto out;
739 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700740 }
741
742 pool = shm_pool_create(display,
743 data_length_for_shm_surface(rectangle));
744 if (!pool)
745 return NULL;
746
747 surface =
748 display_create_shm_surface_from_pool(display, rectangle,
749 flags, pool);
750
751 if (!surface) {
752 shm_pool_destroy(pool);
753 return NULL;
754 }
755
756 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200757 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700758 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400759
Pekka Paalanen99436862012-11-19 17:15:59 +0200760out:
761 if (data_ret)
762 *data_ret = data;
763
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400764 return surface;
765}
766
nobled7b87cb02011-02-01 18:51:47 +0000767static int
768check_size(struct rectangle *rect)
769{
770 if (rect->width && rect->height)
771 return 0;
772
773 fprintf(stderr, "tried to create surface of "
774 "width: %d, height: %d\n", rect->width, rect->height);
775 return -1;
776}
777
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400778cairo_surface_t *
779display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100780 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400781 struct rectangle *rectangle,
782 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400783{
nobled7b87cb02011-02-01 18:51:47 +0000784 if (check_size(rectangle) < 0)
785 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200786
787 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200788 return display_create_shm_surface(display, rectangle, flags,
789 NULL, NULL);
790}
791
Pekka Paalanena4eda732012-11-19 17:16:02 +0200792struct shm_surface_leaf {
793 cairo_surface_t *cairo_surface;
794 /* 'data' is automatically destroyed, when 'cairo_surface' is */
795 struct shm_surface_data *data;
796
797 struct shm_pool *resize_pool;
798 int busy;
799};
800
801static void
802shm_surface_leaf_release(struct shm_surface_leaf *leaf)
803{
804 if (leaf->cairo_surface)
805 cairo_surface_destroy(leaf->cairo_surface);
806 /* leaf->data already destroyed via cairo private */
807
808 if (leaf->resize_pool)
809 shm_pool_destroy(leaf->resize_pool);
810}
811
Pekka Paalanen99436862012-11-19 17:15:59 +0200812struct shm_surface {
813 struct toysurface base;
814 struct display *display;
815 struct wl_surface *surface;
816 uint32_t flags;
817 int dx, dy;
818
Pekka Paalanena4eda732012-11-19 17:16:02 +0200819 struct shm_surface_leaf leaf[2];
820 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200821};
822
823static struct shm_surface *
824to_shm_surface(struct toysurface *base)
825{
826 return container_of(base, struct shm_surface, base);
827}
828
Pekka Paalanena4eda732012-11-19 17:16:02 +0200829static void
830shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
831{
832 struct shm_surface_leaf *leaf = data;
833
834 leaf->busy = 0;
835}
836
837static const struct wl_buffer_listener shm_surface_buffer_listener = {
838 shm_surface_buffer_release
839};
840
Pekka Paalanen99436862012-11-19 17:15:59 +0200841static cairo_surface_t *
842shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200843 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200844{
Pekka Paalanenec076692012-11-30 13:37:27 +0200845 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200846 struct shm_surface *surface = to_shm_surface(base);
847 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200848 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200849
850 surface->dx = dx;
851 surface->dy = dy;
852
Pekka Paalanena4eda732012-11-19 17:16:02 +0200853 /* pick a free buffer from the two */
854 if (!surface->leaf[0].busy)
855 leaf = &surface->leaf[0];
856 else if (!surface->leaf[1].busy)
857 leaf = &surface->leaf[1];
858 else {
859 fprintf(stderr, "%s: both buffers are held by the server.\n",
860 __func__);
861 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200862 }
863
Pekka Paalanena4eda732012-11-19 17:16:02 +0200864 if (!resize_hint && leaf->resize_pool) {
865 cairo_surface_destroy(leaf->cairo_surface);
866 leaf->cairo_surface = NULL;
867 shm_pool_destroy(leaf->resize_pool);
868 leaf->resize_pool = NULL;
869 }
870
871 if (leaf->cairo_surface &&
872 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
873 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200874 goto out;
875
Pekka Paalanena4eda732012-11-19 17:16:02 +0200876 if (leaf->cairo_surface)
877 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200878
Pekka Paalanena4eda732012-11-19 17:16:02 +0200879 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200880 /* Create a big pool to allocate from, while continuously
881 * resizing. Mmapping a new pool in the server
882 * is relatively expensive, so reusing a pool performs
883 * better, but may temporarily reserve unneeded memory.
884 */
885 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200886 leaf->resize_pool = shm_pool_create(surface->display,
887 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200888 }
889
Pekka Paalanena4eda732012-11-19 17:16:02 +0200890 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200891 display_create_shm_surface(surface->display, &rect,
892 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200893 leaf->resize_pool,
894 &leaf->data);
895 wl_buffer_add_listener(leaf->data->buffer,
896 &shm_surface_buffer_listener, leaf);
Pekka Paalanen99436862012-11-19 17:15:59 +0200897
898out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200899 surface->current = leaf;
900
901 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200902}
903
904static void
905shm_surface_swap(struct toysurface *base,
906 struct rectangle *server_allocation)
907{
908 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200909 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200910
911 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200912 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200913 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200914 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200915
Pekka Paalanena4eda732012-11-19 17:16:02 +0200916 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200917 surface->dx, surface->dy);
918 wl_surface_damage(surface->surface, 0, 0,
919 server_allocation->width, server_allocation->height);
920 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200921
922 leaf->busy = 1;
923 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200924}
925
926static int
927shm_surface_acquire(struct toysurface *base, EGLContext ctx)
928{
929 return -1;
930}
931
932static void
933shm_surface_release(struct toysurface *base)
934{
935}
936
937static void
938shm_surface_destroy(struct toysurface *base)
939{
940 struct shm_surface *surface = to_shm_surface(base);
941
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942 shm_surface_leaf_release(&surface->leaf[0]);
943 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200944
945 free(surface);
946}
947
948static struct toysurface *
949shm_surface_create(struct display *display, struct wl_surface *wl_surface,
950 uint32_t flags, struct rectangle *rectangle)
951{
952 struct shm_surface *surface;
953
954 surface = calloc(1, sizeof *surface);
955 if (!surface)
956 return NULL;
957
958 surface->base.prepare = shm_surface_prepare;
959 surface->base.swap = shm_surface_swap;
960 surface->base.acquire = shm_surface_acquire;
961 surface->base.release = shm_surface_release;
962 surface->base.destroy = shm_surface_destroy;
963
964 surface->display = display;
965 surface->surface = wl_surface;
966 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200967
968 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400969}
970
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200971/*
972 * The following correspondences between file names and cursors was copied
973 * from: https://bugs.kde.org/attachment.cgi?id=67313
974 */
975
976static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300977 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200978 "sw-resize"
979};
980
981static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300982 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200983 "se-resize"
984};
985
986static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300987 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200988 "s-resize"
989};
990
991static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300992 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200993 "closedhand",
994 "208530c400c041818281048008011002"
995};
996
997static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300998 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200999 "default",
1000 "top_left_arrow",
1001 "left-arrow"
1002};
1003
1004static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001005 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001006 "w-resize"
1007};
1008
1009static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001010 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001011 "e-resize"
1012};
1013
1014static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001015 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001016 "nw-resize"
1017};
1018
1019static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001020 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001021 "ne-resize"
1022};
1023
1024static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001025 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001026 "n-resize"
1027};
1028
1029static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001030 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001031 "ibeam",
1032 "text"
1033};
1034
1035static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001036 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001037 "pointer",
1038 "pointing_hand",
1039 "e29285e634086352946a0e7090d73106"
1040};
1041
1042static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001043 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001044 "wait",
1045 "0426c94ea35c87780ff01dc239897213"
1046};
1047
1048struct cursor_alternatives {
1049 const char **names;
1050 size_t count;
1051};
1052
1053static const struct cursor_alternatives cursors[] = {
1054 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1055 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1056 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1057 {grabbings, ARRAY_LENGTH(grabbings)},
1058 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1059 {left_sides, ARRAY_LENGTH(left_sides)},
1060 {right_sides, ARRAY_LENGTH(right_sides)},
1061 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1062 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1063 {top_sides, ARRAY_LENGTH(top_sides)},
1064 {xterms, ARRAY_LENGTH(xterms)},
1065 {hand1s, ARRAY_LENGTH(hand1s)},
1066 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001067};
1068
1069static void
1070create_cursors(struct display *display)
1071{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001072 char *config_file;
1073 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001074 unsigned int i, j;
1075 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001076 struct config_key shell_keys[] = {
1077 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1078 };
1079 struct config_section cs[] = {
1080 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1081 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001082
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001083 config_file = config_file_path("weston.ini");
1084 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1085 free(config_file);
1086
1087 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001088 display->cursors =
1089 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1090
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001091 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001092 cursor = NULL;
1093 for (j = 0; !cursor && j < cursors[i].count; ++j)
1094 cursor = wl_cursor_theme_get_cursor(
1095 display->cursor_theme, cursors[i].names[j]);
1096
1097 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001098 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001099 cursors[i].names[0]);
1100
1101 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001102 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001103}
1104
1105static void
1106destroy_cursors(struct display *display)
1107{
1108 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001109 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001110}
1111
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001112struct wl_cursor_image *
1113display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001114{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001115 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001116
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001117 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001118}
1119
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001120static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001121window_get_resize_dx_dy(struct window *window, int *x, int *y)
1122{
1123 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1124 *x = window->server_allocation.width - window->allocation.width;
1125 else
1126 *x = 0;
1127
1128 if (window->resize_edges & WINDOW_RESIZING_TOP)
1129 *y = window->server_allocation.height -
1130 window->allocation.height;
1131 else
1132 *y = 0;
1133
1134 window->resize_edges = 0;
1135}
1136
1137static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001138window_attach_surface(struct window *window)
1139{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001140 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001141
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001142 if (window->type == TYPE_NONE) {
1143 window->type = TYPE_TOPLEVEL;
1144 if (display->shell)
1145 wl_shell_surface_set_toplevel(window->shell_surface);
1146 }
1147
Pekka Paalanen512dde82012-10-10 12:49:27 +03001148 if (window->opaque_region) {
1149 wl_surface_set_opaque_region(window->surface,
1150 window->opaque_region);
1151 wl_region_destroy(window->opaque_region);
1152 window->opaque_region = NULL;
1153 }
1154
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001155 if (window->input_region) {
1156 wl_surface_set_input_region(window->surface,
1157 window->input_region);
1158 wl_region_destroy(window->input_region);
1159 window->input_region = NULL;
1160 }
1161
Pekka Paalanen99436862012-11-19 17:15:59 +02001162 window->toysurface->swap(window->toysurface,
1163 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001164}
1165
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001166int
1167window_has_focus(struct window *window)
1168{
1169 return window->focus_count > 0;
1170}
1171
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001172static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001173window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001174{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001175 if (!window->cairo_surface)
1176 return;
1177
1178 window_attach_surface(window);
1179 cairo_surface_destroy(window->cairo_surface);
1180 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001181}
1182
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001183struct display *
1184window_get_display(struct window *window)
1185{
1186 return window->display;
1187}
1188
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001189static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001190window_create_surface(struct window *window)
1191{
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001192 struct rectangle allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001193 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001194 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001195
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001196 if (!window->transparent)
1197 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001198
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001199 switch (window->buffer_transform) {
1200 case WL_OUTPUT_TRANSFORM_90:
1201 case WL_OUTPUT_TRANSFORM_270:
1202 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1203 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1204 allocation.width = window->allocation.height;
1205 allocation.height = window->allocation.width;
1206 break;
1207 default:
1208 break;
1209 }
1210
Pekka Paalanenb3627362012-11-19 17:16:00 +02001211 if (!window->toysurface &&
1212 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1213 window->display->dpy) {
1214 window->toysurface =
1215 egl_window_surface_create(window->display,
1216 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001217 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001218 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001219
Pekka Paalanenb3627362012-11-19 17:16:00 +02001220 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001221 window->toysurface = shm_surface_create(window->display,
1222 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001223 &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001224
Pekka Paalanenec076692012-11-30 13:37:27 +02001225 if (window->resizing)
1226 flags = SURFACE_HINT_RESIZE;
1227 else
1228 flags = 0;
1229
Pekka Paalanen99436862012-11-19 17:15:59 +02001230 window_get_resize_dx_dy(window, &dx, &dy);
1231 window->cairo_surface =
1232 window->toysurface->prepare(window->toysurface, dx, dy,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001233 allocation.width,
1234 allocation.height,
Pekka Paalanenec076692012-11-30 13:37:27 +02001235 flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001236}
1237
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001238int
1239window_get_buffer_transform(struct window *window)
1240{
1241 return window->buffer_transform;
1242}
1243
1244void
1245window_set_buffer_transform(struct window *window,
1246 enum wl_output_transform transform)
1247{
1248 window->buffer_transform = transform;
1249 wl_surface_set_buffer_transform(window->surface, transform);
1250}
1251
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001252static void frame_destroy(struct frame *frame);
1253
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001254void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001255window_destroy(struct window *window)
1256{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001257 struct display *display = window->display;
1258 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001259 struct window_output *window_output;
1260 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001261
1262 if (window->redraw_scheduled)
1263 wl_list_remove(&window->redraw_task.link);
1264
1265 wl_list_for_each(input, &display->input_list, link) {
1266 if (input->pointer_focus == window)
1267 input->pointer_focus = NULL;
1268 if (input->keyboard_focus == window)
1269 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001270 if (input->focus_widget &&
1271 input->focus_widget->window == window)
1272 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001273 }
1274
Rob Bradford7507b572012-05-15 17:55:34 +01001275 wl_list_for_each_safe(window_output, window_output_tmp,
1276 &window->window_output_list, link) {
1277 free (window_output);
1278 }
1279
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001280 if (window->input_region)
1281 wl_region_destroy(window->input_region);
1282 if (window->opaque_region)
1283 wl_region_destroy(window->opaque_region);
1284
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001285 if (window->frame)
1286 frame_destroy(window->frame);
1287
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001288 if (window->shell_surface)
1289 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001290 wl_surface_destroy(window->surface);
1291 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001292
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001293 if (window->toysurface)
1294 window->toysurface->destroy(window->toysurface);
1295
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001296 if (window->frame_cb)
1297 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001298 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001299 free(window);
1300}
1301
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001302static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001303widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001304{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001305 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001306
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001307 wl_list_for_each(child, &widget->child_list, link) {
1308 target = widget_find_widget(child, x, y);
1309 if (target)
1310 return target;
1311 }
1312
1313 if (widget->allocation.x <= x &&
1314 x < widget->allocation.x + widget->allocation.width &&
1315 widget->allocation.y <= y &&
1316 y < widget->allocation.y + widget->allocation.height) {
1317 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001318 }
1319
1320 return NULL;
1321}
1322
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001323static struct widget *
1324widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001325{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001326 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001327
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001328 widget = malloc(sizeof *widget);
1329 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001330 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001331 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001332 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001333 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001334 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001335 widget->tooltip = NULL;
1336 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001337
1338 return widget;
1339}
1340
1341struct widget *
1342window_add_widget(struct window *window, void *data)
1343{
1344 window->widget = widget_create(window, data);
1345 wl_list_init(&window->widget->link);
1346
1347 return window->widget;
1348}
1349
1350struct widget *
1351widget_add_widget(struct widget *parent, void *data)
1352{
1353 struct widget *widget;
1354
1355 widget = widget_create(parent->window, data);
1356 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001357
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001358 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001359}
1360
1361void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001362widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001363{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001364 struct display *display = widget->window->display;
1365 struct input *input;
1366
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001367 if (widget->tooltip) {
1368 free(widget->tooltip);
1369 widget->tooltip = NULL;
1370 }
1371
Pekka Paalanene156fb62012-01-19 13:51:38 +02001372 wl_list_for_each(input, &display->input_list, link) {
1373 if (input->focus_widget == widget)
1374 input->focus_widget = NULL;
1375 }
1376
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001377 wl_list_remove(&widget->link);
1378 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001379}
1380
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001381void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001382widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001383{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001384 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001385}
1386
1387void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001388widget_set_size(struct widget *widget, int32_t width, int32_t height)
1389{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001390 widget->allocation.width = width;
1391 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001392}
1393
1394void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001395widget_set_allocation(struct widget *widget,
1396 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001397{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001398 widget->allocation.x = x;
1399 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001400 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001401}
1402
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001403void
1404widget_set_transparent(struct widget *widget, int transparent)
1405{
1406 widget->opaque = !transparent;
1407}
1408
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001409void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001410widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001411{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001412 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001413}
1414
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001415void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001416widget_set_resize_handler(struct widget *widget,
1417 widget_resize_handler_t handler)
1418{
1419 widget->resize_handler = handler;
1420}
1421
1422void
1423widget_set_redraw_handler(struct widget *widget,
1424 widget_redraw_handler_t handler)
1425{
1426 widget->redraw_handler = handler;
1427}
1428
1429void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001430widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001431{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001432 widget->enter_handler = handler;
1433}
1434
1435void
1436widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1437{
1438 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001439}
1440
1441void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001442widget_set_motion_handler(struct widget *widget,
1443 widget_motion_handler_t handler)
1444{
1445 widget->motion_handler = handler;
1446}
1447
1448void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001449widget_set_button_handler(struct widget *widget,
1450 widget_button_handler_t handler)
1451{
1452 widget->button_handler = handler;
1453}
1454
1455void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001456widget_set_axis_handler(struct widget *widget,
1457 widget_axis_handler_t handler)
1458{
1459 widget->axis_handler = handler;
1460}
1461
1462void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001463widget_schedule_redraw(struct widget *widget)
1464{
1465 window_schedule_redraw(widget->window);
1466}
1467
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001468cairo_surface_t *
1469window_get_surface(struct window *window)
1470{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001471 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001472}
1473
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001474struct wl_surface *
1475window_get_wl_surface(struct window *window)
1476{
1477 return window->surface;
1478}
1479
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001480struct wl_shell_surface *
1481window_get_wl_shell_surface(struct window *window)
1482{
1483 return window->shell_surface;
1484}
1485
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001486static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001487tooltip_redraw_handler(struct widget *widget, void *data)
1488{
1489 cairo_t *cr;
1490 const int32_t r = 3;
1491 struct tooltip *tooltip = data;
1492 int32_t width, height;
1493 struct window *window = widget->window;
1494
1495 cr = cairo_create(window->cairo_surface);
1496 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1497 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1498 cairo_paint(cr);
1499
1500 width = window->allocation.width;
1501 height = window->allocation.height;
1502 rounded_rect(cr, 0, 0, width, height, r);
1503
1504 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1505 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1506 cairo_fill(cr);
1507
1508 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1509 cairo_move_to(cr, 10, 16);
1510 cairo_show_text(cr, tooltip->entry);
1511 cairo_destroy(cr);
1512}
1513
1514static cairo_text_extents_t
1515get_text_extents(struct tooltip *tooltip)
1516{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001517 cairo_t *cr;
1518 cairo_text_extents_t extents;
1519
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001520 /* Use the dummy_surface because tooltip's surface was not
1521 * created yet, and parent does not have a valid surface
1522 * outside repaint, either.
1523 */
1524 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001525 cairo_text_extents(cr, tooltip->entry, &extents);
1526 cairo_destroy(cr);
1527
1528 return extents;
1529}
1530
1531static int
1532window_create_tooltip(struct tooltip *tooltip)
1533{
1534 struct widget *parent = tooltip->parent;
1535 struct display *display = parent->window->display;
1536 struct window *window;
1537 const int offset_y = 27;
1538 const int margin = 3;
1539 cairo_text_extents_t extents;
1540
1541 if (tooltip->widget)
1542 return 0;
1543
1544 window = window_create_transient(display, parent->window, tooltip->x,
1545 tooltip->y + offset_y,
1546 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1547 if (!window)
1548 return -1;
1549
1550 tooltip->window = window;
1551 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1552
1553 extents = get_text_extents(tooltip);
1554 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1555 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1556
1557 return 0;
1558}
1559
1560void
1561widget_destroy_tooltip(struct widget *parent)
1562{
1563 struct tooltip *tooltip = parent->tooltip;
1564
1565 parent->tooltip_count = 0;
1566 if (!tooltip)
1567 return;
1568
1569 if (tooltip->widget) {
1570 widget_destroy(tooltip->widget);
1571 window_destroy(tooltip->window);
1572 tooltip->widget = NULL;
1573 tooltip->window = NULL;
1574 }
1575
1576 close(tooltip->tooltip_fd);
1577 free(tooltip->entry);
1578 free(tooltip);
1579 parent->tooltip = NULL;
1580}
1581
1582static void
1583tooltip_func(struct task *task, uint32_t events)
1584{
1585 struct tooltip *tooltip =
1586 container_of(task, struct tooltip, tooltip_task);
1587 uint64_t exp;
1588
Martin Olsson8df662a2012-07-08 03:03:47 +02001589 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1590 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001591 window_create_tooltip(tooltip);
1592}
1593
1594#define TOOLTIP_TIMEOUT 500
1595static int
1596tooltip_timer_reset(struct tooltip *tooltip)
1597{
1598 struct itimerspec its;
1599
1600 its.it_interval.tv_sec = 0;
1601 its.it_interval.tv_nsec = 0;
1602 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1603 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1604 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1605 fprintf(stderr, "could not set timerfd\n: %m");
1606 return -1;
1607 }
1608
1609 return 0;
1610}
1611
1612int
1613widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1614{
1615 struct tooltip *tooltip = parent->tooltip;
1616
1617 parent->tooltip_count++;
1618 if (tooltip) {
1619 tooltip->x = x;
1620 tooltip->y = y;
1621 tooltip_timer_reset(tooltip);
1622 return 0;
1623 }
1624
1625 /* the handler might be triggered too fast via input device motion, so
1626 * we need this check here to make sure tooltip is fully initialized */
1627 if (parent->tooltip_count > 1)
1628 return 0;
1629
1630 tooltip = malloc(sizeof *tooltip);
1631 if (!tooltip)
1632 return -1;
1633
1634 parent->tooltip = tooltip;
1635 tooltip->parent = parent;
1636 tooltip->widget = NULL;
1637 tooltip->window = NULL;
1638 tooltip->x = x;
1639 tooltip->y = y;
1640 tooltip->entry = strdup(entry);
1641 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1642 if (tooltip->tooltip_fd < 0) {
1643 fprintf(stderr, "could not create timerfd\n: %m");
1644 return -1;
1645 }
1646
1647 tooltip->tooltip_task.run = tooltip_func;
1648 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1649 EPOLLIN, &tooltip->tooltip_task);
1650 tooltip_timer_reset(tooltip);
1651
1652 return 0;
1653}
1654
1655static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001656workspace_manager_state(void *data,
1657 struct workspace_manager *workspace_manager,
1658 uint32_t current,
1659 uint32_t count)
1660{
1661 struct display *display = data;
1662
1663 display->workspace = current;
1664 display->workspace_count = count;
1665}
1666
1667static const struct workspace_manager_listener workspace_manager_listener = {
1668 workspace_manager_state
1669};
1670
1671static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001672frame_resize_handler(struct widget *widget,
1673 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001674{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001675 struct frame *frame = data;
1676 struct widget *child = frame->child;
1677 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001678 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001679 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001680 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001681 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001682 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001683 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001684
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001685 switch (widget->window->type) {
1686 case TYPE_FULLSCREEN:
1687 decoration_width = 0;
1688 decoration_height = 0;
1689
1690 allocation.x = 0;
1691 allocation.y = 0;
1692 allocation.width = width;
1693 allocation.height = height;
1694 opaque_margin = 0;
1695
1696 wl_list_for_each(button, &frame->buttons_list, link)
1697 button->widget->opaque = 1;
1698 break;
1699 case TYPE_MAXIMIZED:
1700 decoration_width = t->width * 2;
1701 decoration_height = t->width + t->titlebar_height;
1702
1703 allocation.x = t->width;
1704 allocation.y = t->titlebar_height;
1705 allocation.width = width - decoration_width;
1706 allocation.height = height - decoration_height;
1707
1708 opaque_margin = 0;
1709
1710 wl_list_for_each(button, &frame->buttons_list, link)
1711 button->widget->opaque = 0;
1712 break;
1713 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001714 decoration_width = (t->width + t->margin) * 2;
1715 decoration_height = t->width +
1716 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001717
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001718 allocation.x = t->width + t->margin;
1719 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001720 allocation.width = width - decoration_width;
1721 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001722
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001723 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001724
1725 wl_list_for_each(button, &frame->buttons_list, link)
1726 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001727 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001728 }
1729
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001730 widget_set_allocation(child, allocation.x, allocation.y,
1731 allocation.width, allocation.height);
1732
1733 if (child->resize_handler)
1734 child->resize_handler(child,
1735 allocation.width,
1736 allocation.height,
1737 child->user_data);
1738
Scott Moreauf7e498c2012-05-14 11:39:29 -06001739 width = child->allocation.width + decoration_width;
1740 height = child->allocation.height + decoration_height;
1741
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001742 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1743
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001744 widget->window->input_region =
1745 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001746 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001747 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001748 shadow_margin, shadow_margin,
1749 width - 2 * shadow_margin,
1750 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001751 } else {
1752 wl_region_add(widget->window->input_region,
1753 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001754 }
1755
Scott Moreauf7e498c2012-05-14 11:39:29 -06001756 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001757
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001758 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001759 wl_region_add(widget->window->opaque_region,
1760 opaque_margin, opaque_margin,
1761 widget->allocation.width - 2 * opaque_margin,
1762 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001763
1764 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001765 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1766 x_l = t->width + shadow_margin;
1767 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001768 wl_list_for_each(button, &frame->buttons_list, link) {
1769 const int button_padding = 4;
1770 w = cairo_image_surface_get_width(button->icon);
1771 h = cairo_image_surface_get_height(button->icon);
1772
1773 if (button->decoration == FRAME_BUTTON_FANCY)
1774 w += 10;
1775
1776 if (button->align == FRAME_BUTTON_LEFT) {
1777 widget_set_allocation(button->widget,
1778 x_l, y , w + 1, h + 1);
1779 x_l += w;
1780 x_l += button_padding;
1781 } else {
1782 x_r -= w;
1783 widget_set_allocation(button->widget,
1784 x_r, y , w + 1, h + 1);
1785 x_r -= button_padding;
1786 }
1787 }
1788}
1789
1790static int
1791frame_button_enter_handler(struct widget *widget,
1792 struct input *input, float x, float y, void *data)
1793{
1794 struct frame_button *frame_button = data;
1795
1796 widget_schedule_redraw(frame_button->widget);
1797 frame_button->state = FRAME_BUTTON_OVER;
1798
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001799 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001800}
1801
1802static void
1803frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1804{
1805 struct frame_button *frame_button = data;
1806
1807 widget_schedule_redraw(frame_button->widget);
1808 frame_button->state = FRAME_BUTTON_DEFAULT;
1809}
1810
1811static void
1812frame_button_button_handler(struct widget *widget,
1813 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001814 uint32_t button,
1815 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001816{
1817 struct frame_button *frame_button = data;
1818 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001819 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001820
1821 if (button != BTN_LEFT)
1822 return;
1823
1824 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001825 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001826 frame_button->state = FRAME_BUTTON_ACTIVE;
1827 widget_schedule_redraw(frame_button->widget);
1828
1829 if (frame_button->type == FRAME_BUTTON_ICON)
1830 window_show_frame_menu(window, input, time);
1831 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001832 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001833 frame_button->state = FRAME_BUTTON_DEFAULT;
1834 widget_schedule_redraw(frame_button->widget);
1835 break;
1836 }
1837
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001838 if (!was_pressed)
1839 return;
1840
Martin Minarik1998b152012-05-10 02:04:35 +02001841 switch (frame_button->type) {
1842 case FRAME_BUTTON_CLOSE:
1843 if (window->close_handler)
1844 window->close_handler(window->parent,
1845 window->user_data);
1846 else
1847 display_exit(window->display);
1848 break;
1849 case FRAME_BUTTON_MINIMIZE:
1850 fprintf(stderr,"Minimize stub\n");
1851 break;
1852 case FRAME_BUTTON_MAXIMIZE:
1853 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1854 break;
1855 default:
1856 /* Unknown operation */
1857 break;
1858 }
1859}
1860
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001861static int
1862frame_button_motion_handler(struct widget *widget,
1863 struct input *input, uint32_t time,
1864 float x, float y, void *data)
1865{
1866 struct frame_button *frame_button = data;
1867 enum frame_button_pointer previous_button_state = frame_button->state;
1868
1869 /* only track state for a pressed button */
1870 if (input->grab != widget)
1871 return CURSOR_LEFT_PTR;
1872
1873 if (x > widget->allocation.x &&
1874 x < (widget->allocation.x + widget->allocation.width) &&
1875 y > widget->allocation.y &&
1876 y < (widget->allocation.y + widget->allocation.height)) {
1877 frame_button->state = FRAME_BUTTON_ACTIVE;
1878 } else {
1879 frame_button->state = FRAME_BUTTON_DEFAULT;
1880 }
1881
1882 if (frame_button->state != previous_button_state)
1883 widget_schedule_redraw(frame_button->widget);
1884
1885 return CURSOR_LEFT_PTR;
1886}
1887
Martin Minarik1998b152012-05-10 02:04:35 +02001888static void
1889frame_button_redraw_handler(struct widget *widget, void *data)
1890{
1891 struct frame_button *frame_button = data;
1892 cairo_t *cr;
1893 int width, height, x, y;
1894 struct window *window = widget->window;
1895
1896 x = widget->allocation.x;
1897 y = widget->allocation.y;
1898 width = widget->allocation.width;
1899 height = widget->allocation.height;
1900
1901 if (!width)
1902 return;
1903 if (!height)
1904 return;
1905 if (widget->opaque)
1906 return;
1907
1908 cr = cairo_create(window->cairo_surface);
1909
1910 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1911 cairo_set_line_width(cr, 1);
1912
1913 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1914 cairo_rectangle (cr, x, y, 25, 16);
1915
1916 cairo_stroke_preserve(cr);
1917
1918 switch (frame_button->state) {
1919 case FRAME_BUTTON_DEFAULT:
1920 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1921 break;
1922 case FRAME_BUTTON_OVER:
1923 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1924 break;
1925 case FRAME_BUTTON_ACTIVE:
1926 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1927 break;
1928 }
1929
1930 cairo_fill (cr);
1931
1932 x += 4;
1933 }
1934
1935 cairo_set_source_surface(cr, frame_button->icon, x, y);
1936 cairo_paint(cr);
1937
1938 cairo_destroy(cr);
1939}
1940
1941static struct widget *
1942frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1943 enum frame_button_align align, enum frame_button_decoration style)
1944{
1945 struct frame_button *frame_button;
1946 const char *icon = data;
1947
1948 frame_button = malloc (sizeof *frame_button);
1949 memset(frame_button, 0, sizeof *frame_button);
1950
1951 frame_button->icon = cairo_image_surface_create_from_png(icon);
1952 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1953 frame_button->frame = frame;
1954 frame_button->type = type;
1955 frame_button->align = align;
1956 frame_button->decoration = style;
1957
1958 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1959
1960 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1961 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1962 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1963 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001964 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001965 return frame_button->widget;
1966}
1967
1968static void
1969frame_button_destroy(struct frame_button *frame_button)
1970{
1971 widget_destroy(frame_button->widget);
1972 wl_list_remove(&frame_button->link);
1973 cairo_surface_destroy(frame_button->icon);
1974 free(frame_button);
1975
1976 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001977}
1978
1979static void
1980frame_redraw_handler(struct widget *widget, void *data)
1981{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001982 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001983 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001984 struct theme *t = window->display->theme;
1985 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001986
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001987 if (window->type == TYPE_FULLSCREEN)
1988 return;
1989
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001990 cr = cairo_create(window->cairo_surface);
1991
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001992 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001993 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001994 if (window->type == TYPE_MAXIMIZED)
1995 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001996 theme_render_frame(t, cr, widget->allocation.width,
1997 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001998
1999 cairo_destroy(cr);
2000}
2001
2002static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002003frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002004{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002005 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002006 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002007 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002008
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002009 location = theme_get_location(t, input->sx, input->sy,
2010 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002011 frame->widget->allocation.height,
2012 window->type == TYPE_MAXIMIZED ?
2013 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002014
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002015 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002016 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002017 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002018 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002019 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002020 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002021 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002022 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002023 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002024 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002025 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002026 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002027 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002028 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002029 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002030 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002031 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002032 case THEME_LOCATION_EXTERIOR:
2033 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002034 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002035 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002036 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002037}
2038
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002039static void
2040frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002041{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002042 struct display *display;
2043
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002044 switch (index) {
2045 case 0: /* close */
2046 if (window->close_handler)
2047 window->close_handler(window->parent,
2048 window->user_data);
2049 else
2050 display_exit(window->display);
2051 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002052 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002053 display = window->display;
2054 if (display->workspace > 0)
2055 workspace_manager_move_surface(display->workspace_manager,
2056 window->surface,
2057 display->workspace - 1);
2058 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002059 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002060 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002061 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002062 workspace_manager_move_surface(display->workspace_manager,
2063 window->surface,
2064 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002065 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002066 case 3: /* fullscreen */
2067 /* we don't have a way to get out of fullscreen for now */
2068 if (window->fullscreen_handler)
2069 window->fullscreen_handler(window, window->user_data);
2070 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002071 }
2072}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002073
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002074void
2075window_show_frame_menu(struct window *window,
2076 struct input *input, uint32_t time)
2077{
2078 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002079 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002080
2081 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002082 "Close",
2083 "Move to workspace above", "Move to workspace below",
2084 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002085 };
2086
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002087 if (window->fullscreen_handler)
2088 count = ARRAY_LENGTH(entries);
2089 else
2090 count = ARRAY_LENGTH(entries) - 1;
2091
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002092 input_get_position(input, &x, &y);
2093 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002094 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002095}
2096
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002097static int
2098frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002099 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002100{
2101 return frame_get_pointer_image_for_location(data, input);
2102}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002103
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002104static int
2105frame_motion_handler(struct widget *widget,
2106 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002107 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002108{
2109 return frame_get_pointer_image_for_location(data, input);
2110}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002111
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002112static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002113frame_button_handler(struct widget *widget,
2114 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002115 uint32_t button, enum wl_pointer_button_state state,
2116 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002117
2118{
2119 struct frame *frame = data;
2120 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002121 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002122 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002123
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002124 location = theme_get_location(display->theme, input->sx, input->sy,
2125 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002126 frame->widget->allocation.height,
2127 window->type == TYPE_MAXIMIZED ?
2128 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002129
Daniel Stone4dbadb12012-05-30 16:31:51 +01002130 if (window->display->shell && button == BTN_LEFT &&
2131 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002132 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002133 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002134 if (!window->shell_surface)
2135 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002136 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002137 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002138 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002139 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002140 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002141 case THEME_LOCATION_RESIZING_TOP:
2142 case THEME_LOCATION_RESIZING_BOTTOM:
2143 case THEME_LOCATION_RESIZING_LEFT:
2144 case THEME_LOCATION_RESIZING_RIGHT:
2145 case THEME_LOCATION_RESIZING_TOP_LEFT:
2146 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2147 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2148 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002149 if (!window->shell_surface)
2150 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002151 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002152
Pekka Paalanen99436862012-11-19 17:15:59 +02002153 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002154 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002155 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002156 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002157 break;
2158 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002159 } else if (button == BTN_RIGHT &&
2160 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002161 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002162 }
2163}
2164
2165struct widget *
2166frame_create(struct window *window, void *data)
2167{
2168 struct frame *frame;
2169
2170 frame = malloc(sizeof *frame);
2171 memset(frame, 0, sizeof *frame);
2172
2173 frame->widget = window_add_widget(window, frame);
2174 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002175
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002176 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2177 widget_set_resize_handler(frame->widget, frame_resize_handler);
2178 widget_set_enter_handler(frame->widget, frame_enter_handler);
2179 widget_set_motion_handler(frame->widget, frame_motion_handler);
2180 widget_set_button_handler(frame->widget, frame_button_handler);
2181
Martin Minarik1998b152012-05-10 02:04:35 +02002182 /* Create empty list for frame buttons */
2183 wl_list_init(&frame->buttons_list);
2184
2185 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2186 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2187
2188 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2189 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2190
2191 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2192 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2193
2194 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2195 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2196
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002197 window->frame = frame;
2198
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002199 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002200}
2201
Kristian Høgsberga1627922012-06-20 17:30:03 -04002202void
2203frame_set_child_size(struct widget *widget, int child_width, int child_height)
2204{
2205 struct display *display = widget->window->display;
2206 struct theme *t = display->theme;
2207 int decoration_width, decoration_height;
2208 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002209 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002210
2211 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002212 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002213 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002214 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002215
2216 width = child_width + decoration_width;
2217 height = child_height + decoration_height;
2218 } else {
2219 width = child_width;
2220 height = child_height;
2221 }
2222
2223 window_schedule_resize(widget->window, width, height);
2224}
2225
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002226static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002227frame_destroy(struct frame *frame)
2228{
Martin Minarik1998b152012-05-10 02:04:35 +02002229 struct frame_button *button, *tmp;
2230
2231 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2232 frame_button_destroy(button);
2233
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002234 /* frame->child must be destroyed by the application */
2235 widget_destroy(frame->widget);
2236 free(frame);
2237}
2238
2239static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002240input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002241 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002242{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002243 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002244 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002245
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002246 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002247 return;
2248
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002249 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002250 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002251 widget = old;
2252 if (input->grab)
2253 widget = input->grab;
2254 if (widget->leave_handler)
2255 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002256 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002257 }
2258
2259 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002260 widget = focus;
2261 if (input->grab)
2262 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002263 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002264 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002265 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002266 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002267
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002268 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002269 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002270}
2271
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002272void
2273input_grab(struct input *input, struct widget *widget, uint32_t button)
2274{
2275 input->grab = widget;
2276 input->grab_button = button;
2277}
2278
2279void
2280input_ungrab(struct input *input)
2281{
2282 struct widget *widget;
2283
2284 input->grab = NULL;
2285 if (input->pointer_focus) {
2286 widget = widget_find_widget(input->pointer_focus->widget,
2287 input->sx, input->sy);
2288 input_set_focus_widget(input, widget, input->sx, input->sy);
2289 }
2290}
2291
2292static void
2293input_remove_pointer_focus(struct input *input)
2294{
2295 struct window *window = input->pointer_focus;
2296
2297 if (!window)
2298 return;
2299
2300 input_set_focus_widget(input, NULL, 0, 0);
2301
2302 input->pointer_focus = NULL;
2303 input->current_cursor = CURSOR_UNSET;
2304}
2305
2306static void
2307pointer_handle_enter(void *data, struct wl_pointer *pointer,
2308 uint32_t serial, struct wl_surface *surface,
2309 wl_fixed_t sx_w, wl_fixed_t sy_w)
2310{
2311 struct input *input = data;
2312 struct window *window;
2313 struct widget *widget;
2314 float sx = wl_fixed_to_double(sx_w);
2315 float sy = wl_fixed_to_double(sy_w);
2316
2317 if (!surface) {
2318 /* enter event for a window we've just destroyed */
2319 return;
2320 }
2321
2322 input->display->serial = serial;
2323 input->pointer_enter_serial = serial;
2324 input->pointer_focus = wl_surface_get_user_data(surface);
2325 window = input->pointer_focus;
2326
Pekka Paalanen99436862012-11-19 17:15:59 +02002327 if (window->resizing) {
2328 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002329 /* Schedule a redraw to free the pool */
2330 window_schedule_redraw(window);
2331 }
2332
2333 input->sx = sx;
2334 input->sy = sy;
2335
2336 widget = widget_find_widget(window->widget, sx, sy);
2337 input_set_focus_widget(input, widget, sx, sy);
2338}
2339
2340static void
2341pointer_handle_leave(void *data, struct wl_pointer *pointer,
2342 uint32_t serial, struct wl_surface *surface)
2343{
2344 struct input *input = data;
2345
2346 input->display->serial = serial;
2347 input_remove_pointer_focus(input);
2348}
2349
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002350static void
Daniel Stone37816df2012-05-16 18:45:18 +01002351pointer_handle_motion(void *data, struct wl_pointer *pointer,
2352 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002353{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002354 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002355 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002356 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002357 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002358 float sx = wl_fixed_to_double(sx_w);
2359 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002360
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002361 input->sx = sx;
2362 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002363
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002364 if (!window)
2365 return;
2366
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002367 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002368 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002369 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002370 }
2371
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002372 if (input->grab)
2373 widget = input->grab;
2374 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002375 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002376 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002377 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002378 input, time, sx, sy,
2379 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002380
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002381 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002382}
2383
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002384static void
Daniel Stone37816df2012-05-16 18:45:18 +01002385pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002386 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002387{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002388 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002389 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002390 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002391
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002392 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002393 if (input->focus_widget && input->grab == NULL &&
2394 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002395 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002396
Neil Roberts6b28aad2012-01-23 19:11:18 +00002397 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002398 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002399 (*widget->button_handler)(widget,
2400 input, time,
2401 button, state,
2402 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002403
Daniel Stone4dbadb12012-05-30 16:31:51 +01002404 if (input->grab && input->grab_button == button &&
2405 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002406 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002407}
2408
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002409static void
Daniel Stone37816df2012-05-16 18:45:18 +01002410pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002411 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002412{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002413 struct input *input = data;
2414 struct widget *widget;
2415
2416 widget = input->focus_widget;
2417 if (input->grab)
2418 widget = input->grab;
2419 if (widget && widget->axis_handler)
2420 (*widget->axis_handler)(widget,
2421 input, time,
2422 axis, value,
2423 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002424}
2425
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002426static const struct wl_pointer_listener pointer_listener = {
2427 pointer_handle_enter,
2428 pointer_handle_leave,
2429 pointer_handle_motion,
2430 pointer_handle_button,
2431 pointer_handle_axis,
2432};
2433
2434static void
2435input_remove_keyboard_focus(struct input *input)
2436{
2437 struct window *window = input->keyboard_focus;
2438 struct itimerspec its;
2439
2440 its.it_interval.tv_sec = 0;
2441 its.it_interval.tv_nsec = 0;
2442 its.it_value.tv_sec = 0;
2443 its.it_value.tv_nsec = 0;
2444 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2445
2446 if (!window)
2447 return;
2448
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002449 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002450 if (window->keyboard_focus_handler)
2451 (*window->keyboard_focus_handler)(window, NULL,
2452 window->user_data);
2453
2454 input->keyboard_focus = NULL;
2455}
2456
2457static void
2458keyboard_repeat_func(struct task *task, uint32_t events)
2459{
2460 struct input *input =
2461 container_of(task, struct input, repeat_task);
2462 struct window *window = input->keyboard_focus;
2463 uint64_t exp;
2464
2465 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2466 /* If we change the timer between the fd becoming
2467 * readable and getting here, there'll be nothing to
2468 * read and we get EAGAIN. */
2469 return;
2470
2471 if (window && window->key_handler) {
2472 (*window->key_handler)(window, input, input->repeat_time,
2473 input->repeat_key, input->repeat_sym,
2474 WL_KEYBOARD_KEY_STATE_PRESSED,
2475 window->user_data);
2476 }
2477}
2478
2479static void
2480keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2481 uint32_t format, int fd, uint32_t size)
2482{
2483 struct input *input = data;
2484 char *map_str;
2485
2486 if (!data) {
2487 close(fd);
2488 return;
2489 }
2490
2491 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2492 close(fd);
2493 return;
2494 }
2495
2496 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2497 if (map_str == MAP_FAILED) {
2498 close(fd);
2499 return;
2500 }
2501
2502 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2503 map_str,
2504 XKB_KEYMAP_FORMAT_TEXT_V1,
2505 0);
2506 munmap(map_str, size);
2507 close(fd);
2508
2509 if (!input->xkb.keymap) {
2510 fprintf(stderr, "failed to compile keymap\n");
2511 return;
2512 }
2513
2514 input->xkb.state = xkb_state_new(input->xkb.keymap);
2515 if (!input->xkb.state) {
2516 fprintf(stderr, "failed to create XKB state\n");
2517 xkb_map_unref(input->xkb.keymap);
2518 input->xkb.keymap = NULL;
2519 return;
2520 }
2521
2522 input->xkb.control_mask =
2523 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2524 input->xkb.alt_mask =
2525 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2526 input->xkb.shift_mask =
2527 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2528}
2529
2530static void
2531keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2532 uint32_t serial, struct wl_surface *surface,
2533 struct wl_array *keys)
2534{
2535 struct input *input = data;
2536 struct window *window;
2537
2538 input->display->serial = serial;
2539 input->keyboard_focus = wl_surface_get_user_data(surface);
2540
2541 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002542 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002543 if (window->keyboard_focus_handler)
2544 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002545 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002546}
2547
2548static void
2549keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2550 uint32_t serial, struct wl_surface *surface)
2551{
2552 struct input *input = data;
2553
2554 input->display->serial = serial;
2555 input_remove_keyboard_focus(input);
2556}
2557
Scott Moreau210d0792012-03-22 10:47:01 -06002558static void
Daniel Stone37816df2012-05-16 18:45:18 +01002559keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002560 uint32_t serial, uint32_t time, uint32_t key,
2561 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002562{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002563 struct input *input = data;
2564 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002565 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002566 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002567 const xkb_keysym_t *syms;
2568 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002569 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002570
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002571 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002572 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002573 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002574 return;
2575
Daniel Stone97f68542012-05-30 16:32:01 +01002576 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002577
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002578 sym = XKB_KEY_NoSymbol;
2579 if (num_syms == 1)
2580 sym = syms[0];
2581
2582 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002583 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002584 window_set_maximized(window,
2585 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002586 } else if (sym == XKB_KEY_F11 &&
2587 window->fullscreen_handler &&
2588 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2589 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002590 } else if (sym == XKB_KEY_F4 &&
2591 input->modifiers == MOD_ALT_MASK &&
2592 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2593 if (window->close_handler)
2594 window->close_handler(window->parent,
2595 window->user_data);
2596 else
2597 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002598 } else if (window->key_handler) {
2599 (*window->key_handler)(window, input, time, key,
2600 sym, state, window->user_data);
2601 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002602
2603 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2604 key == input->repeat_key) {
2605 its.it_interval.tv_sec = 0;
2606 its.it_interval.tv_nsec = 0;
2607 its.it_value.tv_sec = 0;
2608 its.it_value.tv_nsec = 0;
2609 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2610 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2611 input->repeat_sym = sym;
2612 input->repeat_key = key;
2613 input->repeat_time = time;
2614 its.it_interval.tv_sec = 0;
2615 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2616 its.it_value.tv_sec = 0;
2617 its.it_value.tv_nsec = 400 * 1000 * 1000;
2618 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2619 }
2620}
2621
2622static void
Daniel Stone351eb612012-05-31 15:27:47 -04002623keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2624 uint32_t serial, uint32_t mods_depressed,
2625 uint32_t mods_latched, uint32_t mods_locked,
2626 uint32_t group)
2627{
2628 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002629 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002630
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002631 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2632 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002633 mask = xkb_state_serialize_mods(input->xkb.state,
2634 XKB_STATE_DEPRESSED |
2635 XKB_STATE_LATCHED);
2636 input->modifiers = 0;
2637 if (mask & input->xkb.control_mask)
2638 input->modifiers |= MOD_CONTROL_MASK;
2639 if (mask & input->xkb.alt_mask)
2640 input->modifiers |= MOD_ALT_MASK;
2641 if (mask & input->xkb.shift_mask)
2642 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002643}
2644
Daniel Stone37816df2012-05-16 18:45:18 +01002645static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002646 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002647 keyboard_handle_enter,
2648 keyboard_handle_leave,
2649 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002650 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002651};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002652
2653static void
Daniel Stone37816df2012-05-16 18:45:18 +01002654seat_handle_capabilities(void *data, struct wl_seat *seat,
2655 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002656{
Daniel Stone37816df2012-05-16 18:45:18 +01002657 struct input *input = data;
2658
2659 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2660 input->pointer = wl_seat_get_pointer(seat);
2661 wl_pointer_set_user_data(input->pointer, input);
2662 wl_pointer_add_listener(input->pointer, &pointer_listener,
2663 input);
2664 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2665 wl_pointer_destroy(input->pointer);
2666 input->pointer = NULL;
2667 }
2668
2669 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2670 input->keyboard = wl_seat_get_keyboard(seat);
2671 wl_keyboard_set_user_data(input->keyboard, input);
2672 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2673 input);
2674 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2675 wl_keyboard_destroy(input->keyboard);
2676 input->keyboard = NULL;
2677 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002678}
2679
Daniel Stone37816df2012-05-16 18:45:18 +01002680static const struct wl_seat_listener seat_listener = {
2681 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002682};
2683
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002684void
2685input_get_position(struct input *input, int32_t *x, int32_t *y)
2686{
2687 *x = input->sx;
2688 *y = input->sy;
2689}
2690
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002691struct display *
2692input_get_display(struct input *input)
2693{
2694 return input->display;
2695}
2696
Daniel Stone37816df2012-05-16 18:45:18 +01002697struct wl_seat *
2698input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002699{
Daniel Stone37816df2012-05-16 18:45:18 +01002700 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002701}
2702
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002703uint32_t
2704input_get_modifiers(struct input *input)
2705{
2706 return input->modifiers;
2707}
2708
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002709struct widget *
2710input_get_focus_widget(struct input *input)
2711{
2712 return input->focus_widget;
2713}
2714
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002715struct data_offer {
2716 struct wl_data_offer *offer;
2717 struct input *input;
2718 struct wl_array types;
2719 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002720
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002721 struct task io_task;
2722 int fd;
2723 data_func_t func;
2724 int32_t x, y;
2725 void *user_data;
2726};
2727
2728static void
2729data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2730{
2731 struct data_offer *offer = data;
2732 char **p;
2733
2734 p = wl_array_add(&offer->types, sizeof *p);
2735 *p = strdup(type);
2736}
2737
2738static const struct wl_data_offer_listener data_offer_listener = {
2739 data_offer_offer,
2740};
2741
2742static void
2743data_offer_destroy(struct data_offer *offer)
2744{
2745 char **p;
2746
2747 offer->refcount--;
2748 if (offer->refcount == 0) {
2749 wl_data_offer_destroy(offer->offer);
2750 for (p = offer->types.data; *p; p++)
2751 free(*p);
2752 wl_array_release(&offer->types);
2753 free(offer);
2754 }
2755}
2756
2757static void
2758data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002759 struct wl_data_device *data_device,
2760 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002761{
2762 struct data_offer *offer;
2763
2764 offer = malloc(sizeof *offer);
2765
2766 wl_array_init(&offer->types);
2767 offer->refcount = 1;
2768 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002769 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002770 wl_data_offer_add_listener(offer->offer,
2771 &data_offer_listener, offer);
2772}
2773
2774static void
2775data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002776 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002777 wl_fixed_t x_w, wl_fixed_t y_w,
2778 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002779{
2780 struct input *input = data;
2781 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002782 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002783 float x = wl_fixed_to_double(x_w);
2784 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002785 char **p;
2786
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002787 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002788 window = wl_surface_get_user_data(surface);
2789 input->pointer_focus = window;
2790
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002791 if (offer) {
2792 input->drag_offer = wl_data_offer_get_user_data(offer);
2793
2794 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2795 *p = NULL;
2796
2797 types_data = input->drag_offer->types.data;
2798 } else {
2799 input->drag_offer = NULL;
2800 types_data = NULL;
2801 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002802
2803 window = input->pointer_focus;
2804 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002805 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002806 window->user_data);
2807}
2808
2809static void
2810data_device_leave(void *data, struct wl_data_device *data_device)
2811{
2812 struct input *input = data;
2813
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002814 if (input->drag_offer) {
2815 data_offer_destroy(input->drag_offer);
2816 input->drag_offer = NULL;
2817 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002818}
2819
2820static void
2821data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002822 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002823{
2824 struct input *input = data;
2825 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002826 float x = wl_fixed_to_double(x_w);
2827 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002828 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002829
2830 input->sx = x;
2831 input->sy = y;
2832
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002833 if (input->drag_offer)
2834 types_data = input->drag_offer->types.data;
2835 else
2836 types_data = NULL;
2837
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002838 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002839 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002840 window->user_data);
2841}
2842
2843static void
2844data_device_drop(void *data, struct wl_data_device *data_device)
2845{
2846 struct input *input = data;
2847 struct window *window = input->pointer_focus;
2848
2849 if (window->drop_handler)
2850 window->drop_handler(window, input,
2851 input->sx, input->sy, window->user_data);
2852}
2853
2854static void
2855data_device_selection(void *data,
2856 struct wl_data_device *wl_data_device,
2857 struct wl_data_offer *offer)
2858{
2859 struct input *input = data;
2860 char **p;
2861
2862 if (input->selection_offer)
2863 data_offer_destroy(input->selection_offer);
2864
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002865 if (offer) {
2866 input->selection_offer = wl_data_offer_get_user_data(offer);
2867 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2868 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002869 } else {
2870 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002871 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002872}
2873
2874static const struct wl_data_device_listener data_device_listener = {
2875 data_device_data_offer,
2876 data_device_enter,
2877 data_device_leave,
2878 data_device_motion,
2879 data_device_drop,
2880 data_device_selection
2881};
2882
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002883static void
2884input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002885{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002886 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002887 struct wl_cursor *cursor;
2888 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002889
Daniel Stone80972742012-11-07 17:51:39 +11002890 if (!input->pointer)
2891 return;
2892
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002893 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002894 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002895 return;
2896
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002897 if (index >= (int) cursor->image_count) {
2898 fprintf(stderr, "cursor index out of range\n");
2899 return;
2900 }
2901
2902 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002903 buffer = wl_cursor_image_get_buffer(image);
2904 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002905 return;
2906
Kristian Høgsbergae277372012-08-01 09:41:08 -04002907 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002908 input->pointer_surface,
2909 image->hotspot_x, image->hotspot_y);
2910 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2911 wl_surface_damage(input->pointer_surface, 0, 0,
2912 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002913 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002914}
2915
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002916static const struct wl_callback_listener pointer_surface_listener;
2917
2918static void
2919pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2920 uint32_t time)
2921{
2922 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002923 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002924 int i;
2925
2926 if (callback) {
2927 assert(callback == input->cursor_frame_cb);
2928 wl_callback_destroy(callback);
2929 input->cursor_frame_cb = NULL;
2930 }
2931
Daniel Stone80972742012-11-07 17:51:39 +11002932 if (!input->pointer)
2933 return;
2934
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002935 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002936 wl_pointer_set_cursor(input->pointer,
2937 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002938 NULL, 0, 0);
2939 return;
2940 }
2941
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002942 if (input->current_cursor == CURSOR_UNSET)
2943 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002944 cursor = input->display->cursors[input->current_cursor];
2945 if (!cursor)
2946 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002947
2948 /* FIXME We don't have the current time on the first call so we set
2949 * the animation start to the time of the first frame callback. */
2950 if (time == 0)
2951 input->cursor_anim_start = 0;
2952 else if (input->cursor_anim_start == 0)
2953 input->cursor_anim_start = time;
2954
2955 if (time == 0 || input->cursor_anim_start == 0)
2956 i = 0;
2957 else
2958 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2959
Pekka Paalanenbc106382012-10-10 12:49:31 +03002960 if (cursor->image_count > 1) {
2961 input->cursor_frame_cb =
2962 wl_surface_frame(input->pointer_surface);
2963 wl_callback_add_listener(input->cursor_frame_cb,
2964 &pointer_surface_listener, input);
2965 }
2966
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002967 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002968}
2969
2970static const struct wl_callback_listener pointer_surface_listener = {
2971 pointer_surface_frame_callback
2972};
2973
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002974void
2975input_set_pointer_image(struct input *input, int pointer)
2976{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002977 int force = 0;
2978
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002979 if (!input->pointer)
2980 return;
2981
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002982 if (input->pointer_enter_serial > input->cursor_serial)
2983 force = 1;
2984
2985 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002986 return;
2987
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002988 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002989 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002990 if (!input->cursor_frame_cb)
2991 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002992 else if (force) {
2993 /* The current frame callback may be stuck if, for instance,
2994 * the set cursor request was processed by the server after
2995 * this client lost the focus. In this case the cursor surface
2996 * might not be mapped and the frame callback wouldn't ever
2997 * complete. Send a set_cursor and attach to try to map the
2998 * cursor surface again so that the callback will finish */
2999 input_set_pointer_image_index(input, 0);
3000 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003001}
3002
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003003struct wl_data_device *
3004input_get_data_device(struct input *input)
3005{
3006 return input->data_device;
3007}
3008
3009void
3010input_set_selection(struct input *input,
3011 struct wl_data_source *source, uint32_t time)
3012{
3013 wl_data_device_set_selection(input->data_device, source, time);
3014}
3015
3016void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003017input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003018{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003019 wl_data_offer_accept(input->drag_offer->offer,
3020 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003021}
3022
3023static void
3024offer_io_func(struct task *task, uint32_t events)
3025{
3026 struct data_offer *offer =
3027 container_of(task, struct data_offer, io_task);
3028 unsigned int len;
3029 char buffer[4096];
3030
3031 len = read(offer->fd, buffer, sizeof buffer);
3032 offer->func(buffer, len,
3033 offer->x, offer->y, offer->user_data);
3034
3035 if (len == 0) {
3036 close(offer->fd);
3037 data_offer_destroy(offer);
3038 }
3039}
3040
3041static void
3042data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3043 data_func_t func, void *user_data)
3044{
3045 int p[2];
3046
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003047 if (pipe2(p, O_CLOEXEC) == -1)
3048 return;
3049
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003050 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3051 close(p[1]);
3052
3053 offer->io_task.run = offer_io_func;
3054 offer->fd = p[0];
3055 offer->func = func;
3056 offer->refcount++;
3057 offer->user_data = user_data;
3058
3059 display_watch_fd(offer->input->display,
3060 offer->fd, EPOLLIN, &offer->io_task);
3061}
3062
3063void
3064input_receive_drag_data(struct input *input, const char *mime_type,
3065 data_func_t func, void *data)
3066{
3067 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3068 input->drag_offer->x = input->sx;
3069 input->drag_offer->y = input->sy;
3070}
3071
3072int
3073input_receive_selection_data(struct input *input, const char *mime_type,
3074 data_func_t func, void *data)
3075{
3076 char **p;
3077
3078 if (input->selection_offer == NULL)
3079 return -1;
3080
3081 for (p = input->selection_offer->types.data; *p; p++)
3082 if (strcmp(mime_type, *p) == 0)
3083 break;
3084
3085 if (*p == NULL)
3086 return -1;
3087
3088 data_offer_receive_data(input->selection_offer,
3089 mime_type, func, data);
3090 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003091}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003092
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003093int
3094input_receive_selection_data_to_fd(struct input *input,
3095 const char *mime_type, int fd)
3096{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003097 if (input->selection_offer)
3098 wl_data_offer_receive(input->selection_offer->offer,
3099 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003100
3101 return 0;
3102}
3103
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003104void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003105window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003106{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003107 if (!window->shell_surface)
3108 return;
3109
Daniel Stone37816df2012-05-16 18:45:18 +01003110 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003111}
3112
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003113static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003114idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003115{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003116 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003117 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003118
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003119 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003120 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003121 widget_set_allocation(widget,
3122 window->pending_allocation.x,
3123 window->pending_allocation.y,
3124 window->pending_allocation.width,
3125 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003126
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003127 if (window->input_region) {
3128 wl_region_destroy(window->input_region);
3129 window->input_region = NULL;
3130 }
3131
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003132 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003133 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003134
3135 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003136
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003137 if (widget->resize_handler)
3138 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003139 widget->allocation.width,
3140 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003141 widget->user_data);
3142
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003143 if (window->allocation.width != widget->allocation.width ||
3144 window->allocation.height != widget->allocation.height) {
3145 window->allocation = widget->allocation;
3146 window_schedule_redraw(window);
3147 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003148
3149 if (widget->opaque)
3150 wl_region_add(window->opaque_region, 0, 0,
3151 widget->allocation.width,
3152 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003153}
3154
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003155void
3156window_schedule_resize(struct window *window, int width, int height)
3157{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003158 window->pending_allocation.x = 0;
3159 window->pending_allocation.y = 0;
3160 window->pending_allocation.width = width;
3161 window->pending_allocation.height = height;
3162
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003163 if (window->min_allocation.width == 0)
3164 window->min_allocation = window->pending_allocation;
3165 if (window->pending_allocation.width < window->min_allocation.width)
3166 window->pending_allocation.width = window->min_allocation.width;
3167 if (window->pending_allocation.height < window->min_allocation.height)
3168 window->pending_allocation.height = window->min_allocation.height;
3169
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003170 window->resize_needed = 1;
3171 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003172}
3173
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003174void
3175widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3176{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003177 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003178}
3179
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003180static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003181handle_ping(void *data, struct wl_shell_surface *shell_surface,
3182 uint32_t serial)
3183{
3184 wl_shell_surface_pong(shell_surface, serial);
3185}
3186
3187static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003188handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003189 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003190{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003191 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003192
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003193 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003194 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003195}
3196
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003197static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003198menu_destroy(struct menu *menu)
3199{
3200 widget_destroy(menu->widget);
3201 window_destroy(menu->window);
3202 free(menu);
3203}
3204
3205static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003206handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3207{
3208 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003209 struct menu *menu = window->widget->user_data;
3210
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003211 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003212 * device. Or just use wl_callback. And this really needs to
3213 * be a window vfunc that the menu can set. And we need the
3214 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003215
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003216 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003217 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003218 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003219}
3220
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003221static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003222 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003223 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003224 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003225};
3226
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003227void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003228window_get_allocation(struct window *window,
3229 struct rectangle *allocation)
3230{
3231 *allocation = window->allocation;
3232}
3233
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003234static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003235widget_redraw(struct widget *widget)
3236{
3237 struct widget *child;
3238
3239 if (widget->redraw_handler)
3240 widget->redraw_handler(widget, widget->user_data);
3241 wl_list_for_each(child, &widget->child_list, link)
3242 widget_redraw(child);
3243}
3244
3245static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003246frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3247{
3248 struct window *window = data;
3249
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003250 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003251 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003252 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003253 window->redraw_scheduled = 0;
3254 if (window->redraw_needed)
3255 window_schedule_redraw(window);
3256}
3257
3258static const struct wl_callback_listener listener = {
3259 frame_callback
3260};
3261
3262static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003263idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003264{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003265 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003266
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003267 if (window->resize_needed)
3268 idle_resize(window);
3269
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003270 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003271 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003272 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003273 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003274
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003275 window->frame_cb = wl_surface_frame(window->surface);
3276 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003277 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003278}
3279
3280void
3281window_schedule_redraw(struct window *window)
3282{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003283 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003284 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003285 window->redraw_task.run = idle_redraw;
3286 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003287 window->redraw_scheduled = 1;
3288 }
3289}
3290
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003291int
3292window_is_fullscreen(struct window *window)
3293{
3294 return window->type == TYPE_FULLSCREEN;
3295}
3296
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003297void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003298window_set_fullscreen(struct window *window, int fullscreen)
3299{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003300 if (!window->display->shell)
3301 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003302
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003303 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003304 return;
3305
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003306 if (fullscreen) {
3307 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003308 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003309 wl_shell_surface_set_fullscreen(window->shell_surface,
3310 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3311 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003312 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003313 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003314 wl_shell_surface_set_toplevel(window->shell_surface);
3315 window_schedule_resize(window,
3316 window->saved_allocation.width,
3317 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003318 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003319}
3320
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003321int
3322window_is_maximized(struct window *window)
3323{
3324 return window->type == TYPE_MAXIMIZED;
3325}
3326
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003327void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003328window_set_maximized(struct window *window, int maximized)
3329{
3330 if (!window->display->shell)
3331 return;
3332
3333 if ((window->type == TYPE_MAXIMIZED) == maximized)
3334 return;
3335
3336 if (window->type == TYPE_TOPLEVEL) {
3337 window->saved_allocation = window->allocation;
3338 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3339 window->type = TYPE_MAXIMIZED;
3340 } else {
3341 wl_shell_surface_set_toplevel(window->shell_surface);
3342 window->type = TYPE_TOPLEVEL;
3343 window_schedule_resize(window,
3344 window->saved_allocation.width,
3345 window->saved_allocation.height);
3346 }
3347}
3348
3349void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003350window_set_user_data(struct window *window, void *data)
3351{
3352 window->user_data = data;
3353}
3354
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003355void *
3356window_get_user_data(struct window *window)
3357{
3358 return window->user_data;
3359}
3360
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003361void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003362window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003363 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003364{
3365 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003366}
3367
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003368void
3369window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003370 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003371{
3372 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003373}
3374
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003375void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003376window_set_data_handler(struct window *window, window_data_handler_t handler)
3377{
3378 window->data_handler = handler;
3379}
3380
3381void
3382window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3383{
3384 window->drop_handler = handler;
3385}
3386
3387void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003388window_set_close_handler(struct window *window,
3389 window_close_handler_t handler)
3390{
3391 window->close_handler = handler;
3392}
3393
3394void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003395window_set_fullscreen_handler(struct window *window,
3396 window_fullscreen_handler_t handler)
3397{
3398 window->fullscreen_handler = handler;
3399}
3400
3401void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003402window_set_output_handler(struct window *window,
3403 window_output_handler_t handler)
3404{
3405 window->output_handler = handler;
3406}
3407
3408void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003409window_set_title(struct window *window, const char *title)
3410{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003411 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003412 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003413 if (window->shell_surface)
3414 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003415}
3416
3417const char *
3418window_get_title(struct window *window)
3419{
3420 return window->title;
3421}
3422
3423void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003424window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3425{
3426 struct text_cursor_position *text_cursor_position =
3427 window->display->text_cursor_position;
3428
Scott Moreau9295ce02012-06-01 12:46:10 -06003429 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003430 return;
3431
3432 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003433 window->surface,
3434 wl_fixed_from_int(x),
3435 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003436}
3437
3438void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003439window_damage(struct window *window, int32_t x, int32_t y,
3440 int32_t width, int32_t height)
3441{
3442 wl_surface_damage(window->surface, x, y, width, height);
3443}
3444
Casey Dahlin9074db52012-04-19 22:50:09 -04003445static void
3446surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003447 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003448{
Rob Bradford7507b572012-05-15 17:55:34 +01003449 struct window *window = data;
3450 struct output *output;
3451 struct output *output_found = NULL;
3452 struct window_output *window_output;
3453
3454 wl_list_for_each(output, &window->display->output_list, link) {
3455 if (output->output == wl_output) {
3456 output_found = output;
3457 break;
3458 }
3459 }
3460
3461 if (!output_found)
3462 return;
3463
3464 window_output = malloc (sizeof *window_output);
3465 window_output->output = output_found;
3466
3467 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003468
3469 if (window->output_handler)
3470 window->output_handler(window, output_found, 1,
3471 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003472}
3473
3474static void
3475surface_leave(void *data,
3476 struct wl_surface *wl_surface, struct wl_output *output)
3477{
Rob Bradford7507b572012-05-15 17:55:34 +01003478 struct window *window = data;
3479 struct window_output *window_output;
3480 struct window_output *window_output_found = NULL;
3481
3482 wl_list_for_each(window_output, &window->window_output_list, link) {
3483 if (window_output->output->output == output) {
3484 window_output_found = window_output;
3485 break;
3486 }
3487 }
3488
3489 if (window_output_found) {
3490 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003491
3492 if (window->output_handler)
3493 window->output_handler(window, window_output->output,
3494 0, window->user_data);
3495
Rob Bradford7507b572012-05-15 17:55:34 +01003496 free(window_output_found);
3497 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003498}
3499
3500static const struct wl_surface_listener surface_listener = {
3501 surface_enter,
3502 surface_leave
3503};
3504
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003505static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003506window_create_internal(struct display *display,
3507 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003508{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003509 struct window *window;
3510
3511 window = malloc(sizeof *window);
3512 if (window == NULL)
3513 return NULL;
3514
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003515 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003516 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003517 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003518 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003519 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003520 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003521 window->shell_surface =
3522 wl_shell_get_shell_surface(display->shell,
3523 window->surface);
3524 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003525 window->allocation.x = 0;
3526 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003527 window->allocation.width = 0;
3528 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003529 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003530 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003531 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003532 window->input_region = NULL;
3533 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003534
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003535 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003536#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003537 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003538#else
3539 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3540#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003541 else
3542 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003543
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003544 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003545 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003546 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003547
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003548 if (window->shell_surface) {
3549 wl_shell_surface_set_user_data(window->shell_surface, window);
3550 wl_shell_surface_add_listener(window->shell_surface,
3551 &shell_surface_listener, window);
3552 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003553
Rob Bradford7507b572012-05-15 17:55:34 +01003554 wl_list_init (&window->window_output_list);
3555
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003556 return window;
3557}
3558
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003559struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003560window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003561{
3562 struct window *window;
3563
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003564 window = window_create_internal(display, NULL, TYPE_NONE);
3565 if (!window)
3566 return NULL;
3567
3568 return window;
3569}
3570
3571struct window *
3572window_create_custom(struct display *display)
3573{
3574 struct window *window;
3575
3576 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003577 if (!window)
3578 return NULL;
3579
3580 return window;
3581}
3582
3583struct window *
3584window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003585 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003586{
3587 struct window *window;
3588
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003589 window = window_create_internal(parent->display,
3590 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003591 if (!window)
3592 return NULL;
3593
3594 window->x = x;
3595 window->y = y;
3596
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003597 if (display->shell)
3598 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003599 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003600 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003601
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003602 return window;
3603}
3604
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003605static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003606menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003607{
3608 int next;
3609
3610 next = (sy - 8) / 20;
3611 if (menu->current != next) {
3612 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003613 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003614 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003615}
3616
3617static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003618menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003619 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003620 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003621{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003622 struct menu *menu = data;
3623
3624 if (widget == menu->widget)
3625 menu_set_item(data, y);
3626
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003627 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003628}
3629
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003630static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003631menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003632 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003633{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003634 struct menu *menu = data;
3635
3636 if (widget == menu->widget)
3637 menu_set_item(data, y);
3638
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003639 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003640}
3641
3642static void
3643menu_leave_handler(struct widget *widget, struct input *input, void *data)
3644{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003645 struct menu *menu = data;
3646
3647 if (widget == menu->widget)
3648 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003649}
3650
3651static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003652menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003653 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003654 uint32_t button, enum wl_pointer_button_state state,
3655 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003656
3657{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003658 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003659
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003660 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3661 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003662 /* Either relase after press-drag-release or
3663 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003664 menu->func(menu->window->parent,
3665 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003666 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003667 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003668 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003669 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003670 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003671}
3672
3673static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003674menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003675{
3676 cairo_t *cr;
3677 const int32_t r = 3, margin = 3;
3678 struct menu *menu = data;
3679 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003680 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003681
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003682 cr = cairo_create(window->cairo_surface);
3683 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3684 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3685 cairo_paint(cr);
3686
3687 width = window->allocation.width;
3688 height = window->allocation.height;
3689 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003690
3691 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003692 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3693 cairo_fill(cr);
3694
3695 for (i = 0; i < menu->count; i++) {
3696 if (i == menu->current) {
3697 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3698 cairo_rectangle(cr, margin, i * 20 + margin,
3699 width - 2 * margin, 20);
3700 cairo_fill(cr);
3701 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3702 cairo_move_to(cr, 10, i * 20 + 16);
3703 cairo_show_text(cr, menu->entries[i]);
3704 } else {
3705 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3706 cairo_move_to(cr, 10, i * 20 + 16);
3707 cairo_show_text(cr, menu->entries[i]);
3708 }
3709 }
3710
3711 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003712}
3713
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003714void
3715window_show_menu(struct display *display,
3716 struct input *input, uint32_t time, struct window *parent,
3717 int32_t x, int32_t y,
3718 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003719{
3720 struct window *window;
3721 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003722 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003723
3724 menu = malloc(sizeof *menu);
3725 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003726 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003727
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003728 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003729 if (!window) {
3730 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003731 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003732 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003733
3734 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003735 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003736 menu->entries = entries;
3737 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003738 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003739 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003740 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003741 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003742 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003743 window->type = TYPE_MENU;
3744 window->x = x;
3745 window->y = y;
3746
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003747 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003748 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003749 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003750 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003751 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003752
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003753 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003754 widget_set_enter_handler(menu->widget, menu_enter_handler);
3755 widget_set_leave_handler(menu->widget, menu_leave_handler);
3756 widget_set_motion_handler(menu->widget, menu_motion_handler);
3757 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003758
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003759 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003760 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003761}
3762
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003763void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003764window_set_buffer_type(struct window *window, enum window_buffer_type type)
3765{
3766 window->buffer_type = type;
3767}
3768
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003769
3770static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003771display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003772 struct wl_output *wl_output,
3773 int x, int y,
3774 int physical_width,
3775 int physical_height,
3776 int subpixel,
3777 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003778 const char *model,
3779 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003780{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003781 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003782
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003783 output->allocation.x = x;
3784 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003785 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003786}
3787
3788static void
3789display_handle_mode(void *data,
3790 struct wl_output *wl_output,
3791 uint32_t flags,
3792 int width,
3793 int height,
3794 int refresh)
3795{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003796 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003797 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003798
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003799 if (flags & WL_OUTPUT_MODE_CURRENT) {
3800 output->allocation.width = width;
3801 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003802 if (display->output_configure_handler)
3803 (*display->output_configure_handler)(
3804 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003805 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003806}
3807
3808static const struct wl_output_listener output_listener = {
3809 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003810 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003811};
3812
3813static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003814display_add_output(struct display *d, uint32_t id)
3815{
3816 struct output *output;
3817
3818 output = malloc(sizeof *output);
3819 if (output == NULL)
3820 return;
3821
3822 memset(output, 0, sizeof *output);
3823 output->display = d;
3824 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003825 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003826 wl_list_insert(d->output_list.prev, &output->link);
3827
3828 wl_output_add_listener(output->output, &output_listener, output);
3829}
3830
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003831static void
3832output_destroy(struct output *output)
3833{
3834 if (output->destroy_handler)
3835 (*output->destroy_handler)(output, output->user_data);
3836
3837 wl_output_destroy(output->output);
3838 wl_list_remove(&output->link);
3839 free(output);
3840}
3841
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003842void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003843display_set_global_handler(struct display *display,
3844 display_global_handler_t handler)
3845{
3846 struct global *global;
3847
3848 display->global_handler = handler;
3849 if (!handler)
3850 return;
3851
3852 wl_list_for_each(global, &display->global_list, link)
3853 display->global_handler(display,
3854 global->name, global->interface,
3855 global->version, display->user_data);
3856}
3857
3858void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003859display_set_output_configure_handler(struct display *display,
3860 display_output_handler_t handler)
3861{
3862 struct output *output;
3863
3864 display->output_configure_handler = handler;
3865 if (!handler)
3866 return;
3867
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003868 wl_list_for_each(output, &display->output_list, link) {
3869 if (output->allocation.width == 0 &&
3870 output->allocation.height == 0)
3871 continue;
3872
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003873 (*display->output_configure_handler)(output,
3874 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003875 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003876}
3877
3878void
3879output_set_user_data(struct output *output, void *data)
3880{
3881 output->user_data = data;
3882}
3883
3884void *
3885output_get_user_data(struct output *output)
3886{
3887 return output->user_data;
3888}
3889
3890void
3891output_set_destroy_handler(struct output *output,
3892 display_output_handler_t handler)
3893{
3894 output->destroy_handler = handler;
3895 /* FIXME: implement this, once we have way to remove outputs */
3896}
3897
3898void
Scott Moreau4e072362012-09-29 02:03:11 -06003899output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003900{
Scott Moreau4e072362012-09-29 02:03:11 -06003901 struct rectangle allocation = output->allocation;
3902
3903 switch (output->transform) {
3904 case WL_OUTPUT_TRANSFORM_90:
3905 case WL_OUTPUT_TRANSFORM_270:
3906 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3907 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3908 /* Swap width and height */
3909 allocation.width = output->allocation.height;
3910 allocation.height = output->allocation.width;
3911 break;
3912 }
3913
3914 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003915}
3916
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003917struct wl_output *
3918output_get_wl_output(struct output *output)
3919{
3920 return output->output;
3921}
3922
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003923enum wl_output_transform
3924output_get_transform(struct output *output)
3925{
3926 return output->transform;
3927}
3928
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003929static void
Daniel Stone97f68542012-05-30 16:32:01 +01003930fini_xkb(struct input *input)
3931{
3932 xkb_state_unref(input->xkb.state);
3933 xkb_map_unref(input->xkb.keymap);
3934}
3935
3936static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003937display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003938{
3939 struct input *input;
3940
3941 input = malloc(sizeof *input);
3942 if (input == NULL)
3943 return;
3944
3945 memset(input, 0, sizeof *input);
3946 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003947 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003948 input->pointer_focus = NULL;
3949 input->keyboard_focus = NULL;
3950 wl_list_insert(d->input_list.prev, &input->link);
3951
Daniel Stone37816df2012-05-16 18:45:18 +01003952 wl_seat_add_listener(input->seat, &seat_listener, input);
3953 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003954
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003955 input->data_device =
3956 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003957 input->seat);
3958 wl_data_device_add_listener(input->data_device, &data_device_listener,
3959 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003960
3961 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003962
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003963 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3964 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003965 input->repeat_task.run = keyboard_repeat_func;
3966 display_watch_fd(d, input->repeat_timer_fd,
3967 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003968}
3969
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003970static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003971input_destroy(struct input *input)
3972{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003973 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003974 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003975
3976 if (input->drag_offer)
3977 data_offer_destroy(input->drag_offer);
3978
3979 if (input->selection_offer)
3980 data_offer_destroy(input->selection_offer);
3981
3982 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003983 fini_xkb(input);
3984
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003985 wl_surface_destroy(input->pointer_surface);
3986
Pekka Paalanene1207c72011-12-16 12:02:09 +02003987 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003988 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003989 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003990 free(input);
3991}
3992
3993static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003994init_workspace_manager(struct display *d, uint32_t id)
3995{
3996 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003997 wl_registry_bind(d->registry, id,
3998 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003999 if (d->workspace_manager != NULL)
4000 workspace_manager_add_listener(d->workspace_manager,
4001 &workspace_manager_listener,
4002 d);
4003}
4004
4005static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004006registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4007 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004008{
4009 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004010 struct global *global;
4011
4012 global = malloc(sizeof *global);
4013 global->name = id;
4014 global->interface = strdup(interface);
4015 global->version = version;
4016 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004017
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004018 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004019 d->compositor = wl_registry_bind(registry, id,
4020 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004021 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004022 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004023 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004024 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004025 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004026 d->shell = wl_registry_bind(registry,
4027 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004028 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004029 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004030 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4031 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004032 wl_registry_bind(registry, id,
4033 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004034 } else if (strcmp(interface, "text_cursor_position") == 0) {
4035 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004036 wl_registry_bind(registry, id,
4037 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004038 } else if (strcmp(interface, "workspace_manager") == 0) {
4039 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004040 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004041
4042 if (d->global_handler)
4043 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004044}
4045
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004046void *
4047display_bind(struct display *display, uint32_t name,
4048 const struct wl_interface *interface, uint32_t version)
4049{
4050 return wl_registry_bind(display->registry, name, interface, version);
4051}
4052
4053static const struct wl_registry_listener registry_listener = {
4054 registry_handle_global
4055};
4056
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004057#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004058static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004059init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004060{
4061 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004062 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004063
Rob Clark6396ed32012-03-11 19:48:41 -05004064#ifdef USE_CAIRO_GLESV2
4065# define GL_BIT EGL_OPENGL_ES2_BIT
4066#else
4067# define GL_BIT EGL_OPENGL_BIT
4068#endif
4069
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004070 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004071 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004072 EGL_RED_SIZE, 1,
4073 EGL_GREEN_SIZE, 1,
4074 EGL_BLUE_SIZE, 1,
4075 EGL_ALPHA_SIZE, 1,
4076 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004077 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004078 EGL_NONE
4079 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004080
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004081#ifdef USE_CAIRO_GLESV2
4082 static const EGLint context_attribs[] = {
4083 EGL_CONTEXT_CLIENT_VERSION, 2,
4084 EGL_NONE
4085 };
4086 EGLint api = EGL_OPENGL_ES_API;
4087#else
4088 EGLint *context_attribs = NULL;
4089 EGLint api = EGL_OPENGL_API;
4090#endif
4091
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004092 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004093 if (!eglInitialize(d->dpy, &major, &minor)) {
4094 fprintf(stderr, "failed to initialize display\n");
4095 return -1;
4096 }
4097
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004098 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004099 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
4100 return -1;
4101 }
4102
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004103 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4104 &d->argb_config, 1, &n) || n != 1) {
4105 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004106 return -1;
4107 }
4108
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004109 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004110 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004111 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004112 fprintf(stderr, "failed to create context\n");
4113 return -1;
4114 }
4115
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004116 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01004117 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004118 return -1;
4119 }
4120
Benjamin Franzke0c991632011-09-27 21:57:31 +02004121 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4122 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4123 fprintf(stderr, "failed to get cairo egl argb device\n");
4124 return -1;
4125 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004126
4127 return 0;
4128}
4129
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004130static void
4131fini_egl(struct display *display)
4132{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004133 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004134
4135 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4136 EGL_NO_CONTEXT);
4137
4138 eglTerminate(display->dpy);
4139 eglReleaseThread();
4140}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004141#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004142
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004143static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004144init_dummy_surface(struct display *display)
4145{
4146 int len;
4147 void *data;
4148
4149 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4150 data = malloc(len);
4151 display->dummy_surface =
4152 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4153 1, 1, len);
4154 display->dummy_surface_data = data;
4155}
4156
4157static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004158handle_display_data(struct task *task, uint32_t events)
4159{
4160 struct display *display =
4161 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004162 struct epoll_event ep;
4163 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004164
4165 display->display_fd_events = events;
4166
4167 if (events & EPOLLERR || events & EPOLLHUP) {
4168 display_exit(display);
4169 return;
4170 }
4171
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004172 if (events & EPOLLIN) {
4173 ret = wl_display_dispatch(display->display);
4174 if (ret == -1) {
4175 display_exit(display);
4176 return;
4177 }
4178 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004179
4180 if (events & EPOLLOUT) {
4181 ret = wl_display_flush(display->display);
4182 if (ret == 0) {
4183 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4184 ep.data.ptr = &display->display_task;
4185 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4186 display->display_fd, &ep);
4187 } else if (ret == -1 && errno != EAGAIN) {
4188 display_exit(display);
4189 return;
4190 }
4191 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004192}
4193
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004194struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004195display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004196{
4197 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004198
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004199 d = malloc(sizeof *d);
4200 if (d == NULL)
4201 return NULL;
4202
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004203 memset(d, 0, sizeof *d);
4204
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004205 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004206 if (d->display == NULL) {
4207 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004208 return NULL;
4209 }
4210
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004211 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004212 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004213 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004214 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4215 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004216
4217 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004218 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004219 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004220 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004221
Daniel Stone97f68542012-05-30 16:32:01 +01004222 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004223 if (d->xkb_context == NULL) {
4224 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004225 return NULL;
4226 }
4227
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004228 d->workspace = 0;
4229 d->workspace_count = 1;
4230
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004231 d->registry = wl_display_get_registry(d->display);
4232 wl_registry_add_listener(d->registry, &registry_listener, d);
4233 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004234#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004235 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004236 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004237#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004238
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004239 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004240
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004241 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004242
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004243 wl_list_init(&d->window_list);
4244
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004245 init_dummy_surface(d);
4246
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004247 return d;
4248}
4249
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004250static void
4251display_destroy_outputs(struct display *display)
4252{
4253 struct output *tmp;
4254 struct output *output;
4255
4256 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4257 output_destroy(output);
4258}
4259
Pekka Paalanene1207c72011-12-16 12:02:09 +02004260static void
4261display_destroy_inputs(struct display *display)
4262{
4263 struct input *tmp;
4264 struct input *input;
4265
4266 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4267 input_destroy(input);
4268}
4269
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004270void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004271display_destroy(struct display *display)
4272{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004273 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004274 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4275 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004276
4277 if (!wl_list_empty(&display->deferred_list))
4278 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4279
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004280 cairo_surface_destroy(display->dummy_surface);
4281 free(display->dummy_surface_data);
4282
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004283 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004284 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004285
Daniel Stone97f68542012-05-30 16:32:01 +01004286 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004287
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004288 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004289 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004290
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004291#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004292 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004293#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004294
Pekka Paalanenc2052982011-12-16 11:41:32 +02004295 if (display->shell)
4296 wl_shell_destroy(display->shell);
4297
4298 if (display->shm)
4299 wl_shm_destroy(display->shm);
4300
4301 if (display->data_device_manager)
4302 wl_data_device_manager_destroy(display->data_device_manager);
4303
4304 wl_compositor_destroy(display->compositor);
4305
4306 close(display->epoll_fd);
4307
U. Artie Eoff44874d92012-10-02 21:12:35 -07004308 if (!(display->display_fd_events & EPOLLERR) &&
4309 !(display->display_fd_events & EPOLLHUP))
4310 wl_display_flush(display->display);
4311
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004312 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004313 free(display);
4314}
4315
4316void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004317display_set_user_data(struct display *display, void *data)
4318{
4319 display->user_data = data;
4320}
4321
4322void *
4323display_get_user_data(struct display *display)
4324{
4325 return display->user_data;
4326}
4327
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004328struct wl_display *
4329display_get_display(struct display *display)
4330{
4331 return display->display;
4332}
4333
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004334struct output *
4335display_get_output(struct display *display)
4336{
4337 return container_of(display->output_list.next, struct output, link);
4338}
4339
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004340struct wl_compositor *
4341display_get_compositor(struct display *display)
4342{
4343 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004344}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004345
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004346uint32_t
4347display_get_serial(struct display *display)
4348{
4349 return display->serial;
4350}
4351
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004352EGLDisplay
4353display_get_egl_display(struct display *d)
4354{
4355 return d->dpy;
4356}
4357
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004358struct wl_data_source *
4359display_create_data_source(struct display *display)
4360{
4361 return wl_data_device_manager_create_data_source(display->data_device_manager);
4362}
4363
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004364EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004365display_get_argb_egl_config(struct display *d)
4366{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004367 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004368}
4369
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004370struct wl_shell *
4371display_get_shell(struct display *display)
4372{
4373 return display->shell;
4374}
4375
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004376int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004377display_acquire_window_surface(struct display *display,
4378 struct window *window,
4379 EGLContext ctx)
4380{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004381 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004382 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004383
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004384 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004385}
4386
4387void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004388display_release_window_surface(struct display *display,
4389 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004390{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004391 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004392 return;
4393
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004394 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004395}
4396
4397void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004398display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004399{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004400 wl_list_insert(&display->deferred_list, &task->link);
4401}
4402
4403void
4404display_watch_fd(struct display *display,
4405 int fd, uint32_t events, struct task *task)
4406{
4407 struct epoll_event ep;
4408
4409 ep.events = events;
4410 ep.data.ptr = task;
4411 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4412}
4413
4414void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004415display_unwatch_fd(struct display *display, int fd)
4416{
4417 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4418}
4419
4420void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004421display_run(struct display *display)
4422{
4423 struct task *task;
4424 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004425 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004426
Pekka Paalanen826d7952011-12-15 10:14:07 +02004427 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004428 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004429 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004430 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004431 struct task, link);
4432 wl_list_remove(&task->link);
4433 task->run(task, 0);
4434 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004435
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004436 wl_display_dispatch_pending(display->display);
4437
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004438 if (!display->running)
4439 break;
4440
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004441 ret = wl_display_flush(display->display);
4442 if (ret < 0 && errno == EAGAIN) {
4443 ep[0].events =
4444 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4445 ep[0].data.ptr = &display->display_task;
4446
4447 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4448 display->display_fd, &ep[0]);
4449 } else if (ret < 0) {
4450 break;
4451 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004452
4453 count = epoll_wait(display->epoll_fd,
4454 ep, ARRAY_LENGTH(ep), -1);
4455 for (i = 0; i < count; i++) {
4456 task = ep[i].data.ptr;
4457 task->run(task, ep[i].events);
4458 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004459 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004460}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004461
4462void
4463display_exit(struct display *display)
4464{
4465 display->running = 0;
4466}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004467
4468void
4469keysym_modifiers_add(struct wl_array *modifiers_map,
4470 const char *name)
4471{
4472 size_t len = strlen(name) + 1;
4473 char *p;
4474
4475 p = wl_array_add(modifiers_map, len);
4476
4477 if (p == NULL)
4478 return;
4479
4480 strncpy(p, name, len);
4481}
4482
4483static xkb_mod_index_t
4484keysym_modifiers_get_index(struct wl_array *modifiers_map,
4485 const char *name)
4486{
4487 xkb_mod_index_t index = 0;
4488 char *p = modifiers_map->data;
4489
4490 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4491 if (strcmp(p, name) == 0)
4492 return index;
4493
4494 index++;
4495 p += strlen(p) + 1;
4496 }
4497
4498 return XKB_MOD_INVALID;
4499}
4500
4501xkb_mod_mask_t
4502keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4503 const char *name)
4504{
4505 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4506
4507 if (index == XKB_MOD_INVALID)
4508 return XKB_MOD_INVALID;
4509
4510 return 1 << index;
4511}