blob: 9752e7ed7bfdb8608b11030f113e9dd082012602 [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;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400221 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400222 int focus_count;
223
Pekka Paalanen99436862012-11-19 17:15:59 +0200224 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200225 int fullscreen_method;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400226
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500227 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500228 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400229 window_data_handler_t data_handler;
230 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500231 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400232 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200233 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400234
Pekka Paalanen4e373742013-02-13 16:17:13 +0200235 struct surface *main_surface;
236 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300237 struct wl_callback *frame_cb;
238
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200239 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500240
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500241 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400242 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500243};
244
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500245struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500246 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200247 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300248 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500249 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400250 struct wl_list link;
251 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500252 widget_resize_handler_t resize_handler;
253 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500254 widget_enter_handler_t enter_handler;
255 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500256 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500257 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200258 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400259 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500260 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300261 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500262 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400263};
264
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400265struct input {
266 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100267 struct wl_seat *seat;
268 struct wl_pointer *pointer;
269 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400270 struct window *pointer_focus;
271 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300272 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300273 uint32_t cursor_anim_start;
274 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300275 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400276 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400277 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400278 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400279 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400280 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400281
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500282 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500283 struct widget *grab;
284 uint32_t grab_button;
285
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400286 struct wl_data_device *data_device;
287 struct data_offer *drag_offer;
288 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100289
290 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100291 struct xkb_keymap *keymap;
292 struct xkb_state *state;
293 xkb_mod_mask_t control_mask;
294 xkb_mod_mask_t alt_mask;
295 xkb_mod_mask_t shift_mask;
296 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400297
298 struct task repeat_task;
299 int repeat_timer_fd;
300 uint32_t repeat_sym;
301 uint32_t repeat_key;
302 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400303};
304
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500305struct output {
306 struct display *display;
307 struct wl_output *output;
308 struct rectangle allocation;
309 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600310 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200311
312 display_output_handler_t destroy_handler;
313 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500314};
315
Martin Minarik1998b152012-05-10 02:04:35 +0200316enum frame_button_action {
317 FRAME_BUTTON_NULL = 0,
318 FRAME_BUTTON_ICON = 1,
319 FRAME_BUTTON_CLOSE = 2,
320 FRAME_BUTTON_MINIMIZE = 3,
321 FRAME_BUTTON_MAXIMIZE = 4,
322};
323
324enum frame_button_pointer {
325 FRAME_BUTTON_DEFAULT = 0,
326 FRAME_BUTTON_OVER = 1,
327 FRAME_BUTTON_ACTIVE = 2,
328};
329
330enum frame_button_align {
331 FRAME_BUTTON_RIGHT = 0,
332 FRAME_BUTTON_LEFT = 1,
333};
334
335enum frame_button_decoration {
336 FRAME_BUTTON_NONE = 0,
337 FRAME_BUTTON_FANCY = 1,
338};
339
340struct frame_button {
341 struct widget *widget;
342 struct frame *frame;
343 cairo_surface_t *icon;
344 enum frame_button_action type;
345 enum frame_button_pointer state;
346 struct wl_list link; /* buttons_list */
347 enum frame_button_align align;
348 enum frame_button_decoration decoration;
349};
350
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500351struct frame {
352 struct widget *widget;
353 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200354 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500355};
356
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500357struct menu {
358 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500359 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500360 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500361 const char **entries;
362 uint32_t time;
363 int current;
364 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400365 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500366 menu_func_t func;
367};
368
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300369struct tooltip {
370 struct widget *parent;
371 struct window *window;
372 struct widget *widget;
373 char *entry;
374 struct task tooltip_task;
375 int tooltip_fd;
376 float x, y;
377};
378
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700379struct shm_pool {
380 struct wl_shm_pool *pool;
381 size_t size;
382 size_t used;
383 void *data;
384};
385
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400386enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300387 CURSOR_DEFAULT = 100,
388 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400389};
390
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500391enum window_location {
392 WINDOW_INTERIOR = 0,
393 WINDOW_RESIZING_TOP = 1,
394 WINDOW_RESIZING_BOTTOM = 2,
395 WINDOW_RESIZING_LEFT = 4,
396 WINDOW_RESIZING_TOP_LEFT = 5,
397 WINDOW_RESIZING_BOTTOM_LEFT = 6,
398 WINDOW_RESIZING_RIGHT = 8,
399 WINDOW_RESIZING_TOP_RIGHT = 9,
400 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
401 WINDOW_RESIZING_MASK = 15,
402 WINDOW_EXTERIOR = 16,
403 WINDOW_TITLEBAR = 17,
404 WINDOW_CLIENT_AREA = 18,
405};
406
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200407static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400408
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500409#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200411struct egl_window_surface {
412 struct toysurface base;
413 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100414 struct display *display;
415 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200416 struct wl_egl_window *egl_window;
417 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100418};
419
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200420static struct egl_window_surface *
421to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100422{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200423 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100424}
425
426static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200427egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200428 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100429{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200430 struct egl_window_surface *surface = to_egl_window_surface(base);
431
432 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
433 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
434
435 return cairo_surface_reference(surface->cairo_surface);
436}
437
438static void
439egl_window_surface_swap(struct toysurface *base,
440 struct rectangle *server_allocation)
441{
442 struct egl_window_surface *surface = to_egl_window_surface(base);
443
444 cairo_gl_surface_swapbuffers(surface->cairo_surface);
445 wl_egl_window_get_attached_size(surface->egl_window,
446 &server_allocation->width,
447 &server_allocation->height);
448}
449
450static int
451egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
452{
453 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200454 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100455
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200456 device = cairo_surface_get_device(surface->cairo_surface);
457 if (!device)
458 return -1;
459
460 if (!ctx) {
461 if (device == surface->display->argb_device)
462 ctx = surface->display->argb_ctx;
463 else
464 assert(0);
465 }
466
467 cairo_device_flush(device);
468 cairo_device_acquire(device);
469 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
470 surface->egl_surface, ctx))
471 fprintf(stderr, "failed to make surface current\n");
472
473 return 0;
474}
475
476static void
477egl_window_surface_release(struct toysurface *base)
478{
479 struct egl_window_surface *surface = to_egl_window_surface(base);
480 cairo_device_t *device;
481
482 device = cairo_surface_get_device(surface->cairo_surface);
483 if (!device)
484 return;
485
486 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
487 surface->display->argb_ctx))
488 fprintf(stderr, "failed to make context current\n");
489
490 cairo_device_release(device);
491}
492
493static void
494egl_window_surface_destroy(struct toysurface *base)
495{
496 struct egl_window_surface *surface = to_egl_window_surface(base);
497 struct display *d = surface->display;
498
499 cairo_surface_destroy(surface->cairo_surface);
500 eglDestroySurface(d->dpy, surface->egl_surface);
501 wl_egl_window_destroy(surface->egl_window);
502 surface->surface = NULL;
503
504 free(surface);
505}
506
507static struct toysurface *
508egl_window_surface_create(struct display *display,
509 struct wl_surface *wl_surface,
510 uint32_t flags,
511 struct rectangle *rectangle)
512{
513 struct egl_window_surface *surface;
514
Pekka Paalanenb3627362012-11-19 17:16:00 +0200515 if (display->dpy == EGL_NO_DISPLAY)
516 return NULL;
517
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518 surface = calloc(1, sizeof *surface);
519 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520 return NULL;
521
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522 surface->base.prepare = egl_window_surface_prepare;
523 surface->base.swap = egl_window_surface_swap;
524 surface->base.acquire = egl_window_surface_acquire;
525 surface->base.release = egl_window_surface_release;
526 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100527
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200528 surface->display = display;
529 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400530
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200531 surface->egl_window = wl_egl_window_create(surface->surface,
532 rectangle->width,
533 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100534
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200535 surface->egl_surface = eglCreateWindowSurface(display->dpy,
536 display->argb_config,
537 surface->egl_window,
538 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100539
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200540 surface->cairo_surface =
541 cairo_gl_surface_create_for_egl(display->argb_device,
542 surface->egl_surface,
543 rectangle->width,
544 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100545
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200546 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100547}
548
Pekka Paalanenb3627362012-11-19 17:16:00 +0200549#else
550
551static struct toysurface *
552egl_window_surface_create(struct display *display,
553 struct wl_surface *wl_surface,
554 uint32_t flags,
555 struct rectangle *rectangle)
556{
557 return NULL;
558}
559
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560#endif
561
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200562struct shm_surface_data {
563 struct wl_buffer *buffer;
564 struct shm_pool *pool;
565};
566
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400567struct wl_buffer *
568display_get_buffer_for_surface(struct display *display,
569 cairo_surface_t *surface)
570{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200571 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400572
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200573 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400574
575 return data->buffer;
576}
577
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500578static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700579shm_pool_destroy(struct shm_pool *pool);
580
581static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400582shm_surface_data_destroy(void *p)
583{
584 struct shm_surface_data *data = p;
585
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200586 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700587 if (data->pool)
588 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300589
590 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400591}
592
Kristian Høgsberg16626282012-04-03 11:21:27 -0400593static struct wl_shm_pool *
594make_shm_pool(struct display *display, int size, void **data)
595{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400596 struct wl_shm_pool *pool;
597 int fd;
598
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300599 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400600 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300601 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
602 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400603 return NULL;
604 }
605
606 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400607 if (*data == MAP_FAILED) {
608 fprintf(stderr, "mmap failed: %m\n");
609 close(fd);
610 return NULL;
611 }
612
613 pool = wl_shm_create_pool(display->shm, fd, size);
614
615 close(fd);
616
617 return pool;
618}
619
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700620static struct shm_pool *
621shm_pool_create(struct display *display, size_t size)
622{
623 struct shm_pool *pool = malloc(sizeof *pool);
624
625 if (!pool)
626 return NULL;
627
628 pool->pool = make_shm_pool(display, size, &pool->data);
629 if (!pool->pool) {
630 free(pool);
631 return NULL;
632 }
633
634 pool->size = size;
635 pool->used = 0;
636
637 return pool;
638}
639
640static void *
641shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
642{
643 if (pool->used + size > pool->size)
644 return NULL;
645
646 *offset = pool->used;
647 pool->used += size;
648
649 return (char *) pool->data + *offset;
650}
651
652/* destroy the pool. this does not unmap the memory though */
653static void
654shm_pool_destroy(struct shm_pool *pool)
655{
656 munmap(pool->data, pool->size);
657 wl_shm_pool_destroy(pool->pool);
658 free(pool);
659}
660
661/* Start allocating from the beginning of the pool again */
662static void
663shm_pool_reset(struct shm_pool *pool)
664{
665 pool->used = 0;
666}
667
668static int
669data_length_for_shm_surface(struct rectangle *rect)
670{
671 int stride;
672
673 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
674 rect->width);
675 return stride * rect->height;
676}
677
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500678static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700679display_create_shm_surface_from_pool(struct display *display,
680 struct rectangle *rectangle,
681 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682{
683 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400684 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400685 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700686 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400687 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400688
689 data = malloc(sizeof *data);
690 if (data == NULL)
691 return NULL;
692
693 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
694 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700695 length = stride * rectangle->height;
696 data->pool = NULL;
697 map = shm_pool_allocate(pool, length, &offset);
698
699 if (!map) {
700 free(data);
701 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400702 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400703
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400704 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400705 CAIRO_FORMAT_ARGB32,
706 rectangle->width,
707 rectangle->height,
708 stride);
709
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200710 cairo_surface_set_user_data(surface, &shm_surface_data_key,
711 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400712
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400713 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500714 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400715 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500716 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400717
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200718 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
719 rectangle->width,
720 rectangle->height,
721 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400722
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723 return surface;
724}
725
726static cairo_surface_t *
727display_create_shm_surface(struct display *display,
728 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200729 struct shm_pool *alternate_pool,
730 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700731{
732 struct shm_surface_data *data;
733 struct shm_pool *pool;
734 cairo_surface_t *surface;
735
Pekka Paalanen99436862012-11-19 17:15:59 +0200736 if (alternate_pool) {
737 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700738 surface = display_create_shm_surface_from_pool(display,
739 rectangle,
740 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200741 alternate_pool);
742 if (surface) {
743 data = cairo_surface_get_user_data(surface,
744 &shm_surface_data_key);
745 goto out;
746 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700747 }
748
749 pool = shm_pool_create(display,
750 data_length_for_shm_surface(rectangle));
751 if (!pool)
752 return NULL;
753
754 surface =
755 display_create_shm_surface_from_pool(display, rectangle,
756 flags, pool);
757
758 if (!surface) {
759 shm_pool_destroy(pool);
760 return NULL;
761 }
762
763 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200764 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700765 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400766
Pekka Paalanen99436862012-11-19 17:15:59 +0200767out:
768 if (data_ret)
769 *data_ret = data;
770
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400771 return surface;
772}
773
nobled7b87cb02011-02-01 18:51:47 +0000774static int
775check_size(struct rectangle *rect)
776{
777 if (rect->width && rect->height)
778 return 0;
779
780 fprintf(stderr, "tried to create surface of "
781 "width: %d, height: %d\n", rect->width, rect->height);
782 return -1;
783}
784
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400785cairo_surface_t *
786display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400788 struct rectangle *rectangle,
789 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400790{
nobled7b87cb02011-02-01 18:51:47 +0000791 if (check_size(rectangle) < 0)
792 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200793
794 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200795 return display_create_shm_surface(display, rectangle, flags,
796 NULL, NULL);
797}
798
Pekka Paalanena4eda732012-11-19 17:16:02 +0200799struct shm_surface_leaf {
800 cairo_surface_t *cairo_surface;
801 /* 'data' is automatically destroyed, when 'cairo_surface' is */
802 struct shm_surface_data *data;
803
804 struct shm_pool *resize_pool;
805 int busy;
806};
807
808static void
809shm_surface_leaf_release(struct shm_surface_leaf *leaf)
810{
811 if (leaf->cairo_surface)
812 cairo_surface_destroy(leaf->cairo_surface);
813 /* leaf->data already destroyed via cairo private */
814
815 if (leaf->resize_pool)
816 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200817
818 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200819}
820
Pekka Paalanen99436862012-11-19 17:15:59 +0200821struct shm_surface {
822 struct toysurface base;
823 struct display *display;
824 struct wl_surface *surface;
825 uint32_t flags;
826 int dx, dy;
827
Pekka Paalanena4eda732012-11-19 17:16:02 +0200828 struct shm_surface_leaf leaf[2];
829 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200830};
831
832static struct shm_surface *
833to_shm_surface(struct toysurface *base)
834{
835 return container_of(base, struct shm_surface, base);
836}
837
Pekka Paalanena4eda732012-11-19 17:16:02 +0200838static void
839shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
840{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200841 struct shm_surface *surface = data;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200842
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200843 if (surface->leaf[0].data->buffer == buffer)
844 surface->leaf[0].busy = 0;
845 else if (surface->leaf[1].data->buffer == buffer)
846 surface->leaf[1].busy = 0;
847 else
848 assert(0 && "shm_surface_buffer_release: unknown buffer");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200849
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200850 if (!surface->leaf[0].busy && !surface->leaf[1].busy)
851 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200852}
853
854static const struct wl_buffer_listener shm_surface_buffer_listener = {
855 shm_surface_buffer_release
856};
857
Pekka Paalanen99436862012-11-19 17:15:59 +0200858static cairo_surface_t *
859shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200860 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200861{
Pekka Paalanenec076692012-11-30 13:37:27 +0200862 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200863 struct shm_surface *surface = to_shm_surface(base);
864 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200865 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200866
867 surface->dx = dx;
868 surface->dy = dy;
869
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200870 /* See shm_surface_buffer_release() */
871 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
872 surface->leaf[1].cairo_surface) {
873 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
874 __func__);
875 }
876
Pekka Paalanena4eda732012-11-19 17:16:02 +0200877 /* pick a free buffer from the two */
878 if (!surface->leaf[0].busy)
879 leaf = &surface->leaf[0];
880 else if (!surface->leaf[1].busy)
881 leaf = &surface->leaf[1];
882 else {
883 fprintf(stderr, "%s: both buffers are held by the server.\n",
884 __func__);
885 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200886 }
887
Pekka Paalanena4eda732012-11-19 17:16:02 +0200888 if (!resize_hint && leaf->resize_pool) {
889 cairo_surface_destroy(leaf->cairo_surface);
890 leaf->cairo_surface = NULL;
891 shm_pool_destroy(leaf->resize_pool);
892 leaf->resize_pool = NULL;
893 }
894
895 if (leaf->cairo_surface &&
896 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
897 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200898 goto out;
899
Pekka Paalanena4eda732012-11-19 17:16:02 +0200900 if (leaf->cairo_surface)
901 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200902
Pekka Paalanena4eda732012-11-19 17:16:02 +0200903 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200904 /* Create a big pool to allocate from, while continuously
905 * resizing. Mmapping a new pool in the server
906 * is relatively expensive, so reusing a pool performs
907 * better, but may temporarily reserve unneeded memory.
908 */
909 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200910 leaf->resize_pool = shm_pool_create(surface->display,
911 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200912 }
913
Pekka Paalanena4eda732012-11-19 17:16:02 +0200914 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200915 display_create_shm_surface(surface->display, &rect,
916 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200917 leaf->resize_pool,
918 &leaf->data);
919 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200920 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200921
922out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200923 surface->current = leaf;
924
925 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200926}
927
928static void
929shm_surface_swap(struct toysurface *base,
930 struct rectangle *server_allocation)
931{
932 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200933 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200934
935 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200936 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200937 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200939
Pekka Paalanena4eda732012-11-19 17:16:02 +0200940 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200941 surface->dx, surface->dy);
942 wl_surface_damage(surface->surface, 0, 0,
943 server_allocation->width, server_allocation->height);
944 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200945
946 leaf->busy = 1;
947 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200948}
949
950static int
951shm_surface_acquire(struct toysurface *base, EGLContext ctx)
952{
953 return -1;
954}
955
956static void
957shm_surface_release(struct toysurface *base)
958{
959}
960
961static void
962shm_surface_destroy(struct toysurface *base)
963{
964 struct shm_surface *surface = to_shm_surface(base);
965
Pekka Paalanena4eda732012-11-19 17:16:02 +0200966 shm_surface_leaf_release(&surface->leaf[0]);
967 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200968
969 free(surface);
970}
971
972static struct toysurface *
973shm_surface_create(struct display *display, struct wl_surface *wl_surface,
974 uint32_t flags, struct rectangle *rectangle)
975{
976 struct shm_surface *surface;
977
978 surface = calloc(1, sizeof *surface);
979 if (!surface)
980 return NULL;
981
982 surface->base.prepare = shm_surface_prepare;
983 surface->base.swap = shm_surface_swap;
984 surface->base.acquire = shm_surface_acquire;
985 surface->base.release = shm_surface_release;
986 surface->base.destroy = shm_surface_destroy;
987
988 surface->display = display;
989 surface->surface = wl_surface;
990 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200991
992 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400993}
994
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200995/*
996 * The following correspondences between file names and cursors was copied
997 * from: https://bugs.kde.org/attachment.cgi?id=67313
998 */
999
1000static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001001 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001002 "sw-resize"
1003};
1004
1005static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001006 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001007 "se-resize"
1008};
1009
1010static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001011 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001012 "s-resize"
1013};
1014
1015static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001016 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001017 "closedhand",
1018 "208530c400c041818281048008011002"
1019};
1020
1021static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001022 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001023 "default",
1024 "top_left_arrow",
1025 "left-arrow"
1026};
1027
1028static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001029 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001030 "w-resize"
1031};
1032
1033static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001034 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001035 "e-resize"
1036};
1037
1038static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001039 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001040 "nw-resize"
1041};
1042
1043static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001044 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001045 "ne-resize"
1046};
1047
1048static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001049 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001050 "n-resize"
1051};
1052
1053static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001054 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001055 "ibeam",
1056 "text"
1057};
1058
1059static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001060 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001061 "pointer",
1062 "pointing_hand",
1063 "e29285e634086352946a0e7090d73106"
1064};
1065
1066static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001067 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001068 "wait",
1069 "0426c94ea35c87780ff01dc239897213"
1070};
1071
1072struct cursor_alternatives {
1073 const char **names;
1074 size_t count;
1075};
1076
1077static const struct cursor_alternatives cursors[] = {
1078 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1079 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1080 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1081 {grabbings, ARRAY_LENGTH(grabbings)},
1082 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1083 {left_sides, ARRAY_LENGTH(left_sides)},
1084 {right_sides, ARRAY_LENGTH(right_sides)},
1085 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1086 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1087 {top_sides, ARRAY_LENGTH(top_sides)},
1088 {xterms, ARRAY_LENGTH(xterms)},
1089 {hand1s, ARRAY_LENGTH(hand1s)},
1090 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001091};
1092
1093static void
1094create_cursors(struct display *display)
1095{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001096 char *config_file;
1097 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001098 unsigned int i, j;
1099 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001100 struct config_key shell_keys[] = {
1101 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1102 };
1103 struct config_section cs[] = {
1104 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1105 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001106
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001107 config_file = config_file_path("weston.ini");
1108 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1109 free(config_file);
1110
1111 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001112 display->cursors =
1113 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1114
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001115 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001116 cursor = NULL;
1117 for (j = 0; !cursor && j < cursors[i].count; ++j)
1118 cursor = wl_cursor_theme_get_cursor(
1119 display->cursor_theme, cursors[i].names[j]);
1120
1121 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001122 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001123 cursors[i].names[0]);
1124
1125 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001126 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001127}
1128
1129static void
1130destroy_cursors(struct display *display)
1131{
1132 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001133 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001134}
1135
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001136struct wl_cursor_image *
1137display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001138{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001139 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001140
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001141 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001142}
1143
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001144static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001145surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001146{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001147 if (!surface->cairo_surface)
1148 return;
1149
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001150 if (surface->opaque_region) {
1151 wl_surface_set_opaque_region(surface->surface,
1152 surface->opaque_region);
1153 wl_region_destroy(surface->opaque_region);
1154 surface->opaque_region = NULL;
1155 }
1156
1157 if (surface->input_region) {
1158 wl_surface_set_input_region(surface->surface,
1159 surface->input_region);
1160 wl_region_destroy(surface->input_region);
1161 surface->input_region = NULL;
1162 }
1163
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001164 surface->toysurface->swap(surface->toysurface,
1165 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001166
Pekka Paalanen89dee002013-02-13 16:17:20 +02001167 cairo_surface_destroy(surface->cairo_surface);
1168 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001169}
1170
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001171int
1172window_has_focus(struct window *window)
1173{
1174 return window->focus_count > 0;
1175}
1176
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001177static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001178window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001179{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001180 if (window->type == TYPE_NONE) {
1181 window->type = TYPE_TOPLEVEL;
1182 if (window->shell_surface)
1183 wl_shell_surface_set_toplevel(window->shell_surface);
1184 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001185
Pekka Paalanen89dee002013-02-13 16:17:20 +02001186 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001187}
1188
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001189struct display *
1190window_get_display(struct window *window)
1191{
1192 return window->display;
1193}
1194
Pekka Paalanen89dee002013-02-13 16:17:20 +02001195static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001196surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001197{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001198 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001199 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001200
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001201 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001202 case WL_OUTPUT_TRANSFORM_90:
1203 case WL_OUTPUT_TRANSFORM_270:
1204 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1205 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001206 allocation.width = surface->allocation.height;
1207 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001208 break;
1209 default:
1210 break;
1211 }
1212
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001213 if (!surface->toysurface && display->dpy &&
1214 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001215 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001216 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001217 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001218 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001219 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001220 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001221
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001222 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001223 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001224 surface->surface,
1225 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001226
Pekka Paalanen89dee002013-02-13 16:17:20 +02001227 surface->cairo_surface = surface->toysurface->prepare(
1228 surface->toysurface, dx, dy,
1229 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001230}
1231
1232static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001233window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001234{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001235 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001236 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001237 int dx = 0;
1238 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001239
Pekka Paalanenec076692012-11-30 13:37:27 +02001240 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001241 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001242
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001243 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1244 dx = surface->server_allocation.width -
1245 surface->allocation.width;
1246
1247 if (window->resize_edges & WINDOW_RESIZING_TOP)
1248 dy = surface->server_allocation.height -
1249 surface->allocation.height;
1250
1251 window->resize_edges = 0;
1252
Pekka Paalanen89dee002013-02-13 16:17:20 +02001253 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001254}
1255
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001256int
1257window_get_buffer_transform(struct window *window)
1258{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001259 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001260}
1261
1262void
1263window_set_buffer_transform(struct window *window,
1264 enum wl_output_transform transform)
1265{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001266 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001267 wl_surface_set_buffer_transform(window->main_surface->surface,
1268 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001269}
1270
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001271static void frame_destroy(struct frame *frame);
1272
Pekka Paalanen4e373742013-02-13 16:17:13 +02001273static void
1274surface_destroy(struct surface *surface)
1275{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001276 if (surface->input_region)
1277 wl_region_destroy(surface->input_region);
1278
1279 if (surface->opaque_region)
1280 wl_region_destroy(surface->opaque_region);
1281
Pekka Paalanen4e373742013-02-13 16:17:13 +02001282 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001283
1284 if (surface->toysurface)
1285 surface->toysurface->destroy(surface->toysurface);
1286
Pekka Paalanen4e373742013-02-13 16:17:13 +02001287 free(surface);
1288}
1289
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001290void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001291window_destroy(struct window *window)
1292{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001293 struct display *display = window->display;
1294 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001295 struct window_output *window_output;
1296 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001297
1298 if (window->redraw_scheduled)
1299 wl_list_remove(&window->redraw_task.link);
1300
1301 wl_list_for_each(input, &display->input_list, link) {
1302 if (input->pointer_focus == window)
1303 input->pointer_focus = NULL;
1304 if (input->keyboard_focus == window)
1305 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001306 if (input->focus_widget &&
1307 input->focus_widget->window == window)
1308 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001309 }
1310
Rob Bradford7507b572012-05-15 17:55:34 +01001311 wl_list_for_each_safe(window_output, window_output_tmp,
1312 &window->window_output_list, link) {
1313 free (window_output);
1314 }
1315
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001316 if (window->frame)
1317 frame_destroy(window->frame);
1318
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001319 if (window->shell_surface)
1320 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001321
1322 surface_destroy(window->main_surface);
1323
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001324 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001325
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001326 if (window->frame_cb)
1327 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001328 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001329 free(window);
1330}
1331
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001332static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001333widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001334{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001335 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001336
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001337 wl_list_for_each(child, &widget->child_list, link) {
1338 target = widget_find_widget(child, x, y);
1339 if (target)
1340 return target;
1341 }
1342
1343 if (widget->allocation.x <= x &&
1344 x < widget->allocation.x + widget->allocation.width &&
1345 widget->allocation.y <= y &&
1346 y < widget->allocation.y + widget->allocation.height) {
1347 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001348 }
1349
1350 return NULL;
1351}
1352
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001353static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001354window_find_widget(struct window *window, int32_t x, int32_t y)
1355{
1356 return widget_find_widget(window->main_surface->widget, x, y);
1357}
1358
1359static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001360widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001361{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001362 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001363
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001364 widget = malloc(sizeof *widget);
1365 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001366 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001367 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001368 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001369 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001370 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001371 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001372 widget->tooltip = NULL;
1373 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001374 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001375
1376 return widget;
1377}
1378
1379struct widget *
1380window_add_widget(struct window *window, void *data)
1381{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001382 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001383
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001384 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001385 wl_list_init(&widget->link);
1386 window->main_surface->widget = widget;
1387
1388 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001389}
1390
1391struct widget *
1392widget_add_widget(struct widget *parent, void *data)
1393{
1394 struct widget *widget;
1395
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001396 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001397 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001398
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001399 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001400}
1401
1402void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001403widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001404{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001405 struct display *display = widget->window->display;
1406 struct input *input;
1407
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001408 if (widget->tooltip) {
1409 free(widget->tooltip);
1410 widget->tooltip = NULL;
1411 }
1412
Pekka Paalanene156fb62012-01-19 13:51:38 +02001413 wl_list_for_each(input, &display->input_list, link) {
1414 if (input->focus_widget == widget)
1415 input->focus_widget = NULL;
1416 }
1417
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001418 wl_list_remove(&widget->link);
1419 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001420}
1421
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001422void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001423widget_set_default_cursor(struct widget *widget, int cursor)
1424{
1425 widget->default_cursor = cursor;
1426}
1427
1428void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001429widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001430{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001431 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001432}
1433
1434void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001435widget_set_size(struct widget *widget, int32_t width, int32_t height)
1436{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001437 widget->allocation.width = width;
1438 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001439}
1440
1441void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001442widget_set_allocation(struct widget *widget,
1443 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001444{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001445 widget->allocation.x = x;
1446 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001447 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001448}
1449
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001450void
1451widget_set_transparent(struct widget *widget, int transparent)
1452{
1453 widget->opaque = !transparent;
1454}
1455
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001456void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001457widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001458{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001459 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001460}
1461
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001462static cairo_surface_t *
1463widget_get_cairo_surface(struct widget *widget)
1464{
1465 struct surface *surface = widget->surface;
1466 struct window *window = widget->window;
1467
1468 if (!surface->cairo_surface) {
1469 if (surface == window->main_surface)
1470 window_create_main_surface(window);
1471 else
1472 surface_create_surface(surface, 0, 0, 0);
1473 }
1474
1475 return surface->cairo_surface;
1476}
1477
1478cairo_t *
1479widget_cairo_create(struct widget *widget)
1480{
1481 cairo_surface_t *cairo_surface;
1482 cairo_t *cr;
1483
1484 cairo_surface = widget_get_cairo_surface(widget);
1485 cr = cairo_create(cairo_surface);
1486
1487 return cr;
1488}
1489
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001490void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001491widget_set_resize_handler(struct widget *widget,
1492 widget_resize_handler_t handler)
1493{
1494 widget->resize_handler = handler;
1495}
1496
1497void
1498widget_set_redraw_handler(struct widget *widget,
1499 widget_redraw_handler_t handler)
1500{
1501 widget->redraw_handler = handler;
1502}
1503
1504void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001505widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001506{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001507 widget->enter_handler = handler;
1508}
1509
1510void
1511widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1512{
1513 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001514}
1515
1516void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001517widget_set_motion_handler(struct widget *widget,
1518 widget_motion_handler_t handler)
1519{
1520 widget->motion_handler = handler;
1521}
1522
1523void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001524widget_set_button_handler(struct widget *widget,
1525 widget_button_handler_t handler)
1526{
1527 widget->button_handler = handler;
1528}
1529
1530void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001531widget_set_axis_handler(struct widget *widget,
1532 widget_axis_handler_t handler)
1533{
1534 widget->axis_handler = handler;
1535}
1536
1537void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001538widget_schedule_redraw(struct widget *widget)
1539{
1540 window_schedule_redraw(widget->window);
1541}
1542
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001543cairo_surface_t *
1544window_get_surface(struct window *window)
1545{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001546 cairo_surface_t *cairo_surface;
1547
1548 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1549
1550 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001551}
1552
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001553struct wl_surface *
1554window_get_wl_surface(struct window *window)
1555{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001556 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001557}
1558
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001559struct wl_shell_surface *
1560window_get_wl_shell_surface(struct window *window)
1561{
1562 return window->shell_surface;
1563}
1564
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001565static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001566tooltip_redraw_handler(struct widget *widget, void *data)
1567{
1568 cairo_t *cr;
1569 const int32_t r = 3;
1570 struct tooltip *tooltip = data;
1571 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001572
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001573 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001574 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1575 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1576 cairo_paint(cr);
1577
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001578 width = widget->allocation.width;
1579 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001580 rounded_rect(cr, 0, 0, width, height, r);
1581
1582 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1583 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1584 cairo_fill(cr);
1585
1586 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1587 cairo_move_to(cr, 10, 16);
1588 cairo_show_text(cr, tooltip->entry);
1589 cairo_destroy(cr);
1590}
1591
1592static cairo_text_extents_t
1593get_text_extents(struct tooltip *tooltip)
1594{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001595 cairo_t *cr;
1596 cairo_text_extents_t extents;
1597
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001598 /* Use the dummy_surface because tooltip's surface was not
1599 * created yet, and parent does not have a valid surface
1600 * outside repaint, either.
1601 */
1602 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001603 cairo_text_extents(cr, tooltip->entry, &extents);
1604 cairo_destroy(cr);
1605
1606 return extents;
1607}
1608
1609static int
1610window_create_tooltip(struct tooltip *tooltip)
1611{
1612 struct widget *parent = tooltip->parent;
1613 struct display *display = parent->window->display;
1614 struct window *window;
1615 const int offset_y = 27;
1616 const int margin = 3;
1617 cairo_text_extents_t extents;
1618
1619 if (tooltip->widget)
1620 return 0;
1621
1622 window = window_create_transient(display, parent->window, tooltip->x,
1623 tooltip->y + offset_y,
1624 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1625 if (!window)
1626 return -1;
1627
1628 tooltip->window = window;
1629 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1630
1631 extents = get_text_extents(tooltip);
1632 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1633 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1634
1635 return 0;
1636}
1637
1638void
1639widget_destroy_tooltip(struct widget *parent)
1640{
1641 struct tooltip *tooltip = parent->tooltip;
1642
1643 parent->tooltip_count = 0;
1644 if (!tooltip)
1645 return;
1646
1647 if (tooltip->widget) {
1648 widget_destroy(tooltip->widget);
1649 window_destroy(tooltip->window);
1650 tooltip->widget = NULL;
1651 tooltip->window = NULL;
1652 }
1653
1654 close(tooltip->tooltip_fd);
1655 free(tooltip->entry);
1656 free(tooltip);
1657 parent->tooltip = NULL;
1658}
1659
1660static void
1661tooltip_func(struct task *task, uint32_t events)
1662{
1663 struct tooltip *tooltip =
1664 container_of(task, struct tooltip, tooltip_task);
1665 uint64_t exp;
1666
Martin Olsson8df662a2012-07-08 03:03:47 +02001667 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1668 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001669 window_create_tooltip(tooltip);
1670}
1671
1672#define TOOLTIP_TIMEOUT 500
1673static int
1674tooltip_timer_reset(struct tooltip *tooltip)
1675{
1676 struct itimerspec its;
1677
1678 its.it_interval.tv_sec = 0;
1679 its.it_interval.tv_nsec = 0;
1680 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1681 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1682 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1683 fprintf(stderr, "could not set timerfd\n: %m");
1684 return -1;
1685 }
1686
1687 return 0;
1688}
1689
1690int
1691widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1692{
1693 struct tooltip *tooltip = parent->tooltip;
1694
1695 parent->tooltip_count++;
1696 if (tooltip) {
1697 tooltip->x = x;
1698 tooltip->y = y;
1699 tooltip_timer_reset(tooltip);
1700 return 0;
1701 }
1702
1703 /* the handler might be triggered too fast via input device motion, so
1704 * we need this check here to make sure tooltip is fully initialized */
1705 if (parent->tooltip_count > 1)
1706 return 0;
1707
1708 tooltip = malloc(sizeof *tooltip);
1709 if (!tooltip)
1710 return -1;
1711
1712 parent->tooltip = tooltip;
1713 tooltip->parent = parent;
1714 tooltip->widget = NULL;
1715 tooltip->window = NULL;
1716 tooltip->x = x;
1717 tooltip->y = y;
1718 tooltip->entry = strdup(entry);
1719 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1720 if (tooltip->tooltip_fd < 0) {
1721 fprintf(stderr, "could not create timerfd\n: %m");
1722 return -1;
1723 }
1724
1725 tooltip->tooltip_task.run = tooltip_func;
1726 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1727 EPOLLIN, &tooltip->tooltip_task);
1728 tooltip_timer_reset(tooltip);
1729
1730 return 0;
1731}
1732
1733static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001734workspace_manager_state(void *data,
1735 struct workspace_manager *workspace_manager,
1736 uint32_t current,
1737 uint32_t count)
1738{
1739 struct display *display = data;
1740
1741 display->workspace = current;
1742 display->workspace_count = count;
1743}
1744
1745static const struct workspace_manager_listener workspace_manager_listener = {
1746 workspace_manager_state
1747};
1748
1749static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001750frame_resize_handler(struct widget *widget,
1751 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001752{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001753 struct frame *frame = data;
1754 struct widget *child = frame->child;
1755 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001756 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001757 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001758 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001759 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001760 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001761 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001762 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001763
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001764 switch (widget->window->type) {
1765 case TYPE_FULLSCREEN:
1766 decoration_width = 0;
1767 decoration_height = 0;
1768
1769 allocation.x = 0;
1770 allocation.y = 0;
1771 allocation.width = width;
1772 allocation.height = height;
1773 opaque_margin = 0;
1774
1775 wl_list_for_each(button, &frame->buttons_list, link)
1776 button->widget->opaque = 1;
1777 break;
1778 case TYPE_MAXIMIZED:
1779 decoration_width = t->width * 2;
1780 decoration_height = t->width + t->titlebar_height;
1781
1782 allocation.x = t->width;
1783 allocation.y = t->titlebar_height;
1784 allocation.width = width - decoration_width;
1785 allocation.height = height - decoration_height;
1786
1787 opaque_margin = 0;
1788
1789 wl_list_for_each(button, &frame->buttons_list, link)
1790 button->widget->opaque = 0;
1791 break;
1792 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001793 decoration_width = (t->width + t->margin) * 2;
1794 decoration_height = t->width +
1795 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001796
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001797 allocation.x = t->width + t->margin;
1798 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001799 allocation.width = width - decoration_width;
1800 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001801
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001802 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001803
1804 wl_list_for_each(button, &frame->buttons_list, link)
1805 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001806 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001807 }
1808
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001809 widget_set_allocation(child, allocation.x, allocation.y,
1810 allocation.width, allocation.height);
1811
1812 if (child->resize_handler)
1813 child->resize_handler(child,
1814 allocation.width,
1815 allocation.height,
1816 child->user_data);
1817
Scott Moreauf7e498c2012-05-14 11:39:29 -06001818 width = child->allocation.width + decoration_width;
1819 height = child->allocation.height + decoration_height;
1820
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001821 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1822
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001823 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001824 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001825 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001826 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001827 shadow_margin, shadow_margin,
1828 width - 2 * shadow_margin,
1829 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001830 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001831 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001832 }
1833
Scott Moreauf7e498c2012-05-14 11:39:29 -06001834 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001835
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001836 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001837 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001838 opaque_margin, opaque_margin,
1839 widget->allocation.width - 2 * opaque_margin,
1840 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001841
1842 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001843 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1844 x_l = t->width + shadow_margin;
1845 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001846 wl_list_for_each(button, &frame->buttons_list, link) {
1847 const int button_padding = 4;
1848 w = cairo_image_surface_get_width(button->icon);
1849 h = cairo_image_surface_get_height(button->icon);
1850
1851 if (button->decoration == FRAME_BUTTON_FANCY)
1852 w += 10;
1853
1854 if (button->align == FRAME_BUTTON_LEFT) {
1855 widget_set_allocation(button->widget,
1856 x_l, y , w + 1, h + 1);
1857 x_l += w;
1858 x_l += button_padding;
1859 } else {
1860 x_r -= w;
1861 widget_set_allocation(button->widget,
1862 x_r, y , w + 1, h + 1);
1863 x_r -= button_padding;
1864 }
1865 }
1866}
1867
1868static int
1869frame_button_enter_handler(struct widget *widget,
1870 struct input *input, float x, float y, void *data)
1871{
1872 struct frame_button *frame_button = data;
1873
1874 widget_schedule_redraw(frame_button->widget);
1875 frame_button->state = FRAME_BUTTON_OVER;
1876
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001877 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001878}
1879
1880static void
1881frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1882{
1883 struct frame_button *frame_button = data;
1884
1885 widget_schedule_redraw(frame_button->widget);
1886 frame_button->state = FRAME_BUTTON_DEFAULT;
1887}
1888
1889static void
1890frame_button_button_handler(struct widget *widget,
1891 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001892 uint32_t button,
1893 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001894{
1895 struct frame_button *frame_button = data;
1896 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001897 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001898
1899 if (button != BTN_LEFT)
1900 return;
1901
1902 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001903 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001904 frame_button->state = FRAME_BUTTON_ACTIVE;
1905 widget_schedule_redraw(frame_button->widget);
1906
1907 if (frame_button->type == FRAME_BUTTON_ICON)
1908 window_show_frame_menu(window, input, time);
1909 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001910 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001911 frame_button->state = FRAME_BUTTON_DEFAULT;
1912 widget_schedule_redraw(frame_button->widget);
1913 break;
1914 }
1915
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001916 if (!was_pressed)
1917 return;
1918
Martin Minarik1998b152012-05-10 02:04:35 +02001919 switch (frame_button->type) {
1920 case FRAME_BUTTON_CLOSE:
1921 if (window->close_handler)
1922 window->close_handler(window->parent,
1923 window->user_data);
1924 else
1925 display_exit(window->display);
1926 break;
1927 case FRAME_BUTTON_MINIMIZE:
1928 fprintf(stderr,"Minimize stub\n");
1929 break;
1930 case FRAME_BUTTON_MAXIMIZE:
1931 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1932 break;
1933 default:
1934 /* Unknown operation */
1935 break;
1936 }
1937}
1938
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001939static int
1940frame_button_motion_handler(struct widget *widget,
1941 struct input *input, uint32_t time,
1942 float x, float y, void *data)
1943{
1944 struct frame_button *frame_button = data;
1945 enum frame_button_pointer previous_button_state = frame_button->state;
1946
1947 /* only track state for a pressed button */
1948 if (input->grab != widget)
1949 return CURSOR_LEFT_PTR;
1950
1951 if (x > widget->allocation.x &&
1952 x < (widget->allocation.x + widget->allocation.width) &&
1953 y > widget->allocation.y &&
1954 y < (widget->allocation.y + widget->allocation.height)) {
1955 frame_button->state = FRAME_BUTTON_ACTIVE;
1956 } else {
1957 frame_button->state = FRAME_BUTTON_DEFAULT;
1958 }
1959
1960 if (frame_button->state != previous_button_state)
1961 widget_schedule_redraw(frame_button->widget);
1962
1963 return CURSOR_LEFT_PTR;
1964}
1965
Martin Minarik1998b152012-05-10 02:04:35 +02001966static void
1967frame_button_redraw_handler(struct widget *widget, void *data)
1968{
1969 struct frame_button *frame_button = data;
1970 cairo_t *cr;
1971 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02001972
1973 x = widget->allocation.x;
1974 y = widget->allocation.y;
1975 width = widget->allocation.width;
1976 height = widget->allocation.height;
1977
1978 if (!width)
1979 return;
1980 if (!height)
1981 return;
1982 if (widget->opaque)
1983 return;
1984
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001985 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02001986
1987 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1988 cairo_set_line_width(cr, 1);
1989
1990 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1991 cairo_rectangle (cr, x, y, 25, 16);
1992
1993 cairo_stroke_preserve(cr);
1994
1995 switch (frame_button->state) {
1996 case FRAME_BUTTON_DEFAULT:
1997 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1998 break;
1999 case FRAME_BUTTON_OVER:
2000 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2001 break;
2002 case FRAME_BUTTON_ACTIVE:
2003 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2004 break;
2005 }
2006
2007 cairo_fill (cr);
2008
2009 x += 4;
2010 }
2011
2012 cairo_set_source_surface(cr, frame_button->icon, x, y);
2013 cairo_paint(cr);
2014
2015 cairo_destroy(cr);
2016}
2017
2018static struct widget *
2019frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2020 enum frame_button_align align, enum frame_button_decoration style)
2021{
2022 struct frame_button *frame_button;
2023 const char *icon = data;
2024
2025 frame_button = malloc (sizeof *frame_button);
2026 memset(frame_button, 0, sizeof *frame_button);
2027
2028 frame_button->icon = cairo_image_surface_create_from_png(icon);
2029 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2030 frame_button->frame = frame;
2031 frame_button->type = type;
2032 frame_button->align = align;
2033 frame_button->decoration = style;
2034
2035 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2036
2037 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2038 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2039 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2040 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002041 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002042 return frame_button->widget;
2043}
2044
2045static void
2046frame_button_destroy(struct frame_button *frame_button)
2047{
2048 widget_destroy(frame_button->widget);
2049 wl_list_remove(&frame_button->link);
2050 cairo_surface_destroy(frame_button->icon);
2051 free(frame_button);
2052
2053 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002054}
2055
2056static void
2057frame_redraw_handler(struct widget *widget, void *data)
2058{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002059 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002060 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002061 struct theme *t = window->display->theme;
2062 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002063
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002064 if (window->type == TYPE_FULLSCREEN)
2065 return;
2066
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002067 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002068
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002069 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002070 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002071 if (window->type == TYPE_MAXIMIZED)
2072 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002073 theme_render_frame(t, cr, widget->allocation.width,
2074 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002075
2076 cairo_destroy(cr);
2077}
2078
2079static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002080frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002081{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002082 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002083 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002084 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002085
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002086 location = theme_get_location(t, input->sx, input->sy,
2087 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002088 frame->widget->allocation.height,
2089 window->type == TYPE_MAXIMIZED ?
2090 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002091
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002092 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002093 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002094 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002095 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002096 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002097 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002098 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002099 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002100 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002101 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002102 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002103 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002104 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002105 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002106 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002107 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002108 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002109 case THEME_LOCATION_EXTERIOR:
2110 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002111 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002112 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002113 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002114}
2115
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002116static void
2117frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002118{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002119 struct display *display;
2120
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002121 switch (index) {
2122 case 0: /* close */
2123 if (window->close_handler)
2124 window->close_handler(window->parent,
2125 window->user_data);
2126 else
2127 display_exit(window->display);
2128 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002129 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002130 display = window->display;
2131 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002132 workspace_manager_move_surface(
2133 display->workspace_manager,
2134 window->main_surface->surface,
2135 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002136 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002137 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002138 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002139 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002140 workspace_manager_move_surface(
2141 display->workspace_manager,
2142 window->main_surface->surface,
2143 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002144 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002145 case 3: /* fullscreen */
2146 /* we don't have a way to get out of fullscreen for now */
2147 if (window->fullscreen_handler)
2148 window->fullscreen_handler(window, window->user_data);
2149 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002150 }
2151}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002152
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002153void
2154window_show_frame_menu(struct window *window,
2155 struct input *input, uint32_t time)
2156{
2157 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002158 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002159
2160 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002161 "Close",
2162 "Move to workspace above", "Move to workspace below",
2163 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002164 };
2165
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002166 if (window->fullscreen_handler)
2167 count = ARRAY_LENGTH(entries);
2168 else
2169 count = ARRAY_LENGTH(entries) - 1;
2170
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002171 input_get_position(input, &x, &y);
2172 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002173 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002174}
2175
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002176static int
2177frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002178 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002179{
2180 return frame_get_pointer_image_for_location(data, input);
2181}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002182
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002183static int
2184frame_motion_handler(struct widget *widget,
2185 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002186 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002187{
2188 return frame_get_pointer_image_for_location(data, input);
2189}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002190
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002191static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192frame_button_handler(struct widget *widget,
2193 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002194 uint32_t button, enum wl_pointer_button_state state,
2195 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002196
2197{
2198 struct frame *frame = data;
2199 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002200 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002201 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002202
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002203 location = theme_get_location(display->theme, input->sx, input->sy,
2204 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002205 frame->widget->allocation.height,
2206 window->type == TYPE_MAXIMIZED ?
2207 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002208
Daniel Stone4dbadb12012-05-30 16:31:51 +01002209 if (window->display->shell && button == BTN_LEFT &&
2210 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002212 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002213 if (!window->shell_surface)
2214 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002215 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002216 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002217 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002218 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002220 case THEME_LOCATION_RESIZING_TOP:
2221 case THEME_LOCATION_RESIZING_BOTTOM:
2222 case THEME_LOCATION_RESIZING_LEFT:
2223 case THEME_LOCATION_RESIZING_RIGHT:
2224 case THEME_LOCATION_RESIZING_TOP_LEFT:
2225 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2226 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2227 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002228 if (!window->shell_surface)
2229 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002230 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002231
Pekka Paalanen99436862012-11-19 17:15:59 +02002232 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002233 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002234 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002235 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002236 break;
2237 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002238 } else if (button == BTN_RIGHT &&
2239 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002240 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002241 }
2242}
2243
2244struct widget *
2245frame_create(struct window *window, void *data)
2246{
2247 struct frame *frame;
2248
2249 frame = malloc(sizeof *frame);
2250 memset(frame, 0, sizeof *frame);
2251
2252 frame->widget = window_add_widget(window, frame);
2253 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002254
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002255 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2256 widget_set_resize_handler(frame->widget, frame_resize_handler);
2257 widget_set_enter_handler(frame->widget, frame_enter_handler);
2258 widget_set_motion_handler(frame->widget, frame_motion_handler);
2259 widget_set_button_handler(frame->widget, frame_button_handler);
2260
Martin Minarik1998b152012-05-10 02:04:35 +02002261 /* Create empty list for frame buttons */
2262 wl_list_init(&frame->buttons_list);
2263
2264 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2265 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2266
2267 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2268 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2269
2270 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2271 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2272
2273 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2274 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2275
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002276 window->frame = frame;
2277
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002278 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002279}
2280
Kristian Høgsberga1627922012-06-20 17:30:03 -04002281void
2282frame_set_child_size(struct widget *widget, int child_width, int child_height)
2283{
2284 struct display *display = widget->window->display;
2285 struct theme *t = display->theme;
2286 int decoration_width, decoration_height;
2287 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002288 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002289
2290 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002291 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002292 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002293 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002294
2295 width = child_width + decoration_width;
2296 height = child_height + decoration_height;
2297 } else {
2298 width = child_width;
2299 height = child_height;
2300 }
2301
2302 window_schedule_resize(widget->window, width, height);
2303}
2304
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002305static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002306frame_destroy(struct frame *frame)
2307{
Martin Minarik1998b152012-05-10 02:04:35 +02002308 struct frame_button *button, *tmp;
2309
2310 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2311 frame_button_destroy(button);
2312
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002313 /* frame->child must be destroyed by the application */
2314 widget_destroy(frame->widget);
2315 free(frame);
2316}
2317
2318static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002319input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002320 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002321{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002322 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002323 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002324
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002325 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002326 return;
2327
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002328 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002329 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002330 widget = old;
2331 if (input->grab)
2332 widget = input->grab;
2333 if (widget->leave_handler)
2334 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002335 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002336 }
2337
2338 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002339 widget = focus;
2340 if (input->grab)
2341 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002342 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002343 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002344 cursor = widget->enter_handler(focus, input, x, y,
2345 widget->user_data);
2346 else
2347 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002348
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002349 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002350 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002351}
2352
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002353void
2354input_grab(struct input *input, struct widget *widget, uint32_t button)
2355{
2356 input->grab = widget;
2357 input->grab_button = button;
2358}
2359
2360void
2361input_ungrab(struct input *input)
2362{
2363 struct widget *widget;
2364
2365 input->grab = NULL;
2366 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002367 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002368 input->sx, input->sy);
2369 input_set_focus_widget(input, widget, input->sx, input->sy);
2370 }
2371}
2372
2373static void
2374input_remove_pointer_focus(struct input *input)
2375{
2376 struct window *window = input->pointer_focus;
2377
2378 if (!window)
2379 return;
2380
2381 input_set_focus_widget(input, NULL, 0, 0);
2382
2383 input->pointer_focus = NULL;
2384 input->current_cursor = CURSOR_UNSET;
2385}
2386
2387static void
2388pointer_handle_enter(void *data, struct wl_pointer *pointer,
2389 uint32_t serial, struct wl_surface *surface,
2390 wl_fixed_t sx_w, wl_fixed_t sy_w)
2391{
2392 struct input *input = data;
2393 struct window *window;
2394 struct widget *widget;
2395 float sx = wl_fixed_to_double(sx_w);
2396 float sy = wl_fixed_to_double(sy_w);
2397
2398 if (!surface) {
2399 /* enter event for a window we've just destroyed */
2400 return;
2401 }
2402
2403 input->display->serial = serial;
2404 input->pointer_enter_serial = serial;
2405 input->pointer_focus = wl_surface_get_user_data(surface);
2406 window = input->pointer_focus;
2407
Pekka Paalanen99436862012-11-19 17:15:59 +02002408 if (window->resizing) {
2409 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002410 /* Schedule a redraw to free the pool */
2411 window_schedule_redraw(window);
2412 }
2413
2414 input->sx = sx;
2415 input->sy = sy;
2416
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002417 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002418 input_set_focus_widget(input, widget, sx, sy);
2419}
2420
2421static void
2422pointer_handle_leave(void *data, struct wl_pointer *pointer,
2423 uint32_t serial, struct wl_surface *surface)
2424{
2425 struct input *input = data;
2426
2427 input->display->serial = serial;
2428 input_remove_pointer_focus(input);
2429}
2430
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002431static void
Daniel Stone37816df2012-05-16 18:45:18 +01002432pointer_handle_motion(void *data, struct wl_pointer *pointer,
2433 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002434{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002435 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002436 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002437 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002438 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002439 float sx = wl_fixed_to_double(sx_w);
2440 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002441
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002442 input->sx = sx;
2443 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002444
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002445 if (!window)
2446 return;
2447
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002448 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002449 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002450 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002451 }
2452
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002453 if (input->grab)
2454 widget = input->grab;
2455 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002456 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002457 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002458 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002459 input, time, sx, sy,
2460 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002461 else
2462 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002463
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002464 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002465}
2466
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002467static void
Daniel Stone37816df2012-05-16 18:45:18 +01002468pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002469 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002470{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002471 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002472 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002473 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002474
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002475 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002476 if (input->focus_widget && input->grab == NULL &&
2477 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002478 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002479
Neil Roberts6b28aad2012-01-23 19:11:18 +00002480 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002481 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002482 (*widget->button_handler)(widget,
2483 input, time,
2484 button, state,
2485 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002486
Daniel Stone4dbadb12012-05-30 16:31:51 +01002487 if (input->grab && input->grab_button == button &&
2488 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002489 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002490}
2491
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002492static void
Daniel Stone37816df2012-05-16 18:45:18 +01002493pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002494 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002495{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002496 struct input *input = data;
2497 struct widget *widget;
2498
2499 widget = input->focus_widget;
2500 if (input->grab)
2501 widget = input->grab;
2502 if (widget && widget->axis_handler)
2503 (*widget->axis_handler)(widget,
2504 input, time,
2505 axis, value,
2506 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002507}
2508
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002509static const struct wl_pointer_listener pointer_listener = {
2510 pointer_handle_enter,
2511 pointer_handle_leave,
2512 pointer_handle_motion,
2513 pointer_handle_button,
2514 pointer_handle_axis,
2515};
2516
2517static void
2518input_remove_keyboard_focus(struct input *input)
2519{
2520 struct window *window = input->keyboard_focus;
2521 struct itimerspec its;
2522
2523 its.it_interval.tv_sec = 0;
2524 its.it_interval.tv_nsec = 0;
2525 its.it_value.tv_sec = 0;
2526 its.it_value.tv_nsec = 0;
2527 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2528
2529 if (!window)
2530 return;
2531
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002532 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002533 if (window->keyboard_focus_handler)
2534 (*window->keyboard_focus_handler)(window, NULL,
2535 window->user_data);
2536
2537 input->keyboard_focus = NULL;
2538}
2539
2540static void
2541keyboard_repeat_func(struct task *task, uint32_t events)
2542{
2543 struct input *input =
2544 container_of(task, struct input, repeat_task);
2545 struct window *window = input->keyboard_focus;
2546 uint64_t exp;
2547
2548 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2549 /* If we change the timer between the fd becoming
2550 * readable and getting here, there'll be nothing to
2551 * read and we get EAGAIN. */
2552 return;
2553
2554 if (window && window->key_handler) {
2555 (*window->key_handler)(window, input, input->repeat_time,
2556 input->repeat_key, input->repeat_sym,
2557 WL_KEYBOARD_KEY_STATE_PRESSED,
2558 window->user_data);
2559 }
2560}
2561
2562static void
2563keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2564 uint32_t format, int fd, uint32_t size)
2565{
2566 struct input *input = data;
2567 char *map_str;
2568
2569 if (!data) {
2570 close(fd);
2571 return;
2572 }
2573
2574 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2575 close(fd);
2576 return;
2577 }
2578
2579 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2580 if (map_str == MAP_FAILED) {
2581 close(fd);
2582 return;
2583 }
2584
2585 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2586 map_str,
2587 XKB_KEYMAP_FORMAT_TEXT_V1,
2588 0);
2589 munmap(map_str, size);
2590 close(fd);
2591
2592 if (!input->xkb.keymap) {
2593 fprintf(stderr, "failed to compile keymap\n");
2594 return;
2595 }
2596
2597 input->xkb.state = xkb_state_new(input->xkb.keymap);
2598 if (!input->xkb.state) {
2599 fprintf(stderr, "failed to create XKB state\n");
2600 xkb_map_unref(input->xkb.keymap);
2601 input->xkb.keymap = NULL;
2602 return;
2603 }
2604
2605 input->xkb.control_mask =
2606 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2607 input->xkb.alt_mask =
2608 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2609 input->xkb.shift_mask =
2610 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2611}
2612
2613static void
2614keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2615 uint32_t serial, struct wl_surface *surface,
2616 struct wl_array *keys)
2617{
2618 struct input *input = data;
2619 struct window *window;
2620
2621 input->display->serial = serial;
2622 input->keyboard_focus = wl_surface_get_user_data(surface);
2623
2624 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002625 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002626 if (window->keyboard_focus_handler)
2627 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002628 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002629}
2630
2631static void
2632keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2633 uint32_t serial, struct wl_surface *surface)
2634{
2635 struct input *input = data;
2636
2637 input->display->serial = serial;
2638 input_remove_keyboard_focus(input);
2639}
2640
Scott Moreau210d0792012-03-22 10:47:01 -06002641static void
Daniel Stone37816df2012-05-16 18:45:18 +01002642keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002643 uint32_t serial, uint32_t time, uint32_t key,
2644 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002645{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002646 struct input *input = data;
2647 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002648 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002649 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002650 const xkb_keysym_t *syms;
2651 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002652 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002653
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002654 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002655 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002656 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002657 return;
2658
Daniel Stone97f68542012-05-30 16:32:01 +01002659 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002660
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002661 sym = XKB_KEY_NoSymbol;
2662 if (num_syms == 1)
2663 sym = syms[0];
2664
2665 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002666 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002667 window_set_maximized(window,
2668 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002669 } else if (sym == XKB_KEY_F11 &&
2670 window->fullscreen_handler &&
2671 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2672 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002673 } else if (sym == XKB_KEY_F4 &&
2674 input->modifiers == MOD_ALT_MASK &&
2675 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2676 if (window->close_handler)
2677 window->close_handler(window->parent,
2678 window->user_data);
2679 else
2680 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002681 } else if (window->key_handler) {
2682 (*window->key_handler)(window, input, time, key,
2683 sym, state, window->user_data);
2684 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002685
2686 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2687 key == input->repeat_key) {
2688 its.it_interval.tv_sec = 0;
2689 its.it_interval.tv_nsec = 0;
2690 its.it_value.tv_sec = 0;
2691 its.it_value.tv_nsec = 0;
2692 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2693 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2694 input->repeat_sym = sym;
2695 input->repeat_key = key;
2696 input->repeat_time = time;
2697 its.it_interval.tv_sec = 0;
2698 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2699 its.it_value.tv_sec = 0;
2700 its.it_value.tv_nsec = 400 * 1000 * 1000;
2701 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2702 }
2703}
2704
2705static void
Daniel Stone351eb612012-05-31 15:27:47 -04002706keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2707 uint32_t serial, uint32_t mods_depressed,
2708 uint32_t mods_latched, uint32_t mods_locked,
2709 uint32_t group)
2710{
2711 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002712 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002713
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002714 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2715 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002716 mask = xkb_state_serialize_mods(input->xkb.state,
2717 XKB_STATE_DEPRESSED |
2718 XKB_STATE_LATCHED);
2719 input->modifiers = 0;
2720 if (mask & input->xkb.control_mask)
2721 input->modifiers |= MOD_CONTROL_MASK;
2722 if (mask & input->xkb.alt_mask)
2723 input->modifiers |= MOD_ALT_MASK;
2724 if (mask & input->xkb.shift_mask)
2725 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002726}
2727
Daniel Stone37816df2012-05-16 18:45:18 +01002728static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002729 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002730 keyboard_handle_enter,
2731 keyboard_handle_leave,
2732 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002733 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002734};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002735
2736static void
Daniel Stone37816df2012-05-16 18:45:18 +01002737seat_handle_capabilities(void *data, struct wl_seat *seat,
2738 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002739{
Daniel Stone37816df2012-05-16 18:45:18 +01002740 struct input *input = data;
2741
2742 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2743 input->pointer = wl_seat_get_pointer(seat);
2744 wl_pointer_set_user_data(input->pointer, input);
2745 wl_pointer_add_listener(input->pointer, &pointer_listener,
2746 input);
2747 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2748 wl_pointer_destroy(input->pointer);
2749 input->pointer = NULL;
2750 }
2751
2752 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2753 input->keyboard = wl_seat_get_keyboard(seat);
2754 wl_keyboard_set_user_data(input->keyboard, input);
2755 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2756 input);
2757 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2758 wl_keyboard_destroy(input->keyboard);
2759 input->keyboard = NULL;
2760 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002761}
2762
Daniel Stone37816df2012-05-16 18:45:18 +01002763static const struct wl_seat_listener seat_listener = {
2764 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002765};
2766
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002767void
2768input_get_position(struct input *input, int32_t *x, int32_t *y)
2769{
2770 *x = input->sx;
2771 *y = input->sy;
2772}
2773
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002774struct display *
2775input_get_display(struct input *input)
2776{
2777 return input->display;
2778}
2779
Daniel Stone37816df2012-05-16 18:45:18 +01002780struct wl_seat *
2781input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002782{
Daniel Stone37816df2012-05-16 18:45:18 +01002783 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002784}
2785
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002786uint32_t
2787input_get_modifiers(struct input *input)
2788{
2789 return input->modifiers;
2790}
2791
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002792struct widget *
2793input_get_focus_widget(struct input *input)
2794{
2795 return input->focus_widget;
2796}
2797
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002798struct data_offer {
2799 struct wl_data_offer *offer;
2800 struct input *input;
2801 struct wl_array types;
2802 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002803
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002804 struct task io_task;
2805 int fd;
2806 data_func_t func;
2807 int32_t x, y;
2808 void *user_data;
2809};
2810
2811static void
2812data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2813{
2814 struct data_offer *offer = data;
2815 char **p;
2816
2817 p = wl_array_add(&offer->types, sizeof *p);
2818 *p = strdup(type);
2819}
2820
2821static const struct wl_data_offer_listener data_offer_listener = {
2822 data_offer_offer,
2823};
2824
2825static void
2826data_offer_destroy(struct data_offer *offer)
2827{
2828 char **p;
2829
2830 offer->refcount--;
2831 if (offer->refcount == 0) {
2832 wl_data_offer_destroy(offer->offer);
2833 for (p = offer->types.data; *p; p++)
2834 free(*p);
2835 wl_array_release(&offer->types);
2836 free(offer);
2837 }
2838}
2839
2840static void
2841data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002842 struct wl_data_device *data_device,
2843 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002844{
2845 struct data_offer *offer;
2846
2847 offer = malloc(sizeof *offer);
2848
2849 wl_array_init(&offer->types);
2850 offer->refcount = 1;
2851 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002852 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002853 wl_data_offer_add_listener(offer->offer,
2854 &data_offer_listener, offer);
2855}
2856
2857static void
2858data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002859 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002860 wl_fixed_t x_w, wl_fixed_t y_w,
2861 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002862{
2863 struct input *input = data;
2864 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002865 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002866 float x = wl_fixed_to_double(x_w);
2867 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002868 char **p;
2869
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002870 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002871 window = wl_surface_get_user_data(surface);
2872 input->pointer_focus = window;
2873
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002874 if (offer) {
2875 input->drag_offer = wl_data_offer_get_user_data(offer);
2876
2877 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2878 *p = NULL;
2879
2880 types_data = input->drag_offer->types.data;
2881 } else {
2882 input->drag_offer = NULL;
2883 types_data = NULL;
2884 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002885
2886 window = input->pointer_focus;
2887 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002888 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002889 window->user_data);
2890}
2891
2892static void
2893data_device_leave(void *data, struct wl_data_device *data_device)
2894{
2895 struct input *input = data;
2896
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002897 if (input->drag_offer) {
2898 data_offer_destroy(input->drag_offer);
2899 input->drag_offer = NULL;
2900 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002901}
2902
2903static void
2904data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002905 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002906{
2907 struct input *input = data;
2908 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002909 float x = wl_fixed_to_double(x_w);
2910 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002911 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002912
2913 input->sx = x;
2914 input->sy = y;
2915
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002916 if (input->drag_offer)
2917 types_data = input->drag_offer->types.data;
2918 else
2919 types_data = NULL;
2920
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002921 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002922 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002923 window->user_data);
2924}
2925
2926static void
2927data_device_drop(void *data, struct wl_data_device *data_device)
2928{
2929 struct input *input = data;
2930 struct window *window = input->pointer_focus;
2931
2932 if (window->drop_handler)
2933 window->drop_handler(window, input,
2934 input->sx, input->sy, window->user_data);
2935}
2936
2937static void
2938data_device_selection(void *data,
2939 struct wl_data_device *wl_data_device,
2940 struct wl_data_offer *offer)
2941{
2942 struct input *input = data;
2943 char **p;
2944
2945 if (input->selection_offer)
2946 data_offer_destroy(input->selection_offer);
2947
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002948 if (offer) {
2949 input->selection_offer = wl_data_offer_get_user_data(offer);
2950 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2951 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002952 } else {
2953 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002954 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002955}
2956
2957static const struct wl_data_device_listener data_device_listener = {
2958 data_device_data_offer,
2959 data_device_enter,
2960 data_device_leave,
2961 data_device_motion,
2962 data_device_drop,
2963 data_device_selection
2964};
2965
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002966static void
2967input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002968{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002969 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002970 struct wl_cursor *cursor;
2971 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002972
Daniel Stone80972742012-11-07 17:51:39 +11002973 if (!input->pointer)
2974 return;
2975
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002976 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002977 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002978 return;
2979
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002980 if (index >= (int) cursor->image_count) {
2981 fprintf(stderr, "cursor index out of range\n");
2982 return;
2983 }
2984
2985 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002986 buffer = wl_cursor_image_get_buffer(image);
2987 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002988 return;
2989
Kristian Høgsbergae277372012-08-01 09:41:08 -04002990 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002991 input->pointer_surface,
2992 image->hotspot_x, image->hotspot_y);
2993 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2994 wl_surface_damage(input->pointer_surface, 0, 0,
2995 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002996 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002997}
2998
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002999static const struct wl_callback_listener pointer_surface_listener;
3000
3001static void
3002pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3003 uint32_t time)
3004{
3005 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003006 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003007 int i;
3008
3009 if (callback) {
3010 assert(callback == input->cursor_frame_cb);
3011 wl_callback_destroy(callback);
3012 input->cursor_frame_cb = NULL;
3013 }
3014
Daniel Stone80972742012-11-07 17:51:39 +11003015 if (!input->pointer)
3016 return;
3017
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003018 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003019 wl_pointer_set_cursor(input->pointer,
3020 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003021 NULL, 0, 0);
3022 return;
3023 }
3024
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003025 if (input->current_cursor == CURSOR_UNSET)
3026 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003027 cursor = input->display->cursors[input->current_cursor];
3028 if (!cursor)
3029 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003030
3031 /* FIXME We don't have the current time on the first call so we set
3032 * the animation start to the time of the first frame callback. */
3033 if (time == 0)
3034 input->cursor_anim_start = 0;
3035 else if (input->cursor_anim_start == 0)
3036 input->cursor_anim_start = time;
3037
3038 if (time == 0 || input->cursor_anim_start == 0)
3039 i = 0;
3040 else
3041 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3042
Pekka Paalanenbc106382012-10-10 12:49:31 +03003043 if (cursor->image_count > 1) {
3044 input->cursor_frame_cb =
3045 wl_surface_frame(input->pointer_surface);
3046 wl_callback_add_listener(input->cursor_frame_cb,
3047 &pointer_surface_listener, input);
3048 }
3049
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003050 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003051}
3052
3053static const struct wl_callback_listener pointer_surface_listener = {
3054 pointer_surface_frame_callback
3055};
3056
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003057void
3058input_set_pointer_image(struct input *input, int pointer)
3059{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003060 int force = 0;
3061
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003062 if (!input->pointer)
3063 return;
3064
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003065 if (input->pointer_enter_serial > input->cursor_serial)
3066 force = 1;
3067
3068 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003069 return;
3070
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003071 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003072 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003073 if (!input->cursor_frame_cb)
3074 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003075 else if (force) {
3076 /* The current frame callback may be stuck if, for instance,
3077 * the set cursor request was processed by the server after
3078 * this client lost the focus. In this case the cursor surface
3079 * might not be mapped and the frame callback wouldn't ever
3080 * complete. Send a set_cursor and attach to try to map the
3081 * cursor surface again so that the callback will finish */
3082 input_set_pointer_image_index(input, 0);
3083 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003084}
3085
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003086struct wl_data_device *
3087input_get_data_device(struct input *input)
3088{
3089 return input->data_device;
3090}
3091
3092void
3093input_set_selection(struct input *input,
3094 struct wl_data_source *source, uint32_t time)
3095{
3096 wl_data_device_set_selection(input->data_device, source, time);
3097}
3098
3099void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003100input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003101{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003102 wl_data_offer_accept(input->drag_offer->offer,
3103 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003104}
3105
3106static void
3107offer_io_func(struct task *task, uint32_t events)
3108{
3109 struct data_offer *offer =
3110 container_of(task, struct data_offer, io_task);
3111 unsigned int len;
3112 char buffer[4096];
3113
3114 len = read(offer->fd, buffer, sizeof buffer);
3115 offer->func(buffer, len,
3116 offer->x, offer->y, offer->user_data);
3117
3118 if (len == 0) {
3119 close(offer->fd);
3120 data_offer_destroy(offer);
3121 }
3122}
3123
3124static void
3125data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3126 data_func_t func, void *user_data)
3127{
3128 int p[2];
3129
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003130 if (pipe2(p, O_CLOEXEC) == -1)
3131 return;
3132
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003133 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3134 close(p[1]);
3135
3136 offer->io_task.run = offer_io_func;
3137 offer->fd = p[0];
3138 offer->func = func;
3139 offer->refcount++;
3140 offer->user_data = user_data;
3141
3142 display_watch_fd(offer->input->display,
3143 offer->fd, EPOLLIN, &offer->io_task);
3144}
3145
3146void
3147input_receive_drag_data(struct input *input, const char *mime_type,
3148 data_func_t func, void *data)
3149{
3150 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3151 input->drag_offer->x = input->sx;
3152 input->drag_offer->y = input->sy;
3153}
3154
3155int
3156input_receive_selection_data(struct input *input, const char *mime_type,
3157 data_func_t func, void *data)
3158{
3159 char **p;
3160
3161 if (input->selection_offer == NULL)
3162 return -1;
3163
3164 for (p = input->selection_offer->types.data; *p; p++)
3165 if (strcmp(mime_type, *p) == 0)
3166 break;
3167
3168 if (*p == NULL)
3169 return -1;
3170
3171 data_offer_receive_data(input->selection_offer,
3172 mime_type, func, data);
3173 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003174}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003175
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003176int
3177input_receive_selection_data_to_fd(struct input *input,
3178 const char *mime_type, int fd)
3179{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003180 if (input->selection_offer)
3181 wl_data_offer_receive(input->selection_offer->offer,
3182 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003183
3184 return 0;
3185}
3186
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003187void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003188window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003189{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003190 if (!window->shell_surface)
3191 return;
3192
Daniel Stone37816df2012-05-16 18:45:18 +01003193 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003194}
3195
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003196static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003197surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003198{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003199 struct widget *widget = surface->widget;
3200 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003201
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003202 if (surface->input_region) {
3203 wl_region_destroy(surface->input_region);
3204 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003205 }
3206
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003207 if (surface->opaque_region)
3208 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003209
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003210 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003211
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003212 if (widget->resize_handler)
3213 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003214 widget->allocation.width,
3215 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003216 widget->user_data);
3217
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003218 if (surface->allocation.width != widget->allocation.width ||
3219 surface->allocation.height != widget->allocation.height) {
3220 surface->allocation = widget->allocation;
3221 window_schedule_redraw(widget->window);
3222 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003223
3224 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003225 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003226 widget->allocation.width,
3227 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003228}
3229
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003230static void
3231idle_resize(struct window *window)
3232{
3233 window->resize_needed = 0;
3234
3235 widget_set_allocation(window->main_surface->widget,
3236 window->pending_allocation.x,
3237 window->pending_allocation.y,
3238 window->pending_allocation.width,
3239 window->pending_allocation.height);
3240
3241 surface_resize(window->main_surface);
3242}
3243
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003244void
3245window_schedule_resize(struct window *window, int width, int height)
3246{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003247 window->pending_allocation.x = 0;
3248 window->pending_allocation.y = 0;
3249 window->pending_allocation.width = width;
3250 window->pending_allocation.height = height;
3251
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003252 if (window->min_allocation.width == 0)
3253 window->min_allocation = window->pending_allocation;
3254 if (window->pending_allocation.width < window->min_allocation.width)
3255 window->pending_allocation.width = window->min_allocation.width;
3256 if (window->pending_allocation.height < window->min_allocation.height)
3257 window->pending_allocation.height = window->min_allocation.height;
3258
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003259 window->resize_needed = 1;
3260 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003261}
3262
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003263void
3264widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3265{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003266 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003267}
3268
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003269static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003270handle_ping(void *data, struct wl_shell_surface *shell_surface,
3271 uint32_t serial)
3272{
3273 wl_shell_surface_pong(shell_surface, serial);
3274}
3275
3276static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003277handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003278 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003279{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003280 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003281
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003282 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003283 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003284}
3285
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003286static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003287menu_destroy(struct menu *menu)
3288{
3289 widget_destroy(menu->widget);
3290 window_destroy(menu->window);
3291 free(menu);
3292}
3293
3294static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003295handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3296{
3297 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003298 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003299
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003300 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003301 * device. Or just use wl_callback. And this really needs to
3302 * be a window vfunc that the menu can set. And we need the
3303 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003304
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003305 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003306 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003307 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003308}
3309
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003310static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003311 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003312 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003313 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003314};
3315
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003316void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003317window_get_allocation(struct window *window,
3318 struct rectangle *allocation)
3319{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003320 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003321}
3322
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003323static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003324widget_redraw(struct widget *widget)
3325{
3326 struct widget *child;
3327
3328 if (widget->redraw_handler)
3329 widget->redraw_handler(widget, widget->user_data);
3330 wl_list_for_each(child, &widget->child_list, link)
3331 widget_redraw(child);
3332}
3333
3334static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003335frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3336{
3337 struct window *window = data;
3338
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003339 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003340 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003341 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003342 window->redraw_scheduled = 0;
3343 if (window->redraw_needed)
3344 window_schedule_redraw(window);
3345}
3346
3347static const struct wl_callback_listener listener = {
3348 frame_callback
3349};
3350
3351static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003352idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003353{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003354 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003355
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003356 if (window->resize_needed)
3357 idle_resize(window);
3358
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003359 widget_redraw(window->main_surface->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003360 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003361 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003362
Pekka Paalanen4e373742013-02-13 16:17:13 +02003363 window->frame_cb = wl_surface_frame(window->main_surface->surface);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003364 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003365 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003366}
3367
3368void
3369window_schedule_redraw(struct window *window)
3370{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003371 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003372 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003373 window->redraw_task.run = idle_redraw;
3374 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003375 window->redraw_scheduled = 1;
3376 }
3377}
3378
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003379int
3380window_is_fullscreen(struct window *window)
3381{
3382 return window->type == TYPE_FULLSCREEN;
3383}
3384
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003385void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003386window_set_fullscreen(struct window *window, int fullscreen)
3387{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003388 if (!window->display->shell)
3389 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003390
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003391 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003392 return;
3393
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003394 if (fullscreen) {
3395 window->type = TYPE_FULLSCREEN;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003396 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003397 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003398 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003399 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003400 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003401 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003402 wl_shell_surface_set_toplevel(window->shell_surface);
3403 window_schedule_resize(window,
3404 window->saved_allocation.width,
3405 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003406 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003407}
3408
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003409void
3410window_set_fullscreen_method(struct window *window,
3411 enum wl_shell_surface_fullscreen_method method)
3412{
3413 window->fullscreen_method = method;
3414}
3415
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003416int
3417window_is_maximized(struct window *window)
3418{
3419 return window->type == TYPE_MAXIMIZED;
3420}
3421
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003422void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003423window_set_maximized(struct window *window, int maximized)
3424{
3425 if (!window->display->shell)
3426 return;
3427
3428 if ((window->type == TYPE_MAXIMIZED) == maximized)
3429 return;
3430
3431 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003432 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003433 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3434 window->type = TYPE_MAXIMIZED;
3435 } else {
3436 wl_shell_surface_set_toplevel(window->shell_surface);
3437 window->type = TYPE_TOPLEVEL;
3438 window_schedule_resize(window,
3439 window->saved_allocation.width,
3440 window->saved_allocation.height);
3441 }
3442}
3443
3444void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003445window_set_user_data(struct window *window, void *data)
3446{
3447 window->user_data = data;
3448}
3449
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003450void *
3451window_get_user_data(struct window *window)
3452{
3453 return window->user_data;
3454}
3455
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003456void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003457window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003458 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003459{
3460 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003461}
3462
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003463void
3464window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003465 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003466{
3467 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003468}
3469
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003470void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003471window_set_data_handler(struct window *window, window_data_handler_t handler)
3472{
3473 window->data_handler = handler;
3474}
3475
3476void
3477window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3478{
3479 window->drop_handler = handler;
3480}
3481
3482void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003483window_set_close_handler(struct window *window,
3484 window_close_handler_t handler)
3485{
3486 window->close_handler = handler;
3487}
3488
3489void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003490window_set_fullscreen_handler(struct window *window,
3491 window_fullscreen_handler_t handler)
3492{
3493 window->fullscreen_handler = handler;
3494}
3495
3496void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003497window_set_output_handler(struct window *window,
3498 window_output_handler_t handler)
3499{
3500 window->output_handler = handler;
3501}
3502
3503void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003504window_set_title(struct window *window, const char *title)
3505{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003506 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003507 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003508 if (window->shell_surface)
3509 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003510}
3511
3512const char *
3513window_get_title(struct window *window)
3514{
3515 return window->title;
3516}
3517
3518void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003519window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3520{
3521 struct text_cursor_position *text_cursor_position =
3522 window->display->text_cursor_position;
3523
Scott Moreau9295ce02012-06-01 12:46:10 -06003524 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003525 return;
3526
3527 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003528 window->main_surface->surface,
3529 wl_fixed_from_int(x),
3530 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003531}
3532
3533void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003534window_damage(struct window *window, int32_t x, int32_t y,
3535 int32_t width, int32_t height)
3536{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003537 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003538}
3539
Casey Dahlin9074db52012-04-19 22:50:09 -04003540static void
3541surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003542 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003543{
Rob Bradford7507b572012-05-15 17:55:34 +01003544 struct window *window = data;
3545 struct output *output;
3546 struct output *output_found = NULL;
3547 struct window_output *window_output;
3548
3549 wl_list_for_each(output, &window->display->output_list, link) {
3550 if (output->output == wl_output) {
3551 output_found = output;
3552 break;
3553 }
3554 }
3555
3556 if (!output_found)
3557 return;
3558
3559 window_output = malloc (sizeof *window_output);
3560 window_output->output = output_found;
3561
3562 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003563
3564 if (window->output_handler)
3565 window->output_handler(window, output_found, 1,
3566 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003567}
3568
3569static void
3570surface_leave(void *data,
3571 struct wl_surface *wl_surface, struct wl_output *output)
3572{
Rob Bradford7507b572012-05-15 17:55:34 +01003573 struct window *window = data;
3574 struct window_output *window_output;
3575 struct window_output *window_output_found = NULL;
3576
3577 wl_list_for_each(window_output, &window->window_output_list, link) {
3578 if (window_output->output->output == output) {
3579 window_output_found = window_output;
3580 break;
3581 }
3582 }
3583
3584 if (window_output_found) {
3585 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003586
3587 if (window->output_handler)
3588 window->output_handler(window, window_output->output,
3589 0, window->user_data);
3590
Rob Bradford7507b572012-05-15 17:55:34 +01003591 free(window_output_found);
3592 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003593}
3594
3595static const struct wl_surface_listener surface_listener = {
3596 surface_enter,
3597 surface_leave
3598};
3599
Pekka Paalanen4e373742013-02-13 16:17:13 +02003600static struct surface *
3601surface_create(struct window *window)
3602{
3603 struct display *display = window->display;
3604 struct surface *surface;
3605
3606 surface = calloc(1, sizeof *surface);
3607 if (!surface)
3608 return NULL;
3609
3610 surface->window = window;
3611 surface->surface = wl_compositor_create_surface(display->compositor);
3612 wl_surface_add_listener(surface->surface, &surface_listener, window);
3613
3614 return surface;
3615}
3616
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003617static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003618window_create_internal(struct display *display,
3619 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003620{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003621 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003622 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003623
3624 window = malloc(sizeof *window);
3625 if (window == NULL)
3626 return NULL;
3627
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003628 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003629 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003630 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003631
3632 surface = surface_create(window);
3633 window->main_surface = surface;
3634
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003635 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003636 window->shell_surface =
3637 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003638 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003639 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003640
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003641 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003642 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003643
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003644 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003645#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003646 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003647#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003648 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003649#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003650 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003651 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003652
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003653 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003654 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003655 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003656
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003657 if (window->shell_surface) {
3658 wl_shell_surface_set_user_data(window->shell_surface, window);
3659 wl_shell_surface_add_listener(window->shell_surface,
3660 &shell_surface_listener, window);
3661 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003662
Rob Bradford7507b572012-05-15 17:55:34 +01003663 wl_list_init (&window->window_output_list);
3664
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003665 return window;
3666}
3667
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003668struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003669window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003670{
3671 struct window *window;
3672
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003673 window = window_create_internal(display, NULL, TYPE_NONE);
3674 if (!window)
3675 return NULL;
3676
3677 return window;
3678}
3679
3680struct window *
3681window_create_custom(struct display *display)
3682{
3683 struct window *window;
3684
3685 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003686 if (!window)
3687 return NULL;
3688
3689 return window;
3690}
3691
3692struct window *
3693window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003694 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003695{
3696 struct window *window;
3697
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003698 window = window_create_internal(parent->display,
3699 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003700 if (!window)
3701 return NULL;
3702
3703 window->x = x;
3704 window->y = y;
3705
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003706 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02003707 wl_shell_surface_set_transient(
3708 window->shell_surface,
3709 window->parent->main_surface->surface,
3710 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003711
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003712 return window;
3713}
3714
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003715static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003716menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003717{
3718 int next;
3719
3720 next = (sy - 8) / 20;
3721 if (menu->current != next) {
3722 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003723 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003724 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003725}
3726
3727static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003728menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003729 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003730 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003731{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003732 struct menu *menu = data;
3733
3734 if (widget == menu->widget)
3735 menu_set_item(data, y);
3736
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003737 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003738}
3739
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003740static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003741menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003742 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003743{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003744 struct menu *menu = data;
3745
3746 if (widget == menu->widget)
3747 menu_set_item(data, y);
3748
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003749 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003750}
3751
3752static void
3753menu_leave_handler(struct widget *widget, struct input *input, void *data)
3754{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003755 struct menu *menu = data;
3756
3757 if (widget == menu->widget)
3758 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003759}
3760
3761static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003762menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003763 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003764 uint32_t button, enum wl_pointer_button_state state,
3765 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003766
3767{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003768 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003769
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003770 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3771 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003772 /* Either relase after press-drag-release or
3773 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003774 menu->func(menu->window->parent,
3775 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003776 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003777 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003778 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003779 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003780 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003781}
3782
3783static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003784menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003785{
3786 cairo_t *cr;
3787 const int32_t r = 3, margin = 3;
3788 struct menu *menu = data;
3789 int32_t width, height, i;
3790
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02003791 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003792 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3793 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3794 cairo_paint(cr);
3795
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02003796 width = widget->allocation.width;
3797 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003798 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003799
3800 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003801 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3802 cairo_fill(cr);
3803
3804 for (i = 0; i < menu->count; i++) {
3805 if (i == menu->current) {
3806 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3807 cairo_rectangle(cr, margin, i * 20 + margin,
3808 width - 2 * margin, 20);
3809 cairo_fill(cr);
3810 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3811 cairo_move_to(cr, 10, i * 20 + 16);
3812 cairo_show_text(cr, menu->entries[i]);
3813 } else {
3814 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3815 cairo_move_to(cr, 10, i * 20 + 16);
3816 cairo_show_text(cr, menu->entries[i]);
3817 }
3818 }
3819
3820 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003821}
3822
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003823void
3824window_show_menu(struct display *display,
3825 struct input *input, uint32_t time, struct window *parent,
3826 int32_t x, int32_t y,
3827 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003828{
3829 struct window *window;
3830 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003831 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003832
3833 menu = malloc(sizeof *menu);
3834 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003835 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003836
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003837 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003838 if (!window) {
3839 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003840 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003841 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003842
3843 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003844 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003845 menu->entries = entries;
3846 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003847 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003848 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003849 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003850 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003851 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003852 window->type = TYPE_MENU;
3853 window->x = x;
3854 window->y = y;
3855
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003856 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003857 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003858 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02003859 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003860 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003861
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003862 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003863 widget_set_enter_handler(menu->widget, menu_enter_handler);
3864 widget_set_leave_handler(menu->widget, menu_leave_handler);
3865 widget_set_motion_handler(menu->widget, menu_motion_handler);
3866 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003867
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003868 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003869 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003870}
3871
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003872void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003873window_set_buffer_type(struct window *window, enum window_buffer_type type)
3874{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003875 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003876}
3877
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003878
3879static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003880display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003881 struct wl_output *wl_output,
3882 int x, int y,
3883 int physical_width,
3884 int physical_height,
3885 int subpixel,
3886 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003887 const char *model,
3888 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003889{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003890 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003891
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003892 output->allocation.x = x;
3893 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003894 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003895}
3896
3897static void
3898display_handle_mode(void *data,
3899 struct wl_output *wl_output,
3900 uint32_t flags,
3901 int width,
3902 int height,
3903 int refresh)
3904{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003905 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003906 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003907
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003908 if (flags & WL_OUTPUT_MODE_CURRENT) {
3909 output->allocation.width = width;
3910 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003911 if (display->output_configure_handler)
3912 (*display->output_configure_handler)(
3913 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003914 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003915}
3916
3917static const struct wl_output_listener output_listener = {
3918 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003919 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003920};
3921
3922static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003923display_add_output(struct display *d, uint32_t id)
3924{
3925 struct output *output;
3926
3927 output = malloc(sizeof *output);
3928 if (output == NULL)
3929 return;
3930
3931 memset(output, 0, sizeof *output);
3932 output->display = d;
3933 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003934 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003935 wl_list_insert(d->output_list.prev, &output->link);
3936
3937 wl_output_add_listener(output->output, &output_listener, output);
3938}
3939
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003940static void
3941output_destroy(struct output *output)
3942{
3943 if (output->destroy_handler)
3944 (*output->destroy_handler)(output, output->user_data);
3945
3946 wl_output_destroy(output->output);
3947 wl_list_remove(&output->link);
3948 free(output);
3949}
3950
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003951void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003952display_set_global_handler(struct display *display,
3953 display_global_handler_t handler)
3954{
3955 struct global *global;
3956
3957 display->global_handler = handler;
3958 if (!handler)
3959 return;
3960
3961 wl_list_for_each(global, &display->global_list, link)
3962 display->global_handler(display,
3963 global->name, global->interface,
3964 global->version, display->user_data);
3965}
3966
3967void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003968display_set_output_configure_handler(struct display *display,
3969 display_output_handler_t handler)
3970{
3971 struct output *output;
3972
3973 display->output_configure_handler = handler;
3974 if (!handler)
3975 return;
3976
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003977 wl_list_for_each(output, &display->output_list, link) {
3978 if (output->allocation.width == 0 &&
3979 output->allocation.height == 0)
3980 continue;
3981
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003982 (*display->output_configure_handler)(output,
3983 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003984 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003985}
3986
3987void
3988output_set_user_data(struct output *output, void *data)
3989{
3990 output->user_data = data;
3991}
3992
3993void *
3994output_get_user_data(struct output *output)
3995{
3996 return output->user_data;
3997}
3998
3999void
4000output_set_destroy_handler(struct output *output,
4001 display_output_handler_t handler)
4002{
4003 output->destroy_handler = handler;
4004 /* FIXME: implement this, once we have way to remove outputs */
4005}
4006
4007void
Scott Moreau4e072362012-09-29 02:03:11 -06004008output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004009{
Scott Moreau4e072362012-09-29 02:03:11 -06004010 struct rectangle allocation = output->allocation;
4011
4012 switch (output->transform) {
4013 case WL_OUTPUT_TRANSFORM_90:
4014 case WL_OUTPUT_TRANSFORM_270:
4015 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4016 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4017 /* Swap width and height */
4018 allocation.width = output->allocation.height;
4019 allocation.height = output->allocation.width;
4020 break;
4021 }
4022
4023 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004024}
4025
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004026struct wl_output *
4027output_get_wl_output(struct output *output)
4028{
4029 return output->output;
4030}
4031
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004032enum wl_output_transform
4033output_get_transform(struct output *output)
4034{
4035 return output->transform;
4036}
4037
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004038static void
Daniel Stone97f68542012-05-30 16:32:01 +01004039fini_xkb(struct input *input)
4040{
4041 xkb_state_unref(input->xkb.state);
4042 xkb_map_unref(input->xkb.keymap);
4043}
4044
4045static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004046display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004047{
4048 struct input *input;
4049
4050 input = malloc(sizeof *input);
4051 if (input == NULL)
4052 return;
4053
4054 memset(input, 0, sizeof *input);
4055 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004056 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004057 input->pointer_focus = NULL;
4058 input->keyboard_focus = NULL;
4059 wl_list_insert(d->input_list.prev, &input->link);
4060
Daniel Stone37816df2012-05-16 18:45:18 +01004061 wl_seat_add_listener(input->seat, &seat_listener, input);
4062 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004063
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004064 input->data_device =
4065 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004066 input->seat);
4067 wl_data_device_add_listener(input->data_device, &data_device_listener,
4068 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004069
4070 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004071
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004072 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4073 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004074 input->repeat_task.run = keyboard_repeat_func;
4075 display_watch_fd(d, input->repeat_timer_fd,
4076 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004077}
4078
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004079static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004080input_destroy(struct input *input)
4081{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004082 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004083 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004084
4085 if (input->drag_offer)
4086 data_offer_destroy(input->drag_offer);
4087
4088 if (input->selection_offer)
4089 data_offer_destroy(input->selection_offer);
4090
4091 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004092 fini_xkb(input);
4093
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004094 wl_surface_destroy(input->pointer_surface);
4095
Pekka Paalanene1207c72011-12-16 12:02:09 +02004096 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004097 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004098 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004099 free(input);
4100}
4101
4102static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004103init_workspace_manager(struct display *d, uint32_t id)
4104{
4105 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004106 wl_registry_bind(d->registry, id,
4107 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004108 if (d->workspace_manager != NULL)
4109 workspace_manager_add_listener(d->workspace_manager,
4110 &workspace_manager_listener,
4111 d);
4112}
4113
4114static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004115registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4116 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004117{
4118 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004119 struct global *global;
4120
4121 global = malloc(sizeof *global);
4122 global->name = id;
4123 global->interface = strdup(interface);
4124 global->version = version;
4125 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004126
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004127 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004128 d->compositor = wl_registry_bind(registry, id,
4129 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004130 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004131 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004132 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004133 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004134 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004135 d->shell = wl_registry_bind(registry,
4136 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004137 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004138 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004139 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4140 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004141 wl_registry_bind(registry, id,
4142 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004143 } else if (strcmp(interface, "text_cursor_position") == 0) {
4144 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004145 wl_registry_bind(registry, id,
4146 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004147 } else if (strcmp(interface, "workspace_manager") == 0) {
4148 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004149 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004150
4151 if (d->global_handler)
4152 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004153}
4154
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004155static void
4156registry_handle_global_remove(void *data, struct wl_registry *registry,
4157 uint32_t name)
4158{
4159 struct display *d = data;
4160 struct global *global;
4161 struct global *tmp;
4162
4163 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4164 if (global->name != name)
4165 continue;
4166
4167 /* XXX: Should destroy bound globals, and call
4168 * the counterpart of display::global_handler
4169 */
4170 wl_list_remove(&global->link);
4171 free(global->interface);
4172 free(global);
4173 }
4174}
4175
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004176void *
4177display_bind(struct display *display, uint32_t name,
4178 const struct wl_interface *interface, uint32_t version)
4179{
4180 return wl_registry_bind(display->registry, name, interface, version);
4181}
4182
4183static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004184 registry_handle_global,
4185 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004186};
4187
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004188#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004189static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004190init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004191{
4192 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004193 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004194
Rob Clark6396ed32012-03-11 19:48:41 -05004195#ifdef USE_CAIRO_GLESV2
4196# define GL_BIT EGL_OPENGL_ES2_BIT
4197#else
4198# define GL_BIT EGL_OPENGL_BIT
4199#endif
4200
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004201 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004202 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004203 EGL_RED_SIZE, 1,
4204 EGL_GREEN_SIZE, 1,
4205 EGL_BLUE_SIZE, 1,
4206 EGL_ALPHA_SIZE, 1,
4207 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004208 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004209 EGL_NONE
4210 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004211
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004212#ifdef USE_CAIRO_GLESV2
4213 static const EGLint context_attribs[] = {
4214 EGL_CONTEXT_CLIENT_VERSION, 2,
4215 EGL_NONE
4216 };
4217 EGLint api = EGL_OPENGL_ES_API;
4218#else
4219 EGLint *context_attribs = NULL;
4220 EGLint api = EGL_OPENGL_API;
4221#endif
4222
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004223 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004224 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004225 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004226 return -1;
4227 }
4228
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004229 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004230 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004231 return -1;
4232 }
4233
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004234 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4235 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004236 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004237 return -1;
4238 }
4239
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004240 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004241 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004242 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004243 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004244 return -1;
4245 }
4246
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004247 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004248 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004249 return -1;
4250 }
4251
Benjamin Franzke0c991632011-09-27 21:57:31 +02004252 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4253 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004254 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004255 return -1;
4256 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004257
4258 return 0;
4259}
4260
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004261static void
4262fini_egl(struct display *display)
4263{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004264 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004265
4266 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4267 EGL_NO_CONTEXT);
4268
4269 eglTerminate(display->dpy);
4270 eglReleaseThread();
4271}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004272#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004273
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004274static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004275init_dummy_surface(struct display *display)
4276{
4277 int len;
4278 void *data;
4279
4280 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4281 data = malloc(len);
4282 display->dummy_surface =
4283 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4284 1, 1, len);
4285 display->dummy_surface_data = data;
4286}
4287
4288static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004289handle_display_data(struct task *task, uint32_t events)
4290{
4291 struct display *display =
4292 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004293 struct epoll_event ep;
4294 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004295
4296 display->display_fd_events = events;
4297
4298 if (events & EPOLLERR || events & EPOLLHUP) {
4299 display_exit(display);
4300 return;
4301 }
4302
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004303 if (events & EPOLLIN) {
4304 ret = wl_display_dispatch(display->display);
4305 if (ret == -1) {
4306 display_exit(display);
4307 return;
4308 }
4309 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004310
4311 if (events & EPOLLOUT) {
4312 ret = wl_display_flush(display->display);
4313 if (ret == 0) {
4314 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4315 ep.data.ptr = &display->display_task;
4316 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4317 display->display_fd, &ep);
4318 } else if (ret == -1 && errno != EAGAIN) {
4319 display_exit(display);
4320 return;
4321 }
4322 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004323}
4324
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004325struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004326display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004327{
4328 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004329
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004330 d = malloc(sizeof *d);
4331 if (d == NULL)
4332 return NULL;
4333
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004334 memset(d, 0, sizeof *d);
4335
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004336 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004337 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004338 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004339 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004340 return NULL;
4341 }
4342
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004343 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004344 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004345 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004346 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4347 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004348
4349 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004350 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004351 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004352 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004353
Daniel Stone97f68542012-05-30 16:32:01 +01004354 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004355 if (d->xkb_context == NULL) {
4356 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004357 return NULL;
4358 }
4359
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004360 d->workspace = 0;
4361 d->workspace_count = 1;
4362
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004363 d->registry = wl_display_get_registry(d->display);
4364 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004365
4366 if (wl_display_dispatch(d->display) < 0) {
4367 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4368 return NULL;
4369 }
4370
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004371#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004372 if (init_egl(d) < 0)
4373 fprintf(stderr, "EGL does not seem to work, "
4374 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004375#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004376
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004377 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004378
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004379 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004380
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004381 wl_list_init(&d->window_list);
4382
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004383 init_dummy_surface(d);
4384
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004385 return d;
4386}
4387
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004388static void
4389display_destroy_outputs(struct display *display)
4390{
4391 struct output *tmp;
4392 struct output *output;
4393
4394 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4395 output_destroy(output);
4396}
4397
Pekka Paalanene1207c72011-12-16 12:02:09 +02004398static void
4399display_destroy_inputs(struct display *display)
4400{
4401 struct input *tmp;
4402 struct input *input;
4403
4404 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4405 input_destroy(input);
4406}
4407
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004408void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004409display_destroy(struct display *display)
4410{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004411 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004412 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4413 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004414
4415 if (!wl_list_empty(&display->deferred_list))
4416 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4417
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004418 cairo_surface_destroy(display->dummy_surface);
4419 free(display->dummy_surface_data);
4420
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004421 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004422 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004423
Daniel Stone97f68542012-05-30 16:32:01 +01004424 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004425
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004426 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004427 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004428
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004429#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004430 if (display->argb_device)
4431 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004432#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004433
Pekka Paalanenc2052982011-12-16 11:41:32 +02004434 if (display->shell)
4435 wl_shell_destroy(display->shell);
4436
4437 if (display->shm)
4438 wl_shm_destroy(display->shm);
4439
4440 if (display->data_device_manager)
4441 wl_data_device_manager_destroy(display->data_device_manager);
4442
4443 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004444 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004445
4446 close(display->epoll_fd);
4447
U. Artie Eoff44874d92012-10-02 21:12:35 -07004448 if (!(display->display_fd_events & EPOLLERR) &&
4449 !(display->display_fd_events & EPOLLHUP))
4450 wl_display_flush(display->display);
4451
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004452 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004453 free(display);
4454}
4455
4456void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004457display_set_user_data(struct display *display, void *data)
4458{
4459 display->user_data = data;
4460}
4461
4462void *
4463display_get_user_data(struct display *display)
4464{
4465 return display->user_data;
4466}
4467
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004468struct wl_display *
4469display_get_display(struct display *display)
4470{
4471 return display->display;
4472}
4473
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004474struct output *
4475display_get_output(struct display *display)
4476{
4477 return container_of(display->output_list.next, struct output, link);
4478}
4479
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004480struct wl_compositor *
4481display_get_compositor(struct display *display)
4482{
4483 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004484}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004485
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004486uint32_t
4487display_get_serial(struct display *display)
4488{
4489 return display->serial;
4490}
4491
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004492EGLDisplay
4493display_get_egl_display(struct display *d)
4494{
4495 return d->dpy;
4496}
4497
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004498struct wl_data_source *
4499display_create_data_source(struct display *display)
4500{
4501 return wl_data_device_manager_create_data_source(display->data_device_manager);
4502}
4503
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004504EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004505display_get_argb_egl_config(struct display *d)
4506{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004507 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004508}
4509
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004510struct wl_shell *
4511display_get_shell(struct display *display)
4512{
4513 return display->shell;
4514}
4515
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004516int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004517display_acquire_window_surface(struct display *display,
4518 struct window *window,
4519 EGLContext ctx)
4520{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004521 struct surface *surface = window->main_surface;
4522
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004523 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004524 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004525
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004526 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004527}
4528
4529void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004530display_release_window_surface(struct display *display,
4531 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004532{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004533 struct surface *surface = window->main_surface;
4534
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004535 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004536 return;
4537
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004538 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004539}
4540
4541void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004542display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004543{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004544 wl_list_insert(&display->deferred_list, &task->link);
4545}
4546
4547void
4548display_watch_fd(struct display *display,
4549 int fd, uint32_t events, struct task *task)
4550{
4551 struct epoll_event ep;
4552
4553 ep.events = events;
4554 ep.data.ptr = task;
4555 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4556}
4557
4558void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004559display_unwatch_fd(struct display *display, int fd)
4560{
4561 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4562}
4563
4564void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004565display_run(struct display *display)
4566{
4567 struct task *task;
4568 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004569 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004570
Pekka Paalanen826d7952011-12-15 10:14:07 +02004571 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004572 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004573 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004574 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004575 struct task, link);
4576 wl_list_remove(&task->link);
4577 task->run(task, 0);
4578 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004579
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004580 wl_display_dispatch_pending(display->display);
4581
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004582 if (!display->running)
4583 break;
4584
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004585 ret = wl_display_flush(display->display);
4586 if (ret < 0 && errno == EAGAIN) {
4587 ep[0].events =
4588 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4589 ep[0].data.ptr = &display->display_task;
4590
4591 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4592 display->display_fd, &ep[0]);
4593 } else if (ret < 0) {
4594 break;
4595 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004596
4597 count = epoll_wait(display->epoll_fd,
4598 ep, ARRAY_LENGTH(ep), -1);
4599 for (i = 0; i < count; i++) {
4600 task = ep[i].data.ptr;
4601 task->run(task, ep[i].events);
4602 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004603 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004604}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004605
4606void
4607display_exit(struct display *display)
4608{
4609 display->running = 0;
4610}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004611
4612void
4613keysym_modifiers_add(struct wl_array *modifiers_map,
4614 const char *name)
4615{
4616 size_t len = strlen(name) + 1;
4617 char *p;
4618
4619 p = wl_array_add(modifiers_map, len);
4620
4621 if (p == NULL)
4622 return;
4623
4624 strncpy(p, name, len);
4625}
4626
4627static xkb_mod_index_t
4628keysym_modifiers_get_index(struct wl_array *modifiers_map,
4629 const char *name)
4630{
4631 xkb_mod_index_t index = 0;
4632 char *p = modifiers_map->data;
4633
4634 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4635 if (strcmp(p, name) == 0)
4636 return index;
4637
4638 index++;
4639 p += strlen(p) + 1;
4640 }
4641
4642 return XKB_MOD_INVALID;
4643}
4644
4645xkb_mod_mask_t
4646keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4647 const char *name)
4648{
4649 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4650
4651 if (index == XKB_MOD_INVALID)
4652 return XKB_MOD_INVALID;
4653
4654 return 1 << index;
4655}