blob: 8065f68f7d2762275fe90062d660cf9360beea59 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040042#include <pixman.h>
43
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030044#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050045#include <wayland-egl.h>
46
Rob Clark6396ed32012-03-11 19:48:41 -050047#ifdef USE_CAIRO_GLESV2
48#include <GLES2/gl2.h>
49#include <GLES2/gl2ext.h>
50#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050052#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040053#include <EGL/egl.h>
54#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030057#else /* HAVE_CAIRO_EGL */
58typedef void *EGLDisplay;
59typedef void *EGLConfig;
60typedef void *EGLContext;
61#define EGL_NO_DISPLAY ((EGLDisplay)0)
62#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063
Daniel Stone9d4f0302012-02-15 16:33:21 +000064#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030065#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040066
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020068#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040069#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060070#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020071#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030072#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050073
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050074#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050075
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070076struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030077
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040078struct global {
79 uint32_t name;
80 char *interface;
81 uint32_t version;
82 struct wl_list link;
83};
84
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050085struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050086 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040087 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050088 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040089 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040090 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040091 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060092 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020093 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040099
100 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700101 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400102 struct task display_task;
103
104 int epoll_fd;
105 struct wl_list deferred_list;
106
Pekka Paalanen826d7952011-12-15 10:14:07 +0200107 int running;
108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400109 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400110 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500112 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400114 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400115
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300116 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300117 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200119 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400120 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200121
122 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100123
124 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200125
126 uint32_t workspace;
127 uint32_t workspace_count;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128};
129
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400130enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400131 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400132 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400133 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500134 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500136 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_CUSTOM
138};
Rob Bradford7507b572012-05-15 17:55:34 +0100139
140struct window_output {
141 struct output *output;
142 struct wl_list link;
143};
144
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200145struct toysurface {
146 /*
147 * Prepare the surface for drawing. Makes sure there is a surface
148 * of the right size available for rendering, and returns it.
149 * dx,dy are the x,y of wl_surface.attach.
150 * width,height are the new surface size.
151 * Returns the Cairo surface to draw to.
152 */
153 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
154 int width, int height);
155
156 /*
157 * Post the surface to the server, returning the server allocation
158 * rectangle. The Cairo surface from prepare() must be destroyed
159 * after calling this.
160 */
161 void (*swap)(struct toysurface *base,
162 struct rectangle *server_allocation);
163
164 /*
165 * Make the toysurface current with the given EGL context.
166 * Returns 0 on success, and negative of failure.
167 */
168 int (*acquire)(struct toysurface *base, EGLContext ctx);
169
170 /*
171 * Release the toysurface from the EGL context, returning control
172 * to Cairo.
173 */
174 void (*release)(struct toysurface *base);
175
176 /*
177 * Destroy the toysurface, including the Cairo surface, any
178 * backing storage, and the Wayland protocol objects.
179 */
180 void (*destroy)(struct toysurface *base);
181};
182
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500183struct window {
184 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500185 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100186 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500187 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200188 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500189 struct wl_region *input_region;
190 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500191 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500192 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400193 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500194 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500195 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400196 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400197 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400198 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400199 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400200 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400201 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400202 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400203 int focus_count;
204
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400205 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500206
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200207 struct toysurface *toysurface;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400208 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500209
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700210 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400211
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500212 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500213 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400214 window_data_handler_t data_handler;
215 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500216 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400217 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400218
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300219 struct wl_callback *frame_cb;
220
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200221 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500222 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500223
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500224 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400225 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500226};
227
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500228struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500229 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300230 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500231 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400232 struct wl_list link;
233 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500234 widget_resize_handler_t resize_handler;
235 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500236 widget_enter_handler_t enter_handler;
237 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500238 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500239 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200240 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400241 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500242 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300243 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400244};
245
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400246struct input {
247 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100248 struct wl_seat *seat;
249 struct wl_pointer *pointer;
250 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400251 struct window *pointer_focus;
252 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300253 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300254 uint32_t cursor_anim_start;
255 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300256 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400257 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400258 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400259 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400260 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400261 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400262
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500263 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500264 struct widget *grab;
265 uint32_t grab_button;
266
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400267 struct wl_data_device *data_device;
268 struct data_offer *drag_offer;
269 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100270
271 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100272 struct xkb_keymap *keymap;
273 struct xkb_state *state;
274 xkb_mod_mask_t control_mask;
275 xkb_mod_mask_t alt_mask;
276 xkb_mod_mask_t shift_mask;
277 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400278
279 struct task repeat_task;
280 int repeat_timer_fd;
281 uint32_t repeat_sym;
282 uint32_t repeat_key;
283 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400284};
285
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500286struct output {
287 struct display *display;
288 struct wl_output *output;
289 struct rectangle allocation;
290 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600291 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200292
293 display_output_handler_t destroy_handler;
294 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500295};
296
Martin Minarik1998b152012-05-10 02:04:35 +0200297enum frame_button_action {
298 FRAME_BUTTON_NULL = 0,
299 FRAME_BUTTON_ICON = 1,
300 FRAME_BUTTON_CLOSE = 2,
301 FRAME_BUTTON_MINIMIZE = 3,
302 FRAME_BUTTON_MAXIMIZE = 4,
303};
304
305enum frame_button_pointer {
306 FRAME_BUTTON_DEFAULT = 0,
307 FRAME_BUTTON_OVER = 1,
308 FRAME_BUTTON_ACTIVE = 2,
309};
310
311enum frame_button_align {
312 FRAME_BUTTON_RIGHT = 0,
313 FRAME_BUTTON_LEFT = 1,
314};
315
316enum frame_button_decoration {
317 FRAME_BUTTON_NONE = 0,
318 FRAME_BUTTON_FANCY = 1,
319};
320
321struct frame_button {
322 struct widget *widget;
323 struct frame *frame;
324 cairo_surface_t *icon;
325 enum frame_button_action type;
326 enum frame_button_pointer state;
327 struct wl_list link; /* buttons_list */
328 enum frame_button_align align;
329 enum frame_button_decoration decoration;
330};
331
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500332struct frame {
333 struct widget *widget;
334 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200335 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500336};
337
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500338struct menu {
339 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500340 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500341 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500342 const char **entries;
343 uint32_t time;
344 int current;
345 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400346 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500347 menu_func_t func;
348};
349
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300350struct tooltip {
351 struct widget *parent;
352 struct window *window;
353 struct widget *widget;
354 char *entry;
355 struct task tooltip_task;
356 int tooltip_fd;
357 float x, y;
358};
359
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700360struct shm_pool {
361 struct wl_shm_pool *pool;
362 size_t size;
363 size_t used;
364 void *data;
365};
366
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400367enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300368 CURSOR_DEFAULT = 100,
369 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400370};
371
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500372enum window_location {
373 WINDOW_INTERIOR = 0,
374 WINDOW_RESIZING_TOP = 1,
375 WINDOW_RESIZING_BOTTOM = 2,
376 WINDOW_RESIZING_LEFT = 4,
377 WINDOW_RESIZING_TOP_LEFT = 5,
378 WINDOW_RESIZING_BOTTOM_LEFT = 6,
379 WINDOW_RESIZING_RIGHT = 8,
380 WINDOW_RESIZING_TOP_RIGHT = 9,
381 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
382 WINDOW_RESIZING_MASK = 15,
383 WINDOW_EXTERIOR = 16,
384 WINDOW_TITLEBAR = 17,
385 WINDOW_CLIENT_AREA = 18,
386};
387
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200388static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400389
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500390#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200392struct egl_window_surface {
393 struct toysurface base;
394 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100395 struct display *display;
396 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200397 struct wl_egl_window *egl_window;
398 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100399};
400
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200401static struct egl_window_surface *
402to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100403{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200404 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100405}
406
407static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200408egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
409 int width, int height)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100410{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200411 struct egl_window_surface *surface = to_egl_window_surface(base);
412
413 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
414 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
415
416 return cairo_surface_reference(surface->cairo_surface);
417}
418
419static void
420egl_window_surface_swap(struct toysurface *base,
421 struct rectangle *server_allocation)
422{
423 struct egl_window_surface *surface = to_egl_window_surface(base);
424
425 cairo_gl_surface_swapbuffers(surface->cairo_surface);
426 wl_egl_window_get_attached_size(surface->egl_window,
427 &server_allocation->width,
428 &server_allocation->height);
429}
430
431static int
432egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
433{
434 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200435 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100436
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200437 device = cairo_surface_get_device(surface->cairo_surface);
438 if (!device)
439 return -1;
440
441 if (!ctx) {
442 if (device == surface->display->argb_device)
443 ctx = surface->display->argb_ctx;
444 else
445 assert(0);
446 }
447
448 cairo_device_flush(device);
449 cairo_device_acquire(device);
450 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
451 surface->egl_surface, ctx))
452 fprintf(stderr, "failed to make surface current\n");
453
454 return 0;
455}
456
457static void
458egl_window_surface_release(struct toysurface *base)
459{
460 struct egl_window_surface *surface = to_egl_window_surface(base);
461 cairo_device_t *device;
462
463 device = cairo_surface_get_device(surface->cairo_surface);
464 if (!device)
465 return;
466
467 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
468 surface->display->argb_ctx))
469 fprintf(stderr, "failed to make context current\n");
470
471 cairo_device_release(device);
472}
473
474static void
475egl_window_surface_destroy(struct toysurface *base)
476{
477 struct egl_window_surface *surface = to_egl_window_surface(base);
478 struct display *d = surface->display;
479
480 cairo_surface_destroy(surface->cairo_surface);
481 eglDestroySurface(d->dpy, surface->egl_surface);
482 wl_egl_window_destroy(surface->egl_window);
483 surface->surface = NULL;
484
485 free(surface);
486}
487
488static struct toysurface *
489egl_window_surface_create(struct display *display,
490 struct wl_surface *wl_surface,
491 uint32_t flags,
492 struct rectangle *rectangle)
493{
494 struct egl_window_surface *surface;
495
496 surface = calloc(1, sizeof *surface);
497 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100498 return NULL;
499
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200500 surface->base.prepare = egl_window_surface_prepare;
501 surface->base.swap = egl_window_surface_swap;
502 surface->base.acquire = egl_window_surface_acquire;
503 surface->base.release = egl_window_surface_release;
504 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100505
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200506 surface->display = display;
507 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400508
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200509 surface->egl_window = wl_egl_window_create(surface->surface,
510 rectangle->width,
511 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100512
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200513 surface->egl_surface = eglCreateWindowSurface(display->dpy,
514 display->argb_config,
515 surface->egl_window,
516 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100517
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518 surface->cairo_surface =
519 cairo_gl_surface_create_for_egl(display->argb_device,
520 surface->egl_surface,
521 rectangle->width,
522 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100523
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200524 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525}
526
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400527#endif
528
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200529struct shm_surface_data {
530 struct wl_buffer *buffer;
531 struct shm_pool *pool;
532};
533
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534struct wl_buffer *
535display_get_buffer_for_surface(struct display *display,
536 cairo_surface_t *surface)
537{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200538 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200540 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541
542 return data->buffer;
543}
544
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500545static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700546shm_pool_destroy(struct shm_pool *pool);
547
548static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549shm_surface_data_destroy(void *p)
550{
551 struct shm_surface_data *data = p;
552
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200553 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700554 if (data->pool)
555 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300556
557 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400558}
559
Kristian Høgsberg16626282012-04-03 11:21:27 -0400560static struct wl_shm_pool *
561make_shm_pool(struct display *display, int size, void **data)
562{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400563 struct wl_shm_pool *pool;
564 int fd;
565
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300566 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400567 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300568 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
569 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400570 return NULL;
571 }
572
573 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400574 if (*data == MAP_FAILED) {
575 fprintf(stderr, "mmap failed: %m\n");
576 close(fd);
577 return NULL;
578 }
579
580 pool = wl_shm_create_pool(display->shm, fd, size);
581
582 close(fd);
583
584 return pool;
585}
586
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700587static struct shm_pool *
588shm_pool_create(struct display *display, size_t size)
589{
590 struct shm_pool *pool = malloc(sizeof *pool);
591
592 if (!pool)
593 return NULL;
594
595 pool->pool = make_shm_pool(display, size, &pool->data);
596 if (!pool->pool) {
597 free(pool);
598 return NULL;
599 }
600
601 pool->size = size;
602 pool->used = 0;
603
604 return pool;
605}
606
607static void *
608shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
609{
610 if (pool->used + size > pool->size)
611 return NULL;
612
613 *offset = pool->used;
614 pool->used += size;
615
616 return (char *) pool->data + *offset;
617}
618
619/* destroy the pool. this does not unmap the memory though */
620static void
621shm_pool_destroy(struct shm_pool *pool)
622{
623 munmap(pool->data, pool->size);
624 wl_shm_pool_destroy(pool->pool);
625 free(pool);
626}
627
628/* Start allocating from the beginning of the pool again */
629static void
630shm_pool_reset(struct shm_pool *pool)
631{
632 pool->used = 0;
633}
634
635static int
636data_length_for_shm_surface(struct rectangle *rect)
637{
638 int stride;
639
640 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
641 rect->width);
642 return stride * rect->height;
643}
644
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500645static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700646display_create_shm_surface_from_pool(struct display *display,
647 struct rectangle *rectangle,
648 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649{
650 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400651 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400652 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700653 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400654 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400655
656 data = malloc(sizeof *data);
657 if (data == NULL)
658 return NULL;
659
660 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
661 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700662 length = stride * rectangle->height;
663 data->pool = NULL;
664 map = shm_pool_allocate(pool, length, &offset);
665
666 if (!map) {
667 free(data);
668 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400669 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400671 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400672 CAIRO_FORMAT_ARGB32,
673 rectangle->width,
674 rectangle->height,
675 stride);
676
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200677 cairo_surface_set_user_data(surface, &shm_surface_data_key,
678 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400679
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400680 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500681 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400682 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500683 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400684
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200685 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
686 rectangle->width,
687 rectangle->height,
688 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700690 return surface;
691}
692
693static cairo_surface_t *
694display_create_shm_surface(struct display *display,
695 struct rectangle *rectangle, uint32_t flags,
696 struct window *window)
697{
698 struct shm_surface_data *data;
699 struct shm_pool *pool;
700 cairo_surface_t *surface;
701
702 if (window && window->pool) {
703 shm_pool_reset(window->pool);
704 surface = display_create_shm_surface_from_pool(display,
705 rectangle,
706 flags,
707 window->pool);
708 if (surface)
709 return surface;
710 }
711
712 pool = shm_pool_create(display,
713 data_length_for_shm_surface(rectangle));
714 if (!pool)
715 return NULL;
716
717 surface =
718 display_create_shm_surface_from_pool(display, rectangle,
719 flags, pool);
720
721 if (!surface) {
722 shm_pool_destroy(pool);
723 return NULL;
724 }
725
726 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200727 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700728 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400729
730 return surface;
731}
732
nobled7b87cb02011-02-01 18:51:47 +0000733static int
734check_size(struct rectangle *rect)
735{
736 if (rect->width && rect->height)
737 return 0;
738
739 fprintf(stderr, "tried to create surface of "
740 "width: %d, height: %d\n", rect->width, rect->height);
741 return -1;
742}
743
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400744cairo_surface_t *
745display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100746 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400747 struct rectangle *rectangle,
748 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400749{
nobled7b87cb02011-02-01 18:51:47 +0000750 if (check_size(rectangle) < 0)
751 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200752
753 assert(flags & SURFACE_SHM);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400754 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400755}
756
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200757/*
758 * The following correspondences between file names and cursors was copied
759 * from: https://bugs.kde.org/attachment.cgi?id=67313
760 */
761
762static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300763 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200764 "sw-resize"
765};
766
767static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300768 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200769 "se-resize"
770};
771
772static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300773 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200774 "s-resize"
775};
776
777static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300778 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200779 "closedhand",
780 "208530c400c041818281048008011002"
781};
782
783static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300784 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200785 "default",
786 "top_left_arrow",
787 "left-arrow"
788};
789
790static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300791 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200792 "w-resize"
793};
794
795static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300796 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200797 "e-resize"
798};
799
800static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300801 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200802 "nw-resize"
803};
804
805static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300806 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200807 "ne-resize"
808};
809
810static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300811 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200812 "n-resize"
813};
814
815static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300816 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200817 "ibeam",
818 "text"
819};
820
821static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300822 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200823 "pointer",
824 "pointing_hand",
825 "e29285e634086352946a0e7090d73106"
826};
827
828static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400829 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200830 "wait",
831 "0426c94ea35c87780ff01dc239897213"
832};
833
834struct cursor_alternatives {
835 const char **names;
836 size_t count;
837};
838
839static const struct cursor_alternatives cursors[] = {
840 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
841 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
842 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
843 {grabbings, ARRAY_LENGTH(grabbings)},
844 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
845 {left_sides, ARRAY_LENGTH(left_sides)},
846 {right_sides, ARRAY_LENGTH(right_sides)},
847 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
848 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
849 {top_sides, ARRAY_LENGTH(top_sides)},
850 {xterms, ARRAY_LENGTH(xterms)},
851 {hand1s, ARRAY_LENGTH(hand1s)},
852 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300853};
854
855static void
856create_cursors(struct display *display)
857{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100858 char *config_file;
859 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200860 unsigned int i, j;
861 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100862 struct config_key shell_keys[] = {
863 { "cursor-theme", CONFIG_KEY_STRING, &theme },
864 };
865 struct config_section cs[] = {
866 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
867 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300868
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100869 config_file = config_file_path("weston.ini");
870 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
871 free(config_file);
872
873 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300874 display->cursors =
875 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
876
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300877 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200878 cursor = NULL;
879 for (j = 0; !cursor && j < cursors[i].count; ++j)
880 cursor = wl_cursor_theme_get_cursor(
881 display->cursor_theme, cursors[i].names[j]);
882
883 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300884 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200885 cursors[i].names[0]);
886
887 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300888 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300889}
890
891static void
892destroy_cursors(struct display *display)
893{
894 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800895 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300896}
897
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300898struct wl_cursor_image *
899display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400900{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200901 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400902
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300903 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400904}
905
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400906static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200907window_get_resize_dx_dy(struct window *window, int *x, int *y)
908{
909 if (window->resize_edges & WINDOW_RESIZING_LEFT)
910 *x = window->server_allocation.width - window->allocation.width;
911 else
912 *x = 0;
913
914 if (window->resize_edges & WINDOW_RESIZING_TOP)
915 *y = window->server_allocation.height -
916 window->allocation.height;
917 else
918 *y = 0;
919
920 window->resize_edges = 0;
921}
922
923static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500924window_attach_surface(struct window *window)
925{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400926 struct display *display = window->display;
927 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500928 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100929
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400930 if (window->type == TYPE_NONE) {
931 window->type = TYPE_TOPLEVEL;
932 if (display->shell)
933 wl_shell_surface_set_toplevel(window->shell_surface);
934 }
935
Pekka Paalanen512dde82012-10-10 12:49:27 +0300936 if (window->opaque_region) {
937 wl_surface_set_opaque_region(window->surface,
938 window->opaque_region);
939 wl_region_destroy(window->opaque_region);
940 window->opaque_region = NULL;
941 }
942
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300943 if (window->input_region) {
944 wl_surface_set_input_region(window->surface,
945 window->input_region);
946 wl_region_destroy(window->input_region);
947 window->input_region = NULL;
948 }
949
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100950 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000951#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100952 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200953 window->toysurface->swap(window->toysurface,
954 &window->server_allocation);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100955 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000956#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100957 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100958 buffer =
959 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400960 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100961
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200962 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100963 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400964 wl_surface_damage(window->surface, 0, 0,
965 window->allocation.width,
966 window->allocation.height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300967 wl_surface_commit(window->surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100968 window->server_allocation = window->allocation;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100969 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000970 default:
971 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100972 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500973}
974
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400975int
976window_has_focus(struct window *window)
977{
978 return window->focus_count > 0;
979}
980
Pekka Paalanena8d4c842012-11-19 15:32:48 +0200981static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400982window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500983{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200984 if (!window->cairo_surface)
985 return;
986
987 window_attach_surface(window);
988 cairo_surface_destroy(window->cairo_surface);
989 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500990}
991
Pekka Paalanence36f6d2012-11-19 15:32:47 +0200992static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400993window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400994{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500995 cairo_surface_reference(surface);
996
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400997 if (window->cairo_surface != NULL)
998 cairo_surface_destroy(window->cairo_surface);
999
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -05001000 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001001}
1002
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001003struct display *
1004window_get_display(struct window *window)
1005{
1006 return window->display;
1007}
1008
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001009static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001010window_create_surface(struct window *window)
1011{
1012 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001013 uint32_t flags = 0;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001014
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001015 if (!window->transparent)
1016 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001017
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001018 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001019#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001020 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001021 if (!window->toysurface && window->display->dpy)
1022 window->toysurface =
1023 egl_window_surface_create(window->display,
1024 window->surface,
1025 flags,
1026 &window->allocation);
1027
1028 if (window->toysurface) {
1029 int dx, dy;
1030
1031 window_get_resize_dx_dy(window, &dx, &dy);
1032 surface = window->toysurface->prepare(window->toysurface,
1033 dx, dy,
1034 window->allocation.width,
1035 window->allocation.height);
Pekka Paalanen768117f2012-11-19 17:15:57 +02001036 break;
1037 }
1038 /* fall through */
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001039#endif
1040 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001041 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001042 &window->allocation,
1043 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001044 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -08001045 default:
1046 surface = NULL;
1047 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001048 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001049
1050 window_set_surface(window, surface);
1051 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001052}
1053
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001054static void frame_destroy(struct frame *frame);
1055
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001056void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001057window_destroy(struct window *window)
1058{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001059 struct display *display = window->display;
1060 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001061 struct window_output *window_output;
1062 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001063
1064 if (window->redraw_scheduled)
1065 wl_list_remove(&window->redraw_task.link);
1066
1067 wl_list_for_each(input, &display->input_list, link) {
1068 if (input->pointer_focus == window)
1069 input->pointer_focus = NULL;
1070 if (input->keyboard_focus == window)
1071 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001072 if (input->focus_widget &&
1073 input->focus_widget->window == window)
1074 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001075 }
1076
Rob Bradford7507b572012-05-15 17:55:34 +01001077 wl_list_for_each_safe(window_output, window_output_tmp,
1078 &window->window_output_list, link) {
1079 free (window_output);
1080 }
1081
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001082 if (window->input_region)
1083 wl_region_destroy(window->input_region);
1084 if (window->opaque_region)
1085 wl_region_destroy(window->opaque_region);
1086
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001087 if (window->frame)
1088 frame_destroy(window->frame);
1089
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001090 if (window->shell_surface)
1091 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001092 wl_surface_destroy(window->surface);
1093 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001094
1095 if (window->cairo_surface != NULL)
1096 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001097
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001098 if (window->toysurface)
1099 window->toysurface->destroy(window->toysurface);
1100
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001101 if (window->frame_cb)
1102 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001103 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001104 free(window);
1105}
1106
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001107static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001108widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001109{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001110 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001111
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001112 wl_list_for_each(child, &widget->child_list, link) {
1113 target = widget_find_widget(child, x, y);
1114 if (target)
1115 return target;
1116 }
1117
1118 if (widget->allocation.x <= x &&
1119 x < widget->allocation.x + widget->allocation.width &&
1120 widget->allocation.y <= y &&
1121 y < widget->allocation.y + widget->allocation.height) {
1122 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001123 }
1124
1125 return NULL;
1126}
1127
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001128static struct widget *
1129widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001130{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001131 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001132
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001133 widget = malloc(sizeof *widget);
1134 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001135 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001136 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001137 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001138 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001139 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001140 widget->tooltip = NULL;
1141 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001142
1143 return widget;
1144}
1145
1146struct widget *
1147window_add_widget(struct window *window, void *data)
1148{
1149 window->widget = widget_create(window, data);
1150 wl_list_init(&window->widget->link);
1151
1152 return window->widget;
1153}
1154
1155struct widget *
1156widget_add_widget(struct widget *parent, void *data)
1157{
1158 struct widget *widget;
1159
1160 widget = widget_create(parent->window, data);
1161 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001162
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001163 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001164}
1165
1166void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001167widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001168{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001169 struct display *display = widget->window->display;
1170 struct input *input;
1171
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001172 if (widget->tooltip) {
1173 free(widget->tooltip);
1174 widget->tooltip = NULL;
1175 }
1176
Pekka Paalanene156fb62012-01-19 13:51:38 +02001177 wl_list_for_each(input, &display->input_list, link) {
1178 if (input->focus_widget == widget)
1179 input->focus_widget = NULL;
1180 }
1181
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001182 wl_list_remove(&widget->link);
1183 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001184}
1185
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001186void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001187widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001188{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001189 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001190}
1191
1192void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001193widget_set_size(struct widget *widget, int32_t width, int32_t height)
1194{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001195 widget->allocation.width = width;
1196 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001197}
1198
1199void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001200widget_set_allocation(struct widget *widget,
1201 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001202{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001203 widget->allocation.x = x;
1204 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001205 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001206}
1207
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001208void
1209widget_set_transparent(struct widget *widget, int transparent)
1210{
1211 widget->opaque = !transparent;
1212}
1213
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001214void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001215widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001216{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001217 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001218}
1219
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001220void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001221widget_set_resize_handler(struct widget *widget,
1222 widget_resize_handler_t handler)
1223{
1224 widget->resize_handler = handler;
1225}
1226
1227void
1228widget_set_redraw_handler(struct widget *widget,
1229 widget_redraw_handler_t handler)
1230{
1231 widget->redraw_handler = handler;
1232}
1233
1234void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001235widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001236{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001237 widget->enter_handler = handler;
1238}
1239
1240void
1241widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1242{
1243 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001244}
1245
1246void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001247widget_set_motion_handler(struct widget *widget,
1248 widget_motion_handler_t handler)
1249{
1250 widget->motion_handler = handler;
1251}
1252
1253void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001254widget_set_button_handler(struct widget *widget,
1255 widget_button_handler_t handler)
1256{
1257 widget->button_handler = handler;
1258}
1259
1260void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001261widget_set_axis_handler(struct widget *widget,
1262 widget_axis_handler_t handler)
1263{
1264 widget->axis_handler = handler;
1265}
1266
1267void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001268widget_schedule_redraw(struct widget *widget)
1269{
1270 window_schedule_redraw(widget->window);
1271}
1272
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001273cairo_surface_t *
1274window_get_surface(struct window *window)
1275{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001276 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001277}
1278
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001279struct wl_surface *
1280window_get_wl_surface(struct window *window)
1281{
1282 return window->surface;
1283}
1284
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001285struct wl_shell_surface *
1286window_get_wl_shell_surface(struct window *window)
1287{
1288 return window->shell_surface;
1289}
1290
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001291static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001292tooltip_redraw_handler(struct widget *widget, void *data)
1293{
1294 cairo_t *cr;
1295 const int32_t r = 3;
1296 struct tooltip *tooltip = data;
1297 int32_t width, height;
1298 struct window *window = widget->window;
1299
1300 cr = cairo_create(window->cairo_surface);
1301 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1302 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1303 cairo_paint(cr);
1304
1305 width = window->allocation.width;
1306 height = window->allocation.height;
1307 rounded_rect(cr, 0, 0, width, height, r);
1308
1309 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1310 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1311 cairo_fill(cr);
1312
1313 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1314 cairo_move_to(cr, 10, 16);
1315 cairo_show_text(cr, tooltip->entry);
1316 cairo_destroy(cr);
1317}
1318
1319static cairo_text_extents_t
1320get_text_extents(struct tooltip *tooltip)
1321{
1322 struct window *window;
1323 cairo_t *cr;
1324 cairo_text_extents_t extents;
1325
1326 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1327 * created yet */
1328 window = tooltip->widget->window->parent;
1329 cr = cairo_create(window->cairo_surface);
1330 cairo_text_extents(cr, tooltip->entry, &extents);
1331 cairo_destroy(cr);
1332
1333 return extents;
1334}
1335
1336static int
1337window_create_tooltip(struct tooltip *tooltip)
1338{
1339 struct widget *parent = tooltip->parent;
1340 struct display *display = parent->window->display;
1341 struct window *window;
1342 const int offset_y = 27;
1343 const int margin = 3;
1344 cairo_text_extents_t extents;
1345
1346 if (tooltip->widget)
1347 return 0;
1348
1349 window = window_create_transient(display, parent->window, tooltip->x,
1350 tooltip->y + offset_y,
1351 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1352 if (!window)
1353 return -1;
1354
1355 tooltip->window = window;
1356 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1357
1358 extents = get_text_extents(tooltip);
1359 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1360 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1361
1362 return 0;
1363}
1364
1365void
1366widget_destroy_tooltip(struct widget *parent)
1367{
1368 struct tooltip *tooltip = parent->tooltip;
1369
1370 parent->tooltip_count = 0;
1371 if (!tooltip)
1372 return;
1373
1374 if (tooltip->widget) {
1375 widget_destroy(tooltip->widget);
1376 window_destroy(tooltip->window);
1377 tooltip->widget = NULL;
1378 tooltip->window = NULL;
1379 }
1380
1381 close(tooltip->tooltip_fd);
1382 free(tooltip->entry);
1383 free(tooltip);
1384 parent->tooltip = NULL;
1385}
1386
1387static void
1388tooltip_func(struct task *task, uint32_t events)
1389{
1390 struct tooltip *tooltip =
1391 container_of(task, struct tooltip, tooltip_task);
1392 uint64_t exp;
1393
Martin Olsson8df662a2012-07-08 03:03:47 +02001394 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1395 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001396 window_create_tooltip(tooltip);
1397}
1398
1399#define TOOLTIP_TIMEOUT 500
1400static int
1401tooltip_timer_reset(struct tooltip *tooltip)
1402{
1403 struct itimerspec its;
1404
1405 its.it_interval.tv_sec = 0;
1406 its.it_interval.tv_nsec = 0;
1407 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1408 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1409 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1410 fprintf(stderr, "could not set timerfd\n: %m");
1411 return -1;
1412 }
1413
1414 return 0;
1415}
1416
1417int
1418widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1419{
1420 struct tooltip *tooltip = parent->tooltip;
1421
1422 parent->tooltip_count++;
1423 if (tooltip) {
1424 tooltip->x = x;
1425 tooltip->y = y;
1426 tooltip_timer_reset(tooltip);
1427 return 0;
1428 }
1429
1430 /* the handler might be triggered too fast via input device motion, so
1431 * we need this check here to make sure tooltip is fully initialized */
1432 if (parent->tooltip_count > 1)
1433 return 0;
1434
1435 tooltip = malloc(sizeof *tooltip);
1436 if (!tooltip)
1437 return -1;
1438
1439 parent->tooltip = tooltip;
1440 tooltip->parent = parent;
1441 tooltip->widget = NULL;
1442 tooltip->window = NULL;
1443 tooltip->x = x;
1444 tooltip->y = y;
1445 tooltip->entry = strdup(entry);
1446 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1447 if (tooltip->tooltip_fd < 0) {
1448 fprintf(stderr, "could not create timerfd\n: %m");
1449 return -1;
1450 }
1451
1452 tooltip->tooltip_task.run = tooltip_func;
1453 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1454 EPOLLIN, &tooltip->tooltip_task);
1455 tooltip_timer_reset(tooltip);
1456
1457 return 0;
1458}
1459
1460static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001461workspace_manager_state(void *data,
1462 struct workspace_manager *workspace_manager,
1463 uint32_t current,
1464 uint32_t count)
1465{
1466 struct display *display = data;
1467
1468 display->workspace = current;
1469 display->workspace_count = count;
1470}
1471
1472static const struct workspace_manager_listener workspace_manager_listener = {
1473 workspace_manager_state
1474};
1475
1476static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001477frame_resize_handler(struct widget *widget,
1478 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001479{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001480 struct frame *frame = data;
1481 struct widget *child = frame->child;
1482 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001483 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001484 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001485 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001486 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001487 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001488 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001489
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001490 switch (widget->window->type) {
1491 case TYPE_FULLSCREEN:
1492 decoration_width = 0;
1493 decoration_height = 0;
1494
1495 allocation.x = 0;
1496 allocation.y = 0;
1497 allocation.width = width;
1498 allocation.height = height;
1499 opaque_margin = 0;
1500
1501 wl_list_for_each(button, &frame->buttons_list, link)
1502 button->widget->opaque = 1;
1503 break;
1504 case TYPE_MAXIMIZED:
1505 decoration_width = t->width * 2;
1506 decoration_height = t->width + t->titlebar_height;
1507
1508 allocation.x = t->width;
1509 allocation.y = t->titlebar_height;
1510 allocation.width = width - decoration_width;
1511 allocation.height = height - decoration_height;
1512
1513 opaque_margin = 0;
1514
1515 wl_list_for_each(button, &frame->buttons_list, link)
1516 button->widget->opaque = 0;
1517 break;
1518 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001519 decoration_width = (t->width + t->margin) * 2;
1520 decoration_height = t->width +
1521 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001522
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001523 allocation.x = t->width + t->margin;
1524 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001525 allocation.width = width - decoration_width;
1526 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001527
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001528 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001529
1530 wl_list_for_each(button, &frame->buttons_list, link)
1531 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001532 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001533 }
1534
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001535 widget_set_allocation(child, allocation.x, allocation.y,
1536 allocation.width, allocation.height);
1537
1538 if (child->resize_handler)
1539 child->resize_handler(child,
1540 allocation.width,
1541 allocation.height,
1542 child->user_data);
1543
Scott Moreauf7e498c2012-05-14 11:39:29 -06001544 width = child->allocation.width + decoration_width;
1545 height = child->allocation.height + decoration_height;
1546
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001547 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1548
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001549 widget->window->input_region =
1550 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001551 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001552 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001553 shadow_margin, shadow_margin,
1554 width - 2 * shadow_margin,
1555 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001556 } else {
1557 wl_region_add(widget->window->input_region,
1558 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001559 }
1560
Scott Moreauf7e498c2012-05-14 11:39:29 -06001561 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001562
1563 if (child->opaque) {
1564 widget->window->opaque_region =
1565 wl_compositor_create_region(display->compositor);
1566 wl_region_add(widget->window->opaque_region,
1567 opaque_margin, opaque_margin,
1568 widget->allocation.width - 2 * opaque_margin,
1569 widget->allocation.height - 2 * opaque_margin);
1570 }
Martin Minarik1998b152012-05-10 02:04:35 +02001571
1572 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001573 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1574 x_l = t->width + shadow_margin;
1575 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001576 wl_list_for_each(button, &frame->buttons_list, link) {
1577 const int button_padding = 4;
1578 w = cairo_image_surface_get_width(button->icon);
1579 h = cairo_image_surface_get_height(button->icon);
1580
1581 if (button->decoration == FRAME_BUTTON_FANCY)
1582 w += 10;
1583
1584 if (button->align == FRAME_BUTTON_LEFT) {
1585 widget_set_allocation(button->widget,
1586 x_l, y , w + 1, h + 1);
1587 x_l += w;
1588 x_l += button_padding;
1589 } else {
1590 x_r -= w;
1591 widget_set_allocation(button->widget,
1592 x_r, y , w + 1, h + 1);
1593 x_r -= button_padding;
1594 }
1595 }
1596}
1597
1598static int
1599frame_button_enter_handler(struct widget *widget,
1600 struct input *input, float x, float y, void *data)
1601{
1602 struct frame_button *frame_button = data;
1603
1604 widget_schedule_redraw(frame_button->widget);
1605 frame_button->state = FRAME_BUTTON_OVER;
1606
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001607 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001608}
1609
1610static void
1611frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1612{
1613 struct frame_button *frame_button = data;
1614
1615 widget_schedule_redraw(frame_button->widget);
1616 frame_button->state = FRAME_BUTTON_DEFAULT;
1617}
1618
1619static void
1620frame_button_button_handler(struct widget *widget,
1621 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001622 uint32_t button,
1623 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001624{
1625 struct frame_button *frame_button = data;
1626 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001627 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001628
1629 if (button != BTN_LEFT)
1630 return;
1631
1632 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001633 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001634 frame_button->state = FRAME_BUTTON_ACTIVE;
1635 widget_schedule_redraw(frame_button->widget);
1636
1637 if (frame_button->type == FRAME_BUTTON_ICON)
1638 window_show_frame_menu(window, input, time);
1639 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001640 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001641 frame_button->state = FRAME_BUTTON_DEFAULT;
1642 widget_schedule_redraw(frame_button->widget);
1643 break;
1644 }
1645
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001646 if (!was_pressed)
1647 return;
1648
Martin Minarik1998b152012-05-10 02:04:35 +02001649 switch (frame_button->type) {
1650 case FRAME_BUTTON_CLOSE:
1651 if (window->close_handler)
1652 window->close_handler(window->parent,
1653 window->user_data);
1654 else
1655 display_exit(window->display);
1656 break;
1657 case FRAME_BUTTON_MINIMIZE:
1658 fprintf(stderr,"Minimize stub\n");
1659 break;
1660 case FRAME_BUTTON_MAXIMIZE:
1661 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1662 break;
1663 default:
1664 /* Unknown operation */
1665 break;
1666 }
1667}
1668
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001669static int
1670frame_button_motion_handler(struct widget *widget,
1671 struct input *input, uint32_t time,
1672 float x, float y, void *data)
1673{
1674 struct frame_button *frame_button = data;
1675 enum frame_button_pointer previous_button_state = frame_button->state;
1676
1677 /* only track state for a pressed button */
1678 if (input->grab != widget)
1679 return CURSOR_LEFT_PTR;
1680
1681 if (x > widget->allocation.x &&
1682 x < (widget->allocation.x + widget->allocation.width) &&
1683 y > widget->allocation.y &&
1684 y < (widget->allocation.y + widget->allocation.height)) {
1685 frame_button->state = FRAME_BUTTON_ACTIVE;
1686 } else {
1687 frame_button->state = FRAME_BUTTON_DEFAULT;
1688 }
1689
1690 if (frame_button->state != previous_button_state)
1691 widget_schedule_redraw(frame_button->widget);
1692
1693 return CURSOR_LEFT_PTR;
1694}
1695
Martin Minarik1998b152012-05-10 02:04:35 +02001696static void
1697frame_button_redraw_handler(struct widget *widget, void *data)
1698{
1699 struct frame_button *frame_button = data;
1700 cairo_t *cr;
1701 int width, height, x, y;
1702 struct window *window = widget->window;
1703
1704 x = widget->allocation.x;
1705 y = widget->allocation.y;
1706 width = widget->allocation.width;
1707 height = widget->allocation.height;
1708
1709 if (!width)
1710 return;
1711 if (!height)
1712 return;
1713 if (widget->opaque)
1714 return;
1715
1716 cr = cairo_create(window->cairo_surface);
1717
1718 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1719 cairo_set_line_width(cr, 1);
1720
1721 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1722 cairo_rectangle (cr, x, y, 25, 16);
1723
1724 cairo_stroke_preserve(cr);
1725
1726 switch (frame_button->state) {
1727 case FRAME_BUTTON_DEFAULT:
1728 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1729 break;
1730 case FRAME_BUTTON_OVER:
1731 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1732 break;
1733 case FRAME_BUTTON_ACTIVE:
1734 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1735 break;
1736 }
1737
1738 cairo_fill (cr);
1739
1740 x += 4;
1741 }
1742
1743 cairo_set_source_surface(cr, frame_button->icon, x, y);
1744 cairo_paint(cr);
1745
1746 cairo_destroy(cr);
1747}
1748
1749static struct widget *
1750frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1751 enum frame_button_align align, enum frame_button_decoration style)
1752{
1753 struct frame_button *frame_button;
1754 const char *icon = data;
1755
1756 frame_button = malloc (sizeof *frame_button);
1757 memset(frame_button, 0, sizeof *frame_button);
1758
1759 frame_button->icon = cairo_image_surface_create_from_png(icon);
1760 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1761 frame_button->frame = frame;
1762 frame_button->type = type;
1763 frame_button->align = align;
1764 frame_button->decoration = style;
1765
1766 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1767
1768 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1769 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1770 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1771 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001772 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001773 return frame_button->widget;
1774}
1775
1776static void
1777frame_button_destroy(struct frame_button *frame_button)
1778{
1779 widget_destroy(frame_button->widget);
1780 wl_list_remove(&frame_button->link);
1781 cairo_surface_destroy(frame_button->icon);
1782 free(frame_button);
1783
1784 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001785}
1786
1787static void
1788frame_redraw_handler(struct widget *widget, void *data)
1789{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001790 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001791 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001792 struct theme *t = window->display->theme;
1793 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001794
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001795 if (window->type == TYPE_FULLSCREEN)
1796 return;
1797
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001798 cr = cairo_create(window->cairo_surface);
1799
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001800 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001801 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001802 if (window->type == TYPE_MAXIMIZED)
1803 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001804 theme_render_frame(t, cr, widget->allocation.width,
1805 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001806
1807 cairo_destroy(cr);
1808}
1809
1810static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001811frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001812{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001813 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001814 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001815 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001816
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001817 location = theme_get_location(t, input->sx, input->sy,
1818 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001819 frame->widget->allocation.height,
1820 window->type == TYPE_MAXIMIZED ?
1821 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001822
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001823 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001824 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001825 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001826 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001827 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001828 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001829 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001830 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001831 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001832 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001833 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001834 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001835 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001836 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001837 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001838 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001839 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001840 case THEME_LOCATION_EXTERIOR:
1841 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001842 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001843 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001844 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001845}
1846
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001847static void
1848frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001849{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001850 struct display *display;
1851
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001852 switch (index) {
1853 case 0: /* close */
1854 if (window->close_handler)
1855 window->close_handler(window->parent,
1856 window->user_data);
1857 else
1858 display_exit(window->display);
1859 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001860 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001861 display = window->display;
1862 if (display->workspace > 0)
1863 workspace_manager_move_surface(display->workspace_manager,
1864 window->surface,
1865 display->workspace - 1);
1866 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001867 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001868 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001869 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001870 workspace_manager_move_surface(display->workspace_manager,
1871 window->surface,
1872 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05001873 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001874 case 3: /* fullscreen */
1875 /* we don't have a way to get out of fullscreen for now */
1876 if (window->fullscreen_handler)
1877 window->fullscreen_handler(window, window->user_data);
1878 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001879 }
1880}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001881
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001882void
1883window_show_frame_menu(struct window *window,
1884 struct input *input, uint32_t time)
1885{
1886 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001887 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001888
1889 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001890 "Close",
1891 "Move to workspace above", "Move to workspace below",
1892 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001893 };
1894
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001895 if (window->fullscreen_handler)
1896 count = ARRAY_LENGTH(entries);
1897 else
1898 count = ARRAY_LENGTH(entries) - 1;
1899
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001900 input_get_position(input, &x, &y);
1901 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001902 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001903}
1904
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001905static int
1906frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001907 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001908{
1909 return frame_get_pointer_image_for_location(data, input);
1910}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001911
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001912static int
1913frame_motion_handler(struct widget *widget,
1914 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001915 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001916{
1917 return frame_get_pointer_image_for_location(data, input);
1918}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001919
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001920static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001921frame_button_handler(struct widget *widget,
1922 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001923 uint32_t button, enum wl_pointer_button_state state,
1924 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001925
1926{
1927 struct frame *frame = data;
1928 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001929 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001930 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001931
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001932 location = theme_get_location(display->theme, input->sx, input->sy,
1933 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001934 frame->widget->allocation.height,
1935 window->type == TYPE_MAXIMIZED ?
1936 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001937
Daniel Stone4dbadb12012-05-30 16:31:51 +01001938 if (window->display->shell && button == BTN_LEFT &&
1939 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001940 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001941 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001942 if (!window->shell_surface)
1943 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001944 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001945 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001946 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001947 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001948 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001949 case THEME_LOCATION_RESIZING_TOP:
1950 case THEME_LOCATION_RESIZING_BOTTOM:
1951 case THEME_LOCATION_RESIZING_LEFT:
1952 case THEME_LOCATION_RESIZING_RIGHT:
1953 case THEME_LOCATION_RESIZING_TOP_LEFT:
1954 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1955 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1956 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001957 if (!window->shell_surface)
1958 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001959 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001960
1961 if (!display->dpy) {
1962 /* If we're using shm, allocate a big
1963 pool to create buffers out of while
1964 we resize. We should probably base
1965 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001966 window->pool =
1967 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001968 }
1969
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001970 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001971 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001972 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001973 break;
1974 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001975 } else if (button == BTN_RIGHT &&
1976 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001977 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001978 }
1979}
1980
1981struct widget *
1982frame_create(struct window *window, void *data)
1983{
1984 struct frame *frame;
1985
1986 frame = malloc(sizeof *frame);
1987 memset(frame, 0, sizeof *frame);
1988
1989 frame->widget = window_add_widget(window, frame);
1990 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001991
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001992 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1993 widget_set_resize_handler(frame->widget, frame_resize_handler);
1994 widget_set_enter_handler(frame->widget, frame_enter_handler);
1995 widget_set_motion_handler(frame->widget, frame_motion_handler);
1996 widget_set_button_handler(frame->widget, frame_button_handler);
1997
Martin Minarik1998b152012-05-10 02:04:35 +02001998 /* Create empty list for frame buttons */
1999 wl_list_init(&frame->buttons_list);
2000
2001 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2002 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2003
2004 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2005 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2006
2007 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2008 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2009
2010 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2011 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2012
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002013 window->frame = frame;
2014
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002015 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002016}
2017
Kristian Høgsberga1627922012-06-20 17:30:03 -04002018void
2019frame_set_child_size(struct widget *widget, int child_width, int child_height)
2020{
2021 struct display *display = widget->window->display;
2022 struct theme *t = display->theme;
2023 int decoration_width, decoration_height;
2024 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002025 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002026
2027 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002028 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002029 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002030 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002031
2032 width = child_width + decoration_width;
2033 height = child_height + decoration_height;
2034 } else {
2035 width = child_width;
2036 height = child_height;
2037 }
2038
2039 window_schedule_resize(widget->window, width, height);
2040}
2041
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002042static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002043frame_destroy(struct frame *frame)
2044{
Martin Minarik1998b152012-05-10 02:04:35 +02002045 struct frame_button *button, *tmp;
2046
2047 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2048 frame_button_destroy(button);
2049
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002050 /* frame->child must be destroyed by the application */
2051 widget_destroy(frame->widget);
2052 free(frame);
2053}
2054
2055static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002056input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002057 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002058{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002059 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002060 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002061
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002062 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002063 return;
2064
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002065 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002066 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002067 widget = old;
2068 if (input->grab)
2069 widget = input->grab;
2070 if (widget->leave_handler)
2071 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002072 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002073 }
2074
2075 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002076 widget = focus;
2077 if (input->grab)
2078 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002079 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002080 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002081 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002082 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002083
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002084 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002085 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002086}
2087
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002088void
2089input_grab(struct input *input, struct widget *widget, uint32_t button)
2090{
2091 input->grab = widget;
2092 input->grab_button = button;
2093}
2094
2095void
2096input_ungrab(struct input *input)
2097{
2098 struct widget *widget;
2099
2100 input->grab = NULL;
2101 if (input->pointer_focus) {
2102 widget = widget_find_widget(input->pointer_focus->widget,
2103 input->sx, input->sy);
2104 input_set_focus_widget(input, widget, input->sx, input->sy);
2105 }
2106}
2107
2108static void
2109input_remove_pointer_focus(struct input *input)
2110{
2111 struct window *window = input->pointer_focus;
2112
2113 if (!window)
2114 return;
2115
2116 input_set_focus_widget(input, NULL, 0, 0);
2117
2118 input->pointer_focus = NULL;
2119 input->current_cursor = CURSOR_UNSET;
2120}
2121
2122static void
2123pointer_handle_enter(void *data, struct wl_pointer *pointer,
2124 uint32_t serial, struct wl_surface *surface,
2125 wl_fixed_t sx_w, wl_fixed_t sy_w)
2126{
2127 struct input *input = data;
2128 struct window *window;
2129 struct widget *widget;
2130 float sx = wl_fixed_to_double(sx_w);
2131 float sy = wl_fixed_to_double(sy_w);
2132
2133 if (!surface) {
2134 /* enter event for a window we've just destroyed */
2135 return;
2136 }
2137
2138 input->display->serial = serial;
2139 input->pointer_enter_serial = serial;
2140 input->pointer_focus = wl_surface_get_user_data(surface);
2141 window = input->pointer_focus;
2142
2143 if (window->pool) {
2144 shm_pool_destroy(window->pool);
2145 window->pool = NULL;
2146 /* Schedule a redraw to free the pool */
2147 window_schedule_redraw(window);
2148 }
2149
2150 input->sx = sx;
2151 input->sy = sy;
2152
2153 widget = widget_find_widget(window->widget, sx, sy);
2154 input_set_focus_widget(input, widget, sx, sy);
2155}
2156
2157static void
2158pointer_handle_leave(void *data, struct wl_pointer *pointer,
2159 uint32_t serial, struct wl_surface *surface)
2160{
2161 struct input *input = data;
2162
2163 input->display->serial = serial;
2164 input_remove_pointer_focus(input);
2165}
2166
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002167static void
Daniel Stone37816df2012-05-16 18:45:18 +01002168pointer_handle_motion(void *data, struct wl_pointer *pointer,
2169 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002170{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002171 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002172 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002173 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002174 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002175 float sx = wl_fixed_to_double(sx_w);
2176 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002177
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002178 input->sx = sx;
2179 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002180
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002181 if (!window)
2182 return;
2183
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002184 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002185 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002186 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002187 }
2188
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002189 if (input->grab)
2190 widget = input->grab;
2191 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002192 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002193 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002194 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002195 input, time, sx, sy,
2196 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002197
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002198 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002199}
2200
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002201static void
Daniel Stone37816df2012-05-16 18:45:18 +01002202pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002203 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002204{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002205 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002206 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002207 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002208
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002209 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002210 if (input->focus_widget && input->grab == NULL &&
2211 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002212 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002213
Neil Roberts6b28aad2012-01-23 19:11:18 +00002214 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002215 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002216 (*widget->button_handler)(widget,
2217 input, time,
2218 button, state,
2219 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002220
Daniel Stone4dbadb12012-05-30 16:31:51 +01002221 if (input->grab && input->grab_button == button &&
2222 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002223 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002224}
2225
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002226static void
Daniel Stone37816df2012-05-16 18:45:18 +01002227pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002228 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002229{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002230 struct input *input = data;
2231 struct widget *widget;
2232
2233 widget = input->focus_widget;
2234 if (input->grab)
2235 widget = input->grab;
2236 if (widget && widget->axis_handler)
2237 (*widget->axis_handler)(widget,
2238 input, time,
2239 axis, value,
2240 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002241}
2242
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002243static const struct wl_pointer_listener pointer_listener = {
2244 pointer_handle_enter,
2245 pointer_handle_leave,
2246 pointer_handle_motion,
2247 pointer_handle_button,
2248 pointer_handle_axis,
2249};
2250
2251static void
2252input_remove_keyboard_focus(struct input *input)
2253{
2254 struct window *window = input->keyboard_focus;
2255 struct itimerspec its;
2256
2257 its.it_interval.tv_sec = 0;
2258 its.it_interval.tv_nsec = 0;
2259 its.it_value.tv_sec = 0;
2260 its.it_value.tv_nsec = 0;
2261 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2262
2263 if (!window)
2264 return;
2265
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002266 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002267 if (window->keyboard_focus_handler)
2268 (*window->keyboard_focus_handler)(window, NULL,
2269 window->user_data);
2270
2271 input->keyboard_focus = NULL;
2272}
2273
2274static void
2275keyboard_repeat_func(struct task *task, uint32_t events)
2276{
2277 struct input *input =
2278 container_of(task, struct input, repeat_task);
2279 struct window *window = input->keyboard_focus;
2280 uint64_t exp;
2281
2282 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2283 /* If we change the timer between the fd becoming
2284 * readable and getting here, there'll be nothing to
2285 * read and we get EAGAIN. */
2286 return;
2287
2288 if (window && window->key_handler) {
2289 (*window->key_handler)(window, input, input->repeat_time,
2290 input->repeat_key, input->repeat_sym,
2291 WL_KEYBOARD_KEY_STATE_PRESSED,
2292 window->user_data);
2293 }
2294}
2295
2296static void
2297keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2298 uint32_t format, int fd, uint32_t size)
2299{
2300 struct input *input = data;
2301 char *map_str;
2302
2303 if (!data) {
2304 close(fd);
2305 return;
2306 }
2307
2308 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2309 close(fd);
2310 return;
2311 }
2312
2313 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2314 if (map_str == MAP_FAILED) {
2315 close(fd);
2316 return;
2317 }
2318
2319 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2320 map_str,
2321 XKB_KEYMAP_FORMAT_TEXT_V1,
2322 0);
2323 munmap(map_str, size);
2324 close(fd);
2325
2326 if (!input->xkb.keymap) {
2327 fprintf(stderr, "failed to compile keymap\n");
2328 return;
2329 }
2330
2331 input->xkb.state = xkb_state_new(input->xkb.keymap);
2332 if (!input->xkb.state) {
2333 fprintf(stderr, "failed to create XKB state\n");
2334 xkb_map_unref(input->xkb.keymap);
2335 input->xkb.keymap = NULL;
2336 return;
2337 }
2338
2339 input->xkb.control_mask =
2340 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2341 input->xkb.alt_mask =
2342 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2343 input->xkb.shift_mask =
2344 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2345}
2346
2347static void
2348keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2349 uint32_t serial, struct wl_surface *surface,
2350 struct wl_array *keys)
2351{
2352 struct input *input = data;
2353 struct window *window;
2354
2355 input->display->serial = serial;
2356 input->keyboard_focus = wl_surface_get_user_data(surface);
2357
2358 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002359 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002360 if (window->keyboard_focus_handler)
2361 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002362 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002363}
2364
2365static void
2366keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2367 uint32_t serial, struct wl_surface *surface)
2368{
2369 struct input *input = data;
2370
2371 input->display->serial = serial;
2372 input_remove_keyboard_focus(input);
2373}
2374
Scott Moreau210d0792012-03-22 10:47:01 -06002375static void
Daniel Stone37816df2012-05-16 18:45:18 +01002376keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002377 uint32_t serial, uint32_t time, uint32_t key,
2378 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002379{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002380 struct input *input = data;
2381 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002382 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002383 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002384 const xkb_keysym_t *syms;
2385 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002386 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002387
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002388 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002389 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002390 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002391 return;
2392
Daniel Stone97f68542012-05-30 16:32:01 +01002393 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002394
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002395 sym = XKB_KEY_NoSymbol;
2396 if (num_syms == 1)
2397 sym = syms[0];
2398
2399 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002400 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002401 window_set_maximized(window,
2402 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002403 } else if (sym == XKB_KEY_F11 &&
2404 window->fullscreen_handler &&
2405 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2406 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002407 } else if (sym == XKB_KEY_F4 &&
2408 input->modifiers == MOD_ALT_MASK &&
2409 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2410 if (window->close_handler)
2411 window->close_handler(window->parent,
2412 window->user_data);
2413 else
2414 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002415 } else if (window->key_handler) {
2416 (*window->key_handler)(window, input, time, key,
2417 sym, state, window->user_data);
2418 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002419
2420 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2421 key == input->repeat_key) {
2422 its.it_interval.tv_sec = 0;
2423 its.it_interval.tv_nsec = 0;
2424 its.it_value.tv_sec = 0;
2425 its.it_value.tv_nsec = 0;
2426 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2427 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2428 input->repeat_sym = sym;
2429 input->repeat_key = key;
2430 input->repeat_time = time;
2431 its.it_interval.tv_sec = 0;
2432 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2433 its.it_value.tv_sec = 0;
2434 its.it_value.tv_nsec = 400 * 1000 * 1000;
2435 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2436 }
2437}
2438
2439static void
Daniel Stone351eb612012-05-31 15:27:47 -04002440keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2441 uint32_t serial, uint32_t mods_depressed,
2442 uint32_t mods_latched, uint32_t mods_locked,
2443 uint32_t group)
2444{
2445 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002446 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002447
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002448 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2449 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002450 mask = xkb_state_serialize_mods(input->xkb.state,
2451 XKB_STATE_DEPRESSED |
2452 XKB_STATE_LATCHED);
2453 input->modifiers = 0;
2454 if (mask & input->xkb.control_mask)
2455 input->modifiers |= MOD_CONTROL_MASK;
2456 if (mask & input->xkb.alt_mask)
2457 input->modifiers |= MOD_ALT_MASK;
2458 if (mask & input->xkb.shift_mask)
2459 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002460}
2461
Daniel Stone37816df2012-05-16 18:45:18 +01002462static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002463 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002464 keyboard_handle_enter,
2465 keyboard_handle_leave,
2466 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002467 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002468};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002469
2470static void
Daniel Stone37816df2012-05-16 18:45:18 +01002471seat_handle_capabilities(void *data, struct wl_seat *seat,
2472 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002473{
Daniel Stone37816df2012-05-16 18:45:18 +01002474 struct input *input = data;
2475
2476 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2477 input->pointer = wl_seat_get_pointer(seat);
2478 wl_pointer_set_user_data(input->pointer, input);
2479 wl_pointer_add_listener(input->pointer, &pointer_listener,
2480 input);
2481 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2482 wl_pointer_destroy(input->pointer);
2483 input->pointer = NULL;
2484 }
2485
2486 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2487 input->keyboard = wl_seat_get_keyboard(seat);
2488 wl_keyboard_set_user_data(input->keyboard, input);
2489 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2490 input);
2491 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2492 wl_keyboard_destroy(input->keyboard);
2493 input->keyboard = NULL;
2494 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002495}
2496
Daniel Stone37816df2012-05-16 18:45:18 +01002497static const struct wl_seat_listener seat_listener = {
2498 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002499};
2500
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002501void
2502input_get_position(struct input *input, int32_t *x, int32_t *y)
2503{
2504 *x = input->sx;
2505 *y = input->sy;
2506}
2507
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002508struct display *
2509input_get_display(struct input *input)
2510{
2511 return input->display;
2512}
2513
Daniel Stone37816df2012-05-16 18:45:18 +01002514struct wl_seat *
2515input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002516{
Daniel Stone37816df2012-05-16 18:45:18 +01002517 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002518}
2519
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002520uint32_t
2521input_get_modifiers(struct input *input)
2522{
2523 return input->modifiers;
2524}
2525
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002526struct widget *
2527input_get_focus_widget(struct input *input)
2528{
2529 return input->focus_widget;
2530}
2531
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002532struct data_offer {
2533 struct wl_data_offer *offer;
2534 struct input *input;
2535 struct wl_array types;
2536 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002537
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002538 struct task io_task;
2539 int fd;
2540 data_func_t func;
2541 int32_t x, y;
2542 void *user_data;
2543};
2544
2545static void
2546data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2547{
2548 struct data_offer *offer = data;
2549 char **p;
2550
2551 p = wl_array_add(&offer->types, sizeof *p);
2552 *p = strdup(type);
2553}
2554
2555static const struct wl_data_offer_listener data_offer_listener = {
2556 data_offer_offer,
2557};
2558
2559static void
2560data_offer_destroy(struct data_offer *offer)
2561{
2562 char **p;
2563
2564 offer->refcount--;
2565 if (offer->refcount == 0) {
2566 wl_data_offer_destroy(offer->offer);
2567 for (p = offer->types.data; *p; p++)
2568 free(*p);
2569 wl_array_release(&offer->types);
2570 free(offer);
2571 }
2572}
2573
2574static void
2575data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002576 struct wl_data_device *data_device,
2577 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002578{
2579 struct data_offer *offer;
2580
2581 offer = malloc(sizeof *offer);
2582
2583 wl_array_init(&offer->types);
2584 offer->refcount = 1;
2585 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002586 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002587 wl_data_offer_add_listener(offer->offer,
2588 &data_offer_listener, offer);
2589}
2590
2591static void
2592data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002593 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002594 wl_fixed_t x_w, wl_fixed_t y_w,
2595 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002596{
2597 struct input *input = data;
2598 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002599 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002600 float x = wl_fixed_to_double(x_w);
2601 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002602 char **p;
2603
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002604 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002605 window = wl_surface_get_user_data(surface);
2606 input->pointer_focus = window;
2607
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002608 if (offer) {
2609 input->drag_offer = wl_data_offer_get_user_data(offer);
2610
2611 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2612 *p = NULL;
2613
2614 types_data = input->drag_offer->types.data;
2615 } else {
2616 input->drag_offer = NULL;
2617 types_data = NULL;
2618 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002619
2620 window = input->pointer_focus;
2621 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002622 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002623 window->user_data);
2624}
2625
2626static void
2627data_device_leave(void *data, struct wl_data_device *data_device)
2628{
2629 struct input *input = data;
2630
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002631 if (input->drag_offer) {
2632 data_offer_destroy(input->drag_offer);
2633 input->drag_offer = NULL;
2634 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002635}
2636
2637static void
2638data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002639 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002640{
2641 struct input *input = data;
2642 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002643 float x = wl_fixed_to_double(x_w);
2644 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002645 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002646
2647 input->sx = x;
2648 input->sy = y;
2649
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002650 if (input->drag_offer)
2651 types_data = input->drag_offer->types.data;
2652 else
2653 types_data = NULL;
2654
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002655 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002656 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002657 window->user_data);
2658}
2659
2660static void
2661data_device_drop(void *data, struct wl_data_device *data_device)
2662{
2663 struct input *input = data;
2664 struct window *window = input->pointer_focus;
2665
2666 if (window->drop_handler)
2667 window->drop_handler(window, input,
2668 input->sx, input->sy, window->user_data);
2669}
2670
2671static void
2672data_device_selection(void *data,
2673 struct wl_data_device *wl_data_device,
2674 struct wl_data_offer *offer)
2675{
2676 struct input *input = data;
2677 char **p;
2678
2679 if (input->selection_offer)
2680 data_offer_destroy(input->selection_offer);
2681
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002682 if (offer) {
2683 input->selection_offer = wl_data_offer_get_user_data(offer);
2684 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2685 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002686 } else {
2687 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002688 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002689}
2690
2691static const struct wl_data_device_listener data_device_listener = {
2692 data_device_data_offer,
2693 data_device_enter,
2694 data_device_leave,
2695 data_device_motion,
2696 data_device_drop,
2697 data_device_selection
2698};
2699
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002700static void
2701input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002702{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002703 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002704 struct wl_cursor *cursor;
2705 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002706
Daniel Stone80972742012-11-07 17:51:39 +11002707 if (!input->pointer)
2708 return;
2709
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002710 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002711 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002712 return;
2713
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002714 if (index >= (int) cursor->image_count) {
2715 fprintf(stderr, "cursor index out of range\n");
2716 return;
2717 }
2718
2719 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002720 buffer = wl_cursor_image_get_buffer(image);
2721 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002722 return;
2723
Kristian Høgsbergae277372012-08-01 09:41:08 -04002724 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002725 input->pointer_surface,
2726 image->hotspot_x, image->hotspot_y);
2727 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2728 wl_surface_damage(input->pointer_surface, 0, 0,
2729 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002730 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002731}
2732
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002733static const struct wl_callback_listener pointer_surface_listener;
2734
2735static void
2736pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2737 uint32_t time)
2738{
2739 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002740 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002741 int i;
2742
2743 if (callback) {
2744 assert(callback == input->cursor_frame_cb);
2745 wl_callback_destroy(callback);
2746 input->cursor_frame_cb = NULL;
2747 }
2748
Daniel Stone80972742012-11-07 17:51:39 +11002749 if (!input->pointer)
2750 return;
2751
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002752 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002753 wl_pointer_set_cursor(input->pointer,
2754 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002755 NULL, 0, 0);
2756 return;
2757 }
2758
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002759 if (input->current_cursor == CURSOR_UNSET)
2760 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002761 cursor = input->display->cursors[input->current_cursor];
2762 if (!cursor)
2763 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002764
2765 /* FIXME We don't have the current time on the first call so we set
2766 * the animation start to the time of the first frame callback. */
2767 if (time == 0)
2768 input->cursor_anim_start = 0;
2769 else if (input->cursor_anim_start == 0)
2770 input->cursor_anim_start = time;
2771
2772 if (time == 0 || input->cursor_anim_start == 0)
2773 i = 0;
2774 else
2775 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2776
Pekka Paalanenbc106382012-10-10 12:49:31 +03002777 if (cursor->image_count > 1) {
2778 input->cursor_frame_cb =
2779 wl_surface_frame(input->pointer_surface);
2780 wl_callback_add_listener(input->cursor_frame_cb,
2781 &pointer_surface_listener, input);
2782 }
2783
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002784 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002785}
2786
2787static const struct wl_callback_listener pointer_surface_listener = {
2788 pointer_surface_frame_callback
2789};
2790
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002791void
2792input_set_pointer_image(struct input *input, int pointer)
2793{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002794 int force = 0;
2795
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002796 if (!input->pointer)
2797 return;
2798
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002799 if (input->pointer_enter_serial > input->cursor_serial)
2800 force = 1;
2801
2802 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002803 return;
2804
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002805 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002806 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002807 if (!input->cursor_frame_cb)
2808 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002809 else if (force) {
2810 /* The current frame callback may be stuck if, for instance,
2811 * the set cursor request was processed by the server after
2812 * this client lost the focus. In this case the cursor surface
2813 * might not be mapped and the frame callback wouldn't ever
2814 * complete. Send a set_cursor and attach to try to map the
2815 * cursor surface again so that the callback will finish */
2816 input_set_pointer_image_index(input, 0);
2817 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002818}
2819
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002820struct wl_data_device *
2821input_get_data_device(struct input *input)
2822{
2823 return input->data_device;
2824}
2825
2826void
2827input_set_selection(struct input *input,
2828 struct wl_data_source *source, uint32_t time)
2829{
2830 wl_data_device_set_selection(input->data_device, source, time);
2831}
2832
2833void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002834input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002835{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002836 wl_data_offer_accept(input->drag_offer->offer,
2837 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002838}
2839
2840static void
2841offer_io_func(struct task *task, uint32_t events)
2842{
2843 struct data_offer *offer =
2844 container_of(task, struct data_offer, io_task);
2845 unsigned int len;
2846 char buffer[4096];
2847
2848 len = read(offer->fd, buffer, sizeof buffer);
2849 offer->func(buffer, len,
2850 offer->x, offer->y, offer->user_data);
2851
2852 if (len == 0) {
2853 close(offer->fd);
2854 data_offer_destroy(offer);
2855 }
2856}
2857
2858static void
2859data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2860 data_func_t func, void *user_data)
2861{
2862 int p[2];
2863
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002864 if (pipe2(p, O_CLOEXEC) == -1)
2865 return;
2866
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002867 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2868 close(p[1]);
2869
2870 offer->io_task.run = offer_io_func;
2871 offer->fd = p[0];
2872 offer->func = func;
2873 offer->refcount++;
2874 offer->user_data = user_data;
2875
2876 display_watch_fd(offer->input->display,
2877 offer->fd, EPOLLIN, &offer->io_task);
2878}
2879
2880void
2881input_receive_drag_data(struct input *input, const char *mime_type,
2882 data_func_t func, void *data)
2883{
2884 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2885 input->drag_offer->x = input->sx;
2886 input->drag_offer->y = input->sy;
2887}
2888
2889int
2890input_receive_selection_data(struct input *input, const char *mime_type,
2891 data_func_t func, void *data)
2892{
2893 char **p;
2894
2895 if (input->selection_offer == NULL)
2896 return -1;
2897
2898 for (p = input->selection_offer->types.data; *p; p++)
2899 if (strcmp(mime_type, *p) == 0)
2900 break;
2901
2902 if (*p == NULL)
2903 return -1;
2904
2905 data_offer_receive_data(input->selection_offer,
2906 mime_type, func, data);
2907 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002908}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002909
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002910int
2911input_receive_selection_data_to_fd(struct input *input,
2912 const char *mime_type, int fd)
2913{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002914 if (input->selection_offer)
2915 wl_data_offer_receive(input->selection_offer->offer,
2916 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002917
2918 return 0;
2919}
2920
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002921void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002922window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002923{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002924 if (!window->shell_surface)
2925 return;
2926
Daniel Stone37816df2012-05-16 18:45:18 +01002927 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002928}
2929
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002930static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002931idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002932{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002933 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002934
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002935 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002936 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002937 widget_set_allocation(widget,
2938 window->pending_allocation.x,
2939 window->pending_allocation.y,
2940 window->pending_allocation.width,
2941 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002942
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002943 if (window->input_region) {
2944 wl_region_destroy(window->input_region);
2945 window->input_region = NULL;
2946 }
2947
2948 if (window->opaque_region) {
2949 wl_region_destroy(window->opaque_region);
2950 window->opaque_region = NULL;
2951 }
2952
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002953 if (widget->resize_handler)
2954 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002955 widget->allocation.width,
2956 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002957 widget->user_data);
2958
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002959 if (window->allocation.width != widget->allocation.width ||
2960 window->allocation.height != widget->allocation.height) {
2961 window->allocation = widget->allocation;
2962 window_schedule_redraw(window);
2963 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002964}
2965
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002966void
2967window_schedule_resize(struct window *window, int width, int height)
2968{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002969 window->pending_allocation.x = 0;
2970 window->pending_allocation.y = 0;
2971 window->pending_allocation.width = width;
2972 window->pending_allocation.height = height;
2973
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002974 if (window->min_allocation.width == 0)
2975 window->min_allocation = window->pending_allocation;
2976 if (window->pending_allocation.width < window->min_allocation.width)
2977 window->pending_allocation.width = window->min_allocation.width;
2978 if (window->pending_allocation.height < window->min_allocation.height)
2979 window->pending_allocation.height = window->min_allocation.height;
2980
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002981 window->resize_needed = 1;
2982 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002983}
2984
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002985void
2986widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2987{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002988 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002989}
2990
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002991static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002992handle_ping(void *data, struct wl_shell_surface *shell_surface,
2993 uint32_t serial)
2994{
2995 wl_shell_surface_pong(shell_surface, serial);
2996}
2997
2998static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002999handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003000 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003001{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003002 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003003
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003004 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003005 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003006}
3007
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003008static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003009menu_destroy(struct menu *menu)
3010{
3011 widget_destroy(menu->widget);
3012 window_destroy(menu->window);
3013 free(menu);
3014}
3015
3016static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003017handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3018{
3019 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003020 struct menu *menu = window->widget->user_data;
3021
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003022 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003023 * device. Or just use wl_callback. And this really needs to
3024 * be a window vfunc that the menu can set. And we need the
3025 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003026
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003027 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003028 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003029 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003030}
3031
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003032static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003033 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003034 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003035 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003036};
3037
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003038void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003039window_get_allocation(struct window *window,
3040 struct rectangle *allocation)
3041{
3042 *allocation = window->allocation;
3043}
3044
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003045static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003046widget_redraw(struct widget *widget)
3047{
3048 struct widget *child;
3049
3050 if (widget->redraw_handler)
3051 widget->redraw_handler(widget, widget->user_data);
3052 wl_list_for_each(child, &widget->child_list, link)
3053 widget_redraw(child);
3054}
3055
3056static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003057frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3058{
3059 struct window *window = data;
3060
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003061 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003062 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003063 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003064 window->redraw_scheduled = 0;
3065 if (window->redraw_needed)
3066 window_schedule_redraw(window);
3067}
3068
3069static const struct wl_callback_listener listener = {
3070 frame_callback
3071};
3072
3073static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003074idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003075{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003076 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003077
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003078 if (window->resize_needed)
3079 idle_resize(window);
3080
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003081 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003082 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003083 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003084 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003085
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003086 window->frame_cb = wl_surface_frame(window->surface);
3087 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003088 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003089}
3090
3091void
3092window_schedule_redraw(struct window *window)
3093{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003094 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003095 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003096 window->redraw_task.run = idle_redraw;
3097 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003098 window->redraw_scheduled = 1;
3099 }
3100}
3101
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003102int
3103window_is_fullscreen(struct window *window)
3104{
3105 return window->type == TYPE_FULLSCREEN;
3106}
3107
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003108void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003109window_set_fullscreen(struct window *window, int fullscreen)
3110{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003111 if (!window->display->shell)
3112 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003113
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003114 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003115 return;
3116
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003117 if (fullscreen) {
3118 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003119 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003120 wl_shell_surface_set_fullscreen(window->shell_surface,
3121 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3122 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003123 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003124 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003125 wl_shell_surface_set_toplevel(window->shell_surface);
3126 window_schedule_resize(window,
3127 window->saved_allocation.width,
3128 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003129 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003130}
3131
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003132int
3133window_is_maximized(struct window *window)
3134{
3135 return window->type == TYPE_MAXIMIZED;
3136}
3137
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003138void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003139window_set_maximized(struct window *window, int maximized)
3140{
3141 if (!window->display->shell)
3142 return;
3143
3144 if ((window->type == TYPE_MAXIMIZED) == maximized)
3145 return;
3146
3147 if (window->type == TYPE_TOPLEVEL) {
3148 window->saved_allocation = window->allocation;
3149 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3150 window->type = TYPE_MAXIMIZED;
3151 } else {
3152 wl_shell_surface_set_toplevel(window->shell_surface);
3153 window->type = TYPE_TOPLEVEL;
3154 window_schedule_resize(window,
3155 window->saved_allocation.width,
3156 window->saved_allocation.height);
3157 }
3158}
3159
3160void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003161window_set_user_data(struct window *window, void *data)
3162{
3163 window->user_data = data;
3164}
3165
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003166void *
3167window_get_user_data(struct window *window)
3168{
3169 return window->user_data;
3170}
3171
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003172void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003173window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003174 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003175{
3176 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003177}
3178
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003179void
3180window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003181 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003182{
3183 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003184}
3185
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003186void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003187window_set_data_handler(struct window *window, window_data_handler_t handler)
3188{
3189 window->data_handler = handler;
3190}
3191
3192void
3193window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3194{
3195 window->drop_handler = handler;
3196}
3197
3198void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003199window_set_close_handler(struct window *window,
3200 window_close_handler_t handler)
3201{
3202 window->close_handler = handler;
3203}
3204
3205void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003206window_set_fullscreen_handler(struct window *window,
3207 window_fullscreen_handler_t handler)
3208{
3209 window->fullscreen_handler = handler;
3210}
3211
3212void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003213window_set_title(struct window *window, const char *title)
3214{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003215 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003216 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003217 if (window->shell_surface)
3218 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003219}
3220
3221const char *
3222window_get_title(struct window *window)
3223{
3224 return window->title;
3225}
3226
3227void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003228window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3229{
3230 struct text_cursor_position *text_cursor_position =
3231 window->display->text_cursor_position;
3232
Scott Moreau9295ce02012-06-01 12:46:10 -06003233 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003234 return;
3235
3236 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003237 window->surface,
3238 wl_fixed_from_int(x),
3239 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003240}
3241
3242void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003243window_damage(struct window *window, int32_t x, int32_t y,
3244 int32_t width, int32_t height)
3245{
3246 wl_surface_damage(window->surface, x, y, width, height);
3247}
3248
Casey Dahlin9074db52012-04-19 22:50:09 -04003249static void
3250surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003251 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003252{
Rob Bradford7507b572012-05-15 17:55:34 +01003253 struct window *window = data;
3254 struct output *output;
3255 struct output *output_found = NULL;
3256 struct window_output *window_output;
3257
3258 wl_list_for_each(output, &window->display->output_list, link) {
3259 if (output->output == wl_output) {
3260 output_found = output;
3261 break;
3262 }
3263 }
3264
3265 if (!output_found)
3266 return;
3267
3268 window_output = malloc (sizeof *window_output);
3269 window_output->output = output_found;
3270
3271 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003272}
3273
3274static void
3275surface_leave(void *data,
3276 struct wl_surface *wl_surface, struct wl_output *output)
3277{
Rob Bradford7507b572012-05-15 17:55:34 +01003278 struct window *window = data;
3279 struct window_output *window_output;
3280 struct window_output *window_output_found = NULL;
3281
3282 wl_list_for_each(window_output, &window->window_output_list, link) {
3283 if (window_output->output->output == output) {
3284 window_output_found = window_output;
3285 break;
3286 }
3287 }
3288
3289 if (window_output_found) {
3290 wl_list_remove(&window_output_found->link);
3291 free(window_output_found);
3292 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003293}
3294
3295static const struct wl_surface_listener surface_listener = {
3296 surface_enter,
3297 surface_leave
3298};
3299
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003300static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003301window_create_internal(struct display *display,
3302 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003303{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003304 struct window *window;
3305
3306 window = malloc(sizeof *window);
3307 if (window == NULL)
3308 return NULL;
3309
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003310 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003311 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003312 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003313 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003314 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003315 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003316 window->shell_surface =
3317 wl_shell_get_shell_surface(display->shell,
3318 window->surface);
3319 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003320 window->allocation.x = 0;
3321 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003322 window->allocation.width = 0;
3323 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003324 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003325 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003326 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003327 window->input_region = NULL;
3328 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003329
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003330 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003331#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003332 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003333#else
3334 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3335#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003336 else
3337 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003338
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003339 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003340 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003341 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003342
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003343 if (window->shell_surface) {
3344 wl_shell_surface_set_user_data(window->shell_surface, window);
3345 wl_shell_surface_add_listener(window->shell_surface,
3346 &shell_surface_listener, window);
3347 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003348
Rob Bradford7507b572012-05-15 17:55:34 +01003349 wl_list_init (&window->window_output_list);
3350
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003351 return window;
3352}
3353
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003354struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003355window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003356{
3357 struct window *window;
3358
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003359 window = window_create_internal(display, NULL, TYPE_NONE);
3360 if (!window)
3361 return NULL;
3362
3363 return window;
3364}
3365
3366struct window *
3367window_create_custom(struct display *display)
3368{
3369 struct window *window;
3370
3371 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003372 if (!window)
3373 return NULL;
3374
3375 return window;
3376}
3377
3378struct window *
3379window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003380 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003381{
3382 struct window *window;
3383
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003384 window = window_create_internal(parent->display,
3385 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003386 if (!window)
3387 return NULL;
3388
3389 window->x = x;
3390 window->y = y;
3391
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003392 if (display->shell)
3393 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003394 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003395 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003396
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003397 return window;
3398}
3399
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003400static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003401menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003402{
3403 int next;
3404
3405 next = (sy - 8) / 20;
3406 if (menu->current != next) {
3407 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003408 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003409 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003410}
3411
3412static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003413menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003414 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003415 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003416{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003417 struct menu *menu = data;
3418
3419 if (widget == menu->widget)
3420 menu_set_item(data, y);
3421
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003422 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003423}
3424
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003425static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003426menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003427 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003428{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003429 struct menu *menu = data;
3430
3431 if (widget == menu->widget)
3432 menu_set_item(data, y);
3433
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003434 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003435}
3436
3437static void
3438menu_leave_handler(struct widget *widget, struct input *input, void *data)
3439{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003440 struct menu *menu = data;
3441
3442 if (widget == menu->widget)
3443 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003444}
3445
3446static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003447menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003448 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003449 uint32_t button, enum wl_pointer_button_state state,
3450 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003451
3452{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003453 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003454
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003455 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3456 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003457 /* Either relase after press-drag-release or
3458 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003459 menu->func(menu->window->parent,
3460 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003461 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003462 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003463 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003464 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003465 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003466}
3467
3468static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003469menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003470{
3471 cairo_t *cr;
3472 const int32_t r = 3, margin = 3;
3473 struct menu *menu = data;
3474 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003475 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003476
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003477 cr = cairo_create(window->cairo_surface);
3478 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3479 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3480 cairo_paint(cr);
3481
3482 width = window->allocation.width;
3483 height = window->allocation.height;
3484 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003485
3486 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003487 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3488 cairo_fill(cr);
3489
3490 for (i = 0; i < menu->count; i++) {
3491 if (i == menu->current) {
3492 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3493 cairo_rectangle(cr, margin, i * 20 + margin,
3494 width - 2 * margin, 20);
3495 cairo_fill(cr);
3496 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3497 cairo_move_to(cr, 10, i * 20 + 16);
3498 cairo_show_text(cr, menu->entries[i]);
3499 } else {
3500 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3501 cairo_move_to(cr, 10, i * 20 + 16);
3502 cairo_show_text(cr, menu->entries[i]);
3503 }
3504 }
3505
3506 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003507}
3508
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003509void
3510window_show_menu(struct display *display,
3511 struct input *input, uint32_t time, struct window *parent,
3512 int32_t x, int32_t y,
3513 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003514{
3515 struct window *window;
3516 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003517 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003518
3519 menu = malloc(sizeof *menu);
3520 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003521 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003522
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003523 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003524 if (!window) {
3525 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003526 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003527 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003528
3529 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003530 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003531 menu->entries = entries;
3532 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003533 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003534 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003535 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003536 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003537 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003538 window->type = TYPE_MENU;
3539 window->x = x;
3540 window->y = y;
3541
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003542 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003543 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003544 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003545 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003546 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003547
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003548 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003549 widget_set_enter_handler(menu->widget, menu_enter_handler);
3550 widget_set_leave_handler(menu->widget, menu_leave_handler);
3551 widget_set_motion_handler(menu->widget, menu_motion_handler);
3552 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003553
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003554 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003555 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003556}
3557
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003558void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003559window_set_buffer_type(struct window *window, enum window_buffer_type type)
3560{
3561 window->buffer_type = type;
3562}
3563
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003564
3565static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003566display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003567 struct wl_output *wl_output,
3568 int x, int y,
3569 int physical_width,
3570 int physical_height,
3571 int subpixel,
3572 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003573 const char *model,
3574 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003575{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003576 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003577
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003578 output->allocation.x = x;
3579 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003580 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003581}
3582
3583static void
3584display_handle_mode(void *data,
3585 struct wl_output *wl_output,
3586 uint32_t flags,
3587 int width,
3588 int height,
3589 int refresh)
3590{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003591 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003592 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003593
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003594 if (flags & WL_OUTPUT_MODE_CURRENT) {
3595 output->allocation.width = width;
3596 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003597 if (display->output_configure_handler)
3598 (*display->output_configure_handler)(
3599 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003600 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003601}
3602
3603static const struct wl_output_listener output_listener = {
3604 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003605 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003606};
3607
3608static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003609display_add_output(struct display *d, uint32_t id)
3610{
3611 struct output *output;
3612
3613 output = malloc(sizeof *output);
3614 if (output == NULL)
3615 return;
3616
3617 memset(output, 0, sizeof *output);
3618 output->display = d;
3619 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003620 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003621 wl_list_insert(d->output_list.prev, &output->link);
3622
3623 wl_output_add_listener(output->output, &output_listener, output);
3624}
3625
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003626static void
3627output_destroy(struct output *output)
3628{
3629 if (output->destroy_handler)
3630 (*output->destroy_handler)(output, output->user_data);
3631
3632 wl_output_destroy(output->output);
3633 wl_list_remove(&output->link);
3634 free(output);
3635}
3636
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003637void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003638display_set_global_handler(struct display *display,
3639 display_global_handler_t handler)
3640{
3641 struct global *global;
3642
3643 display->global_handler = handler;
3644 if (!handler)
3645 return;
3646
3647 wl_list_for_each(global, &display->global_list, link)
3648 display->global_handler(display,
3649 global->name, global->interface,
3650 global->version, display->user_data);
3651}
3652
3653void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003654display_set_output_configure_handler(struct display *display,
3655 display_output_handler_t handler)
3656{
3657 struct output *output;
3658
3659 display->output_configure_handler = handler;
3660 if (!handler)
3661 return;
3662
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003663 wl_list_for_each(output, &display->output_list, link) {
3664 if (output->allocation.width == 0 &&
3665 output->allocation.height == 0)
3666 continue;
3667
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003668 (*display->output_configure_handler)(output,
3669 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003670 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003671}
3672
3673void
3674output_set_user_data(struct output *output, void *data)
3675{
3676 output->user_data = data;
3677}
3678
3679void *
3680output_get_user_data(struct output *output)
3681{
3682 return output->user_data;
3683}
3684
3685void
3686output_set_destroy_handler(struct output *output,
3687 display_output_handler_t handler)
3688{
3689 output->destroy_handler = handler;
3690 /* FIXME: implement this, once we have way to remove outputs */
3691}
3692
3693void
Scott Moreau4e072362012-09-29 02:03:11 -06003694output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003695{
Scott Moreau4e072362012-09-29 02:03:11 -06003696 struct rectangle allocation = output->allocation;
3697
3698 switch (output->transform) {
3699 case WL_OUTPUT_TRANSFORM_90:
3700 case WL_OUTPUT_TRANSFORM_270:
3701 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3702 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3703 /* Swap width and height */
3704 allocation.width = output->allocation.height;
3705 allocation.height = output->allocation.width;
3706 break;
3707 }
3708
3709 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003710}
3711
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003712struct wl_output *
3713output_get_wl_output(struct output *output)
3714{
3715 return output->output;
3716}
3717
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003718static void
Daniel Stone97f68542012-05-30 16:32:01 +01003719fini_xkb(struct input *input)
3720{
3721 xkb_state_unref(input->xkb.state);
3722 xkb_map_unref(input->xkb.keymap);
3723}
3724
3725static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003726display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003727{
3728 struct input *input;
3729
3730 input = malloc(sizeof *input);
3731 if (input == NULL)
3732 return;
3733
3734 memset(input, 0, sizeof *input);
3735 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003736 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003737 input->pointer_focus = NULL;
3738 input->keyboard_focus = NULL;
3739 wl_list_insert(d->input_list.prev, &input->link);
3740
Daniel Stone37816df2012-05-16 18:45:18 +01003741 wl_seat_add_listener(input->seat, &seat_listener, input);
3742 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003743
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003744 input->data_device =
3745 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003746 input->seat);
3747 wl_data_device_add_listener(input->data_device, &data_device_listener,
3748 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003749
3750 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003751
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003752 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3753 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003754 input->repeat_task.run = keyboard_repeat_func;
3755 display_watch_fd(d, input->repeat_timer_fd,
3756 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003757}
3758
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003759static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003760input_destroy(struct input *input)
3761{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003762 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003763 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003764
3765 if (input->drag_offer)
3766 data_offer_destroy(input->drag_offer);
3767
3768 if (input->selection_offer)
3769 data_offer_destroy(input->selection_offer);
3770
3771 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003772 fini_xkb(input);
3773
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003774 wl_surface_destroy(input->pointer_surface);
3775
Pekka Paalanene1207c72011-12-16 12:02:09 +02003776 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003777 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003778 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003779 free(input);
3780}
3781
3782static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003783init_workspace_manager(struct display *d, uint32_t id)
3784{
3785 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003786 wl_registry_bind(d->registry, id,
3787 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003788 if (d->workspace_manager != NULL)
3789 workspace_manager_add_listener(d->workspace_manager,
3790 &workspace_manager_listener,
3791 d);
3792}
3793
3794static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003795registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3796 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003797{
3798 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003799 struct global *global;
3800
3801 global = malloc(sizeof *global);
3802 global->name = id;
3803 global->interface = strdup(interface);
3804 global->version = version;
3805 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003806
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003807 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003808 d->compositor = wl_registry_bind(registry, id,
3809 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003810 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003811 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003812 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003813 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003814 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003815 d->shell = wl_registry_bind(registry,
3816 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003817 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003818 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003819 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3820 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003821 wl_registry_bind(registry, id,
3822 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003823 } else if (strcmp(interface, "text_cursor_position") == 0) {
3824 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003825 wl_registry_bind(registry, id,
3826 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003827 } else if (strcmp(interface, "workspace_manager") == 0) {
3828 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003829 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003830
3831 if (d->global_handler)
3832 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003833}
3834
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003835void *
3836display_bind(struct display *display, uint32_t name,
3837 const struct wl_interface *interface, uint32_t version)
3838{
3839 return wl_registry_bind(display->registry, name, interface, version);
3840}
3841
3842static const struct wl_registry_listener registry_listener = {
3843 registry_handle_global
3844};
3845
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003846#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003847static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003848init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003849{
3850 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003851 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003852
Rob Clark6396ed32012-03-11 19:48:41 -05003853#ifdef USE_CAIRO_GLESV2
3854# define GL_BIT EGL_OPENGL_ES2_BIT
3855#else
3856# define GL_BIT EGL_OPENGL_BIT
3857#endif
3858
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003859 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04003860 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003861 EGL_RED_SIZE, 1,
3862 EGL_GREEN_SIZE, 1,
3863 EGL_BLUE_SIZE, 1,
3864 EGL_ALPHA_SIZE, 1,
3865 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003866 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003867 EGL_NONE
3868 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003869
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003870#ifdef USE_CAIRO_GLESV2
3871 static const EGLint context_attribs[] = {
3872 EGL_CONTEXT_CLIENT_VERSION, 2,
3873 EGL_NONE
3874 };
3875 EGLint api = EGL_OPENGL_ES_API;
3876#else
3877 EGLint *context_attribs = NULL;
3878 EGLint api = EGL_OPENGL_API;
3879#endif
3880
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003881 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003882 if (!eglInitialize(d->dpy, &major, &minor)) {
3883 fprintf(stderr, "failed to initialize display\n");
3884 return -1;
3885 }
3886
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003887 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003888 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3889 return -1;
3890 }
3891
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003892 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3893 &d->argb_config, 1, &n) || n != 1) {
3894 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003895 return -1;
3896 }
3897
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003898 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003899 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003900 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003901 fprintf(stderr, "failed to create context\n");
3902 return -1;
3903 }
3904
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003905 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003906 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003907 return -1;
3908 }
3909
Benjamin Franzke0c991632011-09-27 21:57:31 +02003910 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3911 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3912 fprintf(stderr, "failed to get cairo egl argb device\n");
3913 return -1;
3914 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003915
3916 return 0;
3917}
3918
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003919static void
3920fini_egl(struct display *display)
3921{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003922 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003923
3924 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3925 EGL_NO_CONTEXT);
3926
3927 eglTerminate(display->dpy);
3928 eglReleaseThread();
3929}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003930#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003931
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003932static void
3933handle_display_data(struct task *task, uint32_t events)
3934{
3935 struct display *display =
3936 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003937 struct epoll_event ep;
3938 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003939
3940 display->display_fd_events = events;
3941
3942 if (events & EPOLLERR || events & EPOLLHUP) {
3943 display_exit(display);
3944 return;
3945 }
3946
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04003947 if (events & EPOLLIN) {
3948 ret = wl_display_dispatch(display->display);
3949 if (ret == -1) {
3950 display_exit(display);
3951 return;
3952 }
3953 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003954
3955 if (events & EPOLLOUT) {
3956 ret = wl_display_flush(display->display);
3957 if (ret == 0) {
3958 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3959 ep.data.ptr = &display->display_task;
3960 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3961 display->display_fd, &ep);
3962 } else if (ret == -1 && errno != EAGAIN) {
3963 display_exit(display);
3964 return;
3965 }
3966 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003967}
3968
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003969struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003970display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003971{
3972 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003973
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003974 d = malloc(sizeof *d);
3975 if (d == NULL)
3976 return NULL;
3977
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003978 memset(d, 0, sizeof *d);
3979
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003980 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003981 if (d->display == NULL) {
3982 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003983 return NULL;
3984 }
3985
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003986 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003987 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003988 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003989 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3990 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003991
3992 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003993 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003994 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003995 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003996
Daniel Stone97f68542012-05-30 16:32:01 +01003997 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003998 if (d->xkb_context == NULL) {
3999 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004000 return NULL;
4001 }
4002
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004003 d->workspace = 0;
4004 d->workspace_count = 1;
4005
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004006 d->registry = wl_display_get_registry(d->display);
4007 wl_registry_add_listener(d->registry, &registry_listener, d);
4008 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004009#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004010 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004011 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004012#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004013
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004014 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004015
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004016 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004017
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004018 wl_list_init(&d->window_list);
4019
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004020 return d;
4021}
4022
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004023static void
4024display_destroy_outputs(struct display *display)
4025{
4026 struct output *tmp;
4027 struct output *output;
4028
4029 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4030 output_destroy(output);
4031}
4032
Pekka Paalanene1207c72011-12-16 12:02:09 +02004033static void
4034display_destroy_inputs(struct display *display)
4035{
4036 struct input *tmp;
4037 struct input *input;
4038
4039 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4040 input_destroy(input);
4041}
4042
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004043void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004044display_destroy(struct display *display)
4045{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004046 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004047 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4048 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004049
4050 if (!wl_list_empty(&display->deferred_list))
4051 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4052
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004053 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004054 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004055
Daniel Stone97f68542012-05-30 16:32:01 +01004056 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004057
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004058 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004059 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004060
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004061#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004062 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004063#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004064
Pekka Paalanenc2052982011-12-16 11:41:32 +02004065 if (display->shell)
4066 wl_shell_destroy(display->shell);
4067
4068 if (display->shm)
4069 wl_shm_destroy(display->shm);
4070
4071 if (display->data_device_manager)
4072 wl_data_device_manager_destroy(display->data_device_manager);
4073
4074 wl_compositor_destroy(display->compositor);
4075
4076 close(display->epoll_fd);
4077
U. Artie Eoff44874d92012-10-02 21:12:35 -07004078 if (!(display->display_fd_events & EPOLLERR) &&
4079 !(display->display_fd_events & EPOLLHUP))
4080 wl_display_flush(display->display);
4081
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004082 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004083 free(display);
4084}
4085
4086void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004087display_set_user_data(struct display *display, void *data)
4088{
4089 display->user_data = data;
4090}
4091
4092void *
4093display_get_user_data(struct display *display)
4094{
4095 return display->user_data;
4096}
4097
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004098struct wl_display *
4099display_get_display(struct display *display)
4100{
4101 return display->display;
4102}
4103
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004104struct output *
4105display_get_output(struct display *display)
4106{
4107 return container_of(display->output_list.next, struct output, link);
4108}
4109
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004110struct wl_compositor *
4111display_get_compositor(struct display *display)
4112{
4113 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004114}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004115
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004116uint32_t
4117display_get_serial(struct display *display)
4118{
4119 return display->serial;
4120}
4121
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004122EGLDisplay
4123display_get_egl_display(struct display *d)
4124{
4125 return d->dpy;
4126}
4127
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004128struct wl_data_source *
4129display_create_data_source(struct display *display)
4130{
4131 return wl_data_device_manager_create_data_source(display->data_device_manager);
4132}
4133
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004134EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004135display_get_argb_egl_config(struct display *d)
4136{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004137 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004138}
4139
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004140struct wl_shell *
4141display_get_shell(struct display *display)
4142{
4143 return display->shell;
4144}
4145
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004146int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004147display_acquire_window_surface(struct display *display,
4148 struct window *window,
4149 EGLContext ctx)
4150{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004151 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004152 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004153
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004154 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004155}
4156
4157void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004158display_release_window_surface(struct display *display,
4159 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004160{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004161 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004162 return;
4163
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004164 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004165}
4166
4167void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004168display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004169{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004170 wl_list_insert(&display->deferred_list, &task->link);
4171}
4172
4173void
4174display_watch_fd(struct display *display,
4175 int fd, uint32_t events, struct task *task)
4176{
4177 struct epoll_event ep;
4178
4179 ep.events = events;
4180 ep.data.ptr = task;
4181 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4182}
4183
4184void
4185display_run(struct display *display)
4186{
4187 struct task *task;
4188 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004189 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004190
Pekka Paalanen826d7952011-12-15 10:14:07 +02004191 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004192 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004193 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004194 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004195 struct task, link);
4196 wl_list_remove(&task->link);
4197 task->run(task, 0);
4198 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004199
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004200 wl_display_dispatch_pending(display->display);
4201
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004202 if (!display->running)
4203 break;
4204
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004205 ret = wl_display_flush(display->display);
4206 if (ret < 0 && errno == EAGAIN) {
4207 ep[0].events =
4208 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4209 ep[0].data.ptr = &display->display_task;
4210
4211 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4212 display->display_fd, &ep[0]);
4213 } else if (ret < 0) {
4214 break;
4215 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004216
4217 count = epoll_wait(display->epoll_fd,
4218 ep, ARRAY_LENGTH(ep), -1);
4219 for (i = 0; i < count; i++) {
4220 task = ep[i].data.ptr;
4221 task->run(task, ep[i].events);
4222 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004223 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004224}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004225
4226void
4227display_exit(struct display *display)
4228{
4229 display->running = 0;
4230}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004231
4232void
4233keysym_modifiers_add(struct wl_array *modifiers_map,
4234 const char *name)
4235{
4236 size_t len = strlen(name) + 1;
4237 char *p;
4238
4239 p = wl_array_add(modifiers_map, len);
4240
4241 if (p == NULL)
4242 return;
4243
4244 strncpy(p, name, len);
4245}
4246
4247static xkb_mod_index_t
4248keysym_modifiers_get_index(struct wl_array *modifiers_map,
4249 const char *name)
4250{
4251 xkb_mod_index_t index = 0;
4252 char *p = modifiers_map->data;
4253
4254 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4255 if (strcmp(p, name) == 0)
4256 return index;
4257
4258 index++;
4259 p += strlen(p) + 1;
4260 }
4261
4262 return XKB_MOD_INVALID;
4263}
4264
4265xkb_mod_mask_t
4266keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4267 const char *name)
4268{
4269 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4270
4271 if (index == XKB_MOD_INVALID)
4272 return XKB_MOD_INVALID;
4273
4274 return 1 << index;
4275}