blob: d08542d85a9b015ac63db7ce203b1278ee2c19da [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen99436862012-11-19 17:15:59 +02003 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040024#define _GNU_SOURCE
25
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026#include "../config.h"
27
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040034#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020036#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include <time.h>
38#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030041#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040042
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040043#include <pixman.h>
44
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050046#include <wayland-egl.h>
47
Rob Clark6396ed32012-03-11 19:48:41 -050048#ifdef USE_CAIRO_GLESV2
49#include <GLES2/gl2.h>
50#include <GLES2/gl2ext.h>
51#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050053#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <EGL/egl.h>
55#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040057#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030058#else /* HAVE_CAIRO_EGL */
59typedef void *EGLDisplay;
60typedef void *EGLConfig;
61typedef void *EGLContext;
62#define EGL_NO_DISPLAY ((EGLDisplay)0)
63#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050064
Daniel Stone9d4f0302012-02-15 16:33:21 +000065#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030066#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040067
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050068#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020069#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040070#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060071#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020072#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030073#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050074
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050075#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050076
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070077struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030078
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040079struct global {
80 uint32_t name;
81 char *interface;
82 uint32_t version;
83 struct wl_list link;
84};
85
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050086struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050087 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040088 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050089 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040090 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040091 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040092 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060093 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020094 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040095 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050096 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020098 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040099 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400100
101 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700102 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400103 struct task display_task;
104
105 int epoll_fd;
106 struct wl_list deferred_list;
107
Pekka Paalanen826d7952011-12-15 10:14:07 +0200108 int running;
109
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400110 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400111 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400112 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500113 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400114
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400115 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400116
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300117 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300118 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400119
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400121 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100124
125 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200126
127 uint32_t workspace;
128 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200129
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133};
134
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400136 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400138 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500139 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500141 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142 TYPE_CUSTOM
143};
Rob Bradford7507b572012-05-15 17:55:34 +0100144
145struct window_output {
146 struct output *output;
147 struct wl_list link;
148};
149
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200150struct toysurface {
151 /*
152 * Prepare the surface for drawing. Makes sure there is a surface
153 * of the right size available for rendering, and returns it.
154 * dx,dy are the x,y of wl_surface.attach.
155 * width,height are the new surface size.
Pekka Paalanen99436862012-11-19 17:15:59 +0200156 * If resize_hint is non-zero, the user is doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157 * Returns the Cairo surface to draw to.
158 */
159 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanen99436862012-11-19 17:15:59 +0200160 int width, int height, int resize_hint);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161
162 /*
163 * Post the surface to the server, returning the server allocation
164 * rectangle. The Cairo surface from prepare() must be destroyed
165 * after calling this.
166 */
167 void (*swap)(struct toysurface *base,
168 struct rectangle *server_allocation);
169
170 /*
171 * Make the toysurface current with the given EGL context.
172 * Returns 0 on success, and negative of failure.
173 */
174 int (*acquire)(struct toysurface *base, EGLContext ctx);
175
176 /*
177 * Release the toysurface from the EGL context, returning control
178 * to Cairo.
179 */
180 void (*release)(struct toysurface *base);
181
182 /*
183 * Destroy the toysurface, including the Cairo surface, any
184 * backing storage, and the Wayland protocol objects.
185 */
186 void (*destroy)(struct toysurface *base);
187};
188
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500189struct window {
190 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500191 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100192 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500193 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200194 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500195 struct wl_region *input_region;
196 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500197 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500198 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400199 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500200 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500201 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400202 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400203 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400204 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400205 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400206 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400207 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400208 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400209 int focus_count;
210
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400211 enum window_buffer_type buffer_type;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +0200212 enum wl_output_transform buffer_transform;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200213 struct toysurface *toysurface;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400214 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500215
Pekka Paalanen99436862012-11-19 17:15:59 +0200216 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400217
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500218 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500219 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400220 window_data_handler_t data_handler;
221 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500222 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400223 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400224
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300225 struct wl_callback *frame_cb;
226
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200227 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500228 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500229
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500230 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400231 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500232};
233
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500234struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500235 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300236 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500237 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400238 struct wl_list link;
239 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500240 widget_resize_handler_t resize_handler;
241 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500242 widget_enter_handler_t enter_handler;
243 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500244 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500245 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200246 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400247 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500248 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300249 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400250};
251
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400252struct input {
253 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100254 struct wl_seat *seat;
255 struct wl_pointer *pointer;
256 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400257 struct window *pointer_focus;
258 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300259 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300260 uint32_t cursor_anim_start;
261 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300262 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400263 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400264 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400265 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400266 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400267 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400268
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500269 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500270 struct widget *grab;
271 uint32_t grab_button;
272
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400273 struct wl_data_device *data_device;
274 struct data_offer *drag_offer;
275 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100276
277 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100278 struct xkb_keymap *keymap;
279 struct xkb_state *state;
280 xkb_mod_mask_t control_mask;
281 xkb_mod_mask_t alt_mask;
282 xkb_mod_mask_t shift_mask;
283 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400284
285 struct task repeat_task;
286 int repeat_timer_fd;
287 uint32_t repeat_sym;
288 uint32_t repeat_key;
289 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400290};
291
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500292struct output {
293 struct display *display;
294 struct wl_output *output;
295 struct rectangle allocation;
296 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600297 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200298
299 display_output_handler_t destroy_handler;
300 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500301};
302
Martin Minarik1998b152012-05-10 02:04:35 +0200303enum frame_button_action {
304 FRAME_BUTTON_NULL = 0,
305 FRAME_BUTTON_ICON = 1,
306 FRAME_BUTTON_CLOSE = 2,
307 FRAME_BUTTON_MINIMIZE = 3,
308 FRAME_BUTTON_MAXIMIZE = 4,
309};
310
311enum frame_button_pointer {
312 FRAME_BUTTON_DEFAULT = 0,
313 FRAME_BUTTON_OVER = 1,
314 FRAME_BUTTON_ACTIVE = 2,
315};
316
317enum frame_button_align {
318 FRAME_BUTTON_RIGHT = 0,
319 FRAME_BUTTON_LEFT = 1,
320};
321
322enum frame_button_decoration {
323 FRAME_BUTTON_NONE = 0,
324 FRAME_BUTTON_FANCY = 1,
325};
326
327struct frame_button {
328 struct widget *widget;
329 struct frame *frame;
330 cairo_surface_t *icon;
331 enum frame_button_action type;
332 enum frame_button_pointer state;
333 struct wl_list link; /* buttons_list */
334 enum frame_button_align align;
335 enum frame_button_decoration decoration;
336};
337
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500338struct frame {
339 struct widget *widget;
340 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200341 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500342};
343
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500344struct menu {
345 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500346 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500347 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500348 const char **entries;
349 uint32_t time;
350 int current;
351 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400352 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500353 menu_func_t func;
354};
355
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300356struct tooltip {
357 struct widget *parent;
358 struct window *window;
359 struct widget *widget;
360 char *entry;
361 struct task tooltip_task;
362 int tooltip_fd;
363 float x, y;
364};
365
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700366struct shm_pool {
367 struct wl_shm_pool *pool;
368 size_t size;
369 size_t used;
370 void *data;
371};
372
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400373enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300374 CURSOR_DEFAULT = 100,
375 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400376};
377
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500378enum window_location {
379 WINDOW_INTERIOR = 0,
380 WINDOW_RESIZING_TOP = 1,
381 WINDOW_RESIZING_BOTTOM = 2,
382 WINDOW_RESIZING_LEFT = 4,
383 WINDOW_RESIZING_TOP_LEFT = 5,
384 WINDOW_RESIZING_BOTTOM_LEFT = 6,
385 WINDOW_RESIZING_RIGHT = 8,
386 WINDOW_RESIZING_TOP_RIGHT = 9,
387 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
388 WINDOW_RESIZING_MASK = 15,
389 WINDOW_EXTERIOR = 16,
390 WINDOW_TITLEBAR = 17,
391 WINDOW_CLIENT_AREA = 18,
392};
393
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200394static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500396#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200398struct egl_window_surface {
399 struct toysurface base;
400 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100401 struct display *display;
402 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200403 struct wl_egl_window *egl_window;
404 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100405};
406
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200407static struct egl_window_surface *
408to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100409{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200410 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100411}
412
413static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200414egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanen99436862012-11-19 17:15:59 +0200415 int width, int height, int resize_hint)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100416{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200417 struct egl_window_surface *surface = to_egl_window_surface(base);
418
419 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
420 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
421
422 return cairo_surface_reference(surface->cairo_surface);
423}
424
425static void
426egl_window_surface_swap(struct toysurface *base,
427 struct rectangle *server_allocation)
428{
429 struct egl_window_surface *surface = to_egl_window_surface(base);
430
431 cairo_gl_surface_swapbuffers(surface->cairo_surface);
432 wl_egl_window_get_attached_size(surface->egl_window,
433 &server_allocation->width,
434 &server_allocation->height);
435}
436
437static int
438egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
439{
440 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200441 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100442
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200443 device = cairo_surface_get_device(surface->cairo_surface);
444 if (!device)
445 return -1;
446
447 if (!ctx) {
448 if (device == surface->display->argb_device)
449 ctx = surface->display->argb_ctx;
450 else
451 assert(0);
452 }
453
454 cairo_device_flush(device);
455 cairo_device_acquire(device);
456 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
457 surface->egl_surface, ctx))
458 fprintf(stderr, "failed to make surface current\n");
459
460 return 0;
461}
462
463static void
464egl_window_surface_release(struct toysurface *base)
465{
466 struct egl_window_surface *surface = to_egl_window_surface(base);
467 cairo_device_t *device;
468
469 device = cairo_surface_get_device(surface->cairo_surface);
470 if (!device)
471 return;
472
473 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
474 surface->display->argb_ctx))
475 fprintf(stderr, "failed to make context current\n");
476
477 cairo_device_release(device);
478}
479
480static void
481egl_window_surface_destroy(struct toysurface *base)
482{
483 struct egl_window_surface *surface = to_egl_window_surface(base);
484 struct display *d = surface->display;
485
486 cairo_surface_destroy(surface->cairo_surface);
487 eglDestroySurface(d->dpy, surface->egl_surface);
488 wl_egl_window_destroy(surface->egl_window);
489 surface->surface = NULL;
490
491 free(surface);
492}
493
494static struct toysurface *
495egl_window_surface_create(struct display *display,
496 struct wl_surface *wl_surface,
497 uint32_t flags,
498 struct rectangle *rectangle)
499{
500 struct egl_window_surface *surface;
501
Pekka Paalanenb3627362012-11-19 17:16:00 +0200502 if (display->dpy == EGL_NO_DISPLAY)
503 return NULL;
504
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200505 surface = calloc(1, sizeof *surface);
506 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100507 return NULL;
508
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200509 surface->base.prepare = egl_window_surface_prepare;
510 surface->base.swap = egl_window_surface_swap;
511 surface->base.acquire = egl_window_surface_acquire;
512 surface->base.release = egl_window_surface_release;
513 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100514
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200515 surface->display = display;
516 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400517
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518 surface->egl_window = wl_egl_window_create(surface->surface,
519 rectangle->width,
520 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522 surface->egl_surface = eglCreateWindowSurface(display->dpy,
523 display->argb_config,
524 surface->egl_window,
525 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100526
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200527 surface->cairo_surface =
528 cairo_gl_surface_create_for_egl(display->argb_device,
529 surface->egl_surface,
530 rectangle->width,
531 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100532
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200533 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100534}
535
Pekka Paalanenb3627362012-11-19 17:16:00 +0200536#else
537
538static struct toysurface *
539egl_window_surface_create(struct display *display,
540 struct wl_surface *wl_surface,
541 uint32_t flags,
542 struct rectangle *rectangle)
543{
544 return NULL;
545}
546
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400547#endif
548
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200549struct shm_surface_data {
550 struct wl_buffer *buffer;
551 struct shm_pool *pool;
552};
553
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400554struct wl_buffer *
555display_get_buffer_for_surface(struct display *display,
556 cairo_surface_t *surface)
557{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200558 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200560 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561
562 return data->buffer;
563}
564
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500565static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700566shm_pool_destroy(struct shm_pool *pool);
567
568static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400569shm_surface_data_destroy(void *p)
570{
571 struct shm_surface_data *data = p;
572
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200573 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700574 if (data->pool)
575 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300576
577 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400578}
579
Kristian Høgsberg16626282012-04-03 11:21:27 -0400580static struct wl_shm_pool *
581make_shm_pool(struct display *display, int size, void **data)
582{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400583 struct wl_shm_pool *pool;
584 int fd;
585
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300586 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400587 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300588 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
589 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400590 return NULL;
591 }
592
593 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400594 if (*data == MAP_FAILED) {
595 fprintf(stderr, "mmap failed: %m\n");
596 close(fd);
597 return NULL;
598 }
599
600 pool = wl_shm_create_pool(display->shm, fd, size);
601
602 close(fd);
603
604 return pool;
605}
606
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700607static struct shm_pool *
608shm_pool_create(struct display *display, size_t size)
609{
610 struct shm_pool *pool = malloc(sizeof *pool);
611
612 if (!pool)
613 return NULL;
614
615 pool->pool = make_shm_pool(display, size, &pool->data);
616 if (!pool->pool) {
617 free(pool);
618 return NULL;
619 }
620
621 pool->size = size;
622 pool->used = 0;
623
624 return pool;
625}
626
627static void *
628shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
629{
630 if (pool->used + size > pool->size)
631 return NULL;
632
633 *offset = pool->used;
634 pool->used += size;
635
636 return (char *) pool->data + *offset;
637}
638
639/* destroy the pool. this does not unmap the memory though */
640static void
641shm_pool_destroy(struct shm_pool *pool)
642{
643 munmap(pool->data, pool->size);
644 wl_shm_pool_destroy(pool->pool);
645 free(pool);
646}
647
648/* Start allocating from the beginning of the pool again */
649static void
650shm_pool_reset(struct shm_pool *pool)
651{
652 pool->used = 0;
653}
654
655static int
656data_length_for_shm_surface(struct rectangle *rect)
657{
658 int stride;
659
660 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
661 rect->width);
662 return stride * rect->height;
663}
664
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500665static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700666display_create_shm_surface_from_pool(struct display *display,
667 struct rectangle *rectangle,
668 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400669{
670 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400671 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400672 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700673 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400674 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675
676 data = malloc(sizeof *data);
677 if (data == NULL)
678 return NULL;
679
680 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
681 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700682 length = stride * rectangle->height;
683 data->pool = NULL;
684 map = shm_pool_allocate(pool, length, &offset);
685
686 if (!map) {
687 free(data);
688 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400689 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400690
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400691 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400692 CAIRO_FORMAT_ARGB32,
693 rectangle->width,
694 rectangle->height,
695 stride);
696
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200697 cairo_surface_set_user_data(surface, &shm_surface_data_key,
698 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400699
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400700 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500701 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400702 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500703 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400704
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200705 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
706 rectangle->width,
707 rectangle->height,
708 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400709
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700710 return surface;
711}
712
713static cairo_surface_t *
714display_create_shm_surface(struct display *display,
715 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200716 struct shm_pool *alternate_pool,
717 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700718{
719 struct shm_surface_data *data;
720 struct shm_pool *pool;
721 cairo_surface_t *surface;
722
Pekka Paalanen99436862012-11-19 17:15:59 +0200723 if (alternate_pool) {
724 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700725 surface = display_create_shm_surface_from_pool(display,
726 rectangle,
727 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200728 alternate_pool);
729 if (surface) {
730 data = cairo_surface_get_user_data(surface,
731 &shm_surface_data_key);
732 goto out;
733 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700734 }
735
736 pool = shm_pool_create(display,
737 data_length_for_shm_surface(rectangle));
738 if (!pool)
739 return NULL;
740
741 surface =
742 display_create_shm_surface_from_pool(display, rectangle,
743 flags, pool);
744
745 if (!surface) {
746 shm_pool_destroy(pool);
747 return NULL;
748 }
749
750 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200751 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700752 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400753
Pekka Paalanen99436862012-11-19 17:15:59 +0200754out:
755 if (data_ret)
756 *data_ret = data;
757
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400758 return surface;
759}
760
nobled7b87cb02011-02-01 18:51:47 +0000761static int
762check_size(struct rectangle *rect)
763{
764 if (rect->width && rect->height)
765 return 0;
766
767 fprintf(stderr, "tried to create surface of "
768 "width: %d, height: %d\n", rect->width, rect->height);
769 return -1;
770}
771
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400772cairo_surface_t *
773display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100774 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400775 struct rectangle *rectangle,
776 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400777{
nobled7b87cb02011-02-01 18:51:47 +0000778 if (check_size(rectangle) < 0)
779 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200780
781 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200782 return display_create_shm_surface(display, rectangle, flags,
783 NULL, NULL);
784}
785
Pekka Paalanena4eda732012-11-19 17:16:02 +0200786struct shm_surface_leaf {
787 cairo_surface_t *cairo_surface;
788 /* 'data' is automatically destroyed, when 'cairo_surface' is */
789 struct shm_surface_data *data;
790
791 struct shm_pool *resize_pool;
792 int busy;
793};
794
795static void
796shm_surface_leaf_release(struct shm_surface_leaf *leaf)
797{
798 if (leaf->cairo_surface)
799 cairo_surface_destroy(leaf->cairo_surface);
800 /* leaf->data already destroyed via cairo private */
801
802 if (leaf->resize_pool)
803 shm_pool_destroy(leaf->resize_pool);
804}
805
Pekka Paalanen99436862012-11-19 17:15:59 +0200806struct shm_surface {
807 struct toysurface base;
808 struct display *display;
809 struct wl_surface *surface;
810 uint32_t flags;
811 int dx, dy;
812
Pekka Paalanena4eda732012-11-19 17:16:02 +0200813 struct shm_surface_leaf leaf[2];
814 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200815};
816
817static struct shm_surface *
818to_shm_surface(struct toysurface *base)
819{
820 return container_of(base, struct shm_surface, base);
821}
822
Pekka Paalanena4eda732012-11-19 17:16:02 +0200823static void
824shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
825{
826 struct shm_surface_leaf *leaf = data;
827
828 leaf->busy = 0;
829}
830
831static const struct wl_buffer_listener shm_surface_buffer_listener = {
832 shm_surface_buffer_release
833};
834
Pekka Paalanen99436862012-11-19 17:15:59 +0200835static cairo_surface_t *
836shm_surface_prepare(struct toysurface *base, int dx, int dy,
837 int width, int height, int resize_hint)
838{
839 struct shm_surface *surface = to_shm_surface(base);
840 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200841 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200842
843 surface->dx = dx;
844 surface->dy = dy;
845
Pekka Paalanena4eda732012-11-19 17:16:02 +0200846 /* pick a free buffer from the two */
847 if (!surface->leaf[0].busy)
848 leaf = &surface->leaf[0];
849 else if (!surface->leaf[1].busy)
850 leaf = &surface->leaf[1];
851 else {
852 fprintf(stderr, "%s: both buffers are held by the server.\n",
853 __func__);
854 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200855 }
856
Pekka Paalanena4eda732012-11-19 17:16:02 +0200857 if (!resize_hint && leaf->resize_pool) {
858 cairo_surface_destroy(leaf->cairo_surface);
859 leaf->cairo_surface = NULL;
860 shm_pool_destroy(leaf->resize_pool);
861 leaf->resize_pool = NULL;
862 }
863
864 if (leaf->cairo_surface &&
865 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
866 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200867 goto out;
868
Pekka Paalanena4eda732012-11-19 17:16:02 +0200869 if (leaf->cairo_surface)
870 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200871
Pekka Paalanena4eda732012-11-19 17:16:02 +0200872 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200873 /* Create a big pool to allocate from, while continuously
874 * resizing. Mmapping a new pool in the server
875 * is relatively expensive, so reusing a pool performs
876 * better, but may temporarily reserve unneeded memory.
877 */
878 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200879 leaf->resize_pool = shm_pool_create(surface->display,
880 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200881 }
882
Pekka Paalanena4eda732012-11-19 17:16:02 +0200883 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200884 display_create_shm_surface(surface->display, &rect,
885 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200886 leaf->resize_pool,
887 &leaf->data);
888 wl_buffer_add_listener(leaf->data->buffer,
889 &shm_surface_buffer_listener, leaf);
Pekka Paalanen99436862012-11-19 17:15:59 +0200890
891out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200892 surface->current = leaf;
893
894 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200895}
896
897static void
898shm_surface_swap(struct toysurface *base,
899 struct rectangle *server_allocation)
900{
901 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200902 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200903
904 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200905 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200906 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200907 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200908
Pekka Paalanena4eda732012-11-19 17:16:02 +0200909 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200910 surface->dx, surface->dy);
911 wl_surface_damage(surface->surface, 0, 0,
912 server_allocation->width, server_allocation->height);
913 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200914
915 leaf->busy = 1;
916 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200917}
918
919static int
920shm_surface_acquire(struct toysurface *base, EGLContext ctx)
921{
922 return -1;
923}
924
925static void
926shm_surface_release(struct toysurface *base)
927{
928}
929
930static void
931shm_surface_destroy(struct toysurface *base)
932{
933 struct shm_surface *surface = to_shm_surface(base);
934
Pekka Paalanena4eda732012-11-19 17:16:02 +0200935 shm_surface_leaf_release(&surface->leaf[0]);
936 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200937
938 free(surface);
939}
940
941static struct toysurface *
942shm_surface_create(struct display *display, struct wl_surface *wl_surface,
943 uint32_t flags, struct rectangle *rectangle)
944{
945 struct shm_surface *surface;
946
947 surface = calloc(1, sizeof *surface);
948 if (!surface)
949 return NULL;
950
951 surface->base.prepare = shm_surface_prepare;
952 surface->base.swap = shm_surface_swap;
953 surface->base.acquire = shm_surface_acquire;
954 surface->base.release = shm_surface_release;
955 surface->base.destroy = shm_surface_destroy;
956
957 surface->display = display;
958 surface->surface = wl_surface;
959 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200960
961 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400962}
963
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200964/*
965 * The following correspondences between file names and cursors was copied
966 * from: https://bugs.kde.org/attachment.cgi?id=67313
967 */
968
969static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300970 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200971 "sw-resize"
972};
973
974static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300975 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200976 "se-resize"
977};
978
979static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300980 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200981 "s-resize"
982};
983
984static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300985 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200986 "closedhand",
987 "208530c400c041818281048008011002"
988};
989
990static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300991 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200992 "default",
993 "top_left_arrow",
994 "left-arrow"
995};
996
997static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300998 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200999 "w-resize"
1000};
1001
1002static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001003 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001004 "e-resize"
1005};
1006
1007static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001008 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001009 "nw-resize"
1010};
1011
1012static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001013 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001014 "ne-resize"
1015};
1016
1017static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001018 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001019 "n-resize"
1020};
1021
1022static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001023 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001024 "ibeam",
1025 "text"
1026};
1027
1028static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001029 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001030 "pointer",
1031 "pointing_hand",
1032 "e29285e634086352946a0e7090d73106"
1033};
1034
1035static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001036 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001037 "wait",
1038 "0426c94ea35c87780ff01dc239897213"
1039};
1040
1041struct cursor_alternatives {
1042 const char **names;
1043 size_t count;
1044};
1045
1046static const struct cursor_alternatives cursors[] = {
1047 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1048 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1049 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1050 {grabbings, ARRAY_LENGTH(grabbings)},
1051 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1052 {left_sides, ARRAY_LENGTH(left_sides)},
1053 {right_sides, ARRAY_LENGTH(right_sides)},
1054 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1055 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1056 {top_sides, ARRAY_LENGTH(top_sides)},
1057 {xterms, ARRAY_LENGTH(xterms)},
1058 {hand1s, ARRAY_LENGTH(hand1s)},
1059 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001060};
1061
1062static void
1063create_cursors(struct display *display)
1064{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001065 char *config_file;
1066 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001067 unsigned int i, j;
1068 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001069 struct config_key shell_keys[] = {
1070 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1071 };
1072 struct config_section cs[] = {
1073 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1074 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001075
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001076 config_file = config_file_path("weston.ini");
1077 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1078 free(config_file);
1079
1080 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001081 display->cursors =
1082 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1083
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001084 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001085 cursor = NULL;
1086 for (j = 0; !cursor && j < cursors[i].count; ++j)
1087 cursor = wl_cursor_theme_get_cursor(
1088 display->cursor_theme, cursors[i].names[j]);
1089
1090 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001091 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001092 cursors[i].names[0]);
1093
1094 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001095 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001096}
1097
1098static void
1099destroy_cursors(struct display *display)
1100{
1101 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001102 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001103}
1104
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001105struct wl_cursor_image *
1106display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001107{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001108 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001109
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001110 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001111}
1112
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001113static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001114window_get_resize_dx_dy(struct window *window, int *x, int *y)
1115{
1116 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1117 *x = window->server_allocation.width - window->allocation.width;
1118 else
1119 *x = 0;
1120
1121 if (window->resize_edges & WINDOW_RESIZING_TOP)
1122 *y = window->server_allocation.height -
1123 window->allocation.height;
1124 else
1125 *y = 0;
1126
1127 window->resize_edges = 0;
1128}
1129
1130static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001131window_attach_surface(struct window *window)
1132{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001133 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001134
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001135 if (window->type == TYPE_NONE) {
1136 window->type = TYPE_TOPLEVEL;
1137 if (display->shell)
1138 wl_shell_surface_set_toplevel(window->shell_surface);
1139 }
1140
Pekka Paalanen512dde82012-10-10 12:49:27 +03001141 if (window->opaque_region) {
1142 wl_surface_set_opaque_region(window->surface,
1143 window->opaque_region);
1144 wl_region_destroy(window->opaque_region);
1145 window->opaque_region = NULL;
1146 }
1147
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001148 if (window->input_region) {
1149 wl_surface_set_input_region(window->surface,
1150 window->input_region);
1151 wl_region_destroy(window->input_region);
1152 window->input_region = NULL;
1153 }
1154
Pekka Paalanen99436862012-11-19 17:15:59 +02001155 window->toysurface->swap(window->toysurface,
1156 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001157}
1158
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001159int
1160window_has_focus(struct window *window)
1161{
1162 return window->focus_count > 0;
1163}
1164
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001165static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001166window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001167{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001168 if (!window->cairo_surface)
1169 return;
1170
1171 window_attach_surface(window);
1172 cairo_surface_destroy(window->cairo_surface);
1173 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001174}
1175
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001176struct display *
1177window_get_display(struct window *window)
1178{
1179 return window->display;
1180}
1181
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001182static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001183window_create_surface(struct window *window)
1184{
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001185 struct rectangle allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001186 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001187 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001188
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001189 if (!window->transparent)
1190 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001191
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001192 switch (window->buffer_transform) {
1193 case WL_OUTPUT_TRANSFORM_90:
1194 case WL_OUTPUT_TRANSFORM_270:
1195 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1196 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1197 allocation.width = window->allocation.height;
1198 allocation.height = window->allocation.width;
1199 break;
1200 default:
1201 break;
1202 }
1203
Pekka Paalanenb3627362012-11-19 17:16:00 +02001204 if (!window->toysurface &&
1205 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1206 window->display->dpy) {
1207 window->toysurface =
1208 egl_window_surface_create(window->display,
1209 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001210 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001211 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001212
Pekka Paalanenb3627362012-11-19 17:16:00 +02001213 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001214 window->toysurface = shm_surface_create(window->display,
1215 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001216 &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001217
Pekka Paalanen99436862012-11-19 17:15:59 +02001218 window_get_resize_dx_dy(window, &dx, &dy);
1219 window->cairo_surface =
1220 window->toysurface->prepare(window->toysurface, dx, dy,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001221 allocation.width,
1222 allocation.height,
Pekka Paalanen99436862012-11-19 17:15:59 +02001223 window->resizing);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001224}
1225
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001226int
1227window_get_buffer_transform(struct window *window)
1228{
1229 return window->buffer_transform;
1230}
1231
1232void
1233window_set_buffer_transform(struct window *window,
1234 enum wl_output_transform transform)
1235{
1236 window->buffer_transform = transform;
1237 wl_surface_set_buffer_transform(window->surface, transform);
1238}
1239
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001240static void frame_destroy(struct frame *frame);
1241
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001242void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001243window_destroy(struct window *window)
1244{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001245 struct display *display = window->display;
1246 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001247 struct window_output *window_output;
1248 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001249
1250 if (window->redraw_scheduled)
1251 wl_list_remove(&window->redraw_task.link);
1252
1253 wl_list_for_each(input, &display->input_list, link) {
1254 if (input->pointer_focus == window)
1255 input->pointer_focus = NULL;
1256 if (input->keyboard_focus == window)
1257 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001258 if (input->focus_widget &&
1259 input->focus_widget->window == window)
1260 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001261 }
1262
Rob Bradford7507b572012-05-15 17:55:34 +01001263 wl_list_for_each_safe(window_output, window_output_tmp,
1264 &window->window_output_list, link) {
1265 free (window_output);
1266 }
1267
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001268 if (window->input_region)
1269 wl_region_destroy(window->input_region);
1270 if (window->opaque_region)
1271 wl_region_destroy(window->opaque_region);
1272
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001273 if (window->frame)
1274 frame_destroy(window->frame);
1275
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001276 if (window->shell_surface)
1277 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001278 wl_surface_destroy(window->surface);
1279 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001280
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001281 if (window->toysurface)
1282 window->toysurface->destroy(window->toysurface);
1283
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001284 if (window->frame_cb)
1285 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001286 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001287 free(window);
1288}
1289
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001290static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001291widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001292{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001293 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001294
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001295 wl_list_for_each(child, &widget->child_list, link) {
1296 target = widget_find_widget(child, x, y);
1297 if (target)
1298 return target;
1299 }
1300
1301 if (widget->allocation.x <= x &&
1302 x < widget->allocation.x + widget->allocation.width &&
1303 widget->allocation.y <= y &&
1304 y < widget->allocation.y + widget->allocation.height) {
1305 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001306 }
1307
1308 return NULL;
1309}
1310
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001311static struct widget *
1312widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001313{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001314 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001315
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001316 widget = malloc(sizeof *widget);
1317 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001318 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001319 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001320 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001321 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001322 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001323 widget->tooltip = NULL;
1324 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001325
1326 return widget;
1327}
1328
1329struct widget *
1330window_add_widget(struct window *window, void *data)
1331{
1332 window->widget = widget_create(window, data);
1333 wl_list_init(&window->widget->link);
1334
1335 return window->widget;
1336}
1337
1338struct widget *
1339widget_add_widget(struct widget *parent, void *data)
1340{
1341 struct widget *widget;
1342
1343 widget = widget_create(parent->window, data);
1344 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001345
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001346 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001347}
1348
1349void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001350widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001351{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001352 struct display *display = widget->window->display;
1353 struct input *input;
1354
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001355 if (widget->tooltip) {
1356 free(widget->tooltip);
1357 widget->tooltip = NULL;
1358 }
1359
Pekka Paalanene156fb62012-01-19 13:51:38 +02001360 wl_list_for_each(input, &display->input_list, link) {
1361 if (input->focus_widget == widget)
1362 input->focus_widget = NULL;
1363 }
1364
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001365 wl_list_remove(&widget->link);
1366 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001367}
1368
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001369void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001370widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001371{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001372 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001373}
1374
1375void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001376widget_set_size(struct widget *widget, int32_t width, int32_t height)
1377{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001378 widget->allocation.width = width;
1379 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001380}
1381
1382void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001383widget_set_allocation(struct widget *widget,
1384 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001385{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001386 widget->allocation.x = x;
1387 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001388 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001389}
1390
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001391void
1392widget_set_transparent(struct widget *widget, int transparent)
1393{
1394 widget->opaque = !transparent;
1395}
1396
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001397void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001398widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001399{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001400 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001401}
1402
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001403void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001404widget_set_resize_handler(struct widget *widget,
1405 widget_resize_handler_t handler)
1406{
1407 widget->resize_handler = handler;
1408}
1409
1410void
1411widget_set_redraw_handler(struct widget *widget,
1412 widget_redraw_handler_t handler)
1413{
1414 widget->redraw_handler = handler;
1415}
1416
1417void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001418widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001419{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001420 widget->enter_handler = handler;
1421}
1422
1423void
1424widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1425{
1426 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001427}
1428
1429void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001430widget_set_motion_handler(struct widget *widget,
1431 widget_motion_handler_t handler)
1432{
1433 widget->motion_handler = handler;
1434}
1435
1436void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001437widget_set_button_handler(struct widget *widget,
1438 widget_button_handler_t handler)
1439{
1440 widget->button_handler = handler;
1441}
1442
1443void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001444widget_set_axis_handler(struct widget *widget,
1445 widget_axis_handler_t handler)
1446{
1447 widget->axis_handler = handler;
1448}
1449
1450void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001451widget_schedule_redraw(struct widget *widget)
1452{
1453 window_schedule_redraw(widget->window);
1454}
1455
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001456cairo_surface_t *
1457window_get_surface(struct window *window)
1458{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001459 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001460}
1461
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001462struct wl_surface *
1463window_get_wl_surface(struct window *window)
1464{
1465 return window->surface;
1466}
1467
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001468struct wl_shell_surface *
1469window_get_wl_shell_surface(struct window *window)
1470{
1471 return window->shell_surface;
1472}
1473
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001474static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001475tooltip_redraw_handler(struct widget *widget, void *data)
1476{
1477 cairo_t *cr;
1478 const int32_t r = 3;
1479 struct tooltip *tooltip = data;
1480 int32_t width, height;
1481 struct window *window = widget->window;
1482
1483 cr = cairo_create(window->cairo_surface);
1484 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1485 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1486 cairo_paint(cr);
1487
1488 width = window->allocation.width;
1489 height = window->allocation.height;
1490 rounded_rect(cr, 0, 0, width, height, r);
1491
1492 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1493 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1494 cairo_fill(cr);
1495
1496 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1497 cairo_move_to(cr, 10, 16);
1498 cairo_show_text(cr, tooltip->entry);
1499 cairo_destroy(cr);
1500}
1501
1502static cairo_text_extents_t
1503get_text_extents(struct tooltip *tooltip)
1504{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001505 cairo_t *cr;
1506 cairo_text_extents_t extents;
1507
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001508 /* Use the dummy_surface because tooltip's surface was not
1509 * created yet, and parent does not have a valid surface
1510 * outside repaint, either.
1511 */
1512 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001513 cairo_text_extents(cr, tooltip->entry, &extents);
1514 cairo_destroy(cr);
1515
1516 return extents;
1517}
1518
1519static int
1520window_create_tooltip(struct tooltip *tooltip)
1521{
1522 struct widget *parent = tooltip->parent;
1523 struct display *display = parent->window->display;
1524 struct window *window;
1525 const int offset_y = 27;
1526 const int margin = 3;
1527 cairo_text_extents_t extents;
1528
1529 if (tooltip->widget)
1530 return 0;
1531
1532 window = window_create_transient(display, parent->window, tooltip->x,
1533 tooltip->y + offset_y,
1534 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1535 if (!window)
1536 return -1;
1537
1538 tooltip->window = window;
1539 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1540
1541 extents = get_text_extents(tooltip);
1542 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1543 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1544
1545 return 0;
1546}
1547
1548void
1549widget_destroy_tooltip(struct widget *parent)
1550{
1551 struct tooltip *tooltip = parent->tooltip;
1552
1553 parent->tooltip_count = 0;
1554 if (!tooltip)
1555 return;
1556
1557 if (tooltip->widget) {
1558 widget_destroy(tooltip->widget);
1559 window_destroy(tooltip->window);
1560 tooltip->widget = NULL;
1561 tooltip->window = NULL;
1562 }
1563
1564 close(tooltip->tooltip_fd);
1565 free(tooltip->entry);
1566 free(tooltip);
1567 parent->tooltip = NULL;
1568}
1569
1570static void
1571tooltip_func(struct task *task, uint32_t events)
1572{
1573 struct tooltip *tooltip =
1574 container_of(task, struct tooltip, tooltip_task);
1575 uint64_t exp;
1576
Martin Olsson8df662a2012-07-08 03:03:47 +02001577 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1578 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001579 window_create_tooltip(tooltip);
1580}
1581
1582#define TOOLTIP_TIMEOUT 500
1583static int
1584tooltip_timer_reset(struct tooltip *tooltip)
1585{
1586 struct itimerspec its;
1587
1588 its.it_interval.tv_sec = 0;
1589 its.it_interval.tv_nsec = 0;
1590 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1591 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1592 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1593 fprintf(stderr, "could not set timerfd\n: %m");
1594 return -1;
1595 }
1596
1597 return 0;
1598}
1599
1600int
1601widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1602{
1603 struct tooltip *tooltip = parent->tooltip;
1604
1605 parent->tooltip_count++;
1606 if (tooltip) {
1607 tooltip->x = x;
1608 tooltip->y = y;
1609 tooltip_timer_reset(tooltip);
1610 return 0;
1611 }
1612
1613 /* the handler might be triggered too fast via input device motion, so
1614 * we need this check here to make sure tooltip is fully initialized */
1615 if (parent->tooltip_count > 1)
1616 return 0;
1617
1618 tooltip = malloc(sizeof *tooltip);
1619 if (!tooltip)
1620 return -1;
1621
1622 parent->tooltip = tooltip;
1623 tooltip->parent = parent;
1624 tooltip->widget = NULL;
1625 tooltip->window = NULL;
1626 tooltip->x = x;
1627 tooltip->y = y;
1628 tooltip->entry = strdup(entry);
1629 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1630 if (tooltip->tooltip_fd < 0) {
1631 fprintf(stderr, "could not create timerfd\n: %m");
1632 return -1;
1633 }
1634
1635 tooltip->tooltip_task.run = tooltip_func;
1636 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1637 EPOLLIN, &tooltip->tooltip_task);
1638 tooltip_timer_reset(tooltip);
1639
1640 return 0;
1641}
1642
1643static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001644workspace_manager_state(void *data,
1645 struct workspace_manager *workspace_manager,
1646 uint32_t current,
1647 uint32_t count)
1648{
1649 struct display *display = data;
1650
1651 display->workspace = current;
1652 display->workspace_count = count;
1653}
1654
1655static const struct workspace_manager_listener workspace_manager_listener = {
1656 workspace_manager_state
1657};
1658
1659static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001660frame_resize_handler(struct widget *widget,
1661 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001662{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001663 struct frame *frame = data;
1664 struct widget *child = frame->child;
1665 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001666 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001667 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001668 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001669 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001670 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001671 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001672
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001673 switch (widget->window->type) {
1674 case TYPE_FULLSCREEN:
1675 decoration_width = 0;
1676 decoration_height = 0;
1677
1678 allocation.x = 0;
1679 allocation.y = 0;
1680 allocation.width = width;
1681 allocation.height = height;
1682 opaque_margin = 0;
1683
1684 wl_list_for_each(button, &frame->buttons_list, link)
1685 button->widget->opaque = 1;
1686 break;
1687 case TYPE_MAXIMIZED:
1688 decoration_width = t->width * 2;
1689 decoration_height = t->width + t->titlebar_height;
1690
1691 allocation.x = t->width;
1692 allocation.y = t->titlebar_height;
1693 allocation.width = width - decoration_width;
1694 allocation.height = height - decoration_height;
1695
1696 opaque_margin = 0;
1697
1698 wl_list_for_each(button, &frame->buttons_list, link)
1699 button->widget->opaque = 0;
1700 break;
1701 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001702 decoration_width = (t->width + t->margin) * 2;
1703 decoration_height = t->width +
1704 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001705
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001706 allocation.x = t->width + t->margin;
1707 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001708 allocation.width = width - decoration_width;
1709 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001710
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001711 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001712
1713 wl_list_for_each(button, &frame->buttons_list, link)
1714 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001715 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001716 }
1717
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001718 widget_set_allocation(child, allocation.x, allocation.y,
1719 allocation.width, allocation.height);
1720
1721 if (child->resize_handler)
1722 child->resize_handler(child,
1723 allocation.width,
1724 allocation.height,
1725 child->user_data);
1726
Scott Moreauf7e498c2012-05-14 11:39:29 -06001727 width = child->allocation.width + decoration_width;
1728 height = child->allocation.height + decoration_height;
1729
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001730 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1731
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001732 widget->window->input_region =
1733 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001734 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001735 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001736 shadow_margin, shadow_margin,
1737 width - 2 * shadow_margin,
1738 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001739 } else {
1740 wl_region_add(widget->window->input_region,
1741 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001742 }
1743
Scott Moreauf7e498c2012-05-14 11:39:29 -06001744 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001745
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001746 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001747 wl_region_add(widget->window->opaque_region,
1748 opaque_margin, opaque_margin,
1749 widget->allocation.width - 2 * opaque_margin,
1750 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001751
1752 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001753 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1754 x_l = t->width + shadow_margin;
1755 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001756 wl_list_for_each(button, &frame->buttons_list, link) {
1757 const int button_padding = 4;
1758 w = cairo_image_surface_get_width(button->icon);
1759 h = cairo_image_surface_get_height(button->icon);
1760
1761 if (button->decoration == FRAME_BUTTON_FANCY)
1762 w += 10;
1763
1764 if (button->align == FRAME_BUTTON_LEFT) {
1765 widget_set_allocation(button->widget,
1766 x_l, y , w + 1, h + 1);
1767 x_l += w;
1768 x_l += button_padding;
1769 } else {
1770 x_r -= w;
1771 widget_set_allocation(button->widget,
1772 x_r, y , w + 1, h + 1);
1773 x_r -= button_padding;
1774 }
1775 }
1776}
1777
1778static int
1779frame_button_enter_handler(struct widget *widget,
1780 struct input *input, float x, float y, void *data)
1781{
1782 struct frame_button *frame_button = data;
1783
1784 widget_schedule_redraw(frame_button->widget);
1785 frame_button->state = FRAME_BUTTON_OVER;
1786
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001787 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001788}
1789
1790static void
1791frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1792{
1793 struct frame_button *frame_button = data;
1794
1795 widget_schedule_redraw(frame_button->widget);
1796 frame_button->state = FRAME_BUTTON_DEFAULT;
1797}
1798
1799static void
1800frame_button_button_handler(struct widget *widget,
1801 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001802 uint32_t button,
1803 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001804{
1805 struct frame_button *frame_button = data;
1806 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001807 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001808
1809 if (button != BTN_LEFT)
1810 return;
1811
1812 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001813 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001814 frame_button->state = FRAME_BUTTON_ACTIVE;
1815 widget_schedule_redraw(frame_button->widget);
1816
1817 if (frame_button->type == FRAME_BUTTON_ICON)
1818 window_show_frame_menu(window, input, time);
1819 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001820 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001821 frame_button->state = FRAME_BUTTON_DEFAULT;
1822 widget_schedule_redraw(frame_button->widget);
1823 break;
1824 }
1825
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001826 if (!was_pressed)
1827 return;
1828
Martin Minarik1998b152012-05-10 02:04:35 +02001829 switch (frame_button->type) {
1830 case FRAME_BUTTON_CLOSE:
1831 if (window->close_handler)
1832 window->close_handler(window->parent,
1833 window->user_data);
1834 else
1835 display_exit(window->display);
1836 break;
1837 case FRAME_BUTTON_MINIMIZE:
1838 fprintf(stderr,"Minimize stub\n");
1839 break;
1840 case FRAME_BUTTON_MAXIMIZE:
1841 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1842 break;
1843 default:
1844 /* Unknown operation */
1845 break;
1846 }
1847}
1848
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001849static int
1850frame_button_motion_handler(struct widget *widget,
1851 struct input *input, uint32_t time,
1852 float x, float y, void *data)
1853{
1854 struct frame_button *frame_button = data;
1855 enum frame_button_pointer previous_button_state = frame_button->state;
1856
1857 /* only track state for a pressed button */
1858 if (input->grab != widget)
1859 return CURSOR_LEFT_PTR;
1860
1861 if (x > widget->allocation.x &&
1862 x < (widget->allocation.x + widget->allocation.width) &&
1863 y > widget->allocation.y &&
1864 y < (widget->allocation.y + widget->allocation.height)) {
1865 frame_button->state = FRAME_BUTTON_ACTIVE;
1866 } else {
1867 frame_button->state = FRAME_BUTTON_DEFAULT;
1868 }
1869
1870 if (frame_button->state != previous_button_state)
1871 widget_schedule_redraw(frame_button->widget);
1872
1873 return CURSOR_LEFT_PTR;
1874}
1875
Martin Minarik1998b152012-05-10 02:04:35 +02001876static void
1877frame_button_redraw_handler(struct widget *widget, void *data)
1878{
1879 struct frame_button *frame_button = data;
1880 cairo_t *cr;
1881 int width, height, x, y;
1882 struct window *window = widget->window;
1883
1884 x = widget->allocation.x;
1885 y = widget->allocation.y;
1886 width = widget->allocation.width;
1887 height = widget->allocation.height;
1888
1889 if (!width)
1890 return;
1891 if (!height)
1892 return;
1893 if (widget->opaque)
1894 return;
1895
1896 cr = cairo_create(window->cairo_surface);
1897
1898 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1899 cairo_set_line_width(cr, 1);
1900
1901 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1902 cairo_rectangle (cr, x, y, 25, 16);
1903
1904 cairo_stroke_preserve(cr);
1905
1906 switch (frame_button->state) {
1907 case FRAME_BUTTON_DEFAULT:
1908 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1909 break;
1910 case FRAME_BUTTON_OVER:
1911 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1912 break;
1913 case FRAME_BUTTON_ACTIVE:
1914 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1915 break;
1916 }
1917
1918 cairo_fill (cr);
1919
1920 x += 4;
1921 }
1922
1923 cairo_set_source_surface(cr, frame_button->icon, x, y);
1924 cairo_paint(cr);
1925
1926 cairo_destroy(cr);
1927}
1928
1929static struct widget *
1930frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1931 enum frame_button_align align, enum frame_button_decoration style)
1932{
1933 struct frame_button *frame_button;
1934 const char *icon = data;
1935
1936 frame_button = malloc (sizeof *frame_button);
1937 memset(frame_button, 0, sizeof *frame_button);
1938
1939 frame_button->icon = cairo_image_surface_create_from_png(icon);
1940 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1941 frame_button->frame = frame;
1942 frame_button->type = type;
1943 frame_button->align = align;
1944 frame_button->decoration = style;
1945
1946 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1947
1948 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1949 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1950 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1951 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001952 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001953 return frame_button->widget;
1954}
1955
1956static void
1957frame_button_destroy(struct frame_button *frame_button)
1958{
1959 widget_destroy(frame_button->widget);
1960 wl_list_remove(&frame_button->link);
1961 cairo_surface_destroy(frame_button->icon);
1962 free(frame_button);
1963
1964 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001965}
1966
1967static void
1968frame_redraw_handler(struct widget *widget, void *data)
1969{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001970 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001971 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001972 struct theme *t = window->display->theme;
1973 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001974
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001975 if (window->type == TYPE_FULLSCREEN)
1976 return;
1977
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001978 cr = cairo_create(window->cairo_surface);
1979
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001980 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001981 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001982 if (window->type == TYPE_MAXIMIZED)
1983 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001984 theme_render_frame(t, cr, widget->allocation.width,
1985 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001986
1987 cairo_destroy(cr);
1988}
1989
1990static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001991frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001992{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001993 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001994 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001995 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001996
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001997 location = theme_get_location(t, input->sx, input->sy,
1998 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001999 frame->widget->allocation.height,
2000 window->type == TYPE_MAXIMIZED ?
2001 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002002
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002003 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002004 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002005 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002006 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002007 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002008 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002009 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002010 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002011 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002012 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002013 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002014 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002015 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002016 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002017 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002018 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002019 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002020 case THEME_LOCATION_EXTERIOR:
2021 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002022 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002023 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002024 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002025}
2026
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002027static void
2028frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002029{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002030 struct display *display;
2031
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002032 switch (index) {
2033 case 0: /* close */
2034 if (window->close_handler)
2035 window->close_handler(window->parent,
2036 window->user_data);
2037 else
2038 display_exit(window->display);
2039 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002040 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002041 display = window->display;
2042 if (display->workspace > 0)
2043 workspace_manager_move_surface(display->workspace_manager,
2044 window->surface,
2045 display->workspace - 1);
2046 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002047 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002048 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002049 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002050 workspace_manager_move_surface(display->workspace_manager,
2051 window->surface,
2052 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002053 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002054 case 3: /* fullscreen */
2055 /* we don't have a way to get out of fullscreen for now */
2056 if (window->fullscreen_handler)
2057 window->fullscreen_handler(window, window->user_data);
2058 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002059 }
2060}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002061
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002062void
2063window_show_frame_menu(struct window *window,
2064 struct input *input, uint32_t time)
2065{
2066 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002067 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002068
2069 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002070 "Close",
2071 "Move to workspace above", "Move to workspace below",
2072 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002073 };
2074
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002075 if (window->fullscreen_handler)
2076 count = ARRAY_LENGTH(entries);
2077 else
2078 count = ARRAY_LENGTH(entries) - 1;
2079
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002080 input_get_position(input, &x, &y);
2081 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002082 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002083}
2084
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002085static int
2086frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002087 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002088{
2089 return frame_get_pointer_image_for_location(data, input);
2090}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002091
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002092static int
2093frame_motion_handler(struct widget *widget,
2094 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002095 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002096{
2097 return frame_get_pointer_image_for_location(data, input);
2098}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002099
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002100static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002101frame_button_handler(struct widget *widget,
2102 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002103 uint32_t button, enum wl_pointer_button_state state,
2104 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002105
2106{
2107 struct frame *frame = data;
2108 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002109 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002110 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002111
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002112 location = theme_get_location(display->theme, input->sx, input->sy,
2113 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002114 frame->widget->allocation.height,
2115 window->type == TYPE_MAXIMIZED ?
2116 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002117
Daniel Stone4dbadb12012-05-30 16:31:51 +01002118 if (window->display->shell && button == BTN_LEFT &&
2119 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002120 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002121 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002122 if (!window->shell_surface)
2123 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002124 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002125 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002126 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002127 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002128 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002129 case THEME_LOCATION_RESIZING_TOP:
2130 case THEME_LOCATION_RESIZING_BOTTOM:
2131 case THEME_LOCATION_RESIZING_LEFT:
2132 case THEME_LOCATION_RESIZING_RIGHT:
2133 case THEME_LOCATION_RESIZING_TOP_LEFT:
2134 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2135 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2136 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002137 if (!window->shell_surface)
2138 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002139 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002140
Pekka Paalanen99436862012-11-19 17:15:59 +02002141 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002142 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002143 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002144 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002145 break;
2146 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002147 } else if (button == BTN_RIGHT &&
2148 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002149 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002150 }
2151}
2152
2153struct widget *
2154frame_create(struct window *window, void *data)
2155{
2156 struct frame *frame;
2157
2158 frame = malloc(sizeof *frame);
2159 memset(frame, 0, sizeof *frame);
2160
2161 frame->widget = window_add_widget(window, frame);
2162 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002163
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002164 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2165 widget_set_resize_handler(frame->widget, frame_resize_handler);
2166 widget_set_enter_handler(frame->widget, frame_enter_handler);
2167 widget_set_motion_handler(frame->widget, frame_motion_handler);
2168 widget_set_button_handler(frame->widget, frame_button_handler);
2169
Martin Minarik1998b152012-05-10 02:04:35 +02002170 /* Create empty list for frame buttons */
2171 wl_list_init(&frame->buttons_list);
2172
2173 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2174 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2175
2176 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2177 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2178
2179 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2180 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2181
2182 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2183 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2184
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002185 window->frame = frame;
2186
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002187 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002188}
2189
Kristian Høgsberga1627922012-06-20 17:30:03 -04002190void
2191frame_set_child_size(struct widget *widget, int child_width, int child_height)
2192{
2193 struct display *display = widget->window->display;
2194 struct theme *t = display->theme;
2195 int decoration_width, decoration_height;
2196 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002197 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002198
2199 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002200 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002201 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002202 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002203
2204 width = child_width + decoration_width;
2205 height = child_height + decoration_height;
2206 } else {
2207 width = child_width;
2208 height = child_height;
2209 }
2210
2211 window_schedule_resize(widget->window, width, height);
2212}
2213
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002214static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002215frame_destroy(struct frame *frame)
2216{
Martin Minarik1998b152012-05-10 02:04:35 +02002217 struct frame_button *button, *tmp;
2218
2219 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2220 frame_button_destroy(button);
2221
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002222 /* frame->child must be destroyed by the application */
2223 widget_destroy(frame->widget);
2224 free(frame);
2225}
2226
2227static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002228input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002229 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002230{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002231 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002232 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002233
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002234 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002235 return;
2236
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002237 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002238 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002239 widget = old;
2240 if (input->grab)
2241 widget = input->grab;
2242 if (widget->leave_handler)
2243 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002244 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002245 }
2246
2247 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002248 widget = focus;
2249 if (input->grab)
2250 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002251 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002252 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002253 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002254 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002255
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002256 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002257 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002258}
2259
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002260void
2261input_grab(struct input *input, struct widget *widget, uint32_t button)
2262{
2263 input->grab = widget;
2264 input->grab_button = button;
2265}
2266
2267void
2268input_ungrab(struct input *input)
2269{
2270 struct widget *widget;
2271
2272 input->grab = NULL;
2273 if (input->pointer_focus) {
2274 widget = widget_find_widget(input->pointer_focus->widget,
2275 input->sx, input->sy);
2276 input_set_focus_widget(input, widget, input->sx, input->sy);
2277 }
2278}
2279
2280static void
2281input_remove_pointer_focus(struct input *input)
2282{
2283 struct window *window = input->pointer_focus;
2284
2285 if (!window)
2286 return;
2287
2288 input_set_focus_widget(input, NULL, 0, 0);
2289
2290 input->pointer_focus = NULL;
2291 input->current_cursor = CURSOR_UNSET;
2292}
2293
2294static void
2295pointer_handle_enter(void *data, struct wl_pointer *pointer,
2296 uint32_t serial, struct wl_surface *surface,
2297 wl_fixed_t sx_w, wl_fixed_t sy_w)
2298{
2299 struct input *input = data;
2300 struct window *window;
2301 struct widget *widget;
2302 float sx = wl_fixed_to_double(sx_w);
2303 float sy = wl_fixed_to_double(sy_w);
2304
2305 if (!surface) {
2306 /* enter event for a window we've just destroyed */
2307 return;
2308 }
2309
2310 input->display->serial = serial;
2311 input->pointer_enter_serial = serial;
2312 input->pointer_focus = wl_surface_get_user_data(surface);
2313 window = input->pointer_focus;
2314
Pekka Paalanen99436862012-11-19 17:15:59 +02002315 if (window->resizing) {
2316 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002317 /* Schedule a redraw to free the pool */
2318 window_schedule_redraw(window);
2319 }
2320
2321 input->sx = sx;
2322 input->sy = sy;
2323
2324 widget = widget_find_widget(window->widget, sx, sy);
2325 input_set_focus_widget(input, widget, sx, sy);
2326}
2327
2328static void
2329pointer_handle_leave(void *data, struct wl_pointer *pointer,
2330 uint32_t serial, struct wl_surface *surface)
2331{
2332 struct input *input = data;
2333
2334 input->display->serial = serial;
2335 input_remove_pointer_focus(input);
2336}
2337
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002338static void
Daniel Stone37816df2012-05-16 18:45:18 +01002339pointer_handle_motion(void *data, struct wl_pointer *pointer,
2340 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002341{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002342 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002343 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002344 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002345 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002346 float sx = wl_fixed_to_double(sx_w);
2347 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002348
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002349 input->sx = sx;
2350 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002351
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002352 if (!window)
2353 return;
2354
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002355 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002356 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002357 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002358 }
2359
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002360 if (input->grab)
2361 widget = input->grab;
2362 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002363 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002364 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002365 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002366 input, time, sx, sy,
2367 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002368
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002369 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002370}
2371
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002372static void
Daniel Stone37816df2012-05-16 18:45:18 +01002373pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002374 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002375{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002376 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002377 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002378 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002379
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002380 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002381 if (input->focus_widget && input->grab == NULL &&
2382 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002383 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002384
Neil Roberts6b28aad2012-01-23 19:11:18 +00002385 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002386 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002387 (*widget->button_handler)(widget,
2388 input, time,
2389 button, state,
2390 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002391
Daniel Stone4dbadb12012-05-30 16:31:51 +01002392 if (input->grab && input->grab_button == button &&
2393 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002394 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002395}
2396
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002397static void
Daniel Stone37816df2012-05-16 18:45:18 +01002398pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002399 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002400{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002401 struct input *input = data;
2402 struct widget *widget;
2403
2404 widget = input->focus_widget;
2405 if (input->grab)
2406 widget = input->grab;
2407 if (widget && widget->axis_handler)
2408 (*widget->axis_handler)(widget,
2409 input, time,
2410 axis, value,
2411 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002412}
2413
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002414static const struct wl_pointer_listener pointer_listener = {
2415 pointer_handle_enter,
2416 pointer_handle_leave,
2417 pointer_handle_motion,
2418 pointer_handle_button,
2419 pointer_handle_axis,
2420};
2421
2422static void
2423input_remove_keyboard_focus(struct input *input)
2424{
2425 struct window *window = input->keyboard_focus;
2426 struct itimerspec its;
2427
2428 its.it_interval.tv_sec = 0;
2429 its.it_interval.tv_nsec = 0;
2430 its.it_value.tv_sec = 0;
2431 its.it_value.tv_nsec = 0;
2432 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2433
2434 if (!window)
2435 return;
2436
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002437 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002438 if (window->keyboard_focus_handler)
2439 (*window->keyboard_focus_handler)(window, NULL,
2440 window->user_data);
2441
2442 input->keyboard_focus = NULL;
2443}
2444
2445static void
2446keyboard_repeat_func(struct task *task, uint32_t events)
2447{
2448 struct input *input =
2449 container_of(task, struct input, repeat_task);
2450 struct window *window = input->keyboard_focus;
2451 uint64_t exp;
2452
2453 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2454 /* If we change the timer between the fd becoming
2455 * readable and getting here, there'll be nothing to
2456 * read and we get EAGAIN. */
2457 return;
2458
2459 if (window && window->key_handler) {
2460 (*window->key_handler)(window, input, input->repeat_time,
2461 input->repeat_key, input->repeat_sym,
2462 WL_KEYBOARD_KEY_STATE_PRESSED,
2463 window->user_data);
2464 }
2465}
2466
2467static void
2468keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2469 uint32_t format, int fd, uint32_t size)
2470{
2471 struct input *input = data;
2472 char *map_str;
2473
2474 if (!data) {
2475 close(fd);
2476 return;
2477 }
2478
2479 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2480 close(fd);
2481 return;
2482 }
2483
2484 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2485 if (map_str == MAP_FAILED) {
2486 close(fd);
2487 return;
2488 }
2489
2490 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2491 map_str,
2492 XKB_KEYMAP_FORMAT_TEXT_V1,
2493 0);
2494 munmap(map_str, size);
2495 close(fd);
2496
2497 if (!input->xkb.keymap) {
2498 fprintf(stderr, "failed to compile keymap\n");
2499 return;
2500 }
2501
2502 input->xkb.state = xkb_state_new(input->xkb.keymap);
2503 if (!input->xkb.state) {
2504 fprintf(stderr, "failed to create XKB state\n");
2505 xkb_map_unref(input->xkb.keymap);
2506 input->xkb.keymap = NULL;
2507 return;
2508 }
2509
2510 input->xkb.control_mask =
2511 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2512 input->xkb.alt_mask =
2513 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2514 input->xkb.shift_mask =
2515 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2516}
2517
2518static void
2519keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2520 uint32_t serial, struct wl_surface *surface,
2521 struct wl_array *keys)
2522{
2523 struct input *input = data;
2524 struct window *window;
2525
2526 input->display->serial = serial;
2527 input->keyboard_focus = wl_surface_get_user_data(surface);
2528
2529 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002530 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002531 if (window->keyboard_focus_handler)
2532 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002533 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002534}
2535
2536static void
2537keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2538 uint32_t serial, struct wl_surface *surface)
2539{
2540 struct input *input = data;
2541
2542 input->display->serial = serial;
2543 input_remove_keyboard_focus(input);
2544}
2545
Scott Moreau210d0792012-03-22 10:47:01 -06002546static void
Daniel Stone37816df2012-05-16 18:45:18 +01002547keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002548 uint32_t serial, uint32_t time, uint32_t key,
2549 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002550{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002551 struct input *input = data;
2552 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002553 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002554 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002555 const xkb_keysym_t *syms;
2556 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002557 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002558
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002559 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002560 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002561 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002562 return;
2563
Daniel Stone97f68542012-05-30 16:32:01 +01002564 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002565
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002566 sym = XKB_KEY_NoSymbol;
2567 if (num_syms == 1)
2568 sym = syms[0];
2569
2570 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002571 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002572 window_set_maximized(window,
2573 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002574 } else if (sym == XKB_KEY_F11 &&
2575 window->fullscreen_handler &&
2576 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2577 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002578 } else if (sym == XKB_KEY_F4 &&
2579 input->modifiers == MOD_ALT_MASK &&
2580 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2581 if (window->close_handler)
2582 window->close_handler(window->parent,
2583 window->user_data);
2584 else
2585 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002586 } else if (window->key_handler) {
2587 (*window->key_handler)(window, input, time, key,
2588 sym, state, window->user_data);
2589 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002590
2591 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2592 key == input->repeat_key) {
2593 its.it_interval.tv_sec = 0;
2594 its.it_interval.tv_nsec = 0;
2595 its.it_value.tv_sec = 0;
2596 its.it_value.tv_nsec = 0;
2597 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2598 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2599 input->repeat_sym = sym;
2600 input->repeat_key = key;
2601 input->repeat_time = time;
2602 its.it_interval.tv_sec = 0;
2603 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2604 its.it_value.tv_sec = 0;
2605 its.it_value.tv_nsec = 400 * 1000 * 1000;
2606 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2607 }
2608}
2609
2610static void
Daniel Stone351eb612012-05-31 15:27:47 -04002611keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2612 uint32_t serial, uint32_t mods_depressed,
2613 uint32_t mods_latched, uint32_t mods_locked,
2614 uint32_t group)
2615{
2616 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002617 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002618
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002619 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2620 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002621 mask = xkb_state_serialize_mods(input->xkb.state,
2622 XKB_STATE_DEPRESSED |
2623 XKB_STATE_LATCHED);
2624 input->modifiers = 0;
2625 if (mask & input->xkb.control_mask)
2626 input->modifiers |= MOD_CONTROL_MASK;
2627 if (mask & input->xkb.alt_mask)
2628 input->modifiers |= MOD_ALT_MASK;
2629 if (mask & input->xkb.shift_mask)
2630 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002631}
2632
Daniel Stone37816df2012-05-16 18:45:18 +01002633static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002634 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002635 keyboard_handle_enter,
2636 keyboard_handle_leave,
2637 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002638 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002639};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002640
2641static void
Daniel Stone37816df2012-05-16 18:45:18 +01002642seat_handle_capabilities(void *data, struct wl_seat *seat,
2643 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002644{
Daniel Stone37816df2012-05-16 18:45:18 +01002645 struct input *input = data;
2646
2647 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2648 input->pointer = wl_seat_get_pointer(seat);
2649 wl_pointer_set_user_data(input->pointer, input);
2650 wl_pointer_add_listener(input->pointer, &pointer_listener,
2651 input);
2652 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2653 wl_pointer_destroy(input->pointer);
2654 input->pointer = NULL;
2655 }
2656
2657 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2658 input->keyboard = wl_seat_get_keyboard(seat);
2659 wl_keyboard_set_user_data(input->keyboard, input);
2660 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2661 input);
2662 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2663 wl_keyboard_destroy(input->keyboard);
2664 input->keyboard = NULL;
2665 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002666}
2667
Daniel Stone37816df2012-05-16 18:45:18 +01002668static const struct wl_seat_listener seat_listener = {
2669 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002670};
2671
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002672void
2673input_get_position(struct input *input, int32_t *x, int32_t *y)
2674{
2675 *x = input->sx;
2676 *y = input->sy;
2677}
2678
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002679struct display *
2680input_get_display(struct input *input)
2681{
2682 return input->display;
2683}
2684
Daniel Stone37816df2012-05-16 18:45:18 +01002685struct wl_seat *
2686input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002687{
Daniel Stone37816df2012-05-16 18:45:18 +01002688 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002689}
2690
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002691uint32_t
2692input_get_modifiers(struct input *input)
2693{
2694 return input->modifiers;
2695}
2696
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002697struct widget *
2698input_get_focus_widget(struct input *input)
2699{
2700 return input->focus_widget;
2701}
2702
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002703struct data_offer {
2704 struct wl_data_offer *offer;
2705 struct input *input;
2706 struct wl_array types;
2707 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002708
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002709 struct task io_task;
2710 int fd;
2711 data_func_t func;
2712 int32_t x, y;
2713 void *user_data;
2714};
2715
2716static void
2717data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2718{
2719 struct data_offer *offer = data;
2720 char **p;
2721
2722 p = wl_array_add(&offer->types, sizeof *p);
2723 *p = strdup(type);
2724}
2725
2726static const struct wl_data_offer_listener data_offer_listener = {
2727 data_offer_offer,
2728};
2729
2730static void
2731data_offer_destroy(struct data_offer *offer)
2732{
2733 char **p;
2734
2735 offer->refcount--;
2736 if (offer->refcount == 0) {
2737 wl_data_offer_destroy(offer->offer);
2738 for (p = offer->types.data; *p; p++)
2739 free(*p);
2740 wl_array_release(&offer->types);
2741 free(offer);
2742 }
2743}
2744
2745static void
2746data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002747 struct wl_data_device *data_device,
2748 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002749{
2750 struct data_offer *offer;
2751
2752 offer = malloc(sizeof *offer);
2753
2754 wl_array_init(&offer->types);
2755 offer->refcount = 1;
2756 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002757 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002758 wl_data_offer_add_listener(offer->offer,
2759 &data_offer_listener, offer);
2760}
2761
2762static void
2763data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002764 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002765 wl_fixed_t x_w, wl_fixed_t y_w,
2766 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002767{
2768 struct input *input = data;
2769 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002770 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002771 float x = wl_fixed_to_double(x_w);
2772 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002773 char **p;
2774
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002775 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002776 window = wl_surface_get_user_data(surface);
2777 input->pointer_focus = window;
2778
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002779 if (offer) {
2780 input->drag_offer = wl_data_offer_get_user_data(offer);
2781
2782 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2783 *p = NULL;
2784
2785 types_data = input->drag_offer->types.data;
2786 } else {
2787 input->drag_offer = NULL;
2788 types_data = NULL;
2789 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002790
2791 window = input->pointer_focus;
2792 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002793 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002794 window->user_data);
2795}
2796
2797static void
2798data_device_leave(void *data, struct wl_data_device *data_device)
2799{
2800 struct input *input = data;
2801
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002802 if (input->drag_offer) {
2803 data_offer_destroy(input->drag_offer);
2804 input->drag_offer = NULL;
2805 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002806}
2807
2808static void
2809data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002810 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002811{
2812 struct input *input = data;
2813 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002814 float x = wl_fixed_to_double(x_w);
2815 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002816 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002817
2818 input->sx = x;
2819 input->sy = y;
2820
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002821 if (input->drag_offer)
2822 types_data = input->drag_offer->types.data;
2823 else
2824 types_data = NULL;
2825
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002826 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002827 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002828 window->user_data);
2829}
2830
2831static void
2832data_device_drop(void *data, struct wl_data_device *data_device)
2833{
2834 struct input *input = data;
2835 struct window *window = input->pointer_focus;
2836
2837 if (window->drop_handler)
2838 window->drop_handler(window, input,
2839 input->sx, input->sy, window->user_data);
2840}
2841
2842static void
2843data_device_selection(void *data,
2844 struct wl_data_device *wl_data_device,
2845 struct wl_data_offer *offer)
2846{
2847 struct input *input = data;
2848 char **p;
2849
2850 if (input->selection_offer)
2851 data_offer_destroy(input->selection_offer);
2852
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002853 if (offer) {
2854 input->selection_offer = wl_data_offer_get_user_data(offer);
2855 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2856 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002857 } else {
2858 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002859 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002860}
2861
2862static const struct wl_data_device_listener data_device_listener = {
2863 data_device_data_offer,
2864 data_device_enter,
2865 data_device_leave,
2866 data_device_motion,
2867 data_device_drop,
2868 data_device_selection
2869};
2870
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002871static void
2872input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002873{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002874 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002875 struct wl_cursor *cursor;
2876 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002877
Daniel Stone80972742012-11-07 17:51:39 +11002878 if (!input->pointer)
2879 return;
2880
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002881 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002882 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002883 return;
2884
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002885 if (index >= (int) cursor->image_count) {
2886 fprintf(stderr, "cursor index out of range\n");
2887 return;
2888 }
2889
2890 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002891 buffer = wl_cursor_image_get_buffer(image);
2892 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002893 return;
2894
Kristian Høgsbergae277372012-08-01 09:41:08 -04002895 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002896 input->pointer_surface,
2897 image->hotspot_x, image->hotspot_y);
2898 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2899 wl_surface_damage(input->pointer_surface, 0, 0,
2900 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002901 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002902}
2903
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002904static const struct wl_callback_listener pointer_surface_listener;
2905
2906static void
2907pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2908 uint32_t time)
2909{
2910 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002911 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002912 int i;
2913
2914 if (callback) {
2915 assert(callback == input->cursor_frame_cb);
2916 wl_callback_destroy(callback);
2917 input->cursor_frame_cb = NULL;
2918 }
2919
Daniel Stone80972742012-11-07 17:51:39 +11002920 if (!input->pointer)
2921 return;
2922
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002923 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002924 wl_pointer_set_cursor(input->pointer,
2925 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002926 NULL, 0, 0);
2927 return;
2928 }
2929
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002930 if (input->current_cursor == CURSOR_UNSET)
2931 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002932 cursor = input->display->cursors[input->current_cursor];
2933 if (!cursor)
2934 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002935
2936 /* FIXME We don't have the current time on the first call so we set
2937 * the animation start to the time of the first frame callback. */
2938 if (time == 0)
2939 input->cursor_anim_start = 0;
2940 else if (input->cursor_anim_start == 0)
2941 input->cursor_anim_start = time;
2942
2943 if (time == 0 || input->cursor_anim_start == 0)
2944 i = 0;
2945 else
2946 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2947
Pekka Paalanenbc106382012-10-10 12:49:31 +03002948 if (cursor->image_count > 1) {
2949 input->cursor_frame_cb =
2950 wl_surface_frame(input->pointer_surface);
2951 wl_callback_add_listener(input->cursor_frame_cb,
2952 &pointer_surface_listener, input);
2953 }
2954
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002955 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002956}
2957
2958static const struct wl_callback_listener pointer_surface_listener = {
2959 pointer_surface_frame_callback
2960};
2961
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002962void
2963input_set_pointer_image(struct input *input, int pointer)
2964{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002965 int force = 0;
2966
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002967 if (!input->pointer)
2968 return;
2969
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002970 if (input->pointer_enter_serial > input->cursor_serial)
2971 force = 1;
2972
2973 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002974 return;
2975
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002976 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002977 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002978 if (!input->cursor_frame_cb)
2979 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002980 else if (force) {
2981 /* The current frame callback may be stuck if, for instance,
2982 * the set cursor request was processed by the server after
2983 * this client lost the focus. In this case the cursor surface
2984 * might not be mapped and the frame callback wouldn't ever
2985 * complete. Send a set_cursor and attach to try to map the
2986 * cursor surface again so that the callback will finish */
2987 input_set_pointer_image_index(input, 0);
2988 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002989}
2990
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002991struct wl_data_device *
2992input_get_data_device(struct input *input)
2993{
2994 return input->data_device;
2995}
2996
2997void
2998input_set_selection(struct input *input,
2999 struct wl_data_source *source, uint32_t time)
3000{
3001 wl_data_device_set_selection(input->data_device, source, time);
3002}
3003
3004void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003005input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003006{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003007 wl_data_offer_accept(input->drag_offer->offer,
3008 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003009}
3010
3011static void
3012offer_io_func(struct task *task, uint32_t events)
3013{
3014 struct data_offer *offer =
3015 container_of(task, struct data_offer, io_task);
3016 unsigned int len;
3017 char buffer[4096];
3018
3019 len = read(offer->fd, buffer, sizeof buffer);
3020 offer->func(buffer, len,
3021 offer->x, offer->y, offer->user_data);
3022
3023 if (len == 0) {
3024 close(offer->fd);
3025 data_offer_destroy(offer);
3026 }
3027}
3028
3029static void
3030data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3031 data_func_t func, void *user_data)
3032{
3033 int p[2];
3034
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003035 if (pipe2(p, O_CLOEXEC) == -1)
3036 return;
3037
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003038 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3039 close(p[1]);
3040
3041 offer->io_task.run = offer_io_func;
3042 offer->fd = p[0];
3043 offer->func = func;
3044 offer->refcount++;
3045 offer->user_data = user_data;
3046
3047 display_watch_fd(offer->input->display,
3048 offer->fd, EPOLLIN, &offer->io_task);
3049}
3050
3051void
3052input_receive_drag_data(struct input *input, const char *mime_type,
3053 data_func_t func, void *data)
3054{
3055 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3056 input->drag_offer->x = input->sx;
3057 input->drag_offer->y = input->sy;
3058}
3059
3060int
3061input_receive_selection_data(struct input *input, const char *mime_type,
3062 data_func_t func, void *data)
3063{
3064 char **p;
3065
3066 if (input->selection_offer == NULL)
3067 return -1;
3068
3069 for (p = input->selection_offer->types.data; *p; p++)
3070 if (strcmp(mime_type, *p) == 0)
3071 break;
3072
3073 if (*p == NULL)
3074 return -1;
3075
3076 data_offer_receive_data(input->selection_offer,
3077 mime_type, func, data);
3078 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003079}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003080
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003081int
3082input_receive_selection_data_to_fd(struct input *input,
3083 const char *mime_type, int fd)
3084{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003085 if (input->selection_offer)
3086 wl_data_offer_receive(input->selection_offer->offer,
3087 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003088
3089 return 0;
3090}
3091
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003092void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003093window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003094{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003095 if (!window->shell_surface)
3096 return;
3097
Daniel Stone37816df2012-05-16 18:45:18 +01003098 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003099}
3100
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003101static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003102idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003103{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003104 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003105 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003106
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003107 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003108 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003109 widget_set_allocation(widget,
3110 window->pending_allocation.x,
3111 window->pending_allocation.y,
3112 window->pending_allocation.width,
3113 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003114
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003115 if (window->input_region) {
3116 wl_region_destroy(window->input_region);
3117 window->input_region = NULL;
3118 }
3119
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003120 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003121 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003122
3123 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003124
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003125 if (widget->resize_handler)
3126 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003127 widget->allocation.width,
3128 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003129 widget->user_data);
3130
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003131 if (window->allocation.width != widget->allocation.width ||
3132 window->allocation.height != widget->allocation.height) {
3133 window->allocation = widget->allocation;
3134 window_schedule_redraw(window);
3135 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003136
3137 if (widget->opaque)
3138 wl_region_add(window->opaque_region, 0, 0,
3139 widget->allocation.width,
3140 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003141}
3142
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003143void
3144window_schedule_resize(struct window *window, int width, int height)
3145{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003146 window->pending_allocation.x = 0;
3147 window->pending_allocation.y = 0;
3148 window->pending_allocation.width = width;
3149 window->pending_allocation.height = height;
3150
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003151 if (window->min_allocation.width == 0)
3152 window->min_allocation = window->pending_allocation;
3153 if (window->pending_allocation.width < window->min_allocation.width)
3154 window->pending_allocation.width = window->min_allocation.width;
3155 if (window->pending_allocation.height < window->min_allocation.height)
3156 window->pending_allocation.height = window->min_allocation.height;
3157
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003158 window->resize_needed = 1;
3159 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003160}
3161
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003162void
3163widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3164{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003165 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003166}
3167
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003168static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003169handle_ping(void *data, struct wl_shell_surface *shell_surface,
3170 uint32_t serial)
3171{
3172 wl_shell_surface_pong(shell_surface, serial);
3173}
3174
3175static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003176handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003177 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003178{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003179 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003180
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003181 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003182 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003183}
3184
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003185static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003186menu_destroy(struct menu *menu)
3187{
3188 widget_destroy(menu->widget);
3189 window_destroy(menu->window);
3190 free(menu);
3191}
3192
3193static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003194handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3195{
3196 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003197 struct menu *menu = window->widget->user_data;
3198
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003199 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003200 * device. Or just use wl_callback. And this really needs to
3201 * be a window vfunc that the menu can set. And we need the
3202 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003203
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003204 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003205 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003206 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003207}
3208
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003209static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003210 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003211 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003212 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003213};
3214
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003215void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003216window_get_allocation(struct window *window,
3217 struct rectangle *allocation)
3218{
3219 *allocation = window->allocation;
3220}
3221
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003222static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003223widget_redraw(struct widget *widget)
3224{
3225 struct widget *child;
3226
3227 if (widget->redraw_handler)
3228 widget->redraw_handler(widget, widget->user_data);
3229 wl_list_for_each(child, &widget->child_list, link)
3230 widget_redraw(child);
3231}
3232
3233static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003234frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3235{
3236 struct window *window = data;
3237
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003238 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003239 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003240 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003241 window->redraw_scheduled = 0;
3242 if (window->redraw_needed)
3243 window_schedule_redraw(window);
3244}
3245
3246static const struct wl_callback_listener listener = {
3247 frame_callback
3248};
3249
3250static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003251idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003252{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003253 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003254
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003255 if (window->resize_needed)
3256 idle_resize(window);
3257
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003258 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003259 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003260 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003261 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003262
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003263 window->frame_cb = wl_surface_frame(window->surface);
3264 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003265 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003266}
3267
3268void
3269window_schedule_redraw(struct window *window)
3270{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003271 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003272 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003273 window->redraw_task.run = idle_redraw;
3274 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003275 window->redraw_scheduled = 1;
3276 }
3277}
3278
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003279int
3280window_is_fullscreen(struct window *window)
3281{
3282 return window->type == TYPE_FULLSCREEN;
3283}
3284
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003285void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003286window_set_fullscreen(struct window *window, int fullscreen)
3287{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003288 if (!window->display->shell)
3289 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003290
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003291 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003292 return;
3293
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003294 if (fullscreen) {
3295 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003296 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003297 wl_shell_surface_set_fullscreen(window->shell_surface,
3298 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3299 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003300 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003301 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003302 wl_shell_surface_set_toplevel(window->shell_surface);
3303 window_schedule_resize(window,
3304 window->saved_allocation.width,
3305 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003306 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003307}
3308
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003309int
3310window_is_maximized(struct window *window)
3311{
3312 return window->type == TYPE_MAXIMIZED;
3313}
3314
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003315void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003316window_set_maximized(struct window *window, int maximized)
3317{
3318 if (!window->display->shell)
3319 return;
3320
3321 if ((window->type == TYPE_MAXIMIZED) == maximized)
3322 return;
3323
3324 if (window->type == TYPE_TOPLEVEL) {
3325 window->saved_allocation = window->allocation;
3326 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3327 window->type = TYPE_MAXIMIZED;
3328 } else {
3329 wl_shell_surface_set_toplevel(window->shell_surface);
3330 window->type = TYPE_TOPLEVEL;
3331 window_schedule_resize(window,
3332 window->saved_allocation.width,
3333 window->saved_allocation.height);
3334 }
3335}
3336
3337void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003338window_set_user_data(struct window *window, void *data)
3339{
3340 window->user_data = data;
3341}
3342
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003343void *
3344window_get_user_data(struct window *window)
3345{
3346 return window->user_data;
3347}
3348
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003349void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003350window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003351 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003352{
3353 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003354}
3355
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003356void
3357window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003358 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003359{
3360 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003361}
3362
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003363void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003364window_set_data_handler(struct window *window, window_data_handler_t handler)
3365{
3366 window->data_handler = handler;
3367}
3368
3369void
3370window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3371{
3372 window->drop_handler = handler;
3373}
3374
3375void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003376window_set_close_handler(struct window *window,
3377 window_close_handler_t handler)
3378{
3379 window->close_handler = handler;
3380}
3381
3382void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003383window_set_fullscreen_handler(struct window *window,
3384 window_fullscreen_handler_t handler)
3385{
3386 window->fullscreen_handler = handler;
3387}
3388
3389void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003390window_set_title(struct window *window, const char *title)
3391{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003392 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003393 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003394 if (window->shell_surface)
3395 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003396}
3397
3398const char *
3399window_get_title(struct window *window)
3400{
3401 return window->title;
3402}
3403
3404void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003405window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3406{
3407 struct text_cursor_position *text_cursor_position =
3408 window->display->text_cursor_position;
3409
Scott Moreau9295ce02012-06-01 12:46:10 -06003410 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003411 return;
3412
3413 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003414 window->surface,
3415 wl_fixed_from_int(x),
3416 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003417}
3418
3419void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003420window_damage(struct window *window, int32_t x, int32_t y,
3421 int32_t width, int32_t height)
3422{
3423 wl_surface_damage(window->surface, x, y, width, height);
3424}
3425
Casey Dahlin9074db52012-04-19 22:50:09 -04003426static void
3427surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003428 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003429{
Rob Bradford7507b572012-05-15 17:55:34 +01003430 struct window *window = data;
3431 struct output *output;
3432 struct output *output_found = NULL;
3433 struct window_output *window_output;
3434
3435 wl_list_for_each(output, &window->display->output_list, link) {
3436 if (output->output == wl_output) {
3437 output_found = output;
3438 break;
3439 }
3440 }
3441
3442 if (!output_found)
3443 return;
3444
3445 window_output = malloc (sizeof *window_output);
3446 window_output->output = output_found;
3447
3448 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003449}
3450
3451static void
3452surface_leave(void *data,
3453 struct wl_surface *wl_surface, struct wl_output *output)
3454{
Rob Bradford7507b572012-05-15 17:55:34 +01003455 struct window *window = data;
3456 struct window_output *window_output;
3457 struct window_output *window_output_found = NULL;
3458
3459 wl_list_for_each(window_output, &window->window_output_list, link) {
3460 if (window_output->output->output == output) {
3461 window_output_found = window_output;
3462 break;
3463 }
3464 }
3465
3466 if (window_output_found) {
3467 wl_list_remove(&window_output_found->link);
3468 free(window_output_found);
3469 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003470}
3471
3472static const struct wl_surface_listener surface_listener = {
3473 surface_enter,
3474 surface_leave
3475};
3476
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003477static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003478window_create_internal(struct display *display,
3479 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003480{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003481 struct window *window;
3482
3483 window = malloc(sizeof *window);
3484 if (window == NULL)
3485 return NULL;
3486
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003487 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003488 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003489 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003490 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003491 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003492 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003493 window->shell_surface =
3494 wl_shell_get_shell_surface(display->shell,
3495 window->surface);
3496 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003497 window->allocation.x = 0;
3498 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003499 window->allocation.width = 0;
3500 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003501 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003502 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003503 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003504 window->input_region = NULL;
3505 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003506
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003507 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003508#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003509 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003510#else
3511 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3512#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003513 else
3514 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003515
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003516 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003517 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003518 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003519
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003520 if (window->shell_surface) {
3521 wl_shell_surface_set_user_data(window->shell_surface, window);
3522 wl_shell_surface_add_listener(window->shell_surface,
3523 &shell_surface_listener, window);
3524 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003525
Rob Bradford7507b572012-05-15 17:55:34 +01003526 wl_list_init (&window->window_output_list);
3527
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003528 return window;
3529}
3530
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003531struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003532window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003533{
3534 struct window *window;
3535
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003536 window = window_create_internal(display, NULL, TYPE_NONE);
3537 if (!window)
3538 return NULL;
3539
3540 return window;
3541}
3542
3543struct window *
3544window_create_custom(struct display *display)
3545{
3546 struct window *window;
3547
3548 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003549 if (!window)
3550 return NULL;
3551
3552 return window;
3553}
3554
3555struct window *
3556window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003557 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003558{
3559 struct window *window;
3560
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003561 window = window_create_internal(parent->display,
3562 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003563 if (!window)
3564 return NULL;
3565
3566 window->x = x;
3567 window->y = y;
3568
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003569 if (display->shell)
3570 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003571 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003572 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003573
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003574 return window;
3575}
3576
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003577static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003578menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003579{
3580 int next;
3581
3582 next = (sy - 8) / 20;
3583 if (menu->current != next) {
3584 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003585 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003586 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003587}
3588
3589static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003590menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003591 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003592 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003593{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003594 struct menu *menu = data;
3595
3596 if (widget == menu->widget)
3597 menu_set_item(data, y);
3598
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003599 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003600}
3601
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003602static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003603menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003604 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003605{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003606 struct menu *menu = data;
3607
3608 if (widget == menu->widget)
3609 menu_set_item(data, y);
3610
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003611 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003612}
3613
3614static void
3615menu_leave_handler(struct widget *widget, struct input *input, void *data)
3616{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003617 struct menu *menu = data;
3618
3619 if (widget == menu->widget)
3620 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003621}
3622
3623static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003624menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003625 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003626 uint32_t button, enum wl_pointer_button_state state,
3627 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003628
3629{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003630 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003631
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003632 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3633 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003634 /* Either relase after press-drag-release or
3635 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003636 menu->func(menu->window->parent,
3637 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003638 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003639 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003640 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003641 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003642 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003643}
3644
3645static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003646menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003647{
3648 cairo_t *cr;
3649 const int32_t r = 3, margin = 3;
3650 struct menu *menu = data;
3651 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003652 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003653
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003654 cr = cairo_create(window->cairo_surface);
3655 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3656 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3657 cairo_paint(cr);
3658
3659 width = window->allocation.width;
3660 height = window->allocation.height;
3661 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003662
3663 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003664 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3665 cairo_fill(cr);
3666
3667 for (i = 0; i < menu->count; i++) {
3668 if (i == menu->current) {
3669 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3670 cairo_rectangle(cr, margin, i * 20 + margin,
3671 width - 2 * margin, 20);
3672 cairo_fill(cr);
3673 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3674 cairo_move_to(cr, 10, i * 20 + 16);
3675 cairo_show_text(cr, menu->entries[i]);
3676 } else {
3677 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3678 cairo_move_to(cr, 10, i * 20 + 16);
3679 cairo_show_text(cr, menu->entries[i]);
3680 }
3681 }
3682
3683 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003684}
3685
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003686void
3687window_show_menu(struct display *display,
3688 struct input *input, uint32_t time, struct window *parent,
3689 int32_t x, int32_t y,
3690 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003691{
3692 struct window *window;
3693 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003694 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003695
3696 menu = malloc(sizeof *menu);
3697 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003698 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003699
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003700 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003701 if (!window) {
3702 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003703 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003704 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003705
3706 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003707 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003708 menu->entries = entries;
3709 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003710 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003711 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003712 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003713 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003714 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003715 window->type = TYPE_MENU;
3716 window->x = x;
3717 window->y = y;
3718
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003719 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003720 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003721 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003722 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003723 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003724
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003725 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003726 widget_set_enter_handler(menu->widget, menu_enter_handler);
3727 widget_set_leave_handler(menu->widget, menu_leave_handler);
3728 widget_set_motion_handler(menu->widget, menu_motion_handler);
3729 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003730
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003731 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003732 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003733}
3734
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003735void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003736window_set_buffer_type(struct window *window, enum window_buffer_type type)
3737{
3738 window->buffer_type = type;
3739}
3740
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003741
3742static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003743display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003744 struct wl_output *wl_output,
3745 int x, int y,
3746 int physical_width,
3747 int physical_height,
3748 int subpixel,
3749 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003750 const char *model,
3751 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003752{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003753 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003754
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003755 output->allocation.x = x;
3756 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003757 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003758}
3759
3760static void
3761display_handle_mode(void *data,
3762 struct wl_output *wl_output,
3763 uint32_t flags,
3764 int width,
3765 int height,
3766 int refresh)
3767{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003768 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003769 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003770
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003771 if (flags & WL_OUTPUT_MODE_CURRENT) {
3772 output->allocation.width = width;
3773 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003774 if (display->output_configure_handler)
3775 (*display->output_configure_handler)(
3776 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003777 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003778}
3779
3780static const struct wl_output_listener output_listener = {
3781 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003782 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003783};
3784
3785static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003786display_add_output(struct display *d, uint32_t id)
3787{
3788 struct output *output;
3789
3790 output = malloc(sizeof *output);
3791 if (output == NULL)
3792 return;
3793
3794 memset(output, 0, sizeof *output);
3795 output->display = d;
3796 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003797 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003798 wl_list_insert(d->output_list.prev, &output->link);
3799
3800 wl_output_add_listener(output->output, &output_listener, output);
3801}
3802
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003803static void
3804output_destroy(struct output *output)
3805{
3806 if (output->destroy_handler)
3807 (*output->destroy_handler)(output, output->user_data);
3808
3809 wl_output_destroy(output->output);
3810 wl_list_remove(&output->link);
3811 free(output);
3812}
3813
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003814void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003815display_set_global_handler(struct display *display,
3816 display_global_handler_t handler)
3817{
3818 struct global *global;
3819
3820 display->global_handler = handler;
3821 if (!handler)
3822 return;
3823
3824 wl_list_for_each(global, &display->global_list, link)
3825 display->global_handler(display,
3826 global->name, global->interface,
3827 global->version, display->user_data);
3828}
3829
3830void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003831display_set_output_configure_handler(struct display *display,
3832 display_output_handler_t handler)
3833{
3834 struct output *output;
3835
3836 display->output_configure_handler = handler;
3837 if (!handler)
3838 return;
3839
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003840 wl_list_for_each(output, &display->output_list, link) {
3841 if (output->allocation.width == 0 &&
3842 output->allocation.height == 0)
3843 continue;
3844
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003845 (*display->output_configure_handler)(output,
3846 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003847 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003848}
3849
3850void
3851output_set_user_data(struct output *output, void *data)
3852{
3853 output->user_data = data;
3854}
3855
3856void *
3857output_get_user_data(struct output *output)
3858{
3859 return output->user_data;
3860}
3861
3862void
3863output_set_destroy_handler(struct output *output,
3864 display_output_handler_t handler)
3865{
3866 output->destroy_handler = handler;
3867 /* FIXME: implement this, once we have way to remove outputs */
3868}
3869
3870void
Scott Moreau4e072362012-09-29 02:03:11 -06003871output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003872{
Scott Moreau4e072362012-09-29 02:03:11 -06003873 struct rectangle allocation = output->allocation;
3874
3875 switch (output->transform) {
3876 case WL_OUTPUT_TRANSFORM_90:
3877 case WL_OUTPUT_TRANSFORM_270:
3878 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3879 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3880 /* Swap width and height */
3881 allocation.width = output->allocation.height;
3882 allocation.height = output->allocation.width;
3883 break;
3884 }
3885
3886 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003887}
3888
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003889struct wl_output *
3890output_get_wl_output(struct output *output)
3891{
3892 return output->output;
3893}
3894
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003895static void
Daniel Stone97f68542012-05-30 16:32:01 +01003896fini_xkb(struct input *input)
3897{
3898 xkb_state_unref(input->xkb.state);
3899 xkb_map_unref(input->xkb.keymap);
3900}
3901
3902static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003903display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003904{
3905 struct input *input;
3906
3907 input = malloc(sizeof *input);
3908 if (input == NULL)
3909 return;
3910
3911 memset(input, 0, sizeof *input);
3912 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003913 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003914 input->pointer_focus = NULL;
3915 input->keyboard_focus = NULL;
3916 wl_list_insert(d->input_list.prev, &input->link);
3917
Daniel Stone37816df2012-05-16 18:45:18 +01003918 wl_seat_add_listener(input->seat, &seat_listener, input);
3919 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003920
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003921 input->data_device =
3922 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003923 input->seat);
3924 wl_data_device_add_listener(input->data_device, &data_device_listener,
3925 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003926
3927 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003928
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003929 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3930 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003931 input->repeat_task.run = keyboard_repeat_func;
3932 display_watch_fd(d, input->repeat_timer_fd,
3933 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003934}
3935
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003936static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003937input_destroy(struct input *input)
3938{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003939 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003940 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003941
3942 if (input->drag_offer)
3943 data_offer_destroy(input->drag_offer);
3944
3945 if (input->selection_offer)
3946 data_offer_destroy(input->selection_offer);
3947
3948 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003949 fini_xkb(input);
3950
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003951 wl_surface_destroy(input->pointer_surface);
3952
Pekka Paalanene1207c72011-12-16 12:02:09 +02003953 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003954 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003955 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003956 free(input);
3957}
3958
3959static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003960init_workspace_manager(struct display *d, uint32_t id)
3961{
3962 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003963 wl_registry_bind(d->registry, id,
3964 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003965 if (d->workspace_manager != NULL)
3966 workspace_manager_add_listener(d->workspace_manager,
3967 &workspace_manager_listener,
3968 d);
3969}
3970
3971static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003972registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3973 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003974{
3975 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003976 struct global *global;
3977
3978 global = malloc(sizeof *global);
3979 global->name = id;
3980 global->interface = strdup(interface);
3981 global->version = version;
3982 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003983
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003984 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003985 d->compositor = wl_registry_bind(registry, id,
3986 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003987 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003988 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003989 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003990 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003991 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003992 d->shell = wl_registry_bind(registry,
3993 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003994 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003995 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003996 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3997 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003998 wl_registry_bind(registry, id,
3999 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004000 } else if (strcmp(interface, "text_cursor_position") == 0) {
4001 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004002 wl_registry_bind(registry, id,
4003 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004004 } else if (strcmp(interface, "workspace_manager") == 0) {
4005 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004006 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004007
4008 if (d->global_handler)
4009 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004010}
4011
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004012void *
4013display_bind(struct display *display, uint32_t name,
4014 const struct wl_interface *interface, uint32_t version)
4015{
4016 return wl_registry_bind(display->registry, name, interface, version);
4017}
4018
4019static const struct wl_registry_listener registry_listener = {
4020 registry_handle_global
4021};
4022
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004023#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004024static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004025init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004026{
4027 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004028 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004029
Rob Clark6396ed32012-03-11 19:48:41 -05004030#ifdef USE_CAIRO_GLESV2
4031# define GL_BIT EGL_OPENGL_ES2_BIT
4032#else
4033# define GL_BIT EGL_OPENGL_BIT
4034#endif
4035
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004036 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004037 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004038 EGL_RED_SIZE, 1,
4039 EGL_GREEN_SIZE, 1,
4040 EGL_BLUE_SIZE, 1,
4041 EGL_ALPHA_SIZE, 1,
4042 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004043 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004044 EGL_NONE
4045 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004046
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004047#ifdef USE_CAIRO_GLESV2
4048 static const EGLint context_attribs[] = {
4049 EGL_CONTEXT_CLIENT_VERSION, 2,
4050 EGL_NONE
4051 };
4052 EGLint api = EGL_OPENGL_ES_API;
4053#else
4054 EGLint *context_attribs = NULL;
4055 EGLint api = EGL_OPENGL_API;
4056#endif
4057
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004058 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004059 if (!eglInitialize(d->dpy, &major, &minor)) {
4060 fprintf(stderr, "failed to initialize display\n");
4061 return -1;
4062 }
4063
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004064 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004065 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
4066 return -1;
4067 }
4068
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004069 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4070 &d->argb_config, 1, &n) || n != 1) {
4071 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004072 return -1;
4073 }
4074
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004075 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004076 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004077 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004078 fprintf(stderr, "failed to create context\n");
4079 return -1;
4080 }
4081
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004082 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01004083 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004084 return -1;
4085 }
4086
Benjamin Franzke0c991632011-09-27 21:57:31 +02004087 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4088 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4089 fprintf(stderr, "failed to get cairo egl argb device\n");
4090 return -1;
4091 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004092
4093 return 0;
4094}
4095
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004096static void
4097fini_egl(struct display *display)
4098{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004099 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004100
4101 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4102 EGL_NO_CONTEXT);
4103
4104 eglTerminate(display->dpy);
4105 eglReleaseThread();
4106}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004107#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004108
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004109static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004110init_dummy_surface(struct display *display)
4111{
4112 int len;
4113 void *data;
4114
4115 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4116 data = malloc(len);
4117 display->dummy_surface =
4118 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4119 1, 1, len);
4120 display->dummy_surface_data = data;
4121}
4122
4123static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004124handle_display_data(struct task *task, uint32_t events)
4125{
4126 struct display *display =
4127 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004128 struct epoll_event ep;
4129 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004130
4131 display->display_fd_events = events;
4132
4133 if (events & EPOLLERR || events & EPOLLHUP) {
4134 display_exit(display);
4135 return;
4136 }
4137
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004138 if (events & EPOLLIN) {
4139 ret = wl_display_dispatch(display->display);
4140 if (ret == -1) {
4141 display_exit(display);
4142 return;
4143 }
4144 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004145
4146 if (events & EPOLLOUT) {
4147 ret = wl_display_flush(display->display);
4148 if (ret == 0) {
4149 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4150 ep.data.ptr = &display->display_task;
4151 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4152 display->display_fd, &ep);
4153 } else if (ret == -1 && errno != EAGAIN) {
4154 display_exit(display);
4155 return;
4156 }
4157 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004158}
4159
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004160struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004161display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004162{
4163 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004164
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004165 d = malloc(sizeof *d);
4166 if (d == NULL)
4167 return NULL;
4168
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004169 memset(d, 0, sizeof *d);
4170
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004171 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004172 if (d->display == NULL) {
4173 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004174 return NULL;
4175 }
4176
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004177 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004178 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004179 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004180 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4181 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004182
4183 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004184 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004185 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004186 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004187
Daniel Stone97f68542012-05-30 16:32:01 +01004188 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004189 if (d->xkb_context == NULL) {
4190 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004191 return NULL;
4192 }
4193
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004194 d->workspace = 0;
4195 d->workspace_count = 1;
4196
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004197 d->registry = wl_display_get_registry(d->display);
4198 wl_registry_add_listener(d->registry, &registry_listener, d);
4199 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004200#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004201 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004202 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004203#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004204
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004205 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004206
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004207 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004208
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004209 wl_list_init(&d->window_list);
4210
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004211 init_dummy_surface(d);
4212
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004213 return d;
4214}
4215
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004216static void
4217display_destroy_outputs(struct display *display)
4218{
4219 struct output *tmp;
4220 struct output *output;
4221
4222 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4223 output_destroy(output);
4224}
4225
Pekka Paalanene1207c72011-12-16 12:02:09 +02004226static void
4227display_destroy_inputs(struct display *display)
4228{
4229 struct input *tmp;
4230 struct input *input;
4231
4232 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4233 input_destroy(input);
4234}
4235
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004236void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004237display_destroy(struct display *display)
4238{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004239 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004240 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4241 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004242
4243 if (!wl_list_empty(&display->deferred_list))
4244 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4245
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004246 cairo_surface_destroy(display->dummy_surface);
4247 free(display->dummy_surface_data);
4248
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004249 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004250 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004251
Daniel Stone97f68542012-05-30 16:32:01 +01004252 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004253
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004254 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004255 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004256
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004257#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004258 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004259#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004260
Pekka Paalanenc2052982011-12-16 11:41:32 +02004261 if (display->shell)
4262 wl_shell_destroy(display->shell);
4263
4264 if (display->shm)
4265 wl_shm_destroy(display->shm);
4266
4267 if (display->data_device_manager)
4268 wl_data_device_manager_destroy(display->data_device_manager);
4269
4270 wl_compositor_destroy(display->compositor);
4271
4272 close(display->epoll_fd);
4273
U. Artie Eoff44874d92012-10-02 21:12:35 -07004274 if (!(display->display_fd_events & EPOLLERR) &&
4275 !(display->display_fd_events & EPOLLHUP))
4276 wl_display_flush(display->display);
4277
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004278 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004279 free(display);
4280}
4281
4282void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004283display_set_user_data(struct display *display, void *data)
4284{
4285 display->user_data = data;
4286}
4287
4288void *
4289display_get_user_data(struct display *display)
4290{
4291 return display->user_data;
4292}
4293
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004294struct wl_display *
4295display_get_display(struct display *display)
4296{
4297 return display->display;
4298}
4299
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004300struct output *
4301display_get_output(struct display *display)
4302{
4303 return container_of(display->output_list.next, struct output, link);
4304}
4305
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004306struct wl_compositor *
4307display_get_compositor(struct display *display)
4308{
4309 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004310}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004311
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004312uint32_t
4313display_get_serial(struct display *display)
4314{
4315 return display->serial;
4316}
4317
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004318EGLDisplay
4319display_get_egl_display(struct display *d)
4320{
4321 return d->dpy;
4322}
4323
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004324struct wl_data_source *
4325display_create_data_source(struct display *display)
4326{
4327 return wl_data_device_manager_create_data_source(display->data_device_manager);
4328}
4329
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004330EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004331display_get_argb_egl_config(struct display *d)
4332{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004333 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004334}
4335
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004336struct wl_shell *
4337display_get_shell(struct display *display)
4338{
4339 return display->shell;
4340}
4341
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004342int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004343display_acquire_window_surface(struct display *display,
4344 struct window *window,
4345 EGLContext ctx)
4346{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004347 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004348 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004349
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004350 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004351}
4352
4353void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004354display_release_window_surface(struct display *display,
4355 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004356{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004357 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004358 return;
4359
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004360 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004361}
4362
4363void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004364display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004365{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004366 wl_list_insert(&display->deferred_list, &task->link);
4367}
4368
4369void
4370display_watch_fd(struct display *display,
4371 int fd, uint32_t events, struct task *task)
4372{
4373 struct epoll_event ep;
4374
4375 ep.events = events;
4376 ep.data.ptr = task;
4377 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4378}
4379
4380void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004381display_unwatch_fd(struct display *display, int fd)
4382{
4383 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4384}
4385
4386void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004387display_run(struct display *display)
4388{
4389 struct task *task;
4390 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004391 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004392
Pekka Paalanen826d7952011-12-15 10:14:07 +02004393 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004394 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004395 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004396 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004397 struct task, link);
4398 wl_list_remove(&task->link);
4399 task->run(task, 0);
4400 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004401
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004402 wl_display_dispatch_pending(display->display);
4403
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004404 if (!display->running)
4405 break;
4406
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004407 ret = wl_display_flush(display->display);
4408 if (ret < 0 && errno == EAGAIN) {
4409 ep[0].events =
4410 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4411 ep[0].data.ptr = &display->display_task;
4412
4413 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4414 display->display_fd, &ep[0]);
4415 } else if (ret < 0) {
4416 break;
4417 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004418
4419 count = epoll_wait(display->epoll_fd,
4420 ep, ARRAY_LENGTH(ep), -1);
4421 for (i = 0; i < count; i++) {
4422 task = ep[i].data.ptr;
4423 task->run(task, ep[i].events);
4424 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004425 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004426}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004427
4428void
4429display_exit(struct display *display)
4430{
4431 display->running = 0;
4432}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004433
4434void
4435keysym_modifiers_add(struct wl_array *modifiers_map,
4436 const char *name)
4437{
4438 size_t len = strlen(name) + 1;
4439 char *p;
4440
4441 p = wl_array_add(modifiers_map, len);
4442
4443 if (p == NULL)
4444 return;
4445
4446 strncpy(p, name, len);
4447}
4448
4449static xkb_mod_index_t
4450keysym_modifiers_get_index(struct wl_array *modifiers_map,
4451 const char *name)
4452{
4453 xkb_mod_index_t index = 0;
4454 char *p = modifiers_map->data;
4455
4456 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4457 if (strcmp(p, name) == 0)
4458 return index;
4459
4460 index++;
4461 p += strlen(p) + 1;
4462 }
4463
4464 return XKB_MOD_INVALID;
4465}
4466
4467xkb_mod_mask_t
4468keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4469 const char *name)
4470{
4471 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4472
4473 if (index == XKB_MOD_INVALID)
4474 return XKB_MOD_INVALID;
4475
4476 return 1 << index;
4477}