blob: e3e8eb5948b61e704f89be1d29c62f99ddb54bae [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 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
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030043#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050044#include <wayland-egl.h>
45
Rob Clark6396ed32012-03-11 19:48:41 -050046#ifdef USE_CAIRO_GLESV2
47#include <GLES2/gl2.h>
48#include <GLES2/gl2ext.h>
49#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050051#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <EGL/egl.h>
53#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040054
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030056#else /* HAVE_CAIRO_EGL */
57typedef void *EGLDisplay;
58typedef void *EGLConfig;
59typedef void *EGLContext;
60#define EGL_NO_DISPLAY ((EGLDisplay)0)
61#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050062
Daniel Stone9d4f0302012-02-15 16:33:21 +000063#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030064#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040065
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020067#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040068#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060069#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020070#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030071#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050074
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070075struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030076
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040077struct global {
78 uint32_t name;
79 char *interface;
80 uint32_t version;
81 struct wl_list link;
82};
83
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040086 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050087 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040088 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040093 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050094 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020095 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040097 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040098
99 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700100 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400101 struct task display_task;
102
103 int epoll_fd;
104 struct wl_list deferred_list;
105
Pekka Paalanen826d7952011-12-15 10:14:07 +0200106 int running;
107
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400108 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400109 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400110 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500111 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400112
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400113 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400114
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300115 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300116 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400117
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200118 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400119 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120
121 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100122
123 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200124
125 uint32_t workspace;
126 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200127
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500131};
132
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400133enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400134 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400136 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500137 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400138 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500139 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_CUSTOM
141};
Rob Bradford7507b572012-05-15 17:55:34 +0100142
143struct window_output {
144 struct output *output;
145 struct wl_list link;
146};
147
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200148struct toysurface {
149 /*
150 * Prepare the surface for drawing. Makes sure there is a surface
151 * of the right size available for rendering, and returns it.
152 * dx,dy are the x,y of wl_surface.attach.
153 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200154 * If flags has SURFACE_HINT_RESIZE set, the user is
155 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200156 * Returns the Cairo surface to draw to.
157 */
158 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200159 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200160
161 /*
162 * Post the surface to the server, returning the server allocation
163 * rectangle. The Cairo surface from prepare() must be destroyed
164 * after calling this.
165 */
166 void (*swap)(struct toysurface *base,
167 struct rectangle *server_allocation);
168
169 /*
170 * Make the toysurface current with the given EGL context.
171 * Returns 0 on success, and negative of failure.
172 */
173 int (*acquire)(struct toysurface *base, EGLContext ctx);
174
175 /*
176 * Release the toysurface from the EGL context, returning control
177 * to Cairo.
178 */
179 void (*release)(struct toysurface *base);
180
181 /*
182 * Destroy the toysurface, including the Cairo surface, any
183 * backing storage, and the Wayland protocol objects.
184 */
185 void (*destroy)(struct toysurface *base);
186};
187
Pekka Paalanen4e373742013-02-13 16:17:13 +0200188struct surface {
189 struct window *window;
190
191 struct wl_surface *surface;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200192 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200193 struct widget *widget;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200194
195 struct rectangle allocation;
196 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200197
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200198 struct wl_region *input_region;
199 struct wl_region *opaque_region;
200
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200201 enum window_buffer_type buffer_type;
202 enum wl_output_transform buffer_transform;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200203
204 cairo_surface_t *cairo_surface;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200205};
206
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500207struct window {
208 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500209 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100210 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500211 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200212 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400213 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500214 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500215 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400216 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400217 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400218 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400219 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400220 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100221 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400222 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400223 int focus_count;
224
Pekka Paalanen99436862012-11-19 17:15:59 +0200225 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200226 int fullscreen_method;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400227
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500228 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500229 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400230 window_data_handler_t data_handler;
231 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500232 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400233 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200234 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400235
Pekka Paalanen4e373742013-02-13 16:17:13 +0200236 struct surface *main_surface;
237 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300238 struct wl_callback *frame_cb;
239
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200240 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500241
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500242 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400243 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500244};
245
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500246struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500247 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200248 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300249 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500250 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400251 struct wl_list link;
252 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500253 widget_resize_handler_t resize_handler;
254 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500255 widget_enter_handler_t enter_handler;
256 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500257 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500258 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200259 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400260 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500261 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300262 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500263 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400264};
265
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400266struct input {
267 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100268 struct wl_seat *seat;
269 struct wl_pointer *pointer;
270 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400271 struct window *pointer_focus;
272 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300273 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300274 uint32_t cursor_anim_start;
275 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300276 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400277 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400278 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400279 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400280 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400281 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400282
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500283 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500284 struct widget *grab;
285 uint32_t grab_button;
286
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400287 struct wl_data_device *data_device;
288 struct data_offer *drag_offer;
289 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100290
291 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100292 struct xkb_keymap *keymap;
293 struct xkb_state *state;
294 xkb_mod_mask_t control_mask;
295 xkb_mod_mask_t alt_mask;
296 xkb_mod_mask_t shift_mask;
297 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400298
299 struct task repeat_task;
300 int repeat_timer_fd;
301 uint32_t repeat_sym;
302 uint32_t repeat_key;
303 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400304};
305
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500306struct output {
307 struct display *display;
308 struct wl_output *output;
309 struct rectangle allocation;
310 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600311 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200312
313 display_output_handler_t destroy_handler;
314 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500315};
316
Martin Minarik1998b152012-05-10 02:04:35 +0200317enum frame_button_action {
318 FRAME_BUTTON_NULL = 0,
319 FRAME_BUTTON_ICON = 1,
320 FRAME_BUTTON_CLOSE = 2,
321 FRAME_BUTTON_MINIMIZE = 3,
322 FRAME_BUTTON_MAXIMIZE = 4,
323};
324
325enum frame_button_pointer {
326 FRAME_BUTTON_DEFAULT = 0,
327 FRAME_BUTTON_OVER = 1,
328 FRAME_BUTTON_ACTIVE = 2,
329};
330
331enum frame_button_align {
332 FRAME_BUTTON_RIGHT = 0,
333 FRAME_BUTTON_LEFT = 1,
334};
335
336enum frame_button_decoration {
337 FRAME_BUTTON_NONE = 0,
338 FRAME_BUTTON_FANCY = 1,
339};
340
341struct frame_button {
342 struct widget *widget;
343 struct frame *frame;
344 cairo_surface_t *icon;
345 enum frame_button_action type;
346 enum frame_button_pointer state;
347 struct wl_list link; /* buttons_list */
348 enum frame_button_align align;
349 enum frame_button_decoration decoration;
350};
351
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500352struct frame {
353 struct widget *widget;
354 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200355 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500356};
357
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500358struct menu {
359 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500360 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500361 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500362 const char **entries;
363 uint32_t time;
364 int current;
365 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400366 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500367 menu_func_t func;
368};
369
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300370struct tooltip {
371 struct widget *parent;
372 struct window *window;
373 struct widget *widget;
374 char *entry;
375 struct task tooltip_task;
376 int tooltip_fd;
377 float x, y;
378};
379
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700380struct shm_pool {
381 struct wl_shm_pool *pool;
382 size_t size;
383 size_t used;
384 void *data;
385};
386
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400387enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300388 CURSOR_DEFAULT = 100,
389 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400390};
391
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500392enum window_location {
393 WINDOW_INTERIOR = 0,
394 WINDOW_RESIZING_TOP = 1,
395 WINDOW_RESIZING_BOTTOM = 2,
396 WINDOW_RESIZING_LEFT = 4,
397 WINDOW_RESIZING_TOP_LEFT = 5,
398 WINDOW_RESIZING_BOTTOM_LEFT = 6,
399 WINDOW_RESIZING_RIGHT = 8,
400 WINDOW_RESIZING_TOP_RIGHT = 9,
401 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
402 WINDOW_RESIZING_MASK = 15,
403 WINDOW_EXTERIOR = 16,
404 WINDOW_TITLEBAR = 17,
405 WINDOW_CLIENT_AREA = 18,
406};
407
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200408static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500410#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200412struct egl_window_surface {
413 struct toysurface base;
414 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100415 struct display *display;
416 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200417 struct wl_egl_window *egl_window;
418 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100419};
420
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200421static struct egl_window_surface *
422to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100423{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200424 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100425}
426
427static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200428egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200429 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100430{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200431 struct egl_window_surface *surface = to_egl_window_surface(base);
432
433 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
434 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
435
436 return cairo_surface_reference(surface->cairo_surface);
437}
438
439static void
440egl_window_surface_swap(struct toysurface *base,
441 struct rectangle *server_allocation)
442{
443 struct egl_window_surface *surface = to_egl_window_surface(base);
444
445 cairo_gl_surface_swapbuffers(surface->cairo_surface);
446 wl_egl_window_get_attached_size(surface->egl_window,
447 &server_allocation->width,
448 &server_allocation->height);
449}
450
451static int
452egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
453{
454 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200455 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100456
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200457 device = cairo_surface_get_device(surface->cairo_surface);
458 if (!device)
459 return -1;
460
461 if (!ctx) {
462 if (device == surface->display->argb_device)
463 ctx = surface->display->argb_ctx;
464 else
465 assert(0);
466 }
467
468 cairo_device_flush(device);
469 cairo_device_acquire(device);
470 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
471 surface->egl_surface, ctx))
472 fprintf(stderr, "failed to make surface current\n");
473
474 return 0;
475}
476
477static void
478egl_window_surface_release(struct toysurface *base)
479{
480 struct egl_window_surface *surface = to_egl_window_surface(base);
481 cairo_device_t *device;
482
483 device = cairo_surface_get_device(surface->cairo_surface);
484 if (!device)
485 return;
486
487 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
488 surface->display->argb_ctx))
489 fprintf(stderr, "failed to make context current\n");
490
491 cairo_device_release(device);
492}
493
494static void
495egl_window_surface_destroy(struct toysurface *base)
496{
497 struct egl_window_surface *surface = to_egl_window_surface(base);
498 struct display *d = surface->display;
499
500 cairo_surface_destroy(surface->cairo_surface);
501 eglDestroySurface(d->dpy, surface->egl_surface);
502 wl_egl_window_destroy(surface->egl_window);
503 surface->surface = NULL;
504
505 free(surface);
506}
507
508static struct toysurface *
509egl_window_surface_create(struct display *display,
510 struct wl_surface *wl_surface,
511 uint32_t flags,
512 struct rectangle *rectangle)
513{
514 struct egl_window_surface *surface;
515
Pekka Paalanenb3627362012-11-19 17:16:00 +0200516 if (display->dpy == EGL_NO_DISPLAY)
517 return NULL;
518
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519 surface = calloc(1, sizeof *surface);
520 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521 return NULL;
522
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 surface->base.prepare = egl_window_surface_prepare;
524 surface->base.swap = egl_window_surface_swap;
525 surface->base.acquire = egl_window_surface_acquire;
526 surface->base.release = egl_window_surface_release;
527 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100528
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200529 surface->display = display;
530 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400531
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200532 surface->egl_window = wl_egl_window_create(surface->surface,
533 rectangle->width,
534 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100535
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200536 surface->egl_surface = eglCreateWindowSurface(display->dpy,
537 display->argb_config,
538 surface->egl_window,
539 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100540
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200541 surface->cairo_surface =
542 cairo_gl_surface_create_for_egl(display->argb_device,
543 surface->egl_surface,
544 rectangle->width,
545 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100546
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200547 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100548}
549
Pekka Paalanenb3627362012-11-19 17:16:00 +0200550#else
551
552static struct toysurface *
553egl_window_surface_create(struct display *display,
554 struct wl_surface *wl_surface,
555 uint32_t flags,
556 struct rectangle *rectangle)
557{
558 return NULL;
559}
560
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561#endif
562
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200563struct shm_surface_data {
564 struct wl_buffer *buffer;
565 struct shm_pool *pool;
566};
567
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400568struct wl_buffer *
569display_get_buffer_for_surface(struct display *display,
570 cairo_surface_t *surface)
571{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200572 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200574 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400575
576 return data->buffer;
577}
578
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500579static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700580shm_pool_destroy(struct shm_pool *pool);
581
582static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400583shm_surface_data_destroy(void *p)
584{
585 struct shm_surface_data *data = p;
586
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200587 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700588 if (data->pool)
589 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300590
591 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400592}
593
Kristian Høgsberg16626282012-04-03 11:21:27 -0400594static struct wl_shm_pool *
595make_shm_pool(struct display *display, int size, void **data)
596{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400597 struct wl_shm_pool *pool;
598 int fd;
599
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300600 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400601 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300602 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
603 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400604 return NULL;
605 }
606
607 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400608 if (*data == MAP_FAILED) {
609 fprintf(stderr, "mmap failed: %m\n");
610 close(fd);
611 return NULL;
612 }
613
614 pool = wl_shm_create_pool(display->shm, fd, size);
615
616 close(fd);
617
618 return pool;
619}
620
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700621static struct shm_pool *
622shm_pool_create(struct display *display, size_t size)
623{
624 struct shm_pool *pool = malloc(sizeof *pool);
625
626 if (!pool)
627 return NULL;
628
629 pool->pool = make_shm_pool(display, size, &pool->data);
630 if (!pool->pool) {
631 free(pool);
632 return NULL;
633 }
634
635 pool->size = size;
636 pool->used = 0;
637
638 return pool;
639}
640
641static void *
642shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
643{
644 if (pool->used + size > pool->size)
645 return NULL;
646
647 *offset = pool->used;
648 pool->used += size;
649
650 return (char *) pool->data + *offset;
651}
652
653/* destroy the pool. this does not unmap the memory though */
654static void
655shm_pool_destroy(struct shm_pool *pool)
656{
657 munmap(pool->data, pool->size);
658 wl_shm_pool_destroy(pool->pool);
659 free(pool);
660}
661
662/* Start allocating from the beginning of the pool again */
663static void
664shm_pool_reset(struct shm_pool *pool)
665{
666 pool->used = 0;
667}
668
669static int
670data_length_for_shm_surface(struct rectangle *rect)
671{
672 int stride;
673
674 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
675 rect->width);
676 return stride * rect->height;
677}
678
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500679static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700680display_create_shm_surface_from_pool(struct display *display,
681 struct rectangle *rectangle,
682 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400683{
684 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400685 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400686 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700687 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400688 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689
690 data = malloc(sizeof *data);
691 if (data == NULL)
692 return NULL;
693
694 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
695 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700696 length = stride * rectangle->height;
697 data->pool = NULL;
698 map = shm_pool_allocate(pool, length, &offset);
699
700 if (!map) {
701 free(data);
702 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400703 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400705 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706 CAIRO_FORMAT_ARGB32,
707 rectangle->width,
708 rectangle->height,
709 stride);
710
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200711 cairo_surface_set_user_data(surface, &shm_surface_data_key,
712 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400713
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400714 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500715 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400716 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500717 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400718
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200719 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
720 rectangle->width,
721 rectangle->height,
722 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400723
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700724 return surface;
725}
726
727static cairo_surface_t *
728display_create_shm_surface(struct display *display,
729 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200730 struct shm_pool *alternate_pool,
731 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700732{
733 struct shm_surface_data *data;
734 struct shm_pool *pool;
735 cairo_surface_t *surface;
736
Pekka Paalanen99436862012-11-19 17:15:59 +0200737 if (alternate_pool) {
738 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700739 surface = display_create_shm_surface_from_pool(display,
740 rectangle,
741 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200742 alternate_pool);
743 if (surface) {
744 data = cairo_surface_get_user_data(surface,
745 &shm_surface_data_key);
746 goto out;
747 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700748 }
749
750 pool = shm_pool_create(display,
751 data_length_for_shm_surface(rectangle));
752 if (!pool)
753 return NULL;
754
755 surface =
756 display_create_shm_surface_from_pool(display, rectangle,
757 flags, pool);
758
759 if (!surface) {
760 shm_pool_destroy(pool);
761 return NULL;
762 }
763
764 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200765 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700766 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400767
Pekka Paalanen99436862012-11-19 17:15:59 +0200768out:
769 if (data_ret)
770 *data_ret = data;
771
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400772 return surface;
773}
774
nobled7b87cb02011-02-01 18:51:47 +0000775static int
776check_size(struct rectangle *rect)
777{
778 if (rect->width && rect->height)
779 return 0;
780
781 fprintf(stderr, "tried to create surface of "
782 "width: %d, height: %d\n", rect->width, rect->height);
783 return -1;
784}
785
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400786cairo_surface_t *
787display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400789 struct rectangle *rectangle,
790 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400791{
nobled7b87cb02011-02-01 18:51:47 +0000792 if (check_size(rectangle) < 0)
793 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200794
795 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200796 return display_create_shm_surface(display, rectangle, flags,
797 NULL, NULL);
798}
799
Pekka Paalanena4eda732012-11-19 17:16:02 +0200800struct shm_surface_leaf {
801 cairo_surface_t *cairo_surface;
802 /* 'data' is automatically destroyed, when 'cairo_surface' is */
803 struct shm_surface_data *data;
804
805 struct shm_pool *resize_pool;
806 int busy;
807};
808
809static void
810shm_surface_leaf_release(struct shm_surface_leaf *leaf)
811{
812 if (leaf->cairo_surface)
813 cairo_surface_destroy(leaf->cairo_surface);
814 /* leaf->data already destroyed via cairo private */
815
816 if (leaf->resize_pool)
817 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200818
819 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200820}
821
Pekka Paalanen99436862012-11-19 17:15:59 +0200822struct shm_surface {
823 struct toysurface base;
824 struct display *display;
825 struct wl_surface *surface;
826 uint32_t flags;
827 int dx, dy;
828
Pekka Paalanena4eda732012-11-19 17:16:02 +0200829 struct shm_surface_leaf leaf[2];
830 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200831};
832
833static struct shm_surface *
834to_shm_surface(struct toysurface *base)
835{
836 return container_of(base, struct shm_surface, base);
837}
838
Pekka Paalanena4eda732012-11-19 17:16:02 +0200839static void
840shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
841{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200842 struct shm_surface *surface = data;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200843
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200844 if (surface->leaf[0].data->buffer == buffer)
845 surface->leaf[0].busy = 0;
846 else if (surface->leaf[1].data->buffer == buffer)
847 surface->leaf[1].busy = 0;
848 else
849 assert(0 && "shm_surface_buffer_release: unknown buffer");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200850
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200851 if (!surface->leaf[0].busy && !surface->leaf[1].busy)
852 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200853}
854
855static const struct wl_buffer_listener shm_surface_buffer_listener = {
856 shm_surface_buffer_release
857};
858
Pekka Paalanen99436862012-11-19 17:15:59 +0200859static cairo_surface_t *
860shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200861 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200862{
Pekka Paalanenec076692012-11-30 13:37:27 +0200863 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200864 struct shm_surface *surface = to_shm_surface(base);
865 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200866 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200867
868 surface->dx = dx;
869 surface->dy = dy;
870
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200871 /* See shm_surface_buffer_release() */
872 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
873 surface->leaf[1].cairo_surface) {
874 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
875 __func__);
876 }
877
Pekka Paalanena4eda732012-11-19 17:16:02 +0200878 /* pick a free buffer from the two */
879 if (!surface->leaf[0].busy)
880 leaf = &surface->leaf[0];
881 else if (!surface->leaf[1].busy)
882 leaf = &surface->leaf[1];
883 else {
884 fprintf(stderr, "%s: both buffers are held by the server.\n",
885 __func__);
886 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200887 }
888
Pekka Paalanena4eda732012-11-19 17:16:02 +0200889 if (!resize_hint && leaf->resize_pool) {
890 cairo_surface_destroy(leaf->cairo_surface);
891 leaf->cairo_surface = NULL;
892 shm_pool_destroy(leaf->resize_pool);
893 leaf->resize_pool = NULL;
894 }
895
896 if (leaf->cairo_surface &&
897 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
898 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200899 goto out;
900
Pekka Paalanena4eda732012-11-19 17:16:02 +0200901 if (leaf->cairo_surface)
902 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200903
Pekka Paalanena4eda732012-11-19 17:16:02 +0200904 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200905 /* Create a big pool to allocate from, while continuously
906 * resizing. Mmapping a new pool in the server
907 * is relatively expensive, so reusing a pool performs
908 * better, but may temporarily reserve unneeded memory.
909 */
910 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200911 leaf->resize_pool = shm_pool_create(surface->display,
912 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200913 }
914
Pekka Paalanena4eda732012-11-19 17:16:02 +0200915 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200916 display_create_shm_surface(surface->display, &rect,
917 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200918 leaf->resize_pool,
919 &leaf->data);
920 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200921 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200922
923out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200924 surface->current = leaf;
925
926 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200927}
928
929static void
930shm_surface_swap(struct toysurface *base,
931 struct rectangle *server_allocation)
932{
933 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200934 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200935
936 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200937 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200938 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200939 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200940
Pekka Paalanena4eda732012-11-19 17:16:02 +0200941 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200942 surface->dx, surface->dy);
943 wl_surface_damage(surface->surface, 0, 0,
944 server_allocation->width, server_allocation->height);
945 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200946
947 leaf->busy = 1;
948 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200949}
950
951static int
952shm_surface_acquire(struct toysurface *base, EGLContext ctx)
953{
954 return -1;
955}
956
957static void
958shm_surface_release(struct toysurface *base)
959{
960}
961
962static void
963shm_surface_destroy(struct toysurface *base)
964{
965 struct shm_surface *surface = to_shm_surface(base);
966
Pekka Paalanena4eda732012-11-19 17:16:02 +0200967 shm_surface_leaf_release(&surface->leaf[0]);
968 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200969
970 free(surface);
971}
972
973static struct toysurface *
974shm_surface_create(struct display *display, struct wl_surface *wl_surface,
975 uint32_t flags, struct rectangle *rectangle)
976{
977 struct shm_surface *surface;
978
979 surface = calloc(1, sizeof *surface);
980 if (!surface)
981 return NULL;
982
983 surface->base.prepare = shm_surface_prepare;
984 surface->base.swap = shm_surface_swap;
985 surface->base.acquire = shm_surface_acquire;
986 surface->base.release = shm_surface_release;
987 surface->base.destroy = shm_surface_destroy;
988
989 surface->display = display;
990 surface->surface = wl_surface;
991 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200992
993 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400994}
995
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200996/*
997 * The following correspondences between file names and cursors was copied
998 * from: https://bugs.kde.org/attachment.cgi?id=67313
999 */
1000
1001static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001002 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001003 "sw-resize"
1004};
1005
1006static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001007 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001008 "se-resize"
1009};
1010
1011static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001012 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001013 "s-resize"
1014};
1015
1016static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001017 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001018 "closedhand",
1019 "208530c400c041818281048008011002"
1020};
1021
1022static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001023 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001024 "default",
1025 "top_left_arrow",
1026 "left-arrow"
1027};
1028
1029static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001030 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001031 "w-resize"
1032};
1033
1034static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001035 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001036 "e-resize"
1037};
1038
1039static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001040 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001041 "nw-resize"
1042};
1043
1044static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001045 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001046 "ne-resize"
1047};
1048
1049static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001050 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001051 "n-resize"
1052};
1053
1054static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001055 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001056 "ibeam",
1057 "text"
1058};
1059
1060static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001061 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001062 "pointer",
1063 "pointing_hand",
1064 "e29285e634086352946a0e7090d73106"
1065};
1066
1067static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001068 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001069 "wait",
1070 "0426c94ea35c87780ff01dc239897213"
1071};
1072
1073struct cursor_alternatives {
1074 const char **names;
1075 size_t count;
1076};
1077
1078static const struct cursor_alternatives cursors[] = {
1079 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1080 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1081 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1082 {grabbings, ARRAY_LENGTH(grabbings)},
1083 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1084 {left_sides, ARRAY_LENGTH(left_sides)},
1085 {right_sides, ARRAY_LENGTH(right_sides)},
1086 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1087 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1088 {top_sides, ARRAY_LENGTH(top_sides)},
1089 {xterms, ARRAY_LENGTH(xterms)},
1090 {hand1s, ARRAY_LENGTH(hand1s)},
1091 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001092};
1093
1094static void
1095create_cursors(struct display *display)
1096{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001097 char *config_file;
1098 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001099 unsigned int i, j;
1100 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001101 struct config_key shell_keys[] = {
1102 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1103 };
1104 struct config_section cs[] = {
1105 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1106 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001107
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001108 config_file = config_file_path("weston.ini");
1109 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1110 free(config_file);
1111
1112 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001113 display->cursors =
1114 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1115
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001116 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001117 cursor = NULL;
1118 for (j = 0; !cursor && j < cursors[i].count; ++j)
1119 cursor = wl_cursor_theme_get_cursor(
1120 display->cursor_theme, cursors[i].names[j]);
1121
1122 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001123 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001124 cursors[i].names[0]);
1125
1126 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001127 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001128}
1129
1130static void
1131destroy_cursors(struct display *display)
1132{
1133 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001134 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001135}
1136
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001137struct wl_cursor_image *
1138display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001140 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001141
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001142 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001143}
1144
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001145static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001146surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001147{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001148 if (!surface->cairo_surface)
1149 return;
1150
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001151 if (surface->opaque_region) {
1152 wl_surface_set_opaque_region(surface->surface,
1153 surface->opaque_region);
1154 wl_region_destroy(surface->opaque_region);
1155 surface->opaque_region = NULL;
1156 }
1157
1158 if (surface->input_region) {
1159 wl_surface_set_input_region(surface->surface,
1160 surface->input_region);
1161 wl_region_destroy(surface->input_region);
1162 surface->input_region = NULL;
1163 }
1164
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001165 surface->toysurface->swap(surface->toysurface,
1166 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001167
Pekka Paalanen89dee002013-02-13 16:17:20 +02001168 cairo_surface_destroy(surface->cairo_surface);
1169 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001170}
1171
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001172int
1173window_has_focus(struct window *window)
1174{
1175 return window->focus_count > 0;
1176}
1177
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001178static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001179window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001180{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001181 if (window->type == TYPE_NONE) {
1182 window->type = TYPE_TOPLEVEL;
1183 if (window->shell_surface)
1184 wl_shell_surface_set_toplevel(window->shell_surface);
1185 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001186
Pekka Paalanen89dee002013-02-13 16:17:20 +02001187 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001188}
1189
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001190struct display *
1191window_get_display(struct window *window)
1192{
1193 return window->display;
1194}
1195
Pekka Paalanen89dee002013-02-13 16:17:20 +02001196static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001197surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001198{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001199 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001200 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001201
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001202 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001203 case WL_OUTPUT_TRANSFORM_90:
1204 case WL_OUTPUT_TRANSFORM_270:
1205 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1206 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001207 allocation.width = surface->allocation.height;
1208 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001209 break;
1210 default:
1211 break;
1212 }
1213
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001214 if (!surface->toysurface && display->dpy &&
1215 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001216 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001217 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001218 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001219 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001220 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001221 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001222
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001223 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001224 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001225 surface->surface,
1226 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001227
Pekka Paalanen89dee002013-02-13 16:17:20 +02001228 surface->cairo_surface = surface->toysurface->prepare(
1229 surface->toysurface, dx, dy,
1230 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001231}
1232
1233static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001234window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001235{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001236 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001237 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001238 int dx = 0;
1239 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001240
Pekka Paalanenec076692012-11-30 13:37:27 +02001241 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001242 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001243
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001244 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1245 dx = surface->server_allocation.width -
1246 surface->allocation.width;
1247
1248 if (window->resize_edges & WINDOW_RESIZING_TOP)
1249 dy = surface->server_allocation.height -
1250 surface->allocation.height;
1251
1252 window->resize_edges = 0;
1253
Pekka Paalanen89dee002013-02-13 16:17:20 +02001254 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001255}
1256
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001257int
1258window_get_buffer_transform(struct window *window)
1259{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001260 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001261}
1262
1263void
1264window_set_buffer_transform(struct window *window,
1265 enum wl_output_transform transform)
1266{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001267 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001268 wl_surface_set_buffer_transform(window->main_surface->surface,
1269 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001270}
1271
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001272static void frame_destroy(struct frame *frame);
1273
Pekka Paalanen4e373742013-02-13 16:17:13 +02001274static void
1275surface_destroy(struct surface *surface)
1276{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001277 if (surface->input_region)
1278 wl_region_destroy(surface->input_region);
1279
1280 if (surface->opaque_region)
1281 wl_region_destroy(surface->opaque_region);
1282
Pekka Paalanen4e373742013-02-13 16:17:13 +02001283 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001284
1285 if (surface->toysurface)
1286 surface->toysurface->destroy(surface->toysurface);
1287
Pekka Paalanen4e373742013-02-13 16:17:13 +02001288 free(surface);
1289}
1290
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001291void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001292window_destroy(struct window *window)
1293{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001294 struct display *display = window->display;
1295 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001296 struct window_output *window_output;
1297 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001298
1299 if (window->redraw_scheduled)
1300 wl_list_remove(&window->redraw_task.link);
1301
1302 wl_list_for_each(input, &display->input_list, link) {
1303 if (input->pointer_focus == window)
1304 input->pointer_focus = NULL;
1305 if (input->keyboard_focus == window)
1306 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001307 if (input->focus_widget &&
1308 input->focus_widget->window == window)
1309 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001310 }
1311
Rob Bradford7507b572012-05-15 17:55:34 +01001312 wl_list_for_each_safe(window_output, window_output_tmp,
1313 &window->window_output_list, link) {
1314 free (window_output);
1315 }
1316
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001317 if (window->frame)
1318 frame_destroy(window->frame);
1319
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001320 if (window->shell_surface)
1321 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001322
1323 surface_destroy(window->main_surface);
1324
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001325 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001326
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001327 if (window->frame_cb)
1328 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001329 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001330 free(window);
1331}
1332
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001333static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001334widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001335{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001336 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001337
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001338 wl_list_for_each(child, &widget->child_list, link) {
1339 target = widget_find_widget(child, x, y);
1340 if (target)
1341 return target;
1342 }
1343
1344 if (widget->allocation.x <= x &&
1345 x < widget->allocation.x + widget->allocation.width &&
1346 widget->allocation.y <= y &&
1347 y < widget->allocation.y + widget->allocation.height) {
1348 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001349 }
1350
1351 return NULL;
1352}
1353
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001354static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001355window_find_widget(struct window *window, int32_t x, int32_t y)
1356{
1357 return widget_find_widget(window->main_surface->widget, x, y);
1358}
1359
1360static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001361widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001362{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001363 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001364
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001365 widget = malloc(sizeof *widget);
1366 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001367 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001368 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001369 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001370 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001371 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001372 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001373 widget->tooltip = NULL;
1374 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001375 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001376
1377 return widget;
1378}
1379
1380struct widget *
1381window_add_widget(struct window *window, void *data)
1382{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001383 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001384
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001385 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001386 wl_list_init(&widget->link);
1387 window->main_surface->widget = widget;
1388
1389 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001390}
1391
1392struct widget *
1393widget_add_widget(struct widget *parent, void *data)
1394{
1395 struct widget *widget;
1396
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001397 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001398 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001399
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001400 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001401}
1402
1403void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001404widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001405{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001406 struct display *display = widget->window->display;
1407 struct input *input;
1408
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001409 if (widget->tooltip) {
1410 free(widget->tooltip);
1411 widget->tooltip = NULL;
1412 }
1413
Pekka Paalanene156fb62012-01-19 13:51:38 +02001414 wl_list_for_each(input, &display->input_list, link) {
1415 if (input->focus_widget == widget)
1416 input->focus_widget = NULL;
1417 }
1418
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001419 wl_list_remove(&widget->link);
1420 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001421}
1422
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001423void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001424widget_set_default_cursor(struct widget *widget, int cursor)
1425{
1426 widget->default_cursor = cursor;
1427}
1428
1429void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001430widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001431{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001432 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001433}
1434
1435void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001436widget_set_size(struct widget *widget, int32_t width, int32_t height)
1437{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001438 widget->allocation.width = width;
1439 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001440}
1441
1442void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001443widget_set_allocation(struct widget *widget,
1444 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001445{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001446 widget->allocation.x = x;
1447 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001448 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001449}
1450
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001451void
1452widget_set_transparent(struct widget *widget, int transparent)
1453{
1454 widget->opaque = !transparent;
1455}
1456
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001457void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001458widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001459{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001460 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001461}
1462
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001463static cairo_surface_t *
1464widget_get_cairo_surface(struct widget *widget)
1465{
1466 struct surface *surface = widget->surface;
1467 struct window *window = widget->window;
1468
1469 if (!surface->cairo_surface) {
1470 if (surface == window->main_surface)
1471 window_create_main_surface(window);
1472 else
1473 surface_create_surface(surface, 0, 0, 0);
1474 }
1475
1476 return surface->cairo_surface;
1477}
1478
1479cairo_t *
1480widget_cairo_create(struct widget *widget)
1481{
1482 cairo_surface_t *cairo_surface;
1483 cairo_t *cr;
1484
1485 cairo_surface = widget_get_cairo_surface(widget);
1486 cr = cairo_create(cairo_surface);
1487
1488 return cr;
1489}
1490
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001491void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001492widget_set_resize_handler(struct widget *widget,
1493 widget_resize_handler_t handler)
1494{
1495 widget->resize_handler = handler;
1496}
1497
1498void
1499widget_set_redraw_handler(struct widget *widget,
1500 widget_redraw_handler_t handler)
1501{
1502 widget->redraw_handler = handler;
1503}
1504
1505void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001506widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001507{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001508 widget->enter_handler = handler;
1509}
1510
1511void
1512widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1513{
1514 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001515}
1516
1517void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001518widget_set_motion_handler(struct widget *widget,
1519 widget_motion_handler_t handler)
1520{
1521 widget->motion_handler = handler;
1522}
1523
1524void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001525widget_set_button_handler(struct widget *widget,
1526 widget_button_handler_t handler)
1527{
1528 widget->button_handler = handler;
1529}
1530
1531void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001532widget_set_axis_handler(struct widget *widget,
1533 widget_axis_handler_t handler)
1534{
1535 widget->axis_handler = handler;
1536}
1537
1538void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001539widget_schedule_redraw(struct widget *widget)
1540{
1541 window_schedule_redraw(widget->window);
1542}
1543
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001544cairo_surface_t *
1545window_get_surface(struct window *window)
1546{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001547 cairo_surface_t *cairo_surface;
1548
1549 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1550
1551 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001552}
1553
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001554struct wl_surface *
1555window_get_wl_surface(struct window *window)
1556{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001557 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001558}
1559
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001560struct wl_shell_surface *
1561window_get_wl_shell_surface(struct window *window)
1562{
1563 return window->shell_surface;
1564}
1565
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001566static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001567tooltip_redraw_handler(struct widget *widget, void *data)
1568{
1569 cairo_t *cr;
1570 const int32_t r = 3;
1571 struct tooltip *tooltip = data;
1572 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001573
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001574 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001575 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1576 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1577 cairo_paint(cr);
1578
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001579 width = widget->allocation.width;
1580 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001581 rounded_rect(cr, 0, 0, width, height, r);
1582
1583 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1584 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1585 cairo_fill(cr);
1586
1587 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1588 cairo_move_to(cr, 10, 16);
1589 cairo_show_text(cr, tooltip->entry);
1590 cairo_destroy(cr);
1591}
1592
1593static cairo_text_extents_t
1594get_text_extents(struct tooltip *tooltip)
1595{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001596 cairo_t *cr;
1597 cairo_text_extents_t extents;
1598
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001599 /* Use the dummy_surface because tooltip's surface was not
1600 * created yet, and parent does not have a valid surface
1601 * outside repaint, either.
1602 */
1603 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001604 cairo_text_extents(cr, tooltip->entry, &extents);
1605 cairo_destroy(cr);
1606
1607 return extents;
1608}
1609
1610static int
1611window_create_tooltip(struct tooltip *tooltip)
1612{
1613 struct widget *parent = tooltip->parent;
1614 struct display *display = parent->window->display;
1615 struct window *window;
1616 const int offset_y = 27;
1617 const int margin = 3;
1618 cairo_text_extents_t extents;
1619
1620 if (tooltip->widget)
1621 return 0;
1622
1623 window = window_create_transient(display, parent->window, tooltip->x,
1624 tooltip->y + offset_y,
1625 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1626 if (!window)
1627 return -1;
1628
1629 tooltip->window = window;
1630 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1631
1632 extents = get_text_extents(tooltip);
1633 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1634 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1635
1636 return 0;
1637}
1638
1639void
1640widget_destroy_tooltip(struct widget *parent)
1641{
1642 struct tooltip *tooltip = parent->tooltip;
1643
1644 parent->tooltip_count = 0;
1645 if (!tooltip)
1646 return;
1647
1648 if (tooltip->widget) {
1649 widget_destroy(tooltip->widget);
1650 window_destroy(tooltip->window);
1651 tooltip->widget = NULL;
1652 tooltip->window = NULL;
1653 }
1654
1655 close(tooltip->tooltip_fd);
1656 free(tooltip->entry);
1657 free(tooltip);
1658 parent->tooltip = NULL;
1659}
1660
1661static void
1662tooltip_func(struct task *task, uint32_t events)
1663{
1664 struct tooltip *tooltip =
1665 container_of(task, struct tooltip, tooltip_task);
1666 uint64_t exp;
1667
Martin Olsson8df662a2012-07-08 03:03:47 +02001668 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1669 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001670 window_create_tooltip(tooltip);
1671}
1672
1673#define TOOLTIP_TIMEOUT 500
1674static int
1675tooltip_timer_reset(struct tooltip *tooltip)
1676{
1677 struct itimerspec its;
1678
1679 its.it_interval.tv_sec = 0;
1680 its.it_interval.tv_nsec = 0;
1681 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1682 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1683 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1684 fprintf(stderr, "could not set timerfd\n: %m");
1685 return -1;
1686 }
1687
1688 return 0;
1689}
1690
1691int
1692widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1693{
1694 struct tooltip *tooltip = parent->tooltip;
1695
1696 parent->tooltip_count++;
1697 if (tooltip) {
1698 tooltip->x = x;
1699 tooltip->y = y;
1700 tooltip_timer_reset(tooltip);
1701 return 0;
1702 }
1703
1704 /* the handler might be triggered too fast via input device motion, so
1705 * we need this check here to make sure tooltip is fully initialized */
1706 if (parent->tooltip_count > 1)
1707 return 0;
1708
1709 tooltip = malloc(sizeof *tooltip);
1710 if (!tooltip)
1711 return -1;
1712
1713 parent->tooltip = tooltip;
1714 tooltip->parent = parent;
1715 tooltip->widget = NULL;
1716 tooltip->window = NULL;
1717 tooltip->x = x;
1718 tooltip->y = y;
1719 tooltip->entry = strdup(entry);
1720 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1721 if (tooltip->tooltip_fd < 0) {
1722 fprintf(stderr, "could not create timerfd\n: %m");
1723 return -1;
1724 }
1725
1726 tooltip->tooltip_task.run = tooltip_func;
1727 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1728 EPOLLIN, &tooltip->tooltip_task);
1729 tooltip_timer_reset(tooltip);
1730
1731 return 0;
1732}
1733
1734static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001735workspace_manager_state(void *data,
1736 struct workspace_manager *workspace_manager,
1737 uint32_t current,
1738 uint32_t count)
1739{
1740 struct display *display = data;
1741
1742 display->workspace = current;
1743 display->workspace_count = count;
1744}
1745
1746static const struct workspace_manager_listener workspace_manager_listener = {
1747 workspace_manager_state
1748};
1749
1750static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001751frame_resize_handler(struct widget *widget,
1752 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001753{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001754 struct frame *frame = data;
1755 struct widget *child = frame->child;
1756 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001757 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001758 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001759 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001760 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001761 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001762 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001763 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001764
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001765 switch (widget->window->type) {
1766 case TYPE_FULLSCREEN:
1767 decoration_width = 0;
1768 decoration_height = 0;
1769
1770 allocation.x = 0;
1771 allocation.y = 0;
1772 allocation.width = width;
1773 allocation.height = height;
1774 opaque_margin = 0;
1775
1776 wl_list_for_each(button, &frame->buttons_list, link)
1777 button->widget->opaque = 1;
1778 break;
1779 case TYPE_MAXIMIZED:
1780 decoration_width = t->width * 2;
1781 decoration_height = t->width + t->titlebar_height;
1782
1783 allocation.x = t->width;
1784 allocation.y = t->titlebar_height;
1785 allocation.width = width - decoration_width;
1786 allocation.height = height - decoration_height;
1787
1788 opaque_margin = 0;
1789
1790 wl_list_for_each(button, &frame->buttons_list, link)
1791 button->widget->opaque = 0;
1792 break;
1793 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001794 decoration_width = (t->width + t->margin) * 2;
1795 decoration_height = t->width +
1796 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001797
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001798 allocation.x = t->width + t->margin;
1799 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001800 allocation.width = width - decoration_width;
1801 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001802
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001803 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001804
1805 wl_list_for_each(button, &frame->buttons_list, link)
1806 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001807 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001808 }
1809
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001810 widget_set_allocation(child, allocation.x, allocation.y,
1811 allocation.width, allocation.height);
1812
1813 if (child->resize_handler)
1814 child->resize_handler(child,
1815 allocation.width,
1816 allocation.height,
1817 child->user_data);
1818
Scott Moreauf7e498c2012-05-14 11:39:29 -06001819 width = child->allocation.width + decoration_width;
1820 height = child->allocation.height + decoration_height;
1821
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001822 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1823
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001824 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001825 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001826 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001827 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001828 shadow_margin, shadow_margin,
1829 width - 2 * shadow_margin,
1830 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001831 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001832 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001833 }
1834
Scott Moreauf7e498c2012-05-14 11:39:29 -06001835 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001836
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001837 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001838 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001839 opaque_margin, opaque_margin,
1840 widget->allocation.width - 2 * opaque_margin,
1841 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001842
1843 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001844 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1845 x_l = t->width + shadow_margin;
1846 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001847 wl_list_for_each(button, &frame->buttons_list, link) {
1848 const int button_padding = 4;
1849 w = cairo_image_surface_get_width(button->icon);
1850 h = cairo_image_surface_get_height(button->icon);
1851
1852 if (button->decoration == FRAME_BUTTON_FANCY)
1853 w += 10;
1854
1855 if (button->align == FRAME_BUTTON_LEFT) {
1856 widget_set_allocation(button->widget,
1857 x_l, y , w + 1, h + 1);
1858 x_l += w;
1859 x_l += button_padding;
1860 } else {
1861 x_r -= w;
1862 widget_set_allocation(button->widget,
1863 x_r, y , w + 1, h + 1);
1864 x_r -= button_padding;
1865 }
1866 }
1867}
1868
1869static int
1870frame_button_enter_handler(struct widget *widget,
1871 struct input *input, float x, float y, void *data)
1872{
1873 struct frame_button *frame_button = data;
1874
1875 widget_schedule_redraw(frame_button->widget);
1876 frame_button->state = FRAME_BUTTON_OVER;
1877
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001878 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001879}
1880
1881static void
1882frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1883{
1884 struct frame_button *frame_button = data;
1885
1886 widget_schedule_redraw(frame_button->widget);
1887 frame_button->state = FRAME_BUTTON_DEFAULT;
1888}
1889
1890static void
1891frame_button_button_handler(struct widget *widget,
1892 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001893 uint32_t button,
1894 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001895{
1896 struct frame_button *frame_button = data;
1897 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001898 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001899
1900 if (button != BTN_LEFT)
1901 return;
1902
1903 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001904 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001905 frame_button->state = FRAME_BUTTON_ACTIVE;
1906 widget_schedule_redraw(frame_button->widget);
1907
1908 if (frame_button->type == FRAME_BUTTON_ICON)
1909 window_show_frame_menu(window, input, time);
1910 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001911 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001912 frame_button->state = FRAME_BUTTON_DEFAULT;
1913 widget_schedule_redraw(frame_button->widget);
1914 break;
1915 }
1916
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001917 if (!was_pressed)
1918 return;
1919
Martin Minarik1998b152012-05-10 02:04:35 +02001920 switch (frame_button->type) {
1921 case FRAME_BUTTON_CLOSE:
1922 if (window->close_handler)
1923 window->close_handler(window->parent,
1924 window->user_data);
1925 else
1926 display_exit(window->display);
1927 break;
1928 case FRAME_BUTTON_MINIMIZE:
1929 fprintf(stderr,"Minimize stub\n");
1930 break;
1931 case FRAME_BUTTON_MAXIMIZE:
1932 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1933 break;
1934 default:
1935 /* Unknown operation */
1936 break;
1937 }
1938}
1939
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001940static int
1941frame_button_motion_handler(struct widget *widget,
1942 struct input *input, uint32_t time,
1943 float x, float y, void *data)
1944{
1945 struct frame_button *frame_button = data;
1946 enum frame_button_pointer previous_button_state = frame_button->state;
1947
1948 /* only track state for a pressed button */
1949 if (input->grab != widget)
1950 return CURSOR_LEFT_PTR;
1951
1952 if (x > widget->allocation.x &&
1953 x < (widget->allocation.x + widget->allocation.width) &&
1954 y > widget->allocation.y &&
1955 y < (widget->allocation.y + widget->allocation.height)) {
1956 frame_button->state = FRAME_BUTTON_ACTIVE;
1957 } else {
1958 frame_button->state = FRAME_BUTTON_DEFAULT;
1959 }
1960
1961 if (frame_button->state != previous_button_state)
1962 widget_schedule_redraw(frame_button->widget);
1963
1964 return CURSOR_LEFT_PTR;
1965}
1966
Martin Minarik1998b152012-05-10 02:04:35 +02001967static void
1968frame_button_redraw_handler(struct widget *widget, void *data)
1969{
1970 struct frame_button *frame_button = data;
1971 cairo_t *cr;
1972 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02001973
1974 x = widget->allocation.x;
1975 y = widget->allocation.y;
1976 width = widget->allocation.width;
1977 height = widget->allocation.height;
1978
1979 if (!width)
1980 return;
1981 if (!height)
1982 return;
1983 if (widget->opaque)
1984 return;
1985
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001986 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02001987
1988 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1989 cairo_set_line_width(cr, 1);
1990
1991 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1992 cairo_rectangle (cr, x, y, 25, 16);
1993
1994 cairo_stroke_preserve(cr);
1995
1996 switch (frame_button->state) {
1997 case FRAME_BUTTON_DEFAULT:
1998 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1999 break;
2000 case FRAME_BUTTON_OVER:
2001 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2002 break;
2003 case FRAME_BUTTON_ACTIVE:
2004 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2005 break;
2006 }
2007
2008 cairo_fill (cr);
2009
2010 x += 4;
2011 }
2012
2013 cairo_set_source_surface(cr, frame_button->icon, x, y);
2014 cairo_paint(cr);
2015
2016 cairo_destroy(cr);
2017}
2018
2019static struct widget *
2020frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2021 enum frame_button_align align, enum frame_button_decoration style)
2022{
2023 struct frame_button *frame_button;
2024 const char *icon = data;
2025
2026 frame_button = malloc (sizeof *frame_button);
2027 memset(frame_button, 0, sizeof *frame_button);
2028
2029 frame_button->icon = cairo_image_surface_create_from_png(icon);
2030 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2031 frame_button->frame = frame;
2032 frame_button->type = type;
2033 frame_button->align = align;
2034 frame_button->decoration = style;
2035
2036 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2037
2038 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2039 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2040 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2041 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002042 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002043 return frame_button->widget;
2044}
2045
2046static void
2047frame_button_destroy(struct frame_button *frame_button)
2048{
2049 widget_destroy(frame_button->widget);
2050 wl_list_remove(&frame_button->link);
2051 cairo_surface_destroy(frame_button->icon);
2052 free(frame_button);
2053
2054 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002055}
2056
2057static void
2058frame_redraw_handler(struct widget *widget, void *data)
2059{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002060 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002061 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002062 struct theme *t = window->display->theme;
2063 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002064
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002065 if (window->type == TYPE_FULLSCREEN)
2066 return;
2067
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002068 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002069
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002070 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002071 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002072 if (window->type == TYPE_MAXIMIZED)
2073 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002074 theme_render_frame(t, cr, widget->allocation.width,
2075 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002076
2077 cairo_destroy(cr);
2078}
2079
2080static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002081frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002082{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002083 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002084 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002085 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002086
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002087 if (window->type != TYPE_TOPLEVEL)
2088 return CURSOR_LEFT_PTR;
2089
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002090 location = theme_get_location(t, input->sx, input->sy,
2091 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002092 frame->widget->allocation.height,
2093 window->type == TYPE_MAXIMIZED ?
2094 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002095
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002096 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002097 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002098 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002099 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002100 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002101 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002102 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002103 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002104 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002105 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002106 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002107 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002108 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002109 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002110 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002111 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002112 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002113 case THEME_LOCATION_EXTERIOR:
2114 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002115 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002116 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002117 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002118}
2119
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002120static void
2121frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002122{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002123 struct display *display;
2124
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002125 switch (index) {
2126 case 0: /* close */
2127 if (window->close_handler)
2128 window->close_handler(window->parent,
2129 window->user_data);
2130 else
2131 display_exit(window->display);
2132 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002133 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002134 display = window->display;
2135 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002136 workspace_manager_move_surface(
2137 display->workspace_manager,
2138 window->main_surface->surface,
2139 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002140 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002141 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002142 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002143 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002144 workspace_manager_move_surface(
2145 display->workspace_manager,
2146 window->main_surface->surface,
2147 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002148 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002149 case 3: /* fullscreen */
2150 /* we don't have a way to get out of fullscreen for now */
2151 if (window->fullscreen_handler)
2152 window->fullscreen_handler(window, window->user_data);
2153 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002154 }
2155}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002156
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002157void
2158window_show_frame_menu(struct window *window,
2159 struct input *input, uint32_t time)
2160{
2161 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002162 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002163
2164 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002165 "Close",
2166 "Move to workspace above", "Move to workspace below",
2167 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002168 };
2169
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002170 if (window->fullscreen_handler)
2171 count = ARRAY_LENGTH(entries);
2172 else
2173 count = ARRAY_LENGTH(entries) - 1;
2174
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002175 input_get_position(input, &x, &y);
2176 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002177 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002178}
2179
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002180static int
2181frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002182 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002183{
2184 return frame_get_pointer_image_for_location(data, input);
2185}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002186
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002187static int
2188frame_motion_handler(struct widget *widget,
2189 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002190 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002191{
2192 return frame_get_pointer_image_for_location(data, input);
2193}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002194
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002195static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002196frame_button_handler(struct widget *widget,
2197 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002198 uint32_t button, enum wl_pointer_button_state state,
2199 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002200
2201{
2202 struct frame *frame = data;
2203 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002204 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002205 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002206
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002207 if (window->type != TYPE_TOPLEVEL)
2208 return;
2209
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002210 location = theme_get_location(display->theme, input->sx, input->sy,
2211 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002212 frame->widget->allocation.height,
2213 window->type == TYPE_MAXIMIZED ?
2214 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002215
Daniel Stone4dbadb12012-05-30 16:31:51 +01002216 if (window->display->shell && button == BTN_LEFT &&
2217 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002218 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002219 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002220 if (!window->shell_surface)
2221 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002222 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002223 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002224 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002225 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002227 case THEME_LOCATION_RESIZING_TOP:
2228 case THEME_LOCATION_RESIZING_BOTTOM:
2229 case THEME_LOCATION_RESIZING_LEFT:
2230 case THEME_LOCATION_RESIZING_RIGHT:
2231 case THEME_LOCATION_RESIZING_TOP_LEFT:
2232 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2233 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2234 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002235 if (!window->shell_surface)
2236 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002237 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002238
Pekka Paalanen99436862012-11-19 17:15:59 +02002239 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002240 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002241 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002242 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002243 break;
2244 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002245 } else if (button == BTN_RIGHT &&
2246 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002247 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002248 }
2249}
2250
2251struct widget *
2252frame_create(struct window *window, void *data)
2253{
2254 struct frame *frame;
2255
2256 frame = malloc(sizeof *frame);
2257 memset(frame, 0, sizeof *frame);
2258
2259 frame->widget = window_add_widget(window, frame);
2260 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002261
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002262 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2263 widget_set_resize_handler(frame->widget, frame_resize_handler);
2264 widget_set_enter_handler(frame->widget, frame_enter_handler);
2265 widget_set_motion_handler(frame->widget, frame_motion_handler);
2266 widget_set_button_handler(frame->widget, frame_button_handler);
2267
Martin Minarik1998b152012-05-10 02:04:35 +02002268 /* Create empty list for frame buttons */
2269 wl_list_init(&frame->buttons_list);
2270
2271 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2272 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2273
2274 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2275 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2276
2277 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2278 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2279
2280 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2281 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2282
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002283 window->frame = frame;
2284
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002285 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002286}
2287
Kristian Høgsberga1627922012-06-20 17:30:03 -04002288void
2289frame_set_child_size(struct widget *widget, int child_width, int child_height)
2290{
2291 struct display *display = widget->window->display;
2292 struct theme *t = display->theme;
2293 int decoration_width, decoration_height;
2294 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002295 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002296
2297 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002298 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002299 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002300 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002301
2302 width = child_width + decoration_width;
2303 height = child_height + decoration_height;
2304 } else {
2305 width = child_width;
2306 height = child_height;
2307 }
2308
2309 window_schedule_resize(widget->window, width, height);
2310}
2311
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002312static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002313frame_destroy(struct frame *frame)
2314{
Martin Minarik1998b152012-05-10 02:04:35 +02002315 struct frame_button *button, *tmp;
2316
2317 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2318 frame_button_destroy(button);
2319
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002320 /* frame->child must be destroyed by the application */
2321 widget_destroy(frame->widget);
2322 free(frame);
2323}
2324
2325static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002326input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002327 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002328{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002329 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002330 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002331
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002332 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002333 return;
2334
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002335 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002336 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002337 widget = old;
2338 if (input->grab)
2339 widget = input->grab;
2340 if (widget->leave_handler)
2341 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002342 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002343 }
2344
2345 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002346 widget = focus;
2347 if (input->grab)
2348 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002349 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002350 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002351 cursor = widget->enter_handler(focus, input, x, y,
2352 widget->user_data);
2353 else
2354 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002355
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002356 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002357 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002358}
2359
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002360void
2361input_grab(struct input *input, struct widget *widget, uint32_t button)
2362{
2363 input->grab = widget;
2364 input->grab_button = button;
2365}
2366
2367void
2368input_ungrab(struct input *input)
2369{
2370 struct widget *widget;
2371
2372 input->grab = NULL;
2373 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002374 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002375 input->sx, input->sy);
2376 input_set_focus_widget(input, widget, input->sx, input->sy);
2377 }
2378}
2379
2380static void
2381input_remove_pointer_focus(struct input *input)
2382{
2383 struct window *window = input->pointer_focus;
2384
2385 if (!window)
2386 return;
2387
2388 input_set_focus_widget(input, NULL, 0, 0);
2389
2390 input->pointer_focus = NULL;
2391 input->current_cursor = CURSOR_UNSET;
2392}
2393
2394static void
2395pointer_handle_enter(void *data, struct wl_pointer *pointer,
2396 uint32_t serial, struct wl_surface *surface,
2397 wl_fixed_t sx_w, wl_fixed_t sy_w)
2398{
2399 struct input *input = data;
2400 struct window *window;
2401 struct widget *widget;
2402 float sx = wl_fixed_to_double(sx_w);
2403 float sy = wl_fixed_to_double(sy_w);
2404
2405 if (!surface) {
2406 /* enter event for a window we've just destroyed */
2407 return;
2408 }
2409
2410 input->display->serial = serial;
2411 input->pointer_enter_serial = serial;
2412 input->pointer_focus = wl_surface_get_user_data(surface);
2413 window = input->pointer_focus;
2414
Pekka Paalanen99436862012-11-19 17:15:59 +02002415 if (window->resizing) {
2416 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002417 /* Schedule a redraw to free the pool */
2418 window_schedule_redraw(window);
2419 }
2420
2421 input->sx = sx;
2422 input->sy = sy;
2423
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002424 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002425 input_set_focus_widget(input, widget, sx, sy);
2426}
2427
2428static void
2429pointer_handle_leave(void *data, struct wl_pointer *pointer,
2430 uint32_t serial, struct wl_surface *surface)
2431{
2432 struct input *input = data;
2433
2434 input->display->serial = serial;
2435 input_remove_pointer_focus(input);
2436}
2437
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002438static void
Daniel Stone37816df2012-05-16 18:45:18 +01002439pointer_handle_motion(void *data, struct wl_pointer *pointer,
2440 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002441{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002442 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002443 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002444 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002445 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002446 float sx = wl_fixed_to_double(sx_w);
2447 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002448
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002449 input->sx = sx;
2450 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002451
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002452 if (!window)
2453 return;
2454
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002455 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002456 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002457 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002458 }
2459
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002460 if (input->grab)
2461 widget = input->grab;
2462 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002463 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002464 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002465 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002466 input, time, sx, sy,
2467 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002468 else
2469 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002470
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002471 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002472}
2473
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002474static void
Daniel Stone37816df2012-05-16 18:45:18 +01002475pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002476 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002477{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002478 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002479 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002480 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002481
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002482 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002483 if (input->focus_widget && input->grab == NULL &&
2484 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002485 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002486
Neil Roberts6b28aad2012-01-23 19:11:18 +00002487 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002488 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002489 (*widget->button_handler)(widget,
2490 input, time,
2491 button, state,
2492 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002493
Daniel Stone4dbadb12012-05-30 16:31:51 +01002494 if (input->grab && input->grab_button == button &&
2495 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002496 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002497}
2498
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002499static void
Daniel Stone37816df2012-05-16 18:45:18 +01002500pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002501 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002502{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002503 struct input *input = data;
2504 struct widget *widget;
2505
2506 widget = input->focus_widget;
2507 if (input->grab)
2508 widget = input->grab;
2509 if (widget && widget->axis_handler)
2510 (*widget->axis_handler)(widget,
2511 input, time,
2512 axis, value,
2513 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002514}
2515
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002516static const struct wl_pointer_listener pointer_listener = {
2517 pointer_handle_enter,
2518 pointer_handle_leave,
2519 pointer_handle_motion,
2520 pointer_handle_button,
2521 pointer_handle_axis,
2522};
2523
2524static void
2525input_remove_keyboard_focus(struct input *input)
2526{
2527 struct window *window = input->keyboard_focus;
2528 struct itimerspec its;
2529
2530 its.it_interval.tv_sec = 0;
2531 its.it_interval.tv_nsec = 0;
2532 its.it_value.tv_sec = 0;
2533 its.it_value.tv_nsec = 0;
2534 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2535
2536 if (!window)
2537 return;
2538
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002539 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002540 if (window->keyboard_focus_handler)
2541 (*window->keyboard_focus_handler)(window, NULL,
2542 window->user_data);
2543
2544 input->keyboard_focus = NULL;
2545}
2546
2547static void
2548keyboard_repeat_func(struct task *task, uint32_t events)
2549{
2550 struct input *input =
2551 container_of(task, struct input, repeat_task);
2552 struct window *window = input->keyboard_focus;
2553 uint64_t exp;
2554
2555 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2556 /* If we change the timer between the fd becoming
2557 * readable and getting here, there'll be nothing to
2558 * read and we get EAGAIN. */
2559 return;
2560
2561 if (window && window->key_handler) {
2562 (*window->key_handler)(window, input, input->repeat_time,
2563 input->repeat_key, input->repeat_sym,
2564 WL_KEYBOARD_KEY_STATE_PRESSED,
2565 window->user_data);
2566 }
2567}
2568
2569static void
2570keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2571 uint32_t format, int fd, uint32_t size)
2572{
2573 struct input *input = data;
2574 char *map_str;
2575
2576 if (!data) {
2577 close(fd);
2578 return;
2579 }
2580
2581 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2582 close(fd);
2583 return;
2584 }
2585
2586 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2587 if (map_str == MAP_FAILED) {
2588 close(fd);
2589 return;
2590 }
2591
2592 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2593 map_str,
2594 XKB_KEYMAP_FORMAT_TEXT_V1,
2595 0);
2596 munmap(map_str, size);
2597 close(fd);
2598
2599 if (!input->xkb.keymap) {
2600 fprintf(stderr, "failed to compile keymap\n");
2601 return;
2602 }
2603
2604 input->xkb.state = xkb_state_new(input->xkb.keymap);
2605 if (!input->xkb.state) {
2606 fprintf(stderr, "failed to create XKB state\n");
2607 xkb_map_unref(input->xkb.keymap);
2608 input->xkb.keymap = NULL;
2609 return;
2610 }
2611
2612 input->xkb.control_mask =
2613 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2614 input->xkb.alt_mask =
2615 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2616 input->xkb.shift_mask =
2617 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2618}
2619
2620static void
2621keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2622 uint32_t serial, struct wl_surface *surface,
2623 struct wl_array *keys)
2624{
2625 struct input *input = data;
2626 struct window *window;
2627
2628 input->display->serial = serial;
2629 input->keyboard_focus = wl_surface_get_user_data(surface);
2630
2631 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002632 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002633 if (window->keyboard_focus_handler)
2634 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002635 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002636}
2637
2638static void
2639keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2640 uint32_t serial, struct wl_surface *surface)
2641{
2642 struct input *input = data;
2643
2644 input->display->serial = serial;
2645 input_remove_keyboard_focus(input);
2646}
2647
Scott Moreau210d0792012-03-22 10:47:01 -06002648static void
Daniel Stone37816df2012-05-16 18:45:18 +01002649keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002650 uint32_t serial, uint32_t time, uint32_t key,
2651 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002652{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002653 struct input *input = data;
2654 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002655 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002656 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002657 const xkb_keysym_t *syms;
2658 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002659 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002660
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002661 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002662 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002663 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002664 return;
2665
Daniel Stone97f68542012-05-30 16:32:01 +01002666 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002667
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002668 sym = XKB_KEY_NoSymbol;
2669 if (num_syms == 1)
2670 sym = syms[0];
2671
2672 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002673 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002674 window_set_maximized(window,
2675 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002676 } else if (sym == XKB_KEY_F11 &&
2677 window->fullscreen_handler &&
2678 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2679 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002680 } else if (sym == XKB_KEY_F4 &&
2681 input->modifiers == MOD_ALT_MASK &&
2682 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2683 if (window->close_handler)
2684 window->close_handler(window->parent,
2685 window->user_data);
2686 else
2687 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002688 } else if (window->key_handler) {
2689 (*window->key_handler)(window, input, time, key,
2690 sym, state, window->user_data);
2691 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002692
2693 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2694 key == input->repeat_key) {
2695 its.it_interval.tv_sec = 0;
2696 its.it_interval.tv_nsec = 0;
2697 its.it_value.tv_sec = 0;
2698 its.it_value.tv_nsec = 0;
2699 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2700 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2701 input->repeat_sym = sym;
2702 input->repeat_key = key;
2703 input->repeat_time = time;
2704 its.it_interval.tv_sec = 0;
2705 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2706 its.it_value.tv_sec = 0;
2707 its.it_value.tv_nsec = 400 * 1000 * 1000;
2708 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2709 }
2710}
2711
2712static void
Daniel Stone351eb612012-05-31 15:27:47 -04002713keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2714 uint32_t serial, uint32_t mods_depressed,
2715 uint32_t mods_latched, uint32_t mods_locked,
2716 uint32_t group)
2717{
2718 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002719 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002720
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002721 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2722 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002723 mask = xkb_state_serialize_mods(input->xkb.state,
2724 XKB_STATE_DEPRESSED |
2725 XKB_STATE_LATCHED);
2726 input->modifiers = 0;
2727 if (mask & input->xkb.control_mask)
2728 input->modifiers |= MOD_CONTROL_MASK;
2729 if (mask & input->xkb.alt_mask)
2730 input->modifiers |= MOD_ALT_MASK;
2731 if (mask & input->xkb.shift_mask)
2732 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002733}
2734
Daniel Stone37816df2012-05-16 18:45:18 +01002735static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002736 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002737 keyboard_handle_enter,
2738 keyboard_handle_leave,
2739 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002740 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002741};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002742
2743static void
Daniel Stone37816df2012-05-16 18:45:18 +01002744seat_handle_capabilities(void *data, struct wl_seat *seat,
2745 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002746{
Daniel Stone37816df2012-05-16 18:45:18 +01002747 struct input *input = data;
2748
2749 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2750 input->pointer = wl_seat_get_pointer(seat);
2751 wl_pointer_set_user_data(input->pointer, input);
2752 wl_pointer_add_listener(input->pointer, &pointer_listener,
2753 input);
2754 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2755 wl_pointer_destroy(input->pointer);
2756 input->pointer = NULL;
2757 }
2758
2759 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2760 input->keyboard = wl_seat_get_keyboard(seat);
2761 wl_keyboard_set_user_data(input->keyboard, input);
2762 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2763 input);
2764 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2765 wl_keyboard_destroy(input->keyboard);
2766 input->keyboard = NULL;
2767 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002768}
2769
Daniel Stone37816df2012-05-16 18:45:18 +01002770static const struct wl_seat_listener seat_listener = {
2771 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002772};
2773
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002774void
2775input_get_position(struct input *input, int32_t *x, int32_t *y)
2776{
2777 *x = input->sx;
2778 *y = input->sy;
2779}
2780
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002781struct display *
2782input_get_display(struct input *input)
2783{
2784 return input->display;
2785}
2786
Daniel Stone37816df2012-05-16 18:45:18 +01002787struct wl_seat *
2788input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002789{
Daniel Stone37816df2012-05-16 18:45:18 +01002790 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002791}
2792
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002793uint32_t
2794input_get_modifiers(struct input *input)
2795{
2796 return input->modifiers;
2797}
2798
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002799struct widget *
2800input_get_focus_widget(struct input *input)
2801{
2802 return input->focus_widget;
2803}
2804
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002805struct data_offer {
2806 struct wl_data_offer *offer;
2807 struct input *input;
2808 struct wl_array types;
2809 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002810
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002811 struct task io_task;
2812 int fd;
2813 data_func_t func;
2814 int32_t x, y;
2815 void *user_data;
2816};
2817
2818static void
2819data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2820{
2821 struct data_offer *offer = data;
2822 char **p;
2823
2824 p = wl_array_add(&offer->types, sizeof *p);
2825 *p = strdup(type);
2826}
2827
2828static const struct wl_data_offer_listener data_offer_listener = {
2829 data_offer_offer,
2830};
2831
2832static void
2833data_offer_destroy(struct data_offer *offer)
2834{
2835 char **p;
2836
2837 offer->refcount--;
2838 if (offer->refcount == 0) {
2839 wl_data_offer_destroy(offer->offer);
2840 for (p = offer->types.data; *p; p++)
2841 free(*p);
2842 wl_array_release(&offer->types);
2843 free(offer);
2844 }
2845}
2846
2847static void
2848data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002849 struct wl_data_device *data_device,
2850 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002851{
2852 struct data_offer *offer;
2853
2854 offer = malloc(sizeof *offer);
2855
2856 wl_array_init(&offer->types);
2857 offer->refcount = 1;
2858 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002859 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002860 wl_data_offer_add_listener(offer->offer,
2861 &data_offer_listener, offer);
2862}
2863
2864static void
2865data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002866 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002867 wl_fixed_t x_w, wl_fixed_t y_w,
2868 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002869{
2870 struct input *input = data;
2871 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002872 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002873 float x = wl_fixed_to_double(x_w);
2874 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002875 char **p;
2876
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002877 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002878 window = wl_surface_get_user_data(surface);
2879 input->pointer_focus = window;
2880
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002881 if (offer) {
2882 input->drag_offer = wl_data_offer_get_user_data(offer);
2883
2884 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2885 *p = NULL;
2886
2887 types_data = input->drag_offer->types.data;
2888 } else {
2889 input->drag_offer = NULL;
2890 types_data = NULL;
2891 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002892
2893 window = input->pointer_focus;
2894 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002895 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002896 window->user_data);
2897}
2898
2899static void
2900data_device_leave(void *data, struct wl_data_device *data_device)
2901{
2902 struct input *input = data;
2903
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002904 if (input->drag_offer) {
2905 data_offer_destroy(input->drag_offer);
2906 input->drag_offer = NULL;
2907 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002908}
2909
2910static void
2911data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002912 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002913{
2914 struct input *input = data;
2915 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002916 float x = wl_fixed_to_double(x_w);
2917 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002918 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002919
2920 input->sx = x;
2921 input->sy = y;
2922
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002923 if (input->drag_offer)
2924 types_data = input->drag_offer->types.data;
2925 else
2926 types_data = NULL;
2927
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002928 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002929 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002930 window->user_data);
2931}
2932
2933static void
2934data_device_drop(void *data, struct wl_data_device *data_device)
2935{
2936 struct input *input = data;
2937 struct window *window = input->pointer_focus;
2938
2939 if (window->drop_handler)
2940 window->drop_handler(window, input,
2941 input->sx, input->sy, window->user_data);
2942}
2943
2944static void
2945data_device_selection(void *data,
2946 struct wl_data_device *wl_data_device,
2947 struct wl_data_offer *offer)
2948{
2949 struct input *input = data;
2950 char **p;
2951
2952 if (input->selection_offer)
2953 data_offer_destroy(input->selection_offer);
2954
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002955 if (offer) {
2956 input->selection_offer = wl_data_offer_get_user_data(offer);
2957 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2958 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002959 } else {
2960 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002961 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002962}
2963
2964static const struct wl_data_device_listener data_device_listener = {
2965 data_device_data_offer,
2966 data_device_enter,
2967 data_device_leave,
2968 data_device_motion,
2969 data_device_drop,
2970 data_device_selection
2971};
2972
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002973static void
2974input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002975{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002976 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002977 struct wl_cursor *cursor;
2978 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002979
Daniel Stone80972742012-11-07 17:51:39 +11002980 if (!input->pointer)
2981 return;
2982
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002983 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002984 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002985 return;
2986
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002987 if (index >= (int) cursor->image_count) {
2988 fprintf(stderr, "cursor index out of range\n");
2989 return;
2990 }
2991
2992 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002993 buffer = wl_cursor_image_get_buffer(image);
2994 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002995 return;
2996
Kristian Høgsbergae277372012-08-01 09:41:08 -04002997 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002998 input->pointer_surface,
2999 image->hotspot_x, image->hotspot_y);
3000 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3001 wl_surface_damage(input->pointer_surface, 0, 0,
3002 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003003 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003004}
3005
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003006static const struct wl_callback_listener pointer_surface_listener;
3007
3008static void
3009pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3010 uint32_t time)
3011{
3012 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003013 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003014 int i;
3015
3016 if (callback) {
3017 assert(callback == input->cursor_frame_cb);
3018 wl_callback_destroy(callback);
3019 input->cursor_frame_cb = NULL;
3020 }
3021
Daniel Stone80972742012-11-07 17:51:39 +11003022 if (!input->pointer)
3023 return;
3024
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003025 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003026 wl_pointer_set_cursor(input->pointer,
3027 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003028 NULL, 0, 0);
3029 return;
3030 }
3031
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003032 if (input->current_cursor == CURSOR_UNSET)
3033 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003034 cursor = input->display->cursors[input->current_cursor];
3035 if (!cursor)
3036 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003037
3038 /* FIXME We don't have the current time on the first call so we set
3039 * the animation start to the time of the first frame callback. */
3040 if (time == 0)
3041 input->cursor_anim_start = 0;
3042 else if (input->cursor_anim_start == 0)
3043 input->cursor_anim_start = time;
3044
3045 if (time == 0 || input->cursor_anim_start == 0)
3046 i = 0;
3047 else
3048 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3049
Pekka Paalanenbc106382012-10-10 12:49:31 +03003050 if (cursor->image_count > 1) {
3051 input->cursor_frame_cb =
3052 wl_surface_frame(input->pointer_surface);
3053 wl_callback_add_listener(input->cursor_frame_cb,
3054 &pointer_surface_listener, input);
3055 }
3056
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003057 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003058}
3059
3060static const struct wl_callback_listener pointer_surface_listener = {
3061 pointer_surface_frame_callback
3062};
3063
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003064void
3065input_set_pointer_image(struct input *input, int pointer)
3066{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003067 int force = 0;
3068
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003069 if (!input->pointer)
3070 return;
3071
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003072 if (input->pointer_enter_serial > input->cursor_serial)
3073 force = 1;
3074
3075 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003076 return;
3077
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003078 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003079 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003080 if (!input->cursor_frame_cb)
3081 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003082 else if (force) {
3083 /* The current frame callback may be stuck if, for instance,
3084 * the set cursor request was processed by the server after
3085 * this client lost the focus. In this case the cursor surface
3086 * might not be mapped and the frame callback wouldn't ever
3087 * complete. Send a set_cursor and attach to try to map the
3088 * cursor surface again so that the callback will finish */
3089 input_set_pointer_image_index(input, 0);
3090 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003091}
3092
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003093struct wl_data_device *
3094input_get_data_device(struct input *input)
3095{
3096 return input->data_device;
3097}
3098
3099void
3100input_set_selection(struct input *input,
3101 struct wl_data_source *source, uint32_t time)
3102{
3103 wl_data_device_set_selection(input->data_device, source, time);
3104}
3105
3106void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003107input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003108{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003109 wl_data_offer_accept(input->drag_offer->offer,
3110 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003111}
3112
3113static void
3114offer_io_func(struct task *task, uint32_t events)
3115{
3116 struct data_offer *offer =
3117 container_of(task, struct data_offer, io_task);
3118 unsigned int len;
3119 char buffer[4096];
3120
3121 len = read(offer->fd, buffer, sizeof buffer);
3122 offer->func(buffer, len,
3123 offer->x, offer->y, offer->user_data);
3124
3125 if (len == 0) {
3126 close(offer->fd);
3127 data_offer_destroy(offer);
3128 }
3129}
3130
3131static void
3132data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3133 data_func_t func, void *user_data)
3134{
3135 int p[2];
3136
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003137 if (pipe2(p, O_CLOEXEC) == -1)
3138 return;
3139
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003140 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3141 close(p[1]);
3142
3143 offer->io_task.run = offer_io_func;
3144 offer->fd = p[0];
3145 offer->func = func;
3146 offer->refcount++;
3147 offer->user_data = user_data;
3148
3149 display_watch_fd(offer->input->display,
3150 offer->fd, EPOLLIN, &offer->io_task);
3151}
3152
3153void
3154input_receive_drag_data(struct input *input, const char *mime_type,
3155 data_func_t func, void *data)
3156{
3157 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3158 input->drag_offer->x = input->sx;
3159 input->drag_offer->y = input->sy;
3160}
3161
3162int
3163input_receive_selection_data(struct input *input, const char *mime_type,
3164 data_func_t func, void *data)
3165{
3166 char **p;
3167
3168 if (input->selection_offer == NULL)
3169 return -1;
3170
3171 for (p = input->selection_offer->types.data; *p; p++)
3172 if (strcmp(mime_type, *p) == 0)
3173 break;
3174
3175 if (*p == NULL)
3176 return -1;
3177
3178 data_offer_receive_data(input->selection_offer,
3179 mime_type, func, data);
3180 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003181}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003182
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003183int
3184input_receive_selection_data_to_fd(struct input *input,
3185 const char *mime_type, int fd)
3186{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003187 if (input->selection_offer)
3188 wl_data_offer_receive(input->selection_offer->offer,
3189 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003190
3191 return 0;
3192}
3193
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003194void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003195window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003196{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003197 if (!window->shell_surface)
3198 return;
3199
Daniel Stone37816df2012-05-16 18:45:18 +01003200 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003201}
3202
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003203static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003204surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003205{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003206 struct widget *widget = surface->widget;
3207 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003208
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003209 if (surface->input_region) {
3210 wl_region_destroy(surface->input_region);
3211 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003212 }
3213
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003214 if (surface->opaque_region)
3215 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003216
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003217 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003218
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003219 if (widget->resize_handler)
3220 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003221 widget->allocation.width,
3222 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003223 widget->user_data);
3224
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003225 if (surface->allocation.width != widget->allocation.width ||
3226 surface->allocation.height != widget->allocation.height) {
3227 surface->allocation = widget->allocation;
3228 window_schedule_redraw(widget->window);
3229 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003230
3231 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003232 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003233 widget->allocation.width,
3234 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003235}
3236
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003237static void
3238idle_resize(struct window *window)
3239{
3240 window->resize_needed = 0;
3241
3242 widget_set_allocation(window->main_surface->widget,
3243 window->pending_allocation.x,
3244 window->pending_allocation.y,
3245 window->pending_allocation.width,
3246 window->pending_allocation.height);
3247
3248 surface_resize(window->main_surface);
3249}
3250
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003251void
3252window_schedule_resize(struct window *window, int width, int height)
3253{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003254 window->pending_allocation.x = 0;
3255 window->pending_allocation.y = 0;
3256 window->pending_allocation.width = width;
3257 window->pending_allocation.height = height;
3258
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003259 if (window->min_allocation.width == 0)
3260 window->min_allocation = window->pending_allocation;
3261 if (window->pending_allocation.width < window->min_allocation.width)
3262 window->pending_allocation.width = window->min_allocation.width;
3263 if (window->pending_allocation.height < window->min_allocation.height)
3264 window->pending_allocation.height = window->min_allocation.height;
3265
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003266 window->resize_needed = 1;
3267 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003268}
3269
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003270void
3271widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3272{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003273 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003274}
3275
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003276static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003277handle_ping(void *data, struct wl_shell_surface *shell_surface,
3278 uint32_t serial)
3279{
3280 wl_shell_surface_pong(shell_surface, serial);
3281}
3282
3283static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003284handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003285 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003286{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003287 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003288
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003289 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003290 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003291}
3292
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003293static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003294menu_destroy(struct menu *menu)
3295{
3296 widget_destroy(menu->widget);
3297 window_destroy(menu->window);
3298 free(menu);
3299}
3300
3301static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003302handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3303{
3304 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003305 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003306
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003307 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003308 * device. Or just use wl_callback. And this really needs to
3309 * be a window vfunc that the menu can set. And we need the
3310 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003311
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003312 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003313 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003314 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003315}
3316
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003317static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003318 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003319 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003320 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003321};
3322
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003323void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003324window_get_allocation(struct window *window,
3325 struct rectangle *allocation)
3326{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003327 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003328}
3329
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003330static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003331widget_redraw(struct widget *widget)
3332{
3333 struct widget *child;
3334
3335 if (widget->redraw_handler)
3336 widget->redraw_handler(widget, widget->user_data);
3337 wl_list_for_each(child, &widget->child_list, link)
3338 widget_redraw(child);
3339}
3340
3341static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003342frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3343{
3344 struct window *window = data;
3345
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003346 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003347 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003348 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003349 window->redraw_scheduled = 0;
3350 if (window->redraw_needed)
3351 window_schedule_redraw(window);
3352}
3353
3354static const struct wl_callback_listener listener = {
3355 frame_callback
3356};
3357
3358static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003359idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003360{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003361 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003362
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003363 if (window->resize_needed)
3364 idle_resize(window);
3365
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003366 widget_redraw(window->main_surface->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003367 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003368 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003369
Pekka Paalanen4e373742013-02-13 16:17:13 +02003370 window->frame_cb = wl_surface_frame(window->main_surface->surface);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003371 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003372 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003373}
3374
3375void
3376window_schedule_redraw(struct window *window)
3377{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003378 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003379 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003380 window->redraw_task.run = idle_redraw;
3381 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003382 window->redraw_scheduled = 1;
3383 }
3384}
3385
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003386int
3387window_is_fullscreen(struct window *window)
3388{
3389 return window->type == TYPE_FULLSCREEN;
3390}
3391
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003392void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003393window_set_fullscreen(struct window *window, int fullscreen)
3394{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003395 if (!window->display->shell)
3396 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003397
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003398 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003399 return;
3400
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003401 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003402 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003403 if (window->type == TYPE_TOPLEVEL) {
3404 window->saved_allocation = window->main_surface->allocation;
3405 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003406 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003407 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003408 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003409 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003410 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003411 if (window->saved_type == TYPE_MAXIMIZED) {
3412 window_set_maximized(window, 1);
3413 } else {
3414 window->type = TYPE_TOPLEVEL;
3415 wl_shell_surface_set_toplevel(window->shell_surface);
3416 window_schedule_resize(window,
3417 window->saved_allocation.width,
3418 window->saved_allocation.height);
3419 }
3420
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003421 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003422}
3423
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003424void
3425window_set_fullscreen_method(struct window *window,
3426 enum wl_shell_surface_fullscreen_method method)
3427{
3428 window->fullscreen_method = method;
3429}
3430
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003431int
3432window_is_maximized(struct window *window)
3433{
3434 return window->type == TYPE_MAXIMIZED;
3435}
3436
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003437void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003438window_set_maximized(struct window *window, int maximized)
3439{
3440 if (!window->display->shell)
3441 return;
3442
3443 if ((window->type == TYPE_MAXIMIZED) == maximized)
3444 return;
3445
3446 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003447 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003448 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3449 window->type = TYPE_MAXIMIZED;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003450 } else if (window->type == TYPE_FULLSCREEN) {
3451 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3452 window->type = TYPE_MAXIMIZED;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003453 } else {
3454 wl_shell_surface_set_toplevel(window->shell_surface);
3455 window->type = TYPE_TOPLEVEL;
3456 window_schedule_resize(window,
3457 window->saved_allocation.width,
3458 window->saved_allocation.height);
3459 }
3460}
3461
3462void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003463window_set_user_data(struct window *window, void *data)
3464{
3465 window->user_data = data;
3466}
3467
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003468void *
3469window_get_user_data(struct window *window)
3470{
3471 return window->user_data;
3472}
3473
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003474void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003475window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003476 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003477{
3478 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003479}
3480
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003481void
3482window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003483 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003484{
3485 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003486}
3487
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003488void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003489window_set_data_handler(struct window *window, window_data_handler_t handler)
3490{
3491 window->data_handler = handler;
3492}
3493
3494void
3495window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3496{
3497 window->drop_handler = handler;
3498}
3499
3500void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003501window_set_close_handler(struct window *window,
3502 window_close_handler_t handler)
3503{
3504 window->close_handler = handler;
3505}
3506
3507void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003508window_set_fullscreen_handler(struct window *window,
3509 window_fullscreen_handler_t handler)
3510{
3511 window->fullscreen_handler = handler;
3512}
3513
3514void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003515window_set_output_handler(struct window *window,
3516 window_output_handler_t handler)
3517{
3518 window->output_handler = handler;
3519}
3520
3521void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003522window_set_title(struct window *window, const char *title)
3523{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003524 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003525 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003526 if (window->shell_surface)
3527 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003528}
3529
3530const char *
3531window_get_title(struct window *window)
3532{
3533 return window->title;
3534}
3535
3536void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003537window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3538{
3539 struct text_cursor_position *text_cursor_position =
3540 window->display->text_cursor_position;
3541
Scott Moreau9295ce02012-06-01 12:46:10 -06003542 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003543 return;
3544
3545 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003546 window->main_surface->surface,
3547 wl_fixed_from_int(x),
3548 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003549}
3550
3551void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003552window_damage(struct window *window, int32_t x, int32_t y,
3553 int32_t width, int32_t height)
3554{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003555 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003556}
3557
Casey Dahlin9074db52012-04-19 22:50:09 -04003558static void
3559surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003560 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003561{
Rob Bradford7507b572012-05-15 17:55:34 +01003562 struct window *window = data;
3563 struct output *output;
3564 struct output *output_found = NULL;
3565 struct window_output *window_output;
3566
3567 wl_list_for_each(output, &window->display->output_list, link) {
3568 if (output->output == wl_output) {
3569 output_found = output;
3570 break;
3571 }
3572 }
3573
3574 if (!output_found)
3575 return;
3576
3577 window_output = malloc (sizeof *window_output);
3578 window_output->output = output_found;
3579
3580 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003581
3582 if (window->output_handler)
3583 window->output_handler(window, output_found, 1,
3584 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003585}
3586
3587static void
3588surface_leave(void *data,
3589 struct wl_surface *wl_surface, struct wl_output *output)
3590{
Rob Bradford7507b572012-05-15 17:55:34 +01003591 struct window *window = data;
3592 struct window_output *window_output;
3593 struct window_output *window_output_found = NULL;
3594
3595 wl_list_for_each(window_output, &window->window_output_list, link) {
3596 if (window_output->output->output == output) {
3597 window_output_found = window_output;
3598 break;
3599 }
3600 }
3601
3602 if (window_output_found) {
3603 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003604
3605 if (window->output_handler)
3606 window->output_handler(window, window_output->output,
3607 0, window->user_data);
3608
Rob Bradford7507b572012-05-15 17:55:34 +01003609 free(window_output_found);
3610 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003611}
3612
3613static const struct wl_surface_listener surface_listener = {
3614 surface_enter,
3615 surface_leave
3616};
3617
Pekka Paalanen4e373742013-02-13 16:17:13 +02003618static struct surface *
3619surface_create(struct window *window)
3620{
3621 struct display *display = window->display;
3622 struct surface *surface;
3623
3624 surface = calloc(1, sizeof *surface);
3625 if (!surface)
3626 return NULL;
3627
3628 surface->window = window;
3629 surface->surface = wl_compositor_create_surface(display->compositor);
3630 wl_surface_add_listener(surface->surface, &surface_listener, window);
3631
3632 return surface;
3633}
3634
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003635static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003636window_create_internal(struct display *display,
3637 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003638{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003639 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003640 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003641
3642 window = malloc(sizeof *window);
3643 if (window == NULL)
3644 return NULL;
3645
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003646 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003647 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003648 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003649
3650 surface = surface_create(window);
3651 window->main_surface = surface;
3652
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003653 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003654 window->shell_surface =
3655 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003656 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003657 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003658
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003659 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003660 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003661
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003662 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003663#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003664 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003665#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003666 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003667#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003668 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003669 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003670
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003671 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003672 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003673 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003674
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003675 if (window->shell_surface) {
3676 wl_shell_surface_set_user_data(window->shell_surface, window);
3677 wl_shell_surface_add_listener(window->shell_surface,
3678 &shell_surface_listener, window);
3679 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003680
Rob Bradford7507b572012-05-15 17:55:34 +01003681 wl_list_init (&window->window_output_list);
3682
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003683 return window;
3684}
3685
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003686struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003687window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003688{
3689 struct window *window;
3690
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003691 window = window_create_internal(display, NULL, TYPE_NONE);
3692 if (!window)
3693 return NULL;
3694
3695 return window;
3696}
3697
3698struct window *
3699window_create_custom(struct display *display)
3700{
3701 struct window *window;
3702
3703 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003704 if (!window)
3705 return NULL;
3706
3707 return window;
3708}
3709
3710struct window *
3711window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003712 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003713{
3714 struct window *window;
3715
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003716 window = window_create_internal(parent->display,
3717 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003718 if (!window)
3719 return NULL;
3720
3721 window->x = x;
3722 window->y = y;
3723
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003724 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02003725 wl_shell_surface_set_transient(
3726 window->shell_surface,
3727 window->parent->main_surface->surface,
3728 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003729
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003730 return window;
3731}
3732
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003733static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003734menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003735{
3736 int next;
3737
3738 next = (sy - 8) / 20;
3739 if (menu->current != next) {
3740 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003741 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003742 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003743}
3744
3745static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003746menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003747 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003748 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003749{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003750 struct menu *menu = data;
3751
3752 if (widget == menu->widget)
3753 menu_set_item(data, y);
3754
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003755 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003756}
3757
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003758static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003759menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003760 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003761{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003762 struct menu *menu = data;
3763
3764 if (widget == menu->widget)
3765 menu_set_item(data, y);
3766
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003767 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003768}
3769
3770static void
3771menu_leave_handler(struct widget *widget, struct input *input, void *data)
3772{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003773 struct menu *menu = data;
3774
3775 if (widget == menu->widget)
3776 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003777}
3778
3779static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003780menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003781 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003782 uint32_t button, enum wl_pointer_button_state state,
3783 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003784
3785{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003786 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003787
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003788 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3789 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003790 /* Either relase after press-drag-release or
3791 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003792 menu->func(menu->window->parent,
3793 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003794 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003795 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003796 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003797 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003798 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003799}
3800
3801static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003802menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003803{
3804 cairo_t *cr;
3805 const int32_t r = 3, margin = 3;
3806 struct menu *menu = data;
3807 int32_t width, height, i;
3808
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02003809 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003810 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3811 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3812 cairo_paint(cr);
3813
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02003814 width = widget->allocation.width;
3815 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003816 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003817
3818 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003819 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3820 cairo_fill(cr);
3821
3822 for (i = 0; i < menu->count; i++) {
3823 if (i == menu->current) {
3824 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3825 cairo_rectangle(cr, margin, i * 20 + margin,
3826 width - 2 * margin, 20);
3827 cairo_fill(cr);
3828 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3829 cairo_move_to(cr, 10, i * 20 + 16);
3830 cairo_show_text(cr, menu->entries[i]);
3831 } else {
3832 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3833 cairo_move_to(cr, 10, i * 20 + 16);
3834 cairo_show_text(cr, menu->entries[i]);
3835 }
3836 }
3837
3838 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003839}
3840
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003841void
3842window_show_menu(struct display *display,
3843 struct input *input, uint32_t time, struct window *parent,
3844 int32_t x, int32_t y,
3845 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003846{
3847 struct window *window;
3848 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003849 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003850
3851 menu = malloc(sizeof *menu);
3852 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003853 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003854
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003855 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003856 if (!window) {
3857 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003858 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003859 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003860
3861 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003862 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003863 menu->entries = entries;
3864 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003865 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003866 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003867 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003868 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003869 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003870 window->type = TYPE_MENU;
3871 window->x = x;
3872 window->y = y;
3873
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003874 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003875 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003876 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02003877 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003878 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003879
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003880 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003881 widget_set_enter_handler(menu->widget, menu_enter_handler);
3882 widget_set_leave_handler(menu->widget, menu_leave_handler);
3883 widget_set_motion_handler(menu->widget, menu_motion_handler);
3884 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003885
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003886 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003887 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003888}
3889
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003890void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003891window_set_buffer_type(struct window *window, enum window_buffer_type type)
3892{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003893 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003894}
3895
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003896
3897static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003898display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003899 struct wl_output *wl_output,
3900 int x, int y,
3901 int physical_width,
3902 int physical_height,
3903 int subpixel,
3904 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003905 const char *model,
3906 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003907{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003908 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003909
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003910 output->allocation.x = x;
3911 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003912 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003913}
3914
3915static void
3916display_handle_mode(void *data,
3917 struct wl_output *wl_output,
3918 uint32_t flags,
3919 int width,
3920 int height,
3921 int refresh)
3922{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003923 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003924 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003925
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003926 if (flags & WL_OUTPUT_MODE_CURRENT) {
3927 output->allocation.width = width;
3928 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003929 if (display->output_configure_handler)
3930 (*display->output_configure_handler)(
3931 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003932 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003933}
3934
3935static const struct wl_output_listener output_listener = {
3936 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003937 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003938};
3939
3940static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003941display_add_output(struct display *d, uint32_t id)
3942{
3943 struct output *output;
3944
3945 output = malloc(sizeof *output);
3946 if (output == NULL)
3947 return;
3948
3949 memset(output, 0, sizeof *output);
3950 output->display = d;
3951 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003952 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003953 wl_list_insert(d->output_list.prev, &output->link);
3954
3955 wl_output_add_listener(output->output, &output_listener, output);
3956}
3957
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003958static void
3959output_destroy(struct output *output)
3960{
3961 if (output->destroy_handler)
3962 (*output->destroy_handler)(output, output->user_data);
3963
3964 wl_output_destroy(output->output);
3965 wl_list_remove(&output->link);
3966 free(output);
3967}
3968
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003969void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003970display_set_global_handler(struct display *display,
3971 display_global_handler_t handler)
3972{
3973 struct global *global;
3974
3975 display->global_handler = handler;
3976 if (!handler)
3977 return;
3978
3979 wl_list_for_each(global, &display->global_list, link)
3980 display->global_handler(display,
3981 global->name, global->interface,
3982 global->version, display->user_data);
3983}
3984
3985void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003986display_set_output_configure_handler(struct display *display,
3987 display_output_handler_t handler)
3988{
3989 struct output *output;
3990
3991 display->output_configure_handler = handler;
3992 if (!handler)
3993 return;
3994
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003995 wl_list_for_each(output, &display->output_list, link) {
3996 if (output->allocation.width == 0 &&
3997 output->allocation.height == 0)
3998 continue;
3999
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004000 (*display->output_configure_handler)(output,
4001 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004002 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004003}
4004
4005void
4006output_set_user_data(struct output *output, void *data)
4007{
4008 output->user_data = data;
4009}
4010
4011void *
4012output_get_user_data(struct output *output)
4013{
4014 return output->user_data;
4015}
4016
4017void
4018output_set_destroy_handler(struct output *output,
4019 display_output_handler_t handler)
4020{
4021 output->destroy_handler = handler;
4022 /* FIXME: implement this, once we have way to remove outputs */
4023}
4024
4025void
Scott Moreau4e072362012-09-29 02:03:11 -06004026output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004027{
Scott Moreau4e072362012-09-29 02:03:11 -06004028 struct rectangle allocation = output->allocation;
4029
4030 switch (output->transform) {
4031 case WL_OUTPUT_TRANSFORM_90:
4032 case WL_OUTPUT_TRANSFORM_270:
4033 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4034 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4035 /* Swap width and height */
4036 allocation.width = output->allocation.height;
4037 allocation.height = output->allocation.width;
4038 break;
4039 }
4040
4041 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004042}
4043
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004044struct wl_output *
4045output_get_wl_output(struct output *output)
4046{
4047 return output->output;
4048}
4049
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004050enum wl_output_transform
4051output_get_transform(struct output *output)
4052{
4053 return output->transform;
4054}
4055
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004056static void
Daniel Stone97f68542012-05-30 16:32:01 +01004057fini_xkb(struct input *input)
4058{
4059 xkb_state_unref(input->xkb.state);
4060 xkb_map_unref(input->xkb.keymap);
4061}
4062
4063static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004064display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004065{
4066 struct input *input;
4067
4068 input = malloc(sizeof *input);
4069 if (input == NULL)
4070 return;
4071
4072 memset(input, 0, sizeof *input);
4073 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004074 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004075 input->pointer_focus = NULL;
4076 input->keyboard_focus = NULL;
4077 wl_list_insert(d->input_list.prev, &input->link);
4078
Daniel Stone37816df2012-05-16 18:45:18 +01004079 wl_seat_add_listener(input->seat, &seat_listener, input);
4080 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004081
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004082 input->data_device =
4083 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004084 input->seat);
4085 wl_data_device_add_listener(input->data_device, &data_device_listener,
4086 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004087
4088 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004089
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004090 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4091 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004092 input->repeat_task.run = keyboard_repeat_func;
4093 display_watch_fd(d, input->repeat_timer_fd,
4094 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004095}
4096
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004097static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004098input_destroy(struct input *input)
4099{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004100 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004101 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004102
4103 if (input->drag_offer)
4104 data_offer_destroy(input->drag_offer);
4105
4106 if (input->selection_offer)
4107 data_offer_destroy(input->selection_offer);
4108
4109 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004110 fini_xkb(input);
4111
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004112 wl_surface_destroy(input->pointer_surface);
4113
Pekka Paalanene1207c72011-12-16 12:02:09 +02004114 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004115 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004116 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004117 free(input);
4118}
4119
4120static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004121init_workspace_manager(struct display *d, uint32_t id)
4122{
4123 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004124 wl_registry_bind(d->registry, id,
4125 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004126 if (d->workspace_manager != NULL)
4127 workspace_manager_add_listener(d->workspace_manager,
4128 &workspace_manager_listener,
4129 d);
4130}
4131
4132static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004133registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4134 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004135{
4136 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004137 struct global *global;
4138
4139 global = malloc(sizeof *global);
4140 global->name = id;
4141 global->interface = strdup(interface);
4142 global->version = version;
4143 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004144
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004145 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004146 d->compositor = wl_registry_bind(registry, id,
4147 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004148 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004149 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004150 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004151 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004152 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004153 d->shell = wl_registry_bind(registry,
4154 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004155 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004156 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004157 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4158 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004159 wl_registry_bind(registry, id,
4160 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004161 } else if (strcmp(interface, "text_cursor_position") == 0) {
4162 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004163 wl_registry_bind(registry, id,
4164 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004165 } else if (strcmp(interface, "workspace_manager") == 0) {
4166 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004167 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004168
4169 if (d->global_handler)
4170 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004171}
4172
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004173static void
4174registry_handle_global_remove(void *data, struct wl_registry *registry,
4175 uint32_t name)
4176{
4177 struct display *d = data;
4178 struct global *global;
4179 struct global *tmp;
4180
4181 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4182 if (global->name != name)
4183 continue;
4184
4185 /* XXX: Should destroy bound globals, and call
4186 * the counterpart of display::global_handler
4187 */
4188 wl_list_remove(&global->link);
4189 free(global->interface);
4190 free(global);
4191 }
4192}
4193
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004194void *
4195display_bind(struct display *display, uint32_t name,
4196 const struct wl_interface *interface, uint32_t version)
4197{
4198 return wl_registry_bind(display->registry, name, interface, version);
4199}
4200
4201static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004202 registry_handle_global,
4203 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004204};
4205
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004206#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004207static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004208init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004209{
4210 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004211 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004212
Rob Clark6396ed32012-03-11 19:48:41 -05004213#ifdef USE_CAIRO_GLESV2
4214# define GL_BIT EGL_OPENGL_ES2_BIT
4215#else
4216# define GL_BIT EGL_OPENGL_BIT
4217#endif
4218
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004219 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004220 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004221 EGL_RED_SIZE, 1,
4222 EGL_GREEN_SIZE, 1,
4223 EGL_BLUE_SIZE, 1,
4224 EGL_ALPHA_SIZE, 1,
4225 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004226 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004227 EGL_NONE
4228 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004229
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004230#ifdef USE_CAIRO_GLESV2
4231 static const EGLint context_attribs[] = {
4232 EGL_CONTEXT_CLIENT_VERSION, 2,
4233 EGL_NONE
4234 };
4235 EGLint api = EGL_OPENGL_ES_API;
4236#else
4237 EGLint *context_attribs = NULL;
4238 EGLint api = EGL_OPENGL_API;
4239#endif
4240
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004241 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004242 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004243 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004244 return -1;
4245 }
4246
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004247 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004248 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004249 return -1;
4250 }
4251
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004252 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4253 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004254 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004255 return -1;
4256 }
4257
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004258 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004259 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004260 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004261 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004262 return -1;
4263 }
4264
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004265 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004266 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004267 return -1;
4268 }
4269
Benjamin Franzke0c991632011-09-27 21:57:31 +02004270 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4271 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004272 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004273 return -1;
4274 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004275
4276 return 0;
4277}
4278
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004279static void
4280fini_egl(struct display *display)
4281{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004282 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004283
4284 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4285 EGL_NO_CONTEXT);
4286
4287 eglTerminate(display->dpy);
4288 eglReleaseThread();
4289}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004290#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004291
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004292static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004293init_dummy_surface(struct display *display)
4294{
4295 int len;
4296 void *data;
4297
4298 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4299 data = malloc(len);
4300 display->dummy_surface =
4301 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4302 1, 1, len);
4303 display->dummy_surface_data = data;
4304}
4305
4306static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004307handle_display_data(struct task *task, uint32_t events)
4308{
4309 struct display *display =
4310 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004311 struct epoll_event ep;
4312 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004313
4314 display->display_fd_events = events;
4315
4316 if (events & EPOLLERR || events & EPOLLHUP) {
4317 display_exit(display);
4318 return;
4319 }
4320
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004321 if (events & EPOLLIN) {
4322 ret = wl_display_dispatch(display->display);
4323 if (ret == -1) {
4324 display_exit(display);
4325 return;
4326 }
4327 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004328
4329 if (events & EPOLLOUT) {
4330 ret = wl_display_flush(display->display);
4331 if (ret == 0) {
4332 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4333 ep.data.ptr = &display->display_task;
4334 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4335 display->display_fd, &ep);
4336 } else if (ret == -1 && errno != EAGAIN) {
4337 display_exit(display);
4338 return;
4339 }
4340 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004341}
4342
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004343struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004344display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004345{
4346 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004347
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004348 d = malloc(sizeof *d);
4349 if (d == NULL)
4350 return NULL;
4351
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004352 memset(d, 0, sizeof *d);
4353
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004354 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004355 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004356 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004357 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004358 return NULL;
4359 }
4360
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004361 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004362 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004363 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004364 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4365 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004366
4367 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004368 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004369 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004370 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004371
Daniel Stone97f68542012-05-30 16:32:01 +01004372 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004373 if (d->xkb_context == NULL) {
4374 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004375 return NULL;
4376 }
4377
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004378 d->workspace = 0;
4379 d->workspace_count = 1;
4380
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004381 d->registry = wl_display_get_registry(d->display);
4382 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004383
4384 if (wl_display_dispatch(d->display) < 0) {
4385 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4386 return NULL;
4387 }
4388
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004389#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004390 if (init_egl(d) < 0)
4391 fprintf(stderr, "EGL does not seem to work, "
4392 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004393#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004394
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004395 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004396
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004397 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004398
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004399 wl_list_init(&d->window_list);
4400
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004401 init_dummy_surface(d);
4402
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004403 return d;
4404}
4405
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004406static void
4407display_destroy_outputs(struct display *display)
4408{
4409 struct output *tmp;
4410 struct output *output;
4411
4412 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4413 output_destroy(output);
4414}
4415
Pekka Paalanene1207c72011-12-16 12:02:09 +02004416static void
4417display_destroy_inputs(struct display *display)
4418{
4419 struct input *tmp;
4420 struct input *input;
4421
4422 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4423 input_destroy(input);
4424}
4425
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004426void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004427display_destroy(struct display *display)
4428{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004429 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004430 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4431 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004432
4433 if (!wl_list_empty(&display->deferred_list))
4434 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4435
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004436 cairo_surface_destroy(display->dummy_surface);
4437 free(display->dummy_surface_data);
4438
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004439 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004440 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004441
Daniel Stone97f68542012-05-30 16:32:01 +01004442 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004443
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004444 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004445 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004446
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004447#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004448 if (display->argb_device)
4449 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004450#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004451
Pekka Paalanenc2052982011-12-16 11:41:32 +02004452 if (display->shell)
4453 wl_shell_destroy(display->shell);
4454
4455 if (display->shm)
4456 wl_shm_destroy(display->shm);
4457
4458 if (display->data_device_manager)
4459 wl_data_device_manager_destroy(display->data_device_manager);
4460
4461 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004462 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004463
4464 close(display->epoll_fd);
4465
U. Artie Eoff44874d92012-10-02 21:12:35 -07004466 if (!(display->display_fd_events & EPOLLERR) &&
4467 !(display->display_fd_events & EPOLLHUP))
4468 wl_display_flush(display->display);
4469
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004470 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004471 free(display);
4472}
4473
4474void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004475display_set_user_data(struct display *display, void *data)
4476{
4477 display->user_data = data;
4478}
4479
4480void *
4481display_get_user_data(struct display *display)
4482{
4483 return display->user_data;
4484}
4485
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004486struct wl_display *
4487display_get_display(struct display *display)
4488{
4489 return display->display;
4490}
4491
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004492struct output *
4493display_get_output(struct display *display)
4494{
4495 return container_of(display->output_list.next, struct output, link);
4496}
4497
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004498struct wl_compositor *
4499display_get_compositor(struct display *display)
4500{
4501 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004502}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004503
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004504uint32_t
4505display_get_serial(struct display *display)
4506{
4507 return display->serial;
4508}
4509
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004510EGLDisplay
4511display_get_egl_display(struct display *d)
4512{
4513 return d->dpy;
4514}
4515
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004516struct wl_data_source *
4517display_create_data_source(struct display *display)
4518{
4519 return wl_data_device_manager_create_data_source(display->data_device_manager);
4520}
4521
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004522EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004523display_get_argb_egl_config(struct display *d)
4524{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004525 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004526}
4527
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004528struct wl_shell *
4529display_get_shell(struct display *display)
4530{
4531 return display->shell;
4532}
4533
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004534int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004535display_acquire_window_surface(struct display *display,
4536 struct window *window,
4537 EGLContext ctx)
4538{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004539 struct surface *surface = window->main_surface;
4540
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004541 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004542 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004543
Pekka Paalanen6f41b072013-02-20 13:39:17 +02004544 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004545 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004546}
4547
4548void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004549display_release_window_surface(struct display *display,
4550 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004551{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004552 struct surface *surface = window->main_surface;
4553
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004554 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004555 return;
4556
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004557 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004558}
4559
4560void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004561display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004562{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004563 wl_list_insert(&display->deferred_list, &task->link);
4564}
4565
4566void
4567display_watch_fd(struct display *display,
4568 int fd, uint32_t events, struct task *task)
4569{
4570 struct epoll_event ep;
4571
4572 ep.events = events;
4573 ep.data.ptr = task;
4574 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4575}
4576
4577void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004578display_unwatch_fd(struct display *display, int fd)
4579{
4580 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4581}
4582
4583void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004584display_run(struct display *display)
4585{
4586 struct task *task;
4587 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004588 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004589
Pekka Paalanen826d7952011-12-15 10:14:07 +02004590 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004591 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004592 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004593 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004594 struct task, link);
4595 wl_list_remove(&task->link);
4596 task->run(task, 0);
4597 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004598
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004599 wl_display_dispatch_pending(display->display);
4600
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004601 if (!display->running)
4602 break;
4603
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004604 ret = wl_display_flush(display->display);
4605 if (ret < 0 && errno == EAGAIN) {
4606 ep[0].events =
4607 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4608 ep[0].data.ptr = &display->display_task;
4609
4610 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4611 display->display_fd, &ep[0]);
4612 } else if (ret < 0) {
4613 break;
4614 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004615
4616 count = epoll_wait(display->epoll_fd,
4617 ep, ARRAY_LENGTH(ep), -1);
4618 for (i = 0; i < count; i++) {
4619 task = ep[i].data.ptr;
4620 task->run(task, ep[i].events);
4621 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004622 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004623}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004624
4625void
4626display_exit(struct display *display)
4627{
4628 display->running = 0;
4629}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004630
4631void
4632keysym_modifiers_add(struct wl_array *modifiers_map,
4633 const char *name)
4634{
4635 size_t len = strlen(name) + 1;
4636 char *p;
4637
4638 p = wl_array_add(modifiers_map, len);
4639
4640 if (p == NULL)
4641 return;
4642
4643 strncpy(p, name, len);
4644}
4645
4646static xkb_mod_index_t
4647keysym_modifiers_get_index(struct wl_array *modifiers_map,
4648 const char *name)
4649{
4650 xkb_mod_index_t index = 0;
4651 char *p = modifiers_map->data;
4652
4653 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4654 if (strcmp(p, name) == 0)
4655 return index;
4656
4657 index++;
4658 p += strlen(p) + 1;
4659 }
4660
4661 return XKB_MOD_INVALID;
4662}
4663
4664xkb_mod_mask_t
4665keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4666 const char *name)
4667{
4668 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4669
4670 if (index == XKB_MOD_INVALID)
4671 return XKB_MOD_INVALID;
4672
4673 return 1 << index;
4674}