blob: 436f86247f950b5786ed9d7898a6ae525c6c8968 [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>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060064#include "text-cursor-position-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030065#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070069struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030070
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050073 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040074 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040075 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040076 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060077 struct text_cursor_position *text_cursor_position;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050079 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020081 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040082 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040083
84 int display_fd;
85 uint32_t mask;
86 struct task display_task;
87
88 int epoll_fd;
89 struct wl_list deferred_list;
90
Pekka Paalanen826d7952011-12-15 10:14:07 +020091 int running;
92
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040094 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050095 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040096
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040097 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030099 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300100 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400101
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500102 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
103 PFNEGLCREATEIMAGEKHRPROC create_image;
104 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200105
106 display_output_handler_t output_configure_handler;
107
108 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100109
110 struct xkb_context *xkb_context;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500111};
112
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400114 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400116 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500117 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500119 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_CUSTOM
121};
Rob Bradford7507b572012-05-15 17:55:34 +0100122
123struct window_output {
124 struct output *output;
125 struct wl_list link;
126};
127
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128struct window {
129 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500130 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100131 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500132 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200133 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500134 struct wl_region *input_region;
135 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500136 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500138 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500139 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400140 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400141 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400142 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400143 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400144 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400145 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400146 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400147 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400148 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500149
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400150 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500151
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700152 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400153
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500154 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500155 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400156 window_data_handler_t data_handler;
157 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500158 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400159
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200160 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500161 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500162
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500163 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400164 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500165};
166
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500167struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500168 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300169 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500170 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400171 struct wl_list link;
172 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500173 widget_resize_handler_t resize_handler;
174 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500175 widget_enter_handler_t enter_handler;
176 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500177 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500178 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500180 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300181 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400182};
183
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400184struct input {
185 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100186 struct wl_seat *seat;
187 struct wl_pointer *pointer;
188 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400189 struct window *pointer_focus;
190 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300191 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300192 uint32_t cursor_anim_start;
193 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300194 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400195 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400196 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400197 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400198 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400199
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500200 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500201 struct widget *grab;
202 uint32_t grab_button;
203
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400204 struct wl_data_device *data_device;
205 struct data_offer *drag_offer;
206 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100207
208 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100209 struct xkb_keymap *keymap;
210 struct xkb_state *state;
211 xkb_mod_mask_t control_mask;
212 xkb_mod_mask_t alt_mask;
213 xkb_mod_mask_t shift_mask;
214 } xkb;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400215};
216
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500217struct output {
218 struct display *display;
219 struct wl_output *output;
220 struct rectangle allocation;
221 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200222
223 display_output_handler_t destroy_handler;
224 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500225};
226
Martin Minarik1998b152012-05-10 02:04:35 +0200227enum frame_button_action {
228 FRAME_BUTTON_NULL = 0,
229 FRAME_BUTTON_ICON = 1,
230 FRAME_BUTTON_CLOSE = 2,
231 FRAME_BUTTON_MINIMIZE = 3,
232 FRAME_BUTTON_MAXIMIZE = 4,
233};
234
235enum frame_button_pointer {
236 FRAME_BUTTON_DEFAULT = 0,
237 FRAME_BUTTON_OVER = 1,
238 FRAME_BUTTON_ACTIVE = 2,
239};
240
241enum frame_button_align {
242 FRAME_BUTTON_RIGHT = 0,
243 FRAME_BUTTON_LEFT = 1,
244};
245
246enum frame_button_decoration {
247 FRAME_BUTTON_NONE = 0,
248 FRAME_BUTTON_FANCY = 1,
249};
250
251struct frame_button {
252 struct widget *widget;
253 struct frame *frame;
254 cairo_surface_t *icon;
255 enum frame_button_action type;
256 enum frame_button_pointer state;
257 struct wl_list link; /* buttons_list */
258 enum frame_button_align align;
259 enum frame_button_decoration decoration;
260};
261
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500262struct frame {
263 struct widget *widget;
264 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200265 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500266};
267
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500268struct menu {
269 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500270 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500271 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500272 const char **entries;
273 uint32_t time;
274 int current;
275 int count;
276 menu_func_t func;
277};
278
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300279struct tooltip {
280 struct widget *parent;
281 struct window *window;
282 struct widget *widget;
283 char *entry;
284 struct task tooltip_task;
285 int tooltip_fd;
286 float x, y;
287};
288
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700289struct shm_pool {
290 struct wl_shm_pool *pool;
291 size_t size;
292 size_t used;
293 void *data;
294};
295
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400296enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300297 CURSOR_DEFAULT = 100,
298 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400299};
300
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500301enum window_location {
302 WINDOW_INTERIOR = 0,
303 WINDOW_RESIZING_TOP = 1,
304 WINDOW_RESIZING_BOTTOM = 2,
305 WINDOW_RESIZING_LEFT = 4,
306 WINDOW_RESIZING_TOP_LEFT = 5,
307 WINDOW_RESIZING_BOTTOM_LEFT = 6,
308 WINDOW_RESIZING_RIGHT = 8,
309 WINDOW_RESIZING_TOP_RIGHT = 9,
310 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
311 WINDOW_RESIZING_MASK = 15,
312 WINDOW_EXTERIOR = 16,
313 WINDOW_TITLEBAR = 17,
314 WINDOW_CLIENT_AREA = 18,
315};
316
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400317const char *option_xkb_layout = "us";
318const char *option_xkb_variant = "";
319const char *option_xkb_options = "";
320
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400321static const struct weston_option xkb_options[] = {
322 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
323 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
324 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400325};
326
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327static const cairo_user_data_key_t surface_data_key;
328struct surface_data {
329 struct wl_buffer *buffer;
330};
331
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500332#define MULT(_d,c,a,t) \
333 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
334
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500335#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100337struct egl_window_surface_data {
338 struct display *display;
339 struct wl_surface *surface;
340 struct wl_egl_window *window;
341 EGLSurface surf;
342};
343
344static void
345egl_window_surface_data_destroy(void *p)
346{
347 struct egl_window_surface_data *data = p;
348 struct display *d = data->display;
349
350 eglDestroySurface(d->dpy, data->surf);
351 wl_egl_window_destroy(data->window);
352 data->surface = NULL;
353
354 free(p);
355}
356
357static cairo_surface_t *
358display_create_egl_window_surface(struct display *display,
359 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400360 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100361 struct rectangle *rectangle)
362{
363 cairo_surface_t *cairo_surface;
364 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400365 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200366 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100367
368 data = malloc(sizeof *data);
369 if (data == NULL)
370 return NULL;
371
372 data->display = display;
373 data->surface = surface;
374
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500375 config = display->argb_config;
376 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400378 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100379 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400380 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100381
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400382 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500383 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100384
Benjamin Franzke0c991632011-09-27 21:57:31 +0200385 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100386 data->surf,
387 rectangle->width,
388 rectangle->height);
389
390 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
391 data, egl_window_surface_data_destroy);
392
393 return cairo_surface;
394}
395
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396#endif
397
398struct wl_buffer *
399display_get_buffer_for_surface(struct display *display,
400 cairo_surface_t *surface)
401{
402 struct surface_data *data;
403
404 data = cairo_surface_get_user_data (surface, &surface_data_key);
405
406 return data->buffer;
407}
408
409struct shm_surface_data {
410 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700411 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412};
413
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500414static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700415shm_pool_destroy(struct shm_pool *pool);
416
417static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418shm_surface_data_destroy(void *p)
419{
420 struct shm_surface_data *data = p;
421
422 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700423 if (data->pool)
424 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400425}
426
Kristian Høgsberg16626282012-04-03 11:21:27 -0400427static struct wl_shm_pool *
428make_shm_pool(struct display *display, int size, void **data)
429{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400430 struct wl_shm_pool *pool;
431 int fd;
432
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300433 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400434 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300435 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
436 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400437 return NULL;
438 }
439
440 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400441 if (*data == MAP_FAILED) {
442 fprintf(stderr, "mmap failed: %m\n");
443 close(fd);
444 return NULL;
445 }
446
447 pool = wl_shm_create_pool(display->shm, fd, size);
448
449 close(fd);
450
451 return pool;
452}
453
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700454static struct shm_pool *
455shm_pool_create(struct display *display, size_t size)
456{
457 struct shm_pool *pool = malloc(sizeof *pool);
458
459 if (!pool)
460 return NULL;
461
462 pool->pool = make_shm_pool(display, size, &pool->data);
463 if (!pool->pool) {
464 free(pool);
465 return NULL;
466 }
467
468 pool->size = size;
469 pool->used = 0;
470
471 return pool;
472}
473
474static void *
475shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
476{
477 if (pool->used + size > pool->size)
478 return NULL;
479
480 *offset = pool->used;
481 pool->used += size;
482
483 return (char *) pool->data + *offset;
484}
485
486/* destroy the pool. this does not unmap the memory though */
487static void
488shm_pool_destroy(struct shm_pool *pool)
489{
490 munmap(pool->data, pool->size);
491 wl_shm_pool_destroy(pool->pool);
492 free(pool);
493}
494
495/* Start allocating from the beginning of the pool again */
496static void
497shm_pool_reset(struct shm_pool *pool)
498{
499 pool->used = 0;
500}
501
502static int
503data_length_for_shm_surface(struct rectangle *rect)
504{
505 int stride;
506
507 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
508 rect->width);
509 return stride * rect->height;
510}
511
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500512static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700513display_create_shm_surface_from_pool(struct display *display,
514 struct rectangle *rectangle,
515 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516{
517 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400518 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400519 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700520 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400521 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400522
523 data = malloc(sizeof *data);
524 if (data == NULL)
525 return NULL;
526
527 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
528 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700529 length = stride * rectangle->height;
530 data->pool = NULL;
531 map = shm_pool_allocate(pool, length, &offset);
532
533 if (!map) {
534 free(data);
535 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400536 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400537
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400538 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539 CAIRO_FORMAT_ARGB32,
540 rectangle->width,
541 rectangle->height,
542 stride);
543
544 cairo_surface_set_user_data (surface, &surface_data_key,
545 data, shm_surface_data_destroy);
546
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500548 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400549 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500550 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700552 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400553 rectangle->width,
554 rectangle->height,
555 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700557 return surface;
558}
559
560static cairo_surface_t *
561display_create_shm_surface(struct display *display,
562 struct rectangle *rectangle, uint32_t flags,
563 struct window *window)
564{
565 struct shm_surface_data *data;
566 struct shm_pool *pool;
567 cairo_surface_t *surface;
568
569 if (window && window->pool) {
570 shm_pool_reset(window->pool);
571 surface = display_create_shm_surface_from_pool(display,
572 rectangle,
573 flags,
574 window->pool);
575 if (surface)
576 return surface;
577 }
578
579 pool = shm_pool_create(display,
580 data_length_for_shm_surface(rectangle));
581 if (!pool)
582 return NULL;
583
584 surface =
585 display_create_shm_surface_from_pool(display, rectangle,
586 flags, pool);
587
588 if (!surface) {
589 shm_pool_destroy(pool);
590 return NULL;
591 }
592
593 /* make sure we destroy the pool when the surface is destroyed */
594 data = cairo_surface_get_user_data(surface, &surface_data_key);
595 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400596
597 return surface;
598}
599
nobled7b87cb02011-02-01 18:51:47 +0000600static int
601check_size(struct rectangle *rect)
602{
603 if (rect->width && rect->height)
604 return 0;
605
606 fprintf(stderr, "tried to create surface of "
607 "width: %d, height: %d\n", rect->width, rect->height);
608 return -1;
609}
610
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400611cairo_surface_t *
612display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100613 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400614 struct rectangle *rectangle,
615 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400616{
nobled7b87cb02011-02-01 18:51:47 +0000617 if (check_size(rectangle) < 0)
618 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500619#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300620 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400621 return display_create_egl_window_surface(display,
622 surface,
623 flags,
624 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400625#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400626 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627}
628
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300629static const char *cursors[] = {
630 "bottom_left_corner",
631 "bottom_right_corner",
632 "bottom_side",
633 "grabbing",
634 "left_ptr",
635 "left_side",
636 "right_side",
637 "top_left_corner",
638 "top_right_corner",
639 "top_side",
640 "xterm",
641 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400642 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300643};
644
645static void
646create_cursors(struct display *display)
647{
648 unsigned int i;
649
650 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
651 display->cursors =
652 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
653
654 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
655 display->cursors[i] =
656 wl_cursor_theme_get_cursor(display->cursor_theme,
657 cursors[i]);
658}
659
660static void
661destroy_cursors(struct display *display)
662{
663 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800664 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300665}
666
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300667struct wl_cursor_image *
668display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400669{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300670 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300671 wl_cursor_theme_get_cursor(display->cursor_theme,
672 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300674 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400675}
676
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400677static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200678window_get_resize_dx_dy(struct window *window, int *x, int *y)
679{
680 if (window->resize_edges & WINDOW_RESIZING_LEFT)
681 *x = window->server_allocation.width - window->allocation.width;
682 else
683 *x = 0;
684
685 if (window->resize_edges & WINDOW_RESIZING_TOP)
686 *y = window->server_allocation.height -
687 window->allocation.height;
688 else
689 *y = 0;
690
691 window->resize_edges = 0;
692}
693
694static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500695window_attach_surface(struct window *window)
696{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400697 struct display *display = window->display;
698 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000699#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100700 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000701#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500702 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100703
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400704 if (window->type == TYPE_NONE) {
705 window->type = TYPE_TOPLEVEL;
706 if (display->shell)
707 wl_shell_surface_set_toplevel(window->shell_surface);
708 }
709
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100710 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000711#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100712 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
713 data = cairo_surface_get_user_data(window->cairo_surface,
714 &surface_data_key);
715
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100716 cairo_gl_surface_swapbuffers(window->cairo_surface);
717 wl_egl_window_get_attached_size(data->window,
718 &window->server_allocation.width,
719 &window->server_allocation.height);
720 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000721#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100723 buffer =
724 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400725 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200727 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400729 wl_surface_damage(window->surface, 0, 0,
730 window->allocation.width,
731 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400733 cairo_surface_destroy(window->cairo_surface);
734 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000736 default:
737 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100738 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500739
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500740 if (window->input_region) {
741 wl_surface_set_input_region(window->surface,
742 window->input_region);
743 wl_region_destroy(window->input_region);
744 window->input_region = NULL;
745 }
746
747 if (window->opaque_region) {
748 wl_surface_set_opaque_region(window->surface,
749 window->opaque_region);
750 wl_region_destroy(window->opaque_region);
751 window->opaque_region = NULL;
752 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500753}
754
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500755void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400756window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500757{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400758 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100759 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500760}
761
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400762void
763window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400764{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500765 cairo_surface_reference(surface);
766
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400767 if (window->cairo_surface != NULL)
768 cairo_surface_destroy(window->cairo_surface);
769
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500770 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400771}
772
Benjamin Franzke22d54812011-07-16 19:50:32 +0000773#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100774static void
775window_resize_cairo_window_surface(struct window *window)
776{
777 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200778 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100779
780 data = cairo_surface_get_user_data(window->cairo_surface,
781 &surface_data_key);
782
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200783 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784 wl_egl_window_resize(data->window,
785 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200786 window->allocation.height,
787 x,y);
788
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 cairo_gl_surface_set_size(window->cairo_surface,
790 window->allocation.width,
791 window->allocation.height);
792}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000793#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400795struct display *
796window_get_display(struct window *window)
797{
798 return window->display;
799}
800
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400801void
802window_create_surface(struct window *window)
803{
804 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400805 uint32_t flags = 0;
806
807 if (!window->transparent)
808 flags = SURFACE_OPAQUE;
809
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400810 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500811#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100812 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
813 if (window->cairo_surface) {
814 window_resize_cairo_window_surface(window);
815 return;
816 }
817 surface = display_create_surface(window->display,
818 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400819 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821#endif
822 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400823 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400824 &window->allocation,
825 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800827 default:
828 surface = NULL;
829 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400831
832 window_set_surface(window, surface);
833 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834}
835
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200836static void frame_destroy(struct frame *frame);
837
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400838void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500839window_destroy(struct window *window)
840{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200841 struct display *display = window->display;
842 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100843 struct window_output *window_output;
844 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200845
846 if (window->redraw_scheduled)
847 wl_list_remove(&window->redraw_task.link);
848
849 wl_list_for_each(input, &display->input_list, link) {
850 if (input->pointer_focus == window)
851 input->pointer_focus = NULL;
852 if (input->keyboard_focus == window)
853 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500854 if (input->focus_widget &&
855 input->focus_widget->window == window)
856 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200857 }
858
Rob Bradford7507b572012-05-15 17:55:34 +0100859 wl_list_for_each_safe(window_output, window_output_tmp,
860 &window->window_output_list, link) {
861 free (window_output);
862 }
863
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500864 if (window->input_region)
865 wl_region_destroy(window->input_region);
866 if (window->opaque_region)
867 wl_region_destroy(window->opaque_region);
868
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200869 if (window->frame)
870 frame_destroy(window->frame);
871
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200872 if (window->shell_surface)
873 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500874 wl_surface_destroy(window->surface);
875 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200876
877 if (window->cairo_surface != NULL)
878 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200879
880 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500881 free(window);
882}
883
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500884static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500885widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400886{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500887 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400888
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500889 wl_list_for_each(child, &widget->child_list, link) {
890 target = widget_find_widget(child, x, y);
891 if (target)
892 return target;
893 }
894
895 if (widget->allocation.x <= x &&
896 x < widget->allocation.x + widget->allocation.width &&
897 widget->allocation.y <= y &&
898 y < widget->allocation.y + widget->allocation.height) {
899 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400900 }
901
902 return NULL;
903}
904
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500905static struct widget *
906widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400907{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500908 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400909
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500910 widget = malloc(sizeof *widget);
911 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500912 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500913 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500914 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500915 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500916 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300917 widget->tooltip = NULL;
918 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500919
920 return widget;
921}
922
923struct widget *
924window_add_widget(struct window *window, void *data)
925{
926 window->widget = widget_create(window, data);
927 wl_list_init(&window->widget->link);
928
929 return window->widget;
930}
931
932struct widget *
933widget_add_widget(struct widget *parent, void *data)
934{
935 struct widget *widget;
936
937 widget = widget_create(parent->window, data);
938 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400939
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500940 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400941}
942
943void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500944widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400945{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200946 struct display *display = widget->window->display;
947 struct input *input;
948
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300949 if (widget->tooltip) {
950 free(widget->tooltip);
951 widget->tooltip = NULL;
952 }
953
Pekka Paalanene156fb62012-01-19 13:51:38 +0200954 wl_list_for_each(input, &display->input_list, link) {
955 if (input->focus_widget == widget)
956 input->focus_widget = NULL;
957 }
958
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500959 wl_list_remove(&widget->link);
960 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400961}
962
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400963void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500964widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400965{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500966 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400967}
968
969void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500970widget_set_size(struct widget *widget, int32_t width, int32_t height)
971{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500972 widget->allocation.width = width;
973 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500974}
975
976void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500977widget_set_allocation(struct widget *widget,
978 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400979{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500980 widget->allocation.x = x;
981 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200982 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400983}
984
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500985void
986widget_set_transparent(struct widget *widget, int transparent)
987{
988 widget->opaque = !transparent;
989}
990
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400991void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500992widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400993{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500994 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400995}
996
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500997void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500998widget_set_resize_handler(struct widget *widget,
999 widget_resize_handler_t handler)
1000{
1001 widget->resize_handler = handler;
1002}
1003
1004void
1005widget_set_redraw_handler(struct widget *widget,
1006 widget_redraw_handler_t handler)
1007{
1008 widget->redraw_handler = handler;
1009}
1010
1011void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001012widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001013{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001014 widget->enter_handler = handler;
1015}
1016
1017void
1018widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1019{
1020 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001021}
1022
1023void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001024widget_set_motion_handler(struct widget *widget,
1025 widget_motion_handler_t handler)
1026{
1027 widget->motion_handler = handler;
1028}
1029
1030void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001031widget_set_button_handler(struct widget *widget,
1032 widget_button_handler_t handler)
1033{
1034 widget->button_handler = handler;
1035}
1036
1037void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001038widget_schedule_redraw(struct widget *widget)
1039{
1040 window_schedule_redraw(widget->window);
1041}
1042
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001043cairo_surface_t *
1044window_get_surface(struct window *window)
1045{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001046 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001047}
1048
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001049struct wl_surface *
1050window_get_wl_surface(struct window *window)
1051{
1052 return window->surface;
1053}
1054
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001055struct wl_shell_surface *
1056window_get_wl_shell_surface(struct window *window)
1057{
1058 return window->shell_surface;
1059}
1060
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001061static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001062tooltip_redraw_handler(struct widget *widget, void *data)
1063{
1064 cairo_t *cr;
1065 const int32_t r = 3;
1066 struct tooltip *tooltip = data;
1067 int32_t width, height;
1068 struct window *window = widget->window;
1069
1070 cr = cairo_create(window->cairo_surface);
1071 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1072 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1073 cairo_paint(cr);
1074
1075 width = window->allocation.width;
1076 height = window->allocation.height;
1077 rounded_rect(cr, 0, 0, width, height, r);
1078
1079 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1080 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1081 cairo_fill(cr);
1082
1083 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1084 cairo_move_to(cr, 10, 16);
1085 cairo_show_text(cr, tooltip->entry);
1086 cairo_destroy(cr);
1087}
1088
1089static cairo_text_extents_t
1090get_text_extents(struct tooltip *tooltip)
1091{
1092 struct window *window;
1093 cairo_t *cr;
1094 cairo_text_extents_t extents;
1095
1096 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1097 * created yet */
1098 window = tooltip->widget->window->parent;
1099 cr = cairo_create(window->cairo_surface);
1100 cairo_text_extents(cr, tooltip->entry, &extents);
1101 cairo_destroy(cr);
1102
1103 return extents;
1104}
1105
1106static int
1107window_create_tooltip(struct tooltip *tooltip)
1108{
1109 struct widget *parent = tooltip->parent;
1110 struct display *display = parent->window->display;
1111 struct window *window;
1112 const int offset_y = 27;
1113 const int margin = 3;
1114 cairo_text_extents_t extents;
1115
1116 if (tooltip->widget)
1117 return 0;
1118
1119 window = window_create_transient(display, parent->window, tooltip->x,
1120 tooltip->y + offset_y,
1121 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1122 if (!window)
1123 return -1;
1124
1125 tooltip->window = window;
1126 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1127
1128 extents = get_text_extents(tooltip);
1129 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1130 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1131
1132 return 0;
1133}
1134
1135void
1136widget_destroy_tooltip(struct widget *parent)
1137{
1138 struct tooltip *tooltip = parent->tooltip;
1139
1140 parent->tooltip_count = 0;
1141 if (!tooltip)
1142 return;
1143
1144 if (tooltip->widget) {
1145 widget_destroy(tooltip->widget);
1146 window_destroy(tooltip->window);
1147 tooltip->widget = NULL;
1148 tooltip->window = NULL;
1149 }
1150
1151 close(tooltip->tooltip_fd);
1152 free(tooltip->entry);
1153 free(tooltip);
1154 parent->tooltip = NULL;
1155}
1156
1157static void
1158tooltip_func(struct task *task, uint32_t events)
1159{
1160 struct tooltip *tooltip =
1161 container_of(task, struct tooltip, tooltip_task);
1162 uint64_t exp;
1163
1164 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1165 window_create_tooltip(tooltip);
1166}
1167
1168#define TOOLTIP_TIMEOUT 500
1169static int
1170tooltip_timer_reset(struct tooltip *tooltip)
1171{
1172 struct itimerspec its;
1173
1174 its.it_interval.tv_sec = 0;
1175 its.it_interval.tv_nsec = 0;
1176 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1177 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1178 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1179 fprintf(stderr, "could not set timerfd\n: %m");
1180 return -1;
1181 }
1182
1183 return 0;
1184}
1185
1186int
1187widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1188{
1189 struct tooltip *tooltip = parent->tooltip;
1190
1191 parent->tooltip_count++;
1192 if (tooltip) {
1193 tooltip->x = x;
1194 tooltip->y = y;
1195 tooltip_timer_reset(tooltip);
1196 return 0;
1197 }
1198
1199 /* the handler might be triggered too fast via input device motion, so
1200 * we need this check here to make sure tooltip is fully initialized */
1201 if (parent->tooltip_count > 1)
1202 return 0;
1203
1204 tooltip = malloc(sizeof *tooltip);
1205 if (!tooltip)
1206 return -1;
1207
1208 parent->tooltip = tooltip;
1209 tooltip->parent = parent;
1210 tooltip->widget = NULL;
1211 tooltip->window = NULL;
1212 tooltip->x = x;
1213 tooltip->y = y;
1214 tooltip->entry = strdup(entry);
1215 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1216 if (tooltip->tooltip_fd < 0) {
1217 fprintf(stderr, "could not create timerfd\n: %m");
1218 return -1;
1219 }
1220
1221 tooltip->tooltip_task.run = tooltip_func;
1222 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1223 EPOLLIN, &tooltip->tooltip_task);
1224 tooltip_timer_reset(tooltip);
1225
1226 return 0;
1227}
1228
1229static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001230frame_resize_handler(struct widget *widget,
1231 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001232{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001233 struct frame *frame = data;
1234 struct widget *child = frame->child;
1235 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001236 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001237 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001238 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001239 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001240 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001241 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001243 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001244 decoration_width = (t->width + t->margin) * 2;
1245 decoration_height = t->width +
1246 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001247
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001248 allocation.x = t->width + t->margin;
1249 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001250 allocation.width = width - decoration_width;
1251 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001252
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001253 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001254
1255 wl_list_for_each(button, &frame->buttons_list, link)
1256 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001257 } else {
1258 decoration_width = 0;
1259 decoration_height = 0;
1260
1261 allocation.x = 0;
1262 allocation.y = 0;
1263 allocation.width = width;
1264 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001265 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001266
1267 wl_list_for_each(button, &frame->buttons_list, link)
1268 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001269 }
1270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001271 widget_set_allocation(child, allocation.x, allocation.y,
1272 allocation.width, allocation.height);
1273
1274 if (child->resize_handler)
1275 child->resize_handler(child,
1276 allocation.width,
1277 allocation.height,
1278 child->user_data);
1279
Scott Moreauf7e498c2012-05-14 11:39:29 -06001280 width = child->allocation.width + decoration_width;
1281 height = child->allocation.height + decoration_height;
1282
1283 widget->window->input_region =
1284 wl_compositor_create_region(display->compositor);
1285 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001286 t->margin, t->margin,
1287 width - 2 * t->margin,
1288 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001289
1290 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001291
1292 if (child->opaque) {
1293 widget->window->opaque_region =
1294 wl_compositor_create_region(display->compositor);
1295 wl_region_add(widget->window->opaque_region,
1296 opaque_margin, opaque_margin,
1297 widget->allocation.width - 2 * opaque_margin,
1298 widget->allocation.height - 2 * opaque_margin);
1299 }
Martin Minarik1998b152012-05-10 02:04:35 +02001300
1301 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001302 x_r = frame->widget->allocation.width - t->width - t->margin;
1303 x_l = t->width + t->margin;
1304 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001305 wl_list_for_each(button, &frame->buttons_list, link) {
1306 const int button_padding = 4;
1307 w = cairo_image_surface_get_width(button->icon);
1308 h = cairo_image_surface_get_height(button->icon);
1309
1310 if (button->decoration == FRAME_BUTTON_FANCY)
1311 w += 10;
1312
1313 if (button->align == FRAME_BUTTON_LEFT) {
1314 widget_set_allocation(button->widget,
1315 x_l, y , w + 1, h + 1);
1316 x_l += w;
1317 x_l += button_padding;
1318 } else {
1319 x_r -= w;
1320 widget_set_allocation(button->widget,
1321 x_r, y , w + 1, h + 1);
1322 x_r -= button_padding;
1323 }
1324 }
1325}
1326
1327static int
1328frame_button_enter_handler(struct widget *widget,
1329 struct input *input, float x, float y, void *data)
1330{
1331 struct frame_button *frame_button = data;
1332
1333 widget_schedule_redraw(frame_button->widget);
1334 frame_button->state = FRAME_BUTTON_OVER;
1335
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001336 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001337}
1338
1339static void
1340frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1341{
1342 struct frame_button *frame_button = data;
1343
1344 widget_schedule_redraw(frame_button->widget);
1345 frame_button->state = FRAME_BUTTON_DEFAULT;
1346}
1347
1348static void
1349frame_button_button_handler(struct widget *widget,
1350 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001351 uint32_t button,
1352 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001353{
1354 struct frame_button *frame_button = data;
1355 struct window *window = widget->window;
1356
1357 if (button != BTN_LEFT)
1358 return;
1359
1360 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001361 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001362 frame_button->state = FRAME_BUTTON_ACTIVE;
1363 widget_schedule_redraw(frame_button->widget);
1364
1365 if (frame_button->type == FRAME_BUTTON_ICON)
1366 window_show_frame_menu(window, input, time);
1367 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001368 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001369 frame_button->state = FRAME_BUTTON_DEFAULT;
1370 widget_schedule_redraw(frame_button->widget);
1371 break;
1372 }
1373
1374 switch (frame_button->type) {
1375 case FRAME_BUTTON_CLOSE:
1376 if (window->close_handler)
1377 window->close_handler(window->parent,
1378 window->user_data);
1379 else
1380 display_exit(window->display);
1381 break;
1382 case FRAME_BUTTON_MINIMIZE:
1383 fprintf(stderr,"Minimize stub\n");
1384 break;
1385 case FRAME_BUTTON_MAXIMIZE:
1386 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1387 break;
1388 default:
1389 /* Unknown operation */
1390 break;
1391 }
1392}
1393
1394static void
1395frame_button_redraw_handler(struct widget *widget, void *data)
1396{
1397 struct frame_button *frame_button = data;
1398 cairo_t *cr;
1399 int width, height, x, y;
1400 struct window *window = widget->window;
1401
1402 x = widget->allocation.x;
1403 y = widget->allocation.y;
1404 width = widget->allocation.width;
1405 height = widget->allocation.height;
1406
1407 if (!width)
1408 return;
1409 if (!height)
1410 return;
1411 if (widget->opaque)
1412 return;
1413
1414 cr = cairo_create(window->cairo_surface);
1415
1416 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1417 cairo_set_line_width(cr, 1);
1418
1419 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1420 cairo_rectangle (cr, x, y, 25, 16);
1421
1422 cairo_stroke_preserve(cr);
1423
1424 switch (frame_button->state) {
1425 case FRAME_BUTTON_DEFAULT:
1426 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1427 break;
1428 case FRAME_BUTTON_OVER:
1429 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1430 break;
1431 case FRAME_BUTTON_ACTIVE:
1432 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1433 break;
1434 }
1435
1436 cairo_fill (cr);
1437
1438 x += 4;
1439 }
1440
1441 cairo_set_source_surface(cr, frame_button->icon, x, y);
1442 cairo_paint(cr);
1443
1444 cairo_destroy(cr);
1445}
1446
1447static struct widget *
1448frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1449 enum frame_button_align align, enum frame_button_decoration style)
1450{
1451 struct frame_button *frame_button;
1452 const char *icon = data;
1453
1454 frame_button = malloc (sizeof *frame_button);
1455 memset(frame_button, 0, sizeof *frame_button);
1456
1457 frame_button->icon = cairo_image_surface_create_from_png(icon);
1458 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1459 frame_button->frame = frame;
1460 frame_button->type = type;
1461 frame_button->align = align;
1462 frame_button->decoration = style;
1463
1464 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1465
1466 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1467 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1468 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1469 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1470 return frame_button->widget;
1471}
1472
1473static void
1474frame_button_destroy(struct frame_button *frame_button)
1475{
1476 widget_destroy(frame_button->widget);
1477 wl_list_remove(&frame_button->link);
1478 cairo_surface_destroy(frame_button->icon);
1479 free(frame_button);
1480
1481 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001482}
1483
1484static void
1485frame_redraw_handler(struct widget *widget, void *data)
1486{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001487 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001488 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001489 struct theme *t = window->display->theme;
1490 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001491
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001492 if (window->type == TYPE_FULLSCREEN)
1493 return;
1494
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495 cr = cairo_create(window->cairo_surface);
1496
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001497 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001498 flags |= THEME_FRAME_ACTIVE;
1499 theme_render_frame(t, cr, widget->allocation.width,
1500 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001501
1502 cairo_destroy(cr);
1503}
1504
1505static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001506frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001507{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001508 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001509 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001510
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001511 location = theme_get_location(t, input->sx, input->sy,
1512 frame->widget->allocation.width,
1513 frame->widget->allocation.height);
1514
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001515 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001516 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001517 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001518 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001519 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001520 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001521 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001522 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001523 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001525 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001527 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001531 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001532 case THEME_LOCATION_EXTERIOR:
1533 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001534 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001535 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001536 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001537}
1538
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001539static void
1540frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001541{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001542 switch (index) {
1543 case 0: /* close */
1544 if (window->close_handler)
1545 window->close_handler(window->parent,
1546 window->user_data);
1547 else
1548 display_exit(window->display);
1549 break;
1550 case 1: /* fullscreen */
1551 /* we don't have a way to get out of fullscreen for now */
1552 window_set_fullscreen(window, 1);
1553 break;
1554 case 2: /* rotate */
1555 case 3: /* scale */
1556 break;
1557 }
1558}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001559
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001560void
1561window_show_frame_menu(struct window *window,
1562 struct input *input, uint32_t time)
1563{
1564 int32_t x, y;
1565
1566 static const char *entries[] = {
1567 "Close", "Fullscreen", "Rotate", "Scale"
1568 };
1569
1570 input_get_position(input, &x, &y);
1571 window_show_menu(window->display, input, time, window,
1572 x - 10, y - 10, frame_menu_func, entries, 4);
1573}
1574
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001575static int
1576frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001577 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001578{
1579 return frame_get_pointer_image_for_location(data, input);
1580}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001581
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001582static int
1583frame_motion_handler(struct widget *widget,
1584 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001585 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001586{
1587 return frame_get_pointer_image_for_location(data, input);
1588}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001589
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001590static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001591frame_button_handler(struct widget *widget,
1592 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001593 uint32_t button, enum wl_pointer_button_state state,
1594 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001595
1596{
1597 struct frame *frame = data;
1598 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001599 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001600 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001601
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001602 location = theme_get_location(display->theme, input->sx, input->sy,
1603 frame->widget->allocation.width,
1604 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001605
Daniel Stone4dbadb12012-05-30 16:31:51 +01001606 if (window->display->shell && button == BTN_LEFT &&
1607 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001608 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001609 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610 if (!window->shell_surface)
1611 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001612 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001613 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001615 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001616 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001617 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001618 case THEME_LOCATION_RESIZING_TOP:
1619 case THEME_LOCATION_RESIZING_BOTTOM:
1620 case THEME_LOCATION_RESIZING_LEFT:
1621 case THEME_LOCATION_RESIZING_RIGHT:
1622 case THEME_LOCATION_RESIZING_TOP_LEFT:
1623 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1624 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1625 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001626 if (!window->shell_surface)
1627 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001628 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001629
1630 if (!display->dpy) {
1631 /* If we're using shm, allocate a big
1632 pool to create buffers out of while
1633 we resize. We should probably base
1634 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001635 window->pool =
1636 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001637 }
1638
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001639 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001640 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001641 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 break;
1643 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001644 } else if (button == BTN_RIGHT &&
1645 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001646 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001647 }
1648}
1649
1650struct widget *
1651frame_create(struct window *window, void *data)
1652{
1653 struct frame *frame;
1654
1655 frame = malloc(sizeof *frame);
1656 memset(frame, 0, sizeof *frame);
1657
1658 frame->widget = window_add_widget(window, frame);
1659 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001660
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001661 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1662 widget_set_resize_handler(frame->widget, frame_resize_handler);
1663 widget_set_enter_handler(frame->widget, frame_enter_handler);
1664 widget_set_motion_handler(frame->widget, frame_motion_handler);
1665 widget_set_button_handler(frame->widget, frame_button_handler);
1666
Martin Minarik1998b152012-05-10 02:04:35 +02001667 /* Create empty list for frame buttons */
1668 wl_list_init(&frame->buttons_list);
1669
1670 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1671 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1672
1673 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1674 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1675
1676 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1677 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1678
1679 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1680 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1681
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001682 window->frame = frame;
1683
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001684 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001685}
1686
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001687static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001688frame_destroy(struct frame *frame)
1689{
Martin Minarik1998b152012-05-10 02:04:35 +02001690 struct frame_button *button, *tmp;
1691
1692 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1693 frame_button_destroy(button);
1694
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001695 /* frame->child must be destroyed by the application */
1696 widget_destroy(frame->widget);
1697 free(frame);
1698}
1699
1700static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001701input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001702 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001704 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001705 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001707 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708 return;
1709
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001710 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001711 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001712 widget = old;
1713 if (input->grab)
1714 widget = input->grab;
1715 if (widget->leave_handler)
1716 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001717 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001718 }
1719
1720 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001721 widget = focus;
1722 if (input->grab)
1723 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001724 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001725 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001726 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001727 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001728
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001729 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001730 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001731}
1732
1733static void
Daniel Stone37816df2012-05-16 18:45:18 +01001734pointer_handle_motion(void *data, struct wl_pointer *pointer,
1735 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001736{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001737 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001738 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001739 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001740 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001741 float sx = wl_fixed_to_double(sx_w);
1742 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001743
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001744 input->sx = sx;
1745 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001746
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001747 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001748 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001749 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001750 }
1751
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001752 if (input->grab)
1753 widget = input->grab;
1754 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001755 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001756 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001757 cursor = widget->motion_handler(input->focus_widget,
1758 input, time, sx, sy,
1759 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001760
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001761 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001762}
1763
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001764void
1765input_grab(struct input *input, struct widget *widget, uint32_t button)
1766{
1767 input->grab = widget;
1768 input->grab_button = button;
1769}
1770
1771void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001772input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001773{
1774 struct widget *widget;
1775
1776 input->grab = NULL;
1777 if (input->pointer_focus) {
1778 widget = widget_find_widget(input->pointer_focus->widget,
1779 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001780 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001781 }
1782}
1783
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001784static void
Daniel Stone37816df2012-05-16 18:45:18 +01001785pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001786 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001787{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001788 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001789 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001790 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001791
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001792 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001793 if (input->focus_widget && input->grab == NULL &&
1794 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001795 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001796
Neil Roberts6b28aad2012-01-23 19:11:18 +00001797 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001798 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001799 (*widget->button_handler)(widget,
1800 input, time,
1801 button, state,
1802 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001803
Daniel Stone4dbadb12012-05-30 16:31:51 +01001804 if (input->grab && input->grab_button == button &&
1805 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001806 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001807}
1808
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001809static void
Daniel Stone37816df2012-05-16 18:45:18 +01001810pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001811 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001812{
1813}
1814
1815static void
Daniel Stone37816df2012-05-16 18:45:18 +01001816keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001817 uint32_t serial, uint32_t time, uint32_t key,
1818 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001819{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001820 struct input *input = data;
1821 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001822 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001823 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001824 const xkb_keysym_t *syms;
1825 xkb_keysym_t sym;
1826 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001827
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001828 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001829 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001830 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001831 return;
1832
Daniel Stone97f68542012-05-30 16:32:01 +01001833 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001834
Daniel Stone97f68542012-05-30 16:32:01 +01001835 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001836 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001837 XKB_STATE_LATCHED);
1838 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001839 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001840 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001841 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001842 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001843 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001844 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001845
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001846 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001847 input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001848 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001849 window_set_maximized(window,
1850 window->type != TYPE_MAXIMIZED);
1851 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001852 if (num_syms == 1)
1853 sym = syms[0];
1854 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001855 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001856
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001857 (*window->key_handler)(window, input, time, key,
1858 sym, state, window->user_data);
1859 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001860}
1861
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001862static void
Daniel Stone351eb612012-05-31 15:27:47 -04001863keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1864 uint32_t serial, uint32_t mods_depressed,
1865 uint32_t mods_latched, uint32_t mods_locked,
1866 uint32_t group)
1867{
1868 struct input *input = data;
1869
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001870 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1871 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001872}
1873
1874static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001875input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001876{
1877 struct window *window = input->pointer_focus;
1878
1879 if (!window)
1880 return;
1881
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001882 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001883
Pekka Paalanene1207c72011-12-16 12:02:09 +02001884 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001885 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001886}
1887
1888static void
Daniel Stone37816df2012-05-16 18:45:18 +01001889pointer_handle_enter(void *data, struct wl_pointer *pointer,
1890 uint32_t serial, struct wl_surface *surface,
1891 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001892{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001893 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001894 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001895 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001896 float sx = wl_fixed_to_double(sx_w);
1897 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001898
Martin Minarik1998b152012-05-10 02:04:35 +02001899 if (!surface) {
1900 /* enter event for a window we've just destroyed */
1901 return;
1902 }
1903
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001904 input->display->serial = serial;
1905 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001906 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001907 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001908
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001909 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001910 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001911 window->pool = NULL;
1912 /* Schedule a redraw to free the pool */
1913 window_schedule_redraw(window);
1914 }
1915
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001916 input->sx = sx;
1917 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001918
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001919 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001920 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001921}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001922
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001923static void
Daniel Stone37816df2012-05-16 18:45:18 +01001924pointer_handle_leave(void *data, struct wl_pointer *pointer,
1925 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001926{
1927 struct input *input = data;
1928
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001929 input->display->serial = serial;
1930 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001931}
1932
1933static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001934input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001935{
1936 struct window *window = input->keyboard_focus;
1937
1938 if (!window)
1939 return;
1940
1941 window->keyboard_device = NULL;
1942 if (window->keyboard_focus_handler)
1943 (*window->keyboard_focus_handler)(window, NULL,
1944 window->user_data);
1945
1946 input->keyboard_focus = NULL;
1947}
1948
1949static void
Daniel Stone37816df2012-05-16 18:45:18 +01001950keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1951 uint32_t serial, struct wl_surface *surface,
1952 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001953{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001954 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001955 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001956
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001957 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001958 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001959
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001960 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001961 window->keyboard_device = input;
1962 if (window->keyboard_focus_handler)
1963 (*window->keyboard_focus_handler)(window,
1964 window->keyboard_device,
1965 window->user_data);
1966}
1967
1968static void
Daniel Stone37816df2012-05-16 18:45:18 +01001969keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1970 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001971{
1972 struct input *input = data;
1973
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001974 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001975 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001976}
1977
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001978static void
1979keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1980 uint32_t format, int fd, uint32_t size)
1981{
1982 struct input *input = data;
1983 char *map_str;
1984
1985 if (!data) {
1986 close(fd);
1987 return;
1988 }
1989
1990 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1991 close(fd);
1992 return;
1993 }
1994
1995 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1996 if (map_str == MAP_FAILED) {
1997 close(fd);
1998 return;
1999 }
2000
2001 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2002 map_str,
2003 XKB_KEYMAP_FORMAT_TEXT_V1,
2004 0);
2005 munmap(map_str, size);
2006 close(fd);
2007
2008 if (!input->xkb.keymap) {
2009 fprintf(stderr, "failed to compile keymap\n");
2010 return;
2011 }
2012
2013 input->xkb.state = xkb_state_new(input->xkb.keymap);
2014 if (!input->xkb.state) {
2015 fprintf(stderr, "failed to create XKB state\n");
2016 xkb_map_unref(input->xkb.keymap);
2017 input->xkb.keymap = NULL;
2018 return;
2019 }
2020
2021 input->xkb.control_mask =
2022 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2023 input->xkb.alt_mask =
2024 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2025 input->xkb.shift_mask =
2026 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2027}
2028
Daniel Stone37816df2012-05-16 18:45:18 +01002029static const struct wl_pointer_listener pointer_listener = {
2030 pointer_handle_enter,
2031 pointer_handle_leave,
2032 pointer_handle_motion,
2033 pointer_handle_button,
2034 pointer_handle_axis,
2035};
2036
2037static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002038 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002039 keyboard_handle_enter,
2040 keyboard_handle_leave,
2041 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002042 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002043};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002044
2045static void
Daniel Stone37816df2012-05-16 18:45:18 +01002046seat_handle_capabilities(void *data, struct wl_seat *seat,
2047 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002048{
Daniel Stone37816df2012-05-16 18:45:18 +01002049 struct input *input = data;
2050
2051 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2052 input->pointer = wl_seat_get_pointer(seat);
2053 wl_pointer_set_user_data(input->pointer, input);
2054 wl_pointer_add_listener(input->pointer, &pointer_listener,
2055 input);
2056 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2057 wl_pointer_destroy(input->pointer);
2058 input->pointer = NULL;
2059 }
2060
2061 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2062 input->keyboard = wl_seat_get_keyboard(seat);
2063 wl_keyboard_set_user_data(input->keyboard, input);
2064 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2065 input);
2066 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2067 wl_keyboard_destroy(input->keyboard);
2068 input->keyboard = NULL;
2069 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002070}
2071
Daniel Stone37816df2012-05-16 18:45:18 +01002072static const struct wl_seat_listener seat_listener = {
2073 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002074};
2075
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002076void
2077input_get_position(struct input *input, int32_t *x, int32_t *y)
2078{
2079 *x = input->sx;
2080 *y = input->sy;
2081}
2082
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002083struct display *
2084input_get_display(struct input *input)
2085{
2086 return input->display;
2087}
2088
Daniel Stone37816df2012-05-16 18:45:18 +01002089struct wl_seat *
2090input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002091{
Daniel Stone37816df2012-05-16 18:45:18 +01002092 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002093}
2094
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002095uint32_t
2096input_get_modifiers(struct input *input)
2097{
2098 return input->modifiers;
2099}
2100
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002101struct widget *
2102input_get_focus_widget(struct input *input)
2103{
2104 return input->focus_widget;
2105}
2106
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002107struct data_offer {
2108 struct wl_data_offer *offer;
2109 struct input *input;
2110 struct wl_array types;
2111 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002112
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002113 struct task io_task;
2114 int fd;
2115 data_func_t func;
2116 int32_t x, y;
2117 void *user_data;
2118};
2119
2120static void
2121data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2122{
2123 struct data_offer *offer = data;
2124 char **p;
2125
2126 p = wl_array_add(&offer->types, sizeof *p);
2127 *p = strdup(type);
2128}
2129
2130static const struct wl_data_offer_listener data_offer_listener = {
2131 data_offer_offer,
2132};
2133
2134static void
2135data_offer_destroy(struct data_offer *offer)
2136{
2137 char **p;
2138
2139 offer->refcount--;
2140 if (offer->refcount == 0) {
2141 wl_data_offer_destroy(offer->offer);
2142 for (p = offer->types.data; *p; p++)
2143 free(*p);
2144 wl_array_release(&offer->types);
2145 free(offer);
2146 }
2147}
2148
2149static void
2150data_device_data_offer(void *data,
2151 struct wl_data_device *data_device, uint32_t id)
2152{
2153 struct data_offer *offer;
2154
2155 offer = malloc(sizeof *offer);
2156
2157 wl_array_init(&offer->types);
2158 offer->refcount = 1;
2159 offer->input = data;
2160
2161 /* FIXME: Generate typesafe wrappers for this */
2162 offer->offer = (struct wl_data_offer *)
2163 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2164 id, &wl_data_offer_interface);
2165
2166 wl_data_offer_add_listener(offer->offer,
2167 &data_offer_listener, offer);
2168}
2169
2170static void
2171data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002172 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002173 wl_fixed_t x_w, wl_fixed_t y_w,
2174 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002175{
2176 struct input *input = data;
2177 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002178 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002179 float x = wl_fixed_to_double(x_w);
2180 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002181 char **p;
2182
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002183 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002184 window = wl_surface_get_user_data(surface);
2185 input->pointer_focus = window;
2186
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002187 if (offer) {
2188 input->drag_offer = wl_data_offer_get_user_data(offer);
2189
2190 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2191 *p = NULL;
2192
2193 types_data = input->drag_offer->types.data;
2194 } else {
2195 input->drag_offer = NULL;
2196 types_data = NULL;
2197 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002198
2199 window = input->pointer_focus;
2200 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002201 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002202 window->user_data);
2203}
2204
2205static void
2206data_device_leave(void *data, struct wl_data_device *data_device)
2207{
2208 struct input *input = data;
2209
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002210 if (input->drag_offer) {
2211 data_offer_destroy(input->drag_offer);
2212 input->drag_offer = NULL;
2213 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002214}
2215
2216static void
2217data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002218 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002219{
2220 struct input *input = data;
2221 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002222 float x = wl_fixed_to_double(x_w);
2223 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002224 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002225
2226 input->sx = x;
2227 input->sy = y;
2228
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002229 if (input->drag_offer)
2230 types_data = input->drag_offer->types.data;
2231 else
2232 types_data = NULL;
2233
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002234 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002235 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002236 window->user_data);
2237}
2238
2239static void
2240data_device_drop(void *data, struct wl_data_device *data_device)
2241{
2242 struct input *input = data;
2243 struct window *window = input->pointer_focus;
2244
2245 if (window->drop_handler)
2246 window->drop_handler(window, input,
2247 input->sx, input->sy, window->user_data);
2248}
2249
2250static void
2251data_device_selection(void *data,
2252 struct wl_data_device *wl_data_device,
2253 struct wl_data_offer *offer)
2254{
2255 struct input *input = data;
2256 char **p;
2257
2258 if (input->selection_offer)
2259 data_offer_destroy(input->selection_offer);
2260
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002261 if (offer) {
2262 input->selection_offer = wl_data_offer_get_user_data(offer);
2263 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2264 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002265 } else {
2266 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002267 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002268}
2269
2270static const struct wl_data_device_listener data_device_listener = {
2271 data_device_data_offer,
2272 data_device_enter,
2273 data_device_leave,
2274 data_device_motion,
2275 data_device_drop,
2276 data_device_selection
2277};
2278
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002279static void
2280input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002281{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002282 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002283 struct wl_cursor *cursor;
2284 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002285
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002286 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002287 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002288 return;
2289
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002290 if (index >= (int) cursor->image_count) {
2291 fprintf(stderr, "cursor index out of range\n");
2292 return;
2293 }
2294
2295 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002296 buffer = wl_cursor_image_get_buffer(image);
2297 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002298 return;
2299
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002300 wl_pointer_set_cursor(input->pointer, input->display->serial,
2301 input->pointer_surface,
2302 image->hotspot_x, image->hotspot_y);
2303 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2304 wl_surface_damage(input->pointer_surface, 0, 0,
2305 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002306}
2307
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002308static const struct wl_callback_listener pointer_surface_listener;
2309
2310static void
2311pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2312 uint32_t time)
2313{
2314 struct input *input = data;
2315 struct wl_cursor *cursor =
2316 input->display->cursors[input->current_cursor];
2317 int i;
2318
2319 if (callback) {
2320 assert(callback == input->cursor_frame_cb);
2321 wl_callback_destroy(callback);
2322 input->cursor_frame_cb = NULL;
2323 }
2324
2325 if (input->current_cursor == CURSOR_UNSET)
2326 return;
2327
2328 /* FIXME We don't have the current time on the first call so we set
2329 * the animation start to the time of the first frame callback. */
2330 if (time == 0)
2331 input->cursor_anim_start = 0;
2332 else if (input->cursor_anim_start == 0)
2333 input->cursor_anim_start = time;
2334
2335 if (time == 0 || input->cursor_anim_start == 0)
2336 i = 0;
2337 else
2338 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2339
2340 input_set_pointer_image_index(input, i);
2341
2342 if (cursor->image_count == 1)
2343 return;
2344
2345 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2346 wl_callback_add_listener(input->cursor_frame_cb,
2347 &pointer_surface_listener, input);
2348}
2349
2350static const struct wl_callback_listener pointer_surface_listener = {
2351 pointer_surface_frame_callback
2352};
2353
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002354void
2355input_set_pointer_image(struct input *input, int pointer)
2356{
2357 if (pointer == input->current_cursor)
2358 return;
2359
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002360 input->current_cursor = pointer;
2361 if (!input->cursor_frame_cb)
2362 pointer_surface_frame_callback(input, NULL, 0);
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002363}
2364
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002365struct wl_data_device *
2366input_get_data_device(struct input *input)
2367{
2368 return input->data_device;
2369}
2370
2371void
2372input_set_selection(struct input *input,
2373 struct wl_data_source *source, uint32_t time)
2374{
2375 wl_data_device_set_selection(input->data_device, source, time);
2376}
2377
2378void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002379input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002380{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002381 wl_data_offer_accept(input->drag_offer->offer,
2382 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002383}
2384
2385static void
2386offer_io_func(struct task *task, uint32_t events)
2387{
2388 struct data_offer *offer =
2389 container_of(task, struct data_offer, io_task);
2390 unsigned int len;
2391 char buffer[4096];
2392
2393 len = read(offer->fd, buffer, sizeof buffer);
2394 offer->func(buffer, len,
2395 offer->x, offer->y, offer->user_data);
2396
2397 if (len == 0) {
2398 close(offer->fd);
2399 data_offer_destroy(offer);
2400 }
2401}
2402
2403static void
2404data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2405 data_func_t func, void *user_data)
2406{
2407 int p[2];
2408
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002409 if (pipe2(p, O_CLOEXEC) == -1)
2410 return;
2411
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002412 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2413 close(p[1]);
2414
2415 offer->io_task.run = offer_io_func;
2416 offer->fd = p[0];
2417 offer->func = func;
2418 offer->refcount++;
2419 offer->user_data = user_data;
2420
2421 display_watch_fd(offer->input->display,
2422 offer->fd, EPOLLIN, &offer->io_task);
2423}
2424
2425void
2426input_receive_drag_data(struct input *input, const char *mime_type,
2427 data_func_t func, void *data)
2428{
2429 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2430 input->drag_offer->x = input->sx;
2431 input->drag_offer->y = input->sy;
2432}
2433
2434int
2435input_receive_selection_data(struct input *input, const char *mime_type,
2436 data_func_t func, void *data)
2437{
2438 char **p;
2439
2440 if (input->selection_offer == NULL)
2441 return -1;
2442
2443 for (p = input->selection_offer->types.data; *p; p++)
2444 if (strcmp(mime_type, *p) == 0)
2445 break;
2446
2447 if (*p == NULL)
2448 return -1;
2449
2450 data_offer_receive_data(input->selection_offer,
2451 mime_type, func, data);
2452 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002453}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002454
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002455int
2456input_receive_selection_data_to_fd(struct input *input,
2457 const char *mime_type, int fd)
2458{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002459 if (input->selection_offer)
2460 wl_data_offer_receive(input->selection_offer->offer,
2461 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002462
2463 return 0;
2464}
2465
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002466void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002467window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002468{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002469 if (!window->shell_surface)
2470 return;
2471
Daniel Stone37816df2012-05-16 18:45:18 +01002472 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002473}
2474
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002475static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002476idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002477{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002478 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002479
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002480 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002481 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002482 widget_set_allocation(widget,
2483 window->pending_allocation.x,
2484 window->pending_allocation.y,
2485 window->pending_allocation.width,
2486 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002487
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002488 if (window->input_region) {
2489 wl_region_destroy(window->input_region);
2490 window->input_region = NULL;
2491 }
2492
2493 if (window->opaque_region) {
2494 wl_region_destroy(window->opaque_region);
2495 window->opaque_region = NULL;
2496 }
2497
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002498 if (widget->resize_handler)
2499 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002500 widget->allocation.width,
2501 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002502 widget->user_data);
2503
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002504 if (window->allocation.width != widget->allocation.width ||
2505 window->allocation.height != widget->allocation.height) {
2506 window->allocation = widget->allocation;
2507 window_schedule_redraw(window);
2508 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002509}
2510
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002511void
2512window_schedule_resize(struct window *window, int width, int height)
2513{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002514 window->pending_allocation.x = 0;
2515 window->pending_allocation.y = 0;
2516 window->pending_allocation.width = width;
2517 window->pending_allocation.height = height;
2518
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002519 window->resize_needed = 1;
2520 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002521}
2522
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002523void
2524widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2525{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002526 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002527}
2528
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002529static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002530handle_ping(void *data, struct wl_shell_surface *shell_surface,
2531 uint32_t serial)
2532{
2533 wl_shell_surface_pong(shell_surface, serial);
2534}
2535
2536static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002537handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002538 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002539{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002540 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002541
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002542 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002543 return;
2544
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002545 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002546 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002547}
2548
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002549static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002550menu_destroy(struct menu *menu)
2551{
2552 widget_destroy(menu->widget);
2553 window_destroy(menu->window);
2554 free(menu);
2555}
2556
2557static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002558handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2559{
2560 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002561 struct menu *menu = window->widget->user_data;
2562
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002563 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002564 * device. Or just use wl_callback. And this really needs to
2565 * be a window vfunc that the menu can set. And we need the
2566 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002567
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002568 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002569 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002570 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002571}
2572
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002573static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002574 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002575 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002576 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002577};
2578
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002579void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002580window_get_allocation(struct window *window,
2581 struct rectangle *allocation)
2582{
2583 *allocation = window->allocation;
2584}
2585
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002586static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002587widget_redraw(struct widget *widget)
2588{
2589 struct widget *child;
2590
2591 if (widget->redraw_handler)
2592 widget->redraw_handler(widget, widget->user_data);
2593 wl_list_for_each(child, &widget->child_list, link)
2594 widget_redraw(child);
2595}
2596
2597static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002598frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2599{
2600 struct window *window = data;
2601
2602 wl_callback_destroy(callback);
2603 window->redraw_scheduled = 0;
2604 if (window->redraw_needed)
2605 window_schedule_redraw(window);
2606}
2607
2608static const struct wl_callback_listener listener = {
2609 frame_callback
2610};
2611
2612static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002613idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002614{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002615 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002616 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002617
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002618 if (window->resize_needed)
2619 idle_resize(window);
2620
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002621 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002622 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002623 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002624 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002625 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002626
2627 callback = wl_surface_frame(window->surface);
2628 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002629}
2630
2631void
2632window_schedule_redraw(struct window *window)
2633{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002634 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002635 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002636 window->redraw_task.run = idle_redraw;
2637 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002638 window->redraw_scheduled = 1;
2639 }
2640}
2641
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002642void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002643window_set_custom(struct window *window)
2644{
2645 window->type = TYPE_CUSTOM;
2646}
2647
2648void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002649window_set_fullscreen(struct window *window, int fullscreen)
2650{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002651 if (!window->display->shell)
2652 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002653
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002654 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002655 return;
2656
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002657 if (fullscreen) {
2658 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002659 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002660 wl_shell_surface_set_fullscreen(window->shell_surface,
2661 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2662 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002663 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002664 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002665 wl_shell_surface_set_toplevel(window->shell_surface);
2666 window_schedule_resize(window,
2667 window->saved_allocation.width,
2668 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002669 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002670}
2671
2672void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002673window_set_maximized(struct window *window, int maximized)
2674{
2675 if (!window->display->shell)
2676 return;
2677
2678 if ((window->type == TYPE_MAXIMIZED) == maximized)
2679 return;
2680
2681 if (window->type == TYPE_TOPLEVEL) {
2682 window->saved_allocation = window->allocation;
2683 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2684 window->type = TYPE_MAXIMIZED;
2685 } else {
2686 wl_shell_surface_set_toplevel(window->shell_surface);
2687 window->type = TYPE_TOPLEVEL;
2688 window_schedule_resize(window,
2689 window->saved_allocation.width,
2690 window->saved_allocation.height);
2691 }
2692}
2693
2694void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002695window_set_user_data(struct window *window, void *data)
2696{
2697 window->user_data = data;
2698}
2699
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002700void *
2701window_get_user_data(struct window *window)
2702{
2703 return window->user_data;
2704}
2705
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002706void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002707window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002708 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002709{
2710 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002711}
2712
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002713void
2714window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002715 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002716{
2717 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002718}
2719
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002720void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002721window_set_data_handler(struct window *window, window_data_handler_t handler)
2722{
2723 window->data_handler = handler;
2724}
2725
2726void
2727window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2728{
2729 window->drop_handler = handler;
2730}
2731
2732void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002733window_set_close_handler(struct window *window,
2734 window_close_handler_t handler)
2735{
2736 window->close_handler = handler;
2737}
2738
2739void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002740window_set_title(struct window *window, const char *title)
2741{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002742 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002743 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002744 if (window->shell_surface)
2745 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002746}
2747
2748const char *
2749window_get_title(struct window *window)
2750{
2751 return window->title;
2752}
2753
2754void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002755window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2756{
2757 struct text_cursor_position *text_cursor_position =
2758 window->display->text_cursor_position;
2759
Scott Moreau9295ce02012-06-01 12:46:10 -06002760 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002761 return;
2762
2763 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002764 window->surface,
2765 wl_fixed_from_int(x),
2766 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002767}
2768
2769void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002770window_damage(struct window *window, int32_t x, int32_t y,
2771 int32_t width, int32_t height)
2772{
2773 wl_surface_damage(window->surface, x, y, width, height);
2774}
2775
Casey Dahlin9074db52012-04-19 22:50:09 -04002776static void
2777surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002778 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002779{
Rob Bradford7507b572012-05-15 17:55:34 +01002780 struct window *window = data;
2781 struct output *output;
2782 struct output *output_found = NULL;
2783 struct window_output *window_output;
2784
2785 wl_list_for_each(output, &window->display->output_list, link) {
2786 if (output->output == wl_output) {
2787 output_found = output;
2788 break;
2789 }
2790 }
2791
2792 if (!output_found)
2793 return;
2794
2795 window_output = malloc (sizeof *window_output);
2796 window_output->output = output_found;
2797
2798 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002799}
2800
2801static void
2802surface_leave(void *data,
2803 struct wl_surface *wl_surface, struct wl_output *output)
2804{
Rob Bradford7507b572012-05-15 17:55:34 +01002805 struct window *window = data;
2806 struct window_output *window_output;
2807 struct window_output *window_output_found = NULL;
2808
2809 wl_list_for_each(window_output, &window->window_output_list, link) {
2810 if (window_output->output->output == output) {
2811 window_output_found = window_output;
2812 break;
2813 }
2814 }
2815
2816 if (window_output_found) {
2817 wl_list_remove(&window_output_found->link);
2818 free(window_output_found);
2819 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002820}
2821
2822static const struct wl_surface_listener surface_listener = {
2823 surface_enter,
2824 surface_leave
2825};
2826
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002827static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002828window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002829{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002830 struct window *window;
2831
2832 window = malloc(sizeof *window);
2833 if (window == NULL)
2834 return NULL;
2835
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002836 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002837 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002838 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002839 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002840 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002841 if (display->shell) {
2842 window->shell_surface =
2843 wl_shell_get_shell_surface(display->shell,
2844 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002845 if (window->title)
2846 wl_shell_surface_set_title(window->shell_surface,
2847 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002848 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002849 window->allocation.x = 0;
2850 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002851 window->allocation.width = 0;
2852 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002853 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002854 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002855 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002856 window->input_region = NULL;
2857 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002858
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002859 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002860#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002861 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002862#else
2863 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2864#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002865 else
2866 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002867
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002868 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002869 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002870 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002871
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002872 if (window->shell_surface) {
2873 wl_shell_surface_set_user_data(window->shell_surface, window);
2874 wl_shell_surface_add_listener(window->shell_surface,
2875 &shell_surface_listener, window);
2876 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002877
Rob Bradford7507b572012-05-15 17:55:34 +01002878 wl_list_init (&window->window_output_list);
2879
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002880 return window;
2881}
2882
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002883struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002884window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002885{
2886 struct window *window;
2887
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002888 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002889 if (!window)
2890 return NULL;
2891
2892 return window;
2893}
2894
2895struct window *
2896window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002897 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002898{
2899 struct window *window;
2900
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002901 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002902 if (!window)
2903 return NULL;
2904
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002905 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002906 window->x = x;
2907 window->y = y;
2908
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002909 if (display->shell)
2910 wl_shell_surface_set_transient(window->shell_surface,
2911 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002912 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002913
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002914 return window;
2915}
2916
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002917static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002918menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002919{
2920 int next;
2921
2922 next = (sy - 8) / 20;
2923 if (menu->current != next) {
2924 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002925 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002926 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002927}
2928
2929static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002930menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002931 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002932 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002933{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002934 struct menu *menu = data;
2935
2936 if (widget == menu->widget)
2937 menu_set_item(data, y);
2938
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002939 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002940}
2941
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002942static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002943menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002944 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002945{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002946 struct menu *menu = data;
2947
2948 if (widget == menu->widget)
2949 menu_set_item(data, y);
2950
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002951 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002952}
2953
2954static void
2955menu_leave_handler(struct widget *widget, struct input *input, void *data)
2956{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002957 struct menu *menu = data;
2958
2959 if (widget == menu->widget)
2960 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002961}
2962
2963static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002964menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002965 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002966 uint32_t button, enum wl_pointer_button_state state,
2967 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002968
2969{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002970 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002971
Daniel Stone4dbadb12012-05-30 16:31:51 +01002972 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
2973 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002974 /* Either relase after press-drag-release or
2975 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002976 menu->func(menu->window->parent,
2977 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002978 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002979 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002980 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002981}
2982
2983static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002984menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002985{
2986 cairo_t *cr;
2987 const int32_t r = 3, margin = 3;
2988 struct menu *menu = data;
2989 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002990 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002991
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002992 cr = cairo_create(window->cairo_surface);
2993 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2994 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2995 cairo_paint(cr);
2996
2997 width = window->allocation.width;
2998 height = window->allocation.height;
2999 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003000
3001 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003002 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3003 cairo_fill(cr);
3004
3005 for (i = 0; i < menu->count; i++) {
3006 if (i == menu->current) {
3007 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3008 cairo_rectangle(cr, margin, i * 20 + margin,
3009 width - 2 * margin, 20);
3010 cairo_fill(cr);
3011 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3012 cairo_move_to(cr, 10, i * 20 + 16);
3013 cairo_show_text(cr, menu->entries[i]);
3014 } else {
3015 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3016 cairo_move_to(cr, 10, i * 20 + 16);
3017 cairo_show_text(cr, menu->entries[i]);
3018 }
3019 }
3020
3021 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003022}
3023
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003024void
3025window_show_menu(struct display *display,
3026 struct input *input, uint32_t time, struct window *parent,
3027 int32_t x, int32_t y,
3028 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003029{
3030 struct window *window;
3031 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003032 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003033
3034 menu = malloc(sizeof *menu);
3035 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003036 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003037
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003038 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003039 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003040 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003041
3042 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003043 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003044 menu->entries = entries;
3045 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003046 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003047 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003048 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003049 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003050 window->type = TYPE_MENU;
3051 window->x = x;
3052 window->y = y;
3053
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003054 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003055 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003056 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003057 window->parent->shell_surface,
3058 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003059
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003060 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003061 widget_set_enter_handler(menu->widget, menu_enter_handler);
3062 widget_set_leave_handler(menu->widget, menu_leave_handler);
3063 widget_set_motion_handler(menu->widget, menu_motion_handler);
3064 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003065
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003066 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003067 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003068}
3069
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003070void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003071window_set_buffer_type(struct window *window, enum window_buffer_type type)
3072{
3073 window->buffer_type = type;
3074}
3075
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003076
3077static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003078display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003079 struct wl_output *wl_output,
3080 int x, int y,
3081 int physical_width,
3082 int physical_height,
3083 int subpixel,
3084 const char *make,
3085 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003086{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003087 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003088
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003089 output->allocation.x = x;
3090 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003091}
3092
3093static void
3094display_handle_mode(void *data,
3095 struct wl_output *wl_output,
3096 uint32_t flags,
3097 int width,
3098 int height,
3099 int refresh)
3100{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003101 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003102 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003103
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003104 if (flags & WL_OUTPUT_MODE_CURRENT) {
3105 output->allocation.width = width;
3106 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003107 if (display->output_configure_handler)
3108 (*display->output_configure_handler)(
3109 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003110 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003111}
3112
3113static const struct wl_output_listener output_listener = {
3114 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003115 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003116};
3117
3118static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003119display_add_output(struct display *d, uint32_t id)
3120{
3121 struct output *output;
3122
3123 output = malloc(sizeof *output);
3124 if (output == NULL)
3125 return;
3126
3127 memset(output, 0, sizeof *output);
3128 output->display = d;
3129 output->output =
3130 wl_display_bind(d->display, id, &wl_output_interface);
3131 wl_list_insert(d->output_list.prev, &output->link);
3132
3133 wl_output_add_listener(output->output, &output_listener, output);
3134}
3135
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003136static void
3137output_destroy(struct output *output)
3138{
3139 if (output->destroy_handler)
3140 (*output->destroy_handler)(output, output->user_data);
3141
3142 wl_output_destroy(output->output);
3143 wl_list_remove(&output->link);
3144 free(output);
3145}
3146
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003147void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003148display_set_output_configure_handler(struct display *display,
3149 display_output_handler_t handler)
3150{
3151 struct output *output;
3152
3153 display->output_configure_handler = handler;
3154 if (!handler)
3155 return;
3156
3157 wl_list_for_each(output, &display->output_list, link)
3158 (*display->output_configure_handler)(output,
3159 display->user_data);
3160}
3161
3162void
3163output_set_user_data(struct output *output, void *data)
3164{
3165 output->user_data = data;
3166}
3167
3168void *
3169output_get_user_data(struct output *output)
3170{
3171 return output->user_data;
3172}
3173
3174void
3175output_set_destroy_handler(struct output *output,
3176 display_output_handler_t handler)
3177{
3178 output->destroy_handler = handler;
3179 /* FIXME: implement this, once we have way to remove outputs */
3180}
3181
3182void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003183output_get_allocation(struct output *output, struct rectangle *allocation)
3184{
3185 *allocation = output->allocation;
3186}
3187
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003188struct wl_output *
3189output_get_wl_output(struct output *output)
3190{
3191 return output->output;
3192}
3193
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003194static void
Daniel Stone97f68542012-05-30 16:32:01 +01003195fini_xkb(struct input *input)
3196{
3197 xkb_state_unref(input->xkb.state);
3198 xkb_map_unref(input->xkb.keymap);
3199}
3200
3201static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003202display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003203{
3204 struct input *input;
3205
3206 input = malloc(sizeof *input);
3207 if (input == NULL)
3208 return;
3209
3210 memset(input, 0, sizeof *input);
3211 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003212 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003213 input->pointer_focus = NULL;
3214 input->keyboard_focus = NULL;
3215 wl_list_insert(d->input_list.prev, &input->link);
3216
Daniel Stone37816df2012-05-16 18:45:18 +01003217 wl_seat_add_listener(input->seat, &seat_listener, input);
3218 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003219
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003220 input->data_device =
3221 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003222 input->seat);
3223 wl_data_device_add_listener(input->data_device, &data_device_listener,
3224 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003225
3226 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003227}
3228
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003229static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003230input_destroy(struct input *input)
3231{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003232 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003233 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003234
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003235 if (input->xkb.state)
3236 xkb_state_unref(input->xkb.state);
3237 if (input->xkb.keymap)
3238 xkb_map_unref(input->xkb.keymap);
3239
Pekka Paalanene1207c72011-12-16 12:02:09 +02003240 if (input->drag_offer)
3241 data_offer_destroy(input->drag_offer);
3242
3243 if (input->selection_offer)
3244 data_offer_destroy(input->selection_offer);
3245
3246 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003247 fini_xkb(input);
3248
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003249 wl_surface_destroy(input->pointer_surface);
3250
Pekka Paalanene1207c72011-12-16 12:02:09 +02003251 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003252 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003253 free(input);
3254}
3255
3256static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003257display_handle_global(struct wl_display *display, uint32_t id,
3258 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003259{
3260 struct display *d = data;
3261
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003262 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003263 d->compositor =
3264 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003265 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003266 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003267 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003268 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003269 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003270 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003271 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003272 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003273 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3274 d->data_device_manager =
3275 wl_display_bind(display, id,
3276 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003277 } else if (strcmp(interface, "text_cursor_position") == 0) {
3278 d->text_cursor_position =
3279 wl_display_bind(display, id,
3280 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003281 }
3282}
3283
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003284#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003285static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003286init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003287{
3288 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003289 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003290
Rob Clark6396ed32012-03-11 19:48:41 -05003291#ifdef USE_CAIRO_GLESV2
3292# define GL_BIT EGL_OPENGL_ES2_BIT
3293#else
3294# define GL_BIT EGL_OPENGL_BIT
3295#endif
3296
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003297 static const EGLint argb_cfg_attribs[] = {
3298 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003299 EGL_RED_SIZE, 1,
3300 EGL_GREEN_SIZE, 1,
3301 EGL_BLUE_SIZE, 1,
3302 EGL_ALPHA_SIZE, 1,
3303 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003304 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003305 EGL_NONE
3306 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003307
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003308#ifdef USE_CAIRO_GLESV2
3309 static const EGLint context_attribs[] = {
3310 EGL_CONTEXT_CLIENT_VERSION, 2,
3311 EGL_NONE
3312 };
3313 EGLint api = EGL_OPENGL_ES_API;
3314#else
3315 EGLint *context_attribs = NULL;
3316 EGLint api = EGL_OPENGL_API;
3317#endif
3318
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003319 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003320 if (!eglInitialize(d->dpy, &major, &minor)) {
3321 fprintf(stderr, "failed to initialize display\n");
3322 return -1;
3323 }
3324
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003325 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003326 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3327 return -1;
3328 }
3329
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003330 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3331 &d->argb_config, 1, &n) || n != 1) {
3332 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003333 return -1;
3334 }
3335
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003336 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003337 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003338 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003339 fprintf(stderr, "failed to create context\n");
3340 return -1;
3341 }
3342
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003343 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003344 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003345 return -1;
3346 }
3347
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003348#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003349 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3350 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3351 fprintf(stderr, "failed to get cairo egl argb device\n");
3352 return -1;
3353 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003354#endif
3355
3356 return 0;
3357}
3358
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003359static void
3360fini_egl(struct display *display)
3361{
3362#ifdef HAVE_CAIRO_EGL
3363 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003364#endif
3365
3366 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3367 EGL_NO_CONTEXT);
3368
3369 eglTerminate(display->dpy);
3370 eglReleaseThread();
3371}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003372#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003373
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003374static int
3375event_mask_update(uint32_t mask, void *data)
3376{
3377 struct display *d = data;
3378
3379 d->mask = mask;
3380
3381 return 0;
3382}
3383
3384static void
3385handle_display_data(struct task *task, uint32_t events)
3386{
3387 struct display *display =
3388 container_of(task, struct display, display_task);
3389
3390 wl_display_iterate(display->display, display->mask);
3391}
3392
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003393struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003394display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003395{
3396 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003397
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003398 argc = parse_options(xkb_options,
3399 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003400
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003401 d = malloc(sizeof *d);
3402 if (d == NULL)
3403 return NULL;
3404
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003405 memset(d, 0, sizeof *d);
3406
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003407 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003408 if (d->display == NULL) {
3409 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003410 return NULL;
3411 }
3412
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003413 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003414 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3415 d->display_task.run = handle_display_data;
3416 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3417
3418 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003419 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003420 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003421
Daniel Stone97f68542012-05-30 16:32:01 +01003422 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003423 if (d->xkb_context == NULL) {
3424 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003425 return NULL;
3426 }
3427
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003428 /* Set up listener so we'll catch all events. */
3429 wl_display_add_global_listener(d->display,
3430 display_handle_global, d);
3431
3432 /* Process connection events. */
3433 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003434#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003435 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003436 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003437#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003438
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003439 d->image_target_texture_2d =
3440 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3441 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3442 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3443
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003444 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003445
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003446 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003447
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003448 wl_list_init(&d->window_list);
3449
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003450 return d;
3451}
3452
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003453static void
3454display_destroy_outputs(struct display *display)
3455{
3456 struct output *tmp;
3457 struct output *output;
3458
3459 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3460 output_destroy(output);
3461}
3462
Pekka Paalanene1207c72011-12-16 12:02:09 +02003463static void
3464display_destroy_inputs(struct display *display)
3465{
3466 struct input *tmp;
3467 struct input *input;
3468
3469 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3470 input_destroy(input);
3471}
3472
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003473void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003474display_destroy(struct display *display)
3475{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003476 if (!wl_list_empty(&display->window_list))
3477 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3478
3479 if (!wl_list_empty(&display->deferred_list))
3480 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3481
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003482 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003483 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003484
Daniel Stone97f68542012-05-30 16:32:01 +01003485 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003486
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003487 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003488 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003489
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003490#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003491 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003492#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003493
Pekka Paalanenc2052982011-12-16 11:41:32 +02003494 if (display->shell)
3495 wl_shell_destroy(display->shell);
3496
3497 if (display->shm)
3498 wl_shm_destroy(display->shm);
3499
3500 if (display->data_device_manager)
3501 wl_data_device_manager_destroy(display->data_device_manager);
3502
3503 wl_compositor_destroy(display->compositor);
3504
3505 close(display->epoll_fd);
3506
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003507 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003508 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003509 free(display);
3510}
3511
3512void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003513display_set_user_data(struct display *display, void *data)
3514{
3515 display->user_data = data;
3516}
3517
3518void *
3519display_get_user_data(struct display *display)
3520{
3521 return display->user_data;
3522}
3523
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003524struct wl_display *
3525display_get_display(struct display *display)
3526{
3527 return display->display;
3528}
3529
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003530struct output *
3531display_get_output(struct display *display)
3532{
3533 return container_of(display->output_list.next, struct output, link);
3534}
3535
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003536struct wl_compositor *
3537display_get_compositor(struct display *display)
3538{
3539 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003540}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003541
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003542uint32_t
3543display_get_serial(struct display *display)
3544{
3545 return display->serial;
3546}
3547
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003548EGLDisplay
3549display_get_egl_display(struct display *d)
3550{
3551 return d->dpy;
3552}
3553
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003554struct wl_data_source *
3555display_create_data_source(struct display *display)
3556{
3557 return wl_data_device_manager_create_data_source(display->data_device_manager);
3558}
3559
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003560EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003561display_get_argb_egl_config(struct display *d)
3562{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003563 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003564}
3565
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003566struct wl_shell *
3567display_get_shell(struct display *display)
3568{
3569 return display->shell;
3570}
3571
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003572int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003573display_acquire_window_surface(struct display *display,
3574 struct window *window,
3575 EGLContext ctx)
3576{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003577#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003578 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003579 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003580
3581 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003582 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003583 device = cairo_surface_get_device(window->cairo_surface);
3584 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003585 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003586
Benjamin Franzke0c991632011-09-27 21:57:31 +02003587 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003588 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003589 ctx = display->argb_ctx;
3590 else
3591 assert(0);
3592 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003593
3594 data = cairo_surface_get_user_data(window->cairo_surface,
3595 &surface_data_key);
3596
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003597 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003598 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003599 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3600 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003601
3602 return 0;
3603#else
3604 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003605#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003606}
3607
3608void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003609display_release_window_surface(struct display *display,
3610 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003611{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003612#ifdef HAVE_CAIRO_EGL
3613 cairo_device_t *device;
3614
3615 device = cairo_surface_get_device(window->cairo_surface);
3616 if (!device)
3617 return;
3618
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003619 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003620 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003621 cairo_device_release(device);
3622#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003623}
3624
3625void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003626display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003627{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003628 wl_list_insert(&display->deferred_list, &task->link);
3629}
3630
3631void
3632display_watch_fd(struct display *display,
3633 int fd, uint32_t events, struct task *task)
3634{
3635 struct epoll_event ep;
3636
3637 ep.events = events;
3638 ep.data.ptr = task;
3639 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3640}
3641
3642void
3643display_run(struct display *display)
3644{
3645 struct task *task;
3646 struct epoll_event ep[16];
3647 int i, count;
3648
Pekka Paalanen826d7952011-12-15 10:14:07 +02003649 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003650 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003651 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003652
3653 while (!wl_list_empty(&display->deferred_list)) {
3654 task = container_of(display->deferred_list.next,
3655 struct task, link);
3656 wl_list_remove(&task->link);
3657 task->run(task, 0);
3658 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003659
3660 if (!display->running)
3661 break;
3662
3663 wl_display_flush(display->display);
3664
3665 count = epoll_wait(display->epoll_fd,
3666 ep, ARRAY_LENGTH(ep), -1);
3667 for (i = 0; i < count; i++) {
3668 task = ep[i].data.ptr;
3669 task->run(task, ep[i].events);
3670 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003671 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003672}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003673
3674void
3675display_exit(struct display *display)
3676{
3677 display->running = 0;
3678}