blob: a2f339fa3a13102e7043da07b03a9383de76f646 [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øgsbergd3a19652012-07-20 11:32:51 -0400138 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500139 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500140 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400141 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400142 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400143 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400144 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400145 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400146 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400147 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400148 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400149 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500150
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400151 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500152
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700153 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400154
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500155 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500156 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400157 window_data_handler_t data_handler;
158 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500159 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400160 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200162 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500163 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500164
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500165 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400166 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500167};
168
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500169struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500170 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300171 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500172 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400173 struct wl_list link;
174 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500175 widget_resize_handler_t resize_handler;
176 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500177 widget_enter_handler_t enter_handler;
178 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500179 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500180 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400181 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500182 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300183 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400184};
185
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400186struct input {
187 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100188 struct wl_seat *seat;
189 struct wl_pointer *pointer;
190 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400191 struct window *pointer_focus;
192 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300193 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300194 uint32_t cursor_anim_start;
195 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300196 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400197 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400198 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400199 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400200 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400201 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400202
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500203 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500204 struct widget *grab;
205 uint32_t grab_button;
206
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400207 struct wl_data_device *data_device;
208 struct data_offer *drag_offer;
209 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100210
211 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100212 struct xkb_keymap *keymap;
213 struct xkb_state *state;
214 xkb_mod_mask_t control_mask;
215 xkb_mod_mask_t alt_mask;
216 xkb_mod_mask_t shift_mask;
217 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400218
219 struct task repeat_task;
220 int repeat_timer_fd;
221 uint32_t repeat_sym;
222 uint32_t repeat_key;
223 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400224};
225
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500226struct output {
227 struct display *display;
228 struct wl_output *output;
229 struct rectangle allocation;
230 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200231
232 display_output_handler_t destroy_handler;
233 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500234};
235
Martin Minarik1998b152012-05-10 02:04:35 +0200236enum frame_button_action {
237 FRAME_BUTTON_NULL = 0,
238 FRAME_BUTTON_ICON = 1,
239 FRAME_BUTTON_CLOSE = 2,
240 FRAME_BUTTON_MINIMIZE = 3,
241 FRAME_BUTTON_MAXIMIZE = 4,
242};
243
244enum frame_button_pointer {
245 FRAME_BUTTON_DEFAULT = 0,
246 FRAME_BUTTON_OVER = 1,
247 FRAME_BUTTON_ACTIVE = 2,
248};
249
250enum frame_button_align {
251 FRAME_BUTTON_RIGHT = 0,
252 FRAME_BUTTON_LEFT = 1,
253};
254
255enum frame_button_decoration {
256 FRAME_BUTTON_NONE = 0,
257 FRAME_BUTTON_FANCY = 1,
258};
259
260struct frame_button {
261 struct widget *widget;
262 struct frame *frame;
263 cairo_surface_t *icon;
264 enum frame_button_action type;
265 enum frame_button_pointer state;
266 struct wl_list link; /* buttons_list */
267 enum frame_button_align align;
268 enum frame_button_decoration decoration;
269};
270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500271struct frame {
272 struct widget *widget;
273 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200274 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500275};
276
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500277struct menu {
278 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500279 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500280 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500281 const char **entries;
282 uint32_t time;
283 int current;
284 int count;
285 menu_func_t func;
286};
287
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300288struct tooltip {
289 struct widget *parent;
290 struct window *window;
291 struct widget *widget;
292 char *entry;
293 struct task tooltip_task;
294 int tooltip_fd;
295 float x, y;
296};
297
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700298struct shm_pool {
299 struct wl_shm_pool *pool;
300 size_t size;
301 size_t used;
302 void *data;
303};
304
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400305enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300306 CURSOR_DEFAULT = 100,
307 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400308};
309
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500310enum window_location {
311 WINDOW_INTERIOR = 0,
312 WINDOW_RESIZING_TOP = 1,
313 WINDOW_RESIZING_BOTTOM = 2,
314 WINDOW_RESIZING_LEFT = 4,
315 WINDOW_RESIZING_TOP_LEFT = 5,
316 WINDOW_RESIZING_BOTTOM_LEFT = 6,
317 WINDOW_RESIZING_RIGHT = 8,
318 WINDOW_RESIZING_TOP_RIGHT = 9,
319 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
320 WINDOW_RESIZING_MASK = 15,
321 WINDOW_EXTERIOR = 16,
322 WINDOW_TITLEBAR = 17,
323 WINDOW_CLIENT_AREA = 18,
324};
325
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326static const cairo_user_data_key_t surface_data_key;
327struct surface_data {
328 struct wl_buffer *buffer;
329};
330
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500331#define MULT(_d,c,a,t) \
332 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
333
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500334#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100336struct egl_window_surface_data {
337 struct display *display;
338 struct wl_surface *surface;
339 struct wl_egl_window *window;
340 EGLSurface surf;
341};
342
343static void
344egl_window_surface_data_destroy(void *p)
345{
346 struct egl_window_surface_data *data = p;
347 struct display *d = data->display;
348
349 eglDestroySurface(d->dpy, data->surf);
350 wl_egl_window_destroy(data->window);
351 data->surface = NULL;
352
353 free(p);
354}
355
356static cairo_surface_t *
357display_create_egl_window_surface(struct display *display,
358 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400359 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100360 struct rectangle *rectangle)
361{
362 cairo_surface_t *cairo_surface;
363 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400364 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200365 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100366
367 data = malloc(sizeof *data);
368 if (data == NULL)
369 return NULL;
370
371 data->display = display;
372 data->surface = surface;
373
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500374 config = display->argb_config;
375 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400377 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400379 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100380
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400381 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500382 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383
Benjamin Franzke0c991632011-09-27 21:57:31 +0200384 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100385 data->surf,
386 rectangle->width,
387 rectangle->height);
388
389 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
390 data, egl_window_surface_data_destroy);
391
392 return cairo_surface;
393}
394
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395#endif
396
397struct wl_buffer *
398display_get_buffer_for_surface(struct display *display,
399 cairo_surface_t *surface)
400{
401 struct surface_data *data;
402
403 data = cairo_surface_get_user_data (surface, &surface_data_key);
404
405 return data->buffer;
406}
407
408struct shm_surface_data {
409 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700410 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411};
412
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500413static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700414shm_pool_destroy(struct shm_pool *pool);
415
416static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417shm_surface_data_destroy(void *p)
418{
419 struct shm_surface_data *data = p;
420
421 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700422 if (data->pool)
423 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300424
425 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426}
427
Kristian Høgsberg16626282012-04-03 11:21:27 -0400428static struct wl_shm_pool *
429make_shm_pool(struct display *display, int size, void **data)
430{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400431 struct wl_shm_pool *pool;
432 int fd;
433
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300434 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400435 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300436 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
437 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400438 return NULL;
439 }
440
441 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400442 if (*data == MAP_FAILED) {
443 fprintf(stderr, "mmap failed: %m\n");
444 close(fd);
445 return NULL;
446 }
447
448 pool = wl_shm_create_pool(display->shm, fd, size);
449
450 close(fd);
451
452 return pool;
453}
454
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700455static struct shm_pool *
456shm_pool_create(struct display *display, size_t size)
457{
458 struct shm_pool *pool = malloc(sizeof *pool);
459
460 if (!pool)
461 return NULL;
462
463 pool->pool = make_shm_pool(display, size, &pool->data);
464 if (!pool->pool) {
465 free(pool);
466 return NULL;
467 }
468
469 pool->size = size;
470 pool->used = 0;
471
472 return pool;
473}
474
475static void *
476shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
477{
478 if (pool->used + size > pool->size)
479 return NULL;
480
481 *offset = pool->used;
482 pool->used += size;
483
484 return (char *) pool->data + *offset;
485}
486
487/* destroy the pool. this does not unmap the memory though */
488static void
489shm_pool_destroy(struct shm_pool *pool)
490{
491 munmap(pool->data, pool->size);
492 wl_shm_pool_destroy(pool->pool);
493 free(pool);
494}
495
496/* Start allocating from the beginning of the pool again */
497static void
498shm_pool_reset(struct shm_pool *pool)
499{
500 pool->used = 0;
501}
502
503static int
504data_length_for_shm_surface(struct rectangle *rect)
505{
506 int stride;
507
508 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
509 rect->width);
510 return stride * rect->height;
511}
512
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500513static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700514display_create_shm_surface_from_pool(struct display *display,
515 struct rectangle *rectangle,
516 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517{
518 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400519 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400520 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700521 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400522 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523
524 data = malloc(sizeof *data);
525 if (data == NULL)
526 return NULL;
527
528 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
529 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700530 length = stride * rectangle->height;
531 data->pool = NULL;
532 map = shm_pool_allocate(pool, length, &offset);
533
534 if (!map) {
535 free(data);
536 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400537 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400539 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540 CAIRO_FORMAT_ARGB32,
541 rectangle->width,
542 rectangle->height,
543 stride);
544
545 cairo_surface_set_user_data (surface, &surface_data_key,
546 data, shm_surface_data_destroy);
547
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500549 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500551 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400552
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700553 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400554 rectangle->width,
555 rectangle->height,
556 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700558 return surface;
559}
560
561static cairo_surface_t *
562display_create_shm_surface(struct display *display,
563 struct rectangle *rectangle, uint32_t flags,
564 struct window *window)
565{
566 struct shm_surface_data *data;
567 struct shm_pool *pool;
568 cairo_surface_t *surface;
569
570 if (window && window->pool) {
571 shm_pool_reset(window->pool);
572 surface = display_create_shm_surface_from_pool(display,
573 rectangle,
574 flags,
575 window->pool);
576 if (surface)
577 return surface;
578 }
579
580 pool = shm_pool_create(display,
581 data_length_for_shm_surface(rectangle));
582 if (!pool)
583 return NULL;
584
585 surface =
586 display_create_shm_surface_from_pool(display, rectangle,
587 flags, pool);
588
589 if (!surface) {
590 shm_pool_destroy(pool);
591 return NULL;
592 }
593
594 /* make sure we destroy the pool when the surface is destroyed */
595 data = cairo_surface_get_user_data(surface, &surface_data_key);
596 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597
598 return surface;
599}
600
nobled7b87cb02011-02-01 18:51:47 +0000601static int
602check_size(struct rectangle *rect)
603{
604 if (rect->width && rect->height)
605 return 0;
606
607 fprintf(stderr, "tried to create surface of "
608 "width: %d, height: %d\n", rect->width, rect->height);
609 return -1;
610}
611
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400612cairo_surface_t *
613display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100614 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400615 struct rectangle *rectangle,
616 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617{
nobled7b87cb02011-02-01 18:51:47 +0000618 if (check_size(rectangle) < 0)
619 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500620#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300621 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400622 return display_create_egl_window_surface(display,
623 surface,
624 flags,
625 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400627 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628}
629
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300630static const char *cursors[] = {
631 "bottom_left_corner",
632 "bottom_right_corner",
633 "bottom_side",
634 "grabbing",
635 "left_ptr",
636 "left_side",
637 "right_side",
638 "top_left_corner",
639 "top_right_corner",
640 "top_side",
641 "xterm",
642 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400643 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300644};
645
646static void
647create_cursors(struct display *display)
648{
649 unsigned int i;
650
651 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
652 display->cursors =
653 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
654
655 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
656 display->cursors[i] =
657 wl_cursor_theme_get_cursor(display->cursor_theme,
658 cursors[i]);
659}
660
661static void
662destroy_cursors(struct display *display)
663{
664 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800665 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300666}
667
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300668struct wl_cursor_image *
669display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300671 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300672 wl_cursor_theme_get_cursor(display->cursor_theme,
673 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300675 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400676}
677
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400678static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200679window_get_resize_dx_dy(struct window *window, int *x, int *y)
680{
681 if (window->resize_edges & WINDOW_RESIZING_LEFT)
682 *x = window->server_allocation.width - window->allocation.width;
683 else
684 *x = 0;
685
686 if (window->resize_edges & WINDOW_RESIZING_TOP)
687 *y = window->server_allocation.height -
688 window->allocation.height;
689 else
690 *y = 0;
691
692 window->resize_edges = 0;
693}
694
695static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500696window_attach_surface(struct window *window)
697{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400698 struct display *display = window->display;
699 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000700#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100701 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000702#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500703 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100704
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400705 if (window->type == TYPE_NONE) {
706 window->type = TYPE_TOPLEVEL;
707 if (display->shell)
708 wl_shell_surface_set_toplevel(window->shell_surface);
709 }
710
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100711 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000712#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100713 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
714 data = cairo_surface_get_user_data(window->cairo_surface,
715 &surface_data_key);
716
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100717 cairo_gl_surface_swapbuffers(window->cairo_surface);
718 wl_egl_window_get_attached_size(data->window,
719 &window->server_allocation.width,
720 &window->server_allocation.height);
721 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000722#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100723 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100724 buffer =
725 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400726 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200728 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100729 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400730 wl_surface_damage(window->surface, 0, 0,
731 window->allocation.width,
732 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400734 cairo_surface_destroy(window->cairo_surface);
735 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000737 default:
738 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100739 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500740
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500741 if (window->input_region) {
742 wl_surface_set_input_region(window->surface,
743 window->input_region);
744 wl_region_destroy(window->input_region);
745 window->input_region = NULL;
746 }
747
748 if (window->opaque_region) {
749 wl_surface_set_opaque_region(window->surface,
750 window->opaque_region);
751 wl_region_destroy(window->opaque_region);
752 window->opaque_region = NULL;
753 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500754}
755
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500756void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400757window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500758{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400759 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100760 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500761}
762
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400763void
764window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400765{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500766 cairo_surface_reference(surface);
767
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400768 if (window->cairo_surface != NULL)
769 cairo_surface_destroy(window->cairo_surface);
770
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500771 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400772}
773
Benjamin Franzke22d54812011-07-16 19:50:32 +0000774#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100775static void
776window_resize_cairo_window_surface(struct window *window)
777{
778 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200779 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100780
781 data = cairo_surface_get_user_data(window->cairo_surface,
782 &surface_data_key);
783
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200784 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100785 wl_egl_window_resize(data->window,
786 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200787 window->allocation.height,
788 x,y);
789
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100790 cairo_gl_surface_set_size(window->cairo_surface,
791 window->allocation.width,
792 window->allocation.height);
793}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000794#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100795
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400796struct display *
797window_get_display(struct window *window)
798{
799 return window->display;
800}
801
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400802void
803window_create_surface(struct window *window)
804{
805 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400806 uint32_t flags = 0;
807
808 if (!window->transparent)
809 flags = SURFACE_OPAQUE;
810
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400811 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500812#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100813 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
814 if (window->cairo_surface) {
815 window_resize_cairo_window_surface(window);
816 return;
817 }
818 surface = display_create_surface(window->display,
819 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400820 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400822#endif
823 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400824 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400825 &window->allocation,
826 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400827 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800828 default:
829 surface = NULL;
830 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400832
833 window_set_surface(window, surface);
834 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835}
836
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200837static void frame_destroy(struct frame *frame);
838
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400839void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500840window_destroy(struct window *window)
841{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200842 struct display *display = window->display;
843 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100844 struct window_output *window_output;
845 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200846
847 if (window->redraw_scheduled)
848 wl_list_remove(&window->redraw_task.link);
849
850 wl_list_for_each(input, &display->input_list, link) {
851 if (input->pointer_focus == window)
852 input->pointer_focus = NULL;
853 if (input->keyboard_focus == window)
854 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500855 if (input->focus_widget &&
856 input->focus_widget->window == window)
857 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200858 }
859
Rob Bradford7507b572012-05-15 17:55:34 +0100860 wl_list_for_each_safe(window_output, window_output_tmp,
861 &window->window_output_list, link) {
862 free (window_output);
863 }
864
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500865 if (window->input_region)
866 wl_region_destroy(window->input_region);
867 if (window->opaque_region)
868 wl_region_destroy(window->opaque_region);
869
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200870 if (window->frame)
871 frame_destroy(window->frame);
872
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200873 if (window->shell_surface)
874 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500875 wl_surface_destroy(window->surface);
876 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200877
878 if (window->cairo_surface != NULL)
879 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200880
881 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500882 free(window);
883}
884
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500885static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500886widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400887{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500888 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400889
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500890 wl_list_for_each(child, &widget->child_list, link) {
891 target = widget_find_widget(child, x, y);
892 if (target)
893 return target;
894 }
895
896 if (widget->allocation.x <= x &&
897 x < widget->allocation.x + widget->allocation.width &&
898 widget->allocation.y <= y &&
899 y < widget->allocation.y + widget->allocation.height) {
900 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400901 }
902
903 return NULL;
904}
905
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500906static struct widget *
907widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400908{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500909 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400910
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500911 widget = malloc(sizeof *widget);
912 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500913 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500914 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500915 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500916 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500917 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300918 widget->tooltip = NULL;
919 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500920
921 return widget;
922}
923
924struct widget *
925window_add_widget(struct window *window, void *data)
926{
927 window->widget = widget_create(window, data);
928 wl_list_init(&window->widget->link);
929
930 return window->widget;
931}
932
933struct widget *
934widget_add_widget(struct widget *parent, void *data)
935{
936 struct widget *widget;
937
938 widget = widget_create(parent->window, data);
939 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400940
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500941 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400942}
943
944void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500945widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400946{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200947 struct display *display = widget->window->display;
948 struct input *input;
949
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300950 if (widget->tooltip) {
951 free(widget->tooltip);
952 widget->tooltip = NULL;
953 }
954
Pekka Paalanene156fb62012-01-19 13:51:38 +0200955 wl_list_for_each(input, &display->input_list, link) {
956 if (input->focus_widget == widget)
957 input->focus_widget = NULL;
958 }
959
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500960 wl_list_remove(&widget->link);
961 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400962}
963
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400964void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500965widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400966{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500967 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400968}
969
970void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500971widget_set_size(struct widget *widget, int32_t width, int32_t height)
972{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500973 widget->allocation.width = width;
974 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500975}
976
977void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500978widget_set_allocation(struct widget *widget,
979 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400980{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500981 widget->allocation.x = x;
982 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200983 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400984}
985
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500986void
987widget_set_transparent(struct widget *widget, int transparent)
988{
989 widget->opaque = !transparent;
990}
991
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400992void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500993widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400994{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500995 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996}
997
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500998void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500999widget_set_resize_handler(struct widget *widget,
1000 widget_resize_handler_t handler)
1001{
1002 widget->resize_handler = handler;
1003}
1004
1005void
1006widget_set_redraw_handler(struct widget *widget,
1007 widget_redraw_handler_t handler)
1008{
1009 widget->redraw_handler = handler;
1010}
1011
1012void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001013widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001014{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001015 widget->enter_handler = handler;
1016}
1017
1018void
1019widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1020{
1021 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001022}
1023
1024void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001025widget_set_motion_handler(struct widget *widget,
1026 widget_motion_handler_t handler)
1027{
1028 widget->motion_handler = handler;
1029}
1030
1031void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001032widget_set_button_handler(struct widget *widget,
1033 widget_button_handler_t handler)
1034{
1035 widget->button_handler = handler;
1036}
1037
1038void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001039widget_schedule_redraw(struct widget *widget)
1040{
1041 window_schedule_redraw(widget->window);
1042}
1043
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001044cairo_surface_t *
1045window_get_surface(struct window *window)
1046{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001047 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001048}
1049
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001050struct wl_surface *
1051window_get_wl_surface(struct window *window)
1052{
1053 return window->surface;
1054}
1055
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001056struct wl_shell_surface *
1057window_get_wl_shell_surface(struct window *window)
1058{
1059 return window->shell_surface;
1060}
1061
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001062static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001063tooltip_redraw_handler(struct widget *widget, void *data)
1064{
1065 cairo_t *cr;
1066 const int32_t r = 3;
1067 struct tooltip *tooltip = data;
1068 int32_t width, height;
1069 struct window *window = widget->window;
1070
1071 cr = cairo_create(window->cairo_surface);
1072 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1073 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1074 cairo_paint(cr);
1075
1076 width = window->allocation.width;
1077 height = window->allocation.height;
1078 rounded_rect(cr, 0, 0, width, height, r);
1079
1080 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1081 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1082 cairo_fill(cr);
1083
1084 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1085 cairo_move_to(cr, 10, 16);
1086 cairo_show_text(cr, tooltip->entry);
1087 cairo_destroy(cr);
1088}
1089
1090static cairo_text_extents_t
1091get_text_extents(struct tooltip *tooltip)
1092{
1093 struct window *window;
1094 cairo_t *cr;
1095 cairo_text_extents_t extents;
1096
1097 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1098 * created yet */
1099 window = tooltip->widget->window->parent;
1100 cr = cairo_create(window->cairo_surface);
1101 cairo_text_extents(cr, tooltip->entry, &extents);
1102 cairo_destroy(cr);
1103
1104 return extents;
1105}
1106
1107static int
1108window_create_tooltip(struct tooltip *tooltip)
1109{
1110 struct widget *parent = tooltip->parent;
1111 struct display *display = parent->window->display;
1112 struct window *window;
1113 const int offset_y = 27;
1114 const int margin = 3;
1115 cairo_text_extents_t extents;
1116
1117 if (tooltip->widget)
1118 return 0;
1119
1120 window = window_create_transient(display, parent->window, tooltip->x,
1121 tooltip->y + offset_y,
1122 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1123 if (!window)
1124 return -1;
1125
1126 tooltip->window = window;
1127 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1128
1129 extents = get_text_extents(tooltip);
1130 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1131 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1132
1133 return 0;
1134}
1135
1136void
1137widget_destroy_tooltip(struct widget *parent)
1138{
1139 struct tooltip *tooltip = parent->tooltip;
1140
1141 parent->tooltip_count = 0;
1142 if (!tooltip)
1143 return;
1144
1145 if (tooltip->widget) {
1146 widget_destroy(tooltip->widget);
1147 window_destroy(tooltip->window);
1148 tooltip->widget = NULL;
1149 tooltip->window = NULL;
1150 }
1151
1152 close(tooltip->tooltip_fd);
1153 free(tooltip->entry);
1154 free(tooltip);
1155 parent->tooltip = NULL;
1156}
1157
1158static void
1159tooltip_func(struct task *task, uint32_t events)
1160{
1161 struct tooltip *tooltip =
1162 container_of(task, struct tooltip, tooltip_task);
1163 uint64_t exp;
1164
Martin Olsson8df662a2012-07-08 03:03:47 +02001165 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1166 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001167 window_create_tooltip(tooltip);
1168}
1169
1170#define TOOLTIP_TIMEOUT 500
1171static int
1172tooltip_timer_reset(struct tooltip *tooltip)
1173{
1174 struct itimerspec its;
1175
1176 its.it_interval.tv_sec = 0;
1177 its.it_interval.tv_nsec = 0;
1178 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1179 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1180 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1181 fprintf(stderr, "could not set timerfd\n: %m");
1182 return -1;
1183 }
1184
1185 return 0;
1186}
1187
1188int
1189widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1190{
1191 struct tooltip *tooltip = parent->tooltip;
1192
1193 parent->tooltip_count++;
1194 if (tooltip) {
1195 tooltip->x = x;
1196 tooltip->y = y;
1197 tooltip_timer_reset(tooltip);
1198 return 0;
1199 }
1200
1201 /* the handler might be triggered too fast via input device motion, so
1202 * we need this check here to make sure tooltip is fully initialized */
1203 if (parent->tooltip_count > 1)
1204 return 0;
1205
1206 tooltip = malloc(sizeof *tooltip);
1207 if (!tooltip)
1208 return -1;
1209
1210 parent->tooltip = tooltip;
1211 tooltip->parent = parent;
1212 tooltip->widget = NULL;
1213 tooltip->window = NULL;
1214 tooltip->x = x;
1215 tooltip->y = y;
1216 tooltip->entry = strdup(entry);
1217 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1218 if (tooltip->tooltip_fd < 0) {
1219 fprintf(stderr, "could not create timerfd\n: %m");
1220 return -1;
1221 }
1222
1223 tooltip->tooltip_task.run = tooltip_func;
1224 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1225 EPOLLIN, &tooltip->tooltip_task);
1226 tooltip_timer_reset(tooltip);
1227
1228 return 0;
1229}
1230
1231static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001232frame_resize_handler(struct widget *widget,
1233 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001234{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001235 struct frame *frame = data;
1236 struct widget *child = frame->child;
1237 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001238 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001239 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001240 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001241 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001243 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001244
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001245 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001246 decoration_width = (t->width + t->margin) * 2;
1247 decoration_height = t->width +
1248 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001250 allocation.x = t->width + t->margin;
1251 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001252 allocation.width = width - decoration_width;
1253 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001254
Kristian Høgsbergdd263e52012-07-09 22:22:37 -04001255 widget->window->input_region =
1256 wl_compositor_create_region(display->compositor);
1257 wl_region_add(widget->window->input_region,
1258 t->margin, t->margin,
1259 width - 2 * t->margin,
1260 height - 2 * t->margin);
1261
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001262 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001263
1264 wl_list_for_each(button, &frame->buttons_list, link)
1265 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001266 } else {
1267 decoration_width = 0;
1268 decoration_height = 0;
1269
1270 allocation.x = 0;
1271 allocation.y = 0;
1272 allocation.width = width;
1273 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001274 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001275
1276 wl_list_for_each(button, &frame->buttons_list, link)
1277 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001278 }
1279
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001280 widget_set_allocation(child, allocation.x, allocation.y,
1281 allocation.width, allocation.height);
1282
1283 if (child->resize_handler)
1284 child->resize_handler(child,
1285 allocation.width,
1286 allocation.height,
1287 child->user_data);
1288
Scott Moreauf7e498c2012-05-14 11:39:29 -06001289 width = child->allocation.width + decoration_width;
1290 height = child->allocation.height + decoration_height;
1291
Scott Moreauf7e498c2012-05-14 11:39:29 -06001292 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001293
1294 if (child->opaque) {
1295 widget->window->opaque_region =
1296 wl_compositor_create_region(display->compositor);
1297 wl_region_add(widget->window->opaque_region,
1298 opaque_margin, opaque_margin,
1299 widget->allocation.width - 2 * opaque_margin,
1300 widget->allocation.height - 2 * opaque_margin);
1301 }
Martin Minarik1998b152012-05-10 02:04:35 +02001302
1303 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001304 x_r = frame->widget->allocation.width - t->width - t->margin;
1305 x_l = t->width + t->margin;
1306 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001307 wl_list_for_each(button, &frame->buttons_list, link) {
1308 const int button_padding = 4;
1309 w = cairo_image_surface_get_width(button->icon);
1310 h = cairo_image_surface_get_height(button->icon);
1311
1312 if (button->decoration == FRAME_BUTTON_FANCY)
1313 w += 10;
1314
1315 if (button->align == FRAME_BUTTON_LEFT) {
1316 widget_set_allocation(button->widget,
1317 x_l, y , w + 1, h + 1);
1318 x_l += w;
1319 x_l += button_padding;
1320 } else {
1321 x_r -= w;
1322 widget_set_allocation(button->widget,
1323 x_r, y , w + 1, h + 1);
1324 x_r -= button_padding;
1325 }
1326 }
1327}
1328
1329static int
1330frame_button_enter_handler(struct widget *widget,
1331 struct input *input, float x, float y, void *data)
1332{
1333 struct frame_button *frame_button = data;
1334
1335 widget_schedule_redraw(frame_button->widget);
1336 frame_button->state = FRAME_BUTTON_OVER;
1337
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001338 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001339}
1340
1341static void
1342frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1343{
1344 struct frame_button *frame_button = data;
1345
1346 widget_schedule_redraw(frame_button->widget);
1347 frame_button->state = FRAME_BUTTON_DEFAULT;
1348}
1349
1350static void
1351frame_button_button_handler(struct widget *widget,
1352 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001353 uint32_t button,
1354 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001355{
1356 struct frame_button *frame_button = data;
1357 struct window *window = widget->window;
1358
1359 if (button != BTN_LEFT)
1360 return;
1361
1362 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001363 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001364 frame_button->state = FRAME_BUTTON_ACTIVE;
1365 widget_schedule_redraw(frame_button->widget);
1366
1367 if (frame_button->type == FRAME_BUTTON_ICON)
1368 window_show_frame_menu(window, input, time);
1369 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001370 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001371 frame_button->state = FRAME_BUTTON_DEFAULT;
1372 widget_schedule_redraw(frame_button->widget);
1373 break;
1374 }
1375
1376 switch (frame_button->type) {
1377 case FRAME_BUTTON_CLOSE:
1378 if (window->close_handler)
1379 window->close_handler(window->parent,
1380 window->user_data);
1381 else
1382 display_exit(window->display);
1383 break;
1384 case FRAME_BUTTON_MINIMIZE:
1385 fprintf(stderr,"Minimize stub\n");
1386 break;
1387 case FRAME_BUTTON_MAXIMIZE:
1388 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1389 break;
1390 default:
1391 /* Unknown operation */
1392 break;
1393 }
1394}
1395
1396static void
1397frame_button_redraw_handler(struct widget *widget, void *data)
1398{
1399 struct frame_button *frame_button = data;
1400 cairo_t *cr;
1401 int width, height, x, y;
1402 struct window *window = widget->window;
1403
1404 x = widget->allocation.x;
1405 y = widget->allocation.y;
1406 width = widget->allocation.width;
1407 height = widget->allocation.height;
1408
1409 if (!width)
1410 return;
1411 if (!height)
1412 return;
1413 if (widget->opaque)
1414 return;
1415
1416 cr = cairo_create(window->cairo_surface);
1417
1418 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1419 cairo_set_line_width(cr, 1);
1420
1421 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1422 cairo_rectangle (cr, x, y, 25, 16);
1423
1424 cairo_stroke_preserve(cr);
1425
1426 switch (frame_button->state) {
1427 case FRAME_BUTTON_DEFAULT:
1428 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1429 break;
1430 case FRAME_BUTTON_OVER:
1431 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1432 break;
1433 case FRAME_BUTTON_ACTIVE:
1434 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1435 break;
1436 }
1437
1438 cairo_fill (cr);
1439
1440 x += 4;
1441 }
1442
1443 cairo_set_source_surface(cr, frame_button->icon, x, y);
1444 cairo_paint(cr);
1445
1446 cairo_destroy(cr);
1447}
1448
1449static struct widget *
1450frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1451 enum frame_button_align align, enum frame_button_decoration style)
1452{
1453 struct frame_button *frame_button;
1454 const char *icon = data;
1455
1456 frame_button = malloc (sizeof *frame_button);
1457 memset(frame_button, 0, sizeof *frame_button);
1458
1459 frame_button->icon = cairo_image_surface_create_from_png(icon);
1460 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1461 frame_button->frame = frame;
1462 frame_button->type = type;
1463 frame_button->align = align;
1464 frame_button->decoration = style;
1465
1466 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1467
1468 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1469 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1470 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1471 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1472 return frame_button->widget;
1473}
1474
1475static void
1476frame_button_destroy(struct frame_button *frame_button)
1477{
1478 widget_destroy(frame_button->widget);
1479 wl_list_remove(&frame_button->link);
1480 cairo_surface_destroy(frame_button->icon);
1481 free(frame_button);
1482
1483 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001484}
1485
1486static void
1487frame_redraw_handler(struct widget *widget, void *data)
1488{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001489 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001490 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001491 struct theme *t = window->display->theme;
1492 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001493
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001494 if (window->type == TYPE_FULLSCREEN)
1495 return;
1496
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001497 cr = cairo_create(window->cairo_surface);
1498
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001500 flags |= THEME_FRAME_ACTIVE;
1501 theme_render_frame(t, cr, widget->allocation.width,
1502 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001503
1504 cairo_destroy(cr);
1505}
1506
1507static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001508frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001509{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001510 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001511 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001512
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001513 location = theme_get_location(t, input->sx, input->sy,
1514 frame->widget->allocation.width,
1515 frame->widget->allocation.height);
1516
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001517 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001518 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001519 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001520 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001521 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001522 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001523 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001525 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001527 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001531 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001532 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001533 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001534 case THEME_LOCATION_EXTERIOR:
1535 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001536 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001537 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001538 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001539}
1540
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001541static void
1542frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001543{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001544 switch (index) {
1545 case 0: /* close */
1546 if (window->close_handler)
1547 window->close_handler(window->parent,
1548 window->user_data);
1549 else
1550 display_exit(window->display);
1551 break;
1552 case 1: /* fullscreen */
1553 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001554 if (window->fullscreen_handler)
1555 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001556 break;
1557 case 2: /* rotate */
1558 case 3: /* scale */
1559 break;
1560 }
1561}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001562
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001563void
1564window_show_frame_menu(struct window *window,
1565 struct input *input, uint32_t time)
1566{
1567 int32_t x, y;
1568
1569 static const char *entries[] = {
1570 "Close", "Fullscreen", "Rotate", "Scale"
1571 };
1572
1573 input_get_position(input, &x, &y);
1574 window_show_menu(window->display, input, time, window,
1575 x - 10, y - 10, frame_menu_func, entries, 4);
1576}
1577
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001578static int
1579frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001580 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001581{
1582 return frame_get_pointer_image_for_location(data, input);
1583}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001584
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001585static int
1586frame_motion_handler(struct widget *widget,
1587 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001588 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001589{
1590 return frame_get_pointer_image_for_location(data, input);
1591}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001592
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001594frame_button_handler(struct widget *widget,
1595 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001596 uint32_t button, enum wl_pointer_button_state state,
1597 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001598
1599{
1600 struct frame *frame = data;
1601 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001602 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001604
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001605 location = theme_get_location(display->theme, input->sx, input->sy,
1606 frame->widget->allocation.width,
1607 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001608
Daniel Stone4dbadb12012-05-30 16:31:51 +01001609 if (window->display->shell && button == BTN_LEFT &&
1610 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001611 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001612 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001613 if (!window->shell_surface)
1614 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001615 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001616 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001617 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001618 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001619 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001620 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001621 case THEME_LOCATION_RESIZING_TOP:
1622 case THEME_LOCATION_RESIZING_BOTTOM:
1623 case THEME_LOCATION_RESIZING_LEFT:
1624 case THEME_LOCATION_RESIZING_RIGHT:
1625 case THEME_LOCATION_RESIZING_TOP_LEFT:
1626 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1627 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1628 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001629 if (!window->shell_surface)
1630 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001631 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001632
1633 if (!display->dpy) {
1634 /* If we're using shm, allocate a big
1635 pool to create buffers out of while
1636 we resize. We should probably base
1637 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001638 window->pool =
1639 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001640 }
1641
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001643 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001644 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001645 break;
1646 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001647 } else if (button == BTN_RIGHT &&
1648 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001649 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001650 }
1651}
1652
1653struct widget *
1654frame_create(struct window *window, void *data)
1655{
1656 struct frame *frame;
1657
1658 frame = malloc(sizeof *frame);
1659 memset(frame, 0, sizeof *frame);
1660
1661 frame->widget = window_add_widget(window, frame);
1662 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001663
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001664 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1665 widget_set_resize_handler(frame->widget, frame_resize_handler);
1666 widget_set_enter_handler(frame->widget, frame_enter_handler);
1667 widget_set_motion_handler(frame->widget, frame_motion_handler);
1668 widget_set_button_handler(frame->widget, frame_button_handler);
1669
Martin Minarik1998b152012-05-10 02:04:35 +02001670 /* Create empty list for frame buttons */
1671 wl_list_init(&frame->buttons_list);
1672
1673 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1674 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1675
1676 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1677 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1678
1679 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1680 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1681
1682 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1683 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1684
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001685 window->frame = frame;
1686
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001687 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001688}
1689
Kristian Høgsberga1627922012-06-20 17:30:03 -04001690void
1691frame_set_child_size(struct widget *widget, int child_width, int child_height)
1692{
1693 struct display *display = widget->window->display;
1694 struct theme *t = display->theme;
1695 int decoration_width, decoration_height;
1696 int width, height;
1697
1698 if (widget->window->type != TYPE_FULLSCREEN) {
1699 decoration_width = (t->width + t->margin) * 2;
1700 decoration_height = t->width +
1701 t->titlebar_height + t->margin * 2;
1702
1703 width = child_width + decoration_width;
1704 height = child_height + decoration_height;
1705 } else {
1706 width = child_width;
1707 height = child_height;
1708 }
1709
1710 window_schedule_resize(widget->window, width, height);
1711}
1712
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001713static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001714frame_destroy(struct frame *frame)
1715{
Martin Minarik1998b152012-05-10 02:04:35 +02001716 struct frame_button *button, *tmp;
1717
1718 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1719 frame_button_destroy(button);
1720
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001721 /* frame->child must be destroyed by the application */
1722 widget_destroy(frame->widget);
1723 free(frame);
1724}
1725
1726static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001727input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001728 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001729{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001730 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001731 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001732
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001733 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001734 return;
1735
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001736 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001737 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001738 widget = old;
1739 if (input->grab)
1740 widget = input->grab;
1741 if (widget->leave_handler)
1742 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001743 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001744 }
1745
1746 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001747 widget = focus;
1748 if (input->grab)
1749 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001750 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001751 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001752 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001753 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001754
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001755 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001756 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001757}
1758
1759static void
Daniel Stone37816df2012-05-16 18:45:18 +01001760pointer_handle_motion(void *data, struct wl_pointer *pointer,
1761 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001762{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001763 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001764 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001765 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001766 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001767 float sx = wl_fixed_to_double(sx_w);
1768 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001769
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001770 input->sx = sx;
1771 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001772
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001773 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001774 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001775 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001776 }
1777
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001778 if (input->grab)
1779 widget = input->grab;
1780 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001781 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001782 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001783 cursor = widget->motion_handler(input->focus_widget,
1784 input, time, sx, sy,
1785 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001786
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001787 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001788}
1789
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001790void
1791input_grab(struct input *input, struct widget *widget, uint32_t button)
1792{
1793 input->grab = widget;
1794 input->grab_button = button;
1795}
1796
1797void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001798input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001799{
1800 struct widget *widget;
1801
1802 input->grab = NULL;
1803 if (input->pointer_focus) {
1804 widget = widget_find_widget(input->pointer_focus->widget,
1805 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001806 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001807 }
1808}
1809
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001810static void
Daniel Stone37816df2012-05-16 18:45:18 +01001811pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001812 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001813{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001814 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001815 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001816 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001817
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001818 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001819 if (input->focus_widget && input->grab == NULL &&
1820 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001821 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001822
Neil Roberts6b28aad2012-01-23 19:11:18 +00001823 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001824 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001825 (*widget->button_handler)(widget,
1826 input, time,
1827 button, state,
1828 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001829
Daniel Stone4dbadb12012-05-30 16:31:51 +01001830 if (input->grab && input->grab_button == button &&
1831 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001832 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001833}
1834
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001835static void
Daniel Stone37816df2012-05-16 18:45:18 +01001836pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001837 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001838{
1839}
1840
1841static void
Daniel Stone37816df2012-05-16 18:45:18 +01001842keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001843 uint32_t serial, uint32_t time, uint32_t key,
1844 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001845{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001846 struct input *input = data;
1847 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001848 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001849 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001850 const xkb_keysym_t *syms;
1851 xkb_keysym_t sym;
1852 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001853 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001854
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001855 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001856 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001857 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001858 return;
1859
Daniel Stone97f68542012-05-30 16:32:01 +01001860 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001861
Daniel Stone97f68542012-05-30 16:32:01 +01001862 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001863 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001864 XKB_STATE_LATCHED);
1865 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001866 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001867 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001868 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001869 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001870 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001871 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001872
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001873 sym = XKB_KEY_NoSymbol;
1874 if (num_syms == 1)
1875 sym = syms[0];
1876
1877 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001878 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001879 window_set_maximized(window,
1880 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001881 } else if (sym == XKB_KEY_F11 &&
1882 window->fullscreen_handler &&
1883 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1884 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04001885 } else if (sym == XKB_KEY_F4 &&
1886 input->modifiers == MOD_ALT_MASK &&
1887 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1888 if (window->close_handler)
1889 window->close_handler(window->parent,
1890 window->user_data);
1891 else
1892 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001893 } else if (window->key_handler) {
1894 (*window->key_handler)(window, input, time, key,
1895 sym, state, window->user_data);
1896 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001897
1898 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
1899 key == input->repeat_key) {
1900 its.it_interval.tv_sec = 0;
1901 its.it_interval.tv_nsec = 0;
1902 its.it_value.tv_sec = 0;
1903 its.it_value.tv_nsec = 0;
1904 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1905 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1906 input->repeat_sym = sym;
1907 input->repeat_key = key;
1908 input->repeat_time = time;
1909 its.it_interval.tv_sec = 0;
1910 its.it_interval.tv_nsec = 25 * 1000 * 1000;
1911 its.it_value.tv_sec = 0;
1912 its.it_value.tv_nsec = 400 * 1000 * 1000;
1913 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1914 }
1915}
1916
1917static void
1918keyboard_repeat_func(struct task *task, uint32_t events)
1919{
1920 struct input *input =
1921 container_of(task, struct input, repeat_task);
1922 struct window *window = input->keyboard_focus;
1923 uint64_t exp;
1924
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04001925 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1926 /* If we change the timer between the fd becoming
1927 * readable and getting here, there'll be nothing to
1928 * read and we get EAGAIN. */
1929 return;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001930
Kristian Høgsbergbd0cd762012-06-20 15:17:18 -04001931 if (window && window->key_handler) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001932 (*window->key_handler)(window, input, input->repeat_time,
1933 input->repeat_key, input->repeat_sym,
1934 WL_KEYBOARD_KEY_STATE_PRESSED,
1935 window->user_data);
1936 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001937}
1938
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001939static void
Daniel Stone351eb612012-05-31 15:27:47 -04001940keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1941 uint32_t serial, uint32_t mods_depressed,
1942 uint32_t mods_latched, uint32_t mods_locked,
1943 uint32_t group)
1944{
1945 struct input *input = data;
1946
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001947 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1948 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001949}
1950
1951static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001952input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001953{
1954 struct window *window = input->pointer_focus;
1955
1956 if (!window)
1957 return;
1958
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001959 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001960
Pekka Paalanene1207c72011-12-16 12:02:09 +02001961 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001962 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001963}
1964
1965static void
Daniel Stone37816df2012-05-16 18:45:18 +01001966pointer_handle_enter(void *data, struct wl_pointer *pointer,
1967 uint32_t serial, struct wl_surface *surface,
1968 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001969{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001970 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001971 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001972 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001973 float sx = wl_fixed_to_double(sx_w);
1974 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001975
Martin Minarik1998b152012-05-10 02:04:35 +02001976 if (!surface) {
1977 /* enter event for a window we've just destroyed */
1978 return;
1979 }
1980
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001981 input->display->serial = serial;
1982 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001983 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001984 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001985
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001986 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001987 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001988 window->pool = NULL;
1989 /* Schedule a redraw to free the pool */
1990 window_schedule_redraw(window);
1991 }
1992
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001993 input->sx = sx;
1994 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001995
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001996 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001997 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001998}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001999
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002000static void
Daniel Stone37816df2012-05-16 18:45:18 +01002001pointer_handle_leave(void *data, struct wl_pointer *pointer,
2002 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002003{
2004 struct input *input = data;
2005
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002006 input->display->serial = serial;
2007 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002008}
2009
2010static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002011input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02002012{
2013 struct window *window = input->keyboard_focus;
Kristian Høgsbergd3c69c22012-06-20 22:41:59 -04002014 struct itimerspec its;
2015
2016 its.it_interval.tv_sec = 0;
2017 its.it_interval.tv_nsec = 0;
2018 its.it_value.tv_sec = 0;
2019 its.it_value.tv_nsec = 0;
2020 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002021
2022 if (!window)
2023 return;
2024
2025 window->keyboard_device = NULL;
2026 if (window->keyboard_focus_handler)
2027 (*window->keyboard_focus_handler)(window, NULL,
2028 window->user_data);
2029
2030 input->keyboard_focus = NULL;
2031}
2032
2033static void
Daniel Stone37816df2012-05-16 18:45:18 +01002034keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2035 uint32_t serial, struct wl_surface *surface,
2036 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002037{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002038 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02002039 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002040
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002041 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002042 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002043
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002044 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002045 window->keyboard_device = input;
2046 if (window->keyboard_focus_handler)
2047 (*window->keyboard_focus_handler)(window,
2048 window->keyboard_device,
2049 window->user_data);
2050}
2051
2052static void
Daniel Stone37816df2012-05-16 18:45:18 +01002053keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2054 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002055{
2056 struct input *input = data;
2057
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002058 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002059 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002060}
2061
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002062static void
2063keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2064 uint32_t format, int fd, uint32_t size)
2065{
2066 struct input *input = data;
2067 char *map_str;
2068
2069 if (!data) {
2070 close(fd);
2071 return;
2072 }
2073
2074 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2075 close(fd);
2076 return;
2077 }
2078
2079 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2080 if (map_str == MAP_FAILED) {
2081 close(fd);
2082 return;
2083 }
2084
2085 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2086 map_str,
2087 XKB_KEYMAP_FORMAT_TEXT_V1,
2088 0);
2089 munmap(map_str, size);
2090 close(fd);
2091
2092 if (!input->xkb.keymap) {
2093 fprintf(stderr, "failed to compile keymap\n");
2094 return;
2095 }
2096
2097 input->xkb.state = xkb_state_new(input->xkb.keymap);
2098 if (!input->xkb.state) {
2099 fprintf(stderr, "failed to create XKB state\n");
2100 xkb_map_unref(input->xkb.keymap);
2101 input->xkb.keymap = NULL;
2102 return;
2103 }
2104
2105 input->xkb.control_mask =
2106 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2107 input->xkb.alt_mask =
2108 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2109 input->xkb.shift_mask =
2110 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2111}
2112
Daniel Stone37816df2012-05-16 18:45:18 +01002113static const struct wl_pointer_listener pointer_listener = {
2114 pointer_handle_enter,
2115 pointer_handle_leave,
2116 pointer_handle_motion,
2117 pointer_handle_button,
2118 pointer_handle_axis,
2119};
2120
2121static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002122 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002123 keyboard_handle_enter,
2124 keyboard_handle_leave,
2125 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002126 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002127};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002128
2129static void
Daniel Stone37816df2012-05-16 18:45:18 +01002130seat_handle_capabilities(void *data, struct wl_seat *seat,
2131 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002132{
Daniel Stone37816df2012-05-16 18:45:18 +01002133 struct input *input = data;
2134
2135 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2136 input->pointer = wl_seat_get_pointer(seat);
2137 wl_pointer_set_user_data(input->pointer, input);
2138 wl_pointer_add_listener(input->pointer, &pointer_listener,
2139 input);
2140 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2141 wl_pointer_destroy(input->pointer);
2142 input->pointer = NULL;
2143 }
2144
2145 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2146 input->keyboard = wl_seat_get_keyboard(seat);
2147 wl_keyboard_set_user_data(input->keyboard, input);
2148 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2149 input);
2150 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2151 wl_keyboard_destroy(input->keyboard);
2152 input->keyboard = NULL;
2153 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002154}
2155
Daniel Stone37816df2012-05-16 18:45:18 +01002156static const struct wl_seat_listener seat_listener = {
2157 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002158};
2159
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002160void
2161input_get_position(struct input *input, int32_t *x, int32_t *y)
2162{
2163 *x = input->sx;
2164 *y = input->sy;
2165}
2166
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002167struct display *
2168input_get_display(struct input *input)
2169{
2170 return input->display;
2171}
2172
Daniel Stone37816df2012-05-16 18:45:18 +01002173struct wl_seat *
2174input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002175{
Daniel Stone37816df2012-05-16 18:45:18 +01002176 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002177}
2178
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002179uint32_t
2180input_get_modifiers(struct input *input)
2181{
2182 return input->modifiers;
2183}
2184
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002185struct widget *
2186input_get_focus_widget(struct input *input)
2187{
2188 return input->focus_widget;
2189}
2190
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002191struct data_offer {
2192 struct wl_data_offer *offer;
2193 struct input *input;
2194 struct wl_array types;
2195 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002196
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002197 struct task io_task;
2198 int fd;
2199 data_func_t func;
2200 int32_t x, y;
2201 void *user_data;
2202};
2203
2204static void
2205data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2206{
2207 struct data_offer *offer = data;
2208 char **p;
2209
2210 p = wl_array_add(&offer->types, sizeof *p);
2211 *p = strdup(type);
2212}
2213
2214static const struct wl_data_offer_listener data_offer_listener = {
2215 data_offer_offer,
2216};
2217
2218static void
2219data_offer_destroy(struct data_offer *offer)
2220{
2221 char **p;
2222
2223 offer->refcount--;
2224 if (offer->refcount == 0) {
2225 wl_data_offer_destroy(offer->offer);
2226 for (p = offer->types.data; *p; p++)
2227 free(*p);
2228 wl_array_release(&offer->types);
2229 free(offer);
2230 }
2231}
2232
2233static void
2234data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002235 struct wl_data_device *data_device,
2236 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002237{
2238 struct data_offer *offer;
2239
2240 offer = malloc(sizeof *offer);
2241
2242 wl_array_init(&offer->types);
2243 offer->refcount = 1;
2244 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002245 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002246 wl_data_offer_add_listener(offer->offer,
2247 &data_offer_listener, offer);
2248}
2249
2250static void
2251data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002252 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002253 wl_fixed_t x_w, wl_fixed_t y_w,
2254 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002255{
2256 struct input *input = data;
2257 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002258 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002259 float x = wl_fixed_to_double(x_w);
2260 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002261 char **p;
2262
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002263 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002264 window = wl_surface_get_user_data(surface);
2265 input->pointer_focus = window;
2266
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002267 if (offer) {
2268 input->drag_offer = wl_data_offer_get_user_data(offer);
2269
2270 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2271 *p = NULL;
2272
2273 types_data = input->drag_offer->types.data;
2274 } else {
2275 input->drag_offer = NULL;
2276 types_data = NULL;
2277 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002278
2279 window = input->pointer_focus;
2280 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002281 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002282 window->user_data);
2283}
2284
2285static void
2286data_device_leave(void *data, struct wl_data_device *data_device)
2287{
2288 struct input *input = data;
2289
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002290 if (input->drag_offer) {
2291 data_offer_destroy(input->drag_offer);
2292 input->drag_offer = NULL;
2293 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002294}
2295
2296static void
2297data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002298 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002299{
2300 struct input *input = data;
2301 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002302 float x = wl_fixed_to_double(x_w);
2303 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002304 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002305
2306 input->sx = x;
2307 input->sy = y;
2308
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002309 if (input->drag_offer)
2310 types_data = input->drag_offer->types.data;
2311 else
2312 types_data = NULL;
2313
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002314 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002315 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002316 window->user_data);
2317}
2318
2319static void
2320data_device_drop(void *data, struct wl_data_device *data_device)
2321{
2322 struct input *input = data;
2323 struct window *window = input->pointer_focus;
2324
2325 if (window->drop_handler)
2326 window->drop_handler(window, input,
2327 input->sx, input->sy, window->user_data);
2328}
2329
2330static void
2331data_device_selection(void *data,
2332 struct wl_data_device *wl_data_device,
2333 struct wl_data_offer *offer)
2334{
2335 struct input *input = data;
2336 char **p;
2337
2338 if (input->selection_offer)
2339 data_offer_destroy(input->selection_offer);
2340
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002341 if (offer) {
2342 input->selection_offer = wl_data_offer_get_user_data(offer);
2343 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2344 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002345 } else {
2346 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002347 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002348}
2349
2350static const struct wl_data_device_listener data_device_listener = {
2351 data_device_data_offer,
2352 data_device_enter,
2353 data_device_leave,
2354 data_device_motion,
2355 data_device_drop,
2356 data_device_selection
2357};
2358
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002359static void
2360input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002361{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002362 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002363 struct wl_cursor *cursor;
2364 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002365
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002366 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002367 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002368 return;
2369
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002370 if (index >= (int) cursor->image_count) {
2371 fprintf(stderr, "cursor index out of range\n");
2372 return;
2373 }
2374
2375 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002376 buffer = wl_cursor_image_get_buffer(image);
2377 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002378 return;
2379
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002380 wl_pointer_set_cursor(input->pointer, input->display->serial,
2381 input->pointer_surface,
2382 image->hotspot_x, image->hotspot_y);
2383 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2384 wl_surface_damage(input->pointer_surface, 0, 0,
2385 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002386}
2387
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002388static const struct wl_callback_listener pointer_surface_listener;
2389
2390static void
2391pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2392 uint32_t time)
2393{
2394 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002395 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002396 int i;
2397
2398 if (callback) {
2399 assert(callback == input->cursor_frame_cb);
2400 wl_callback_destroy(callback);
2401 input->cursor_frame_cb = NULL;
2402 }
2403
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002404 if (input->current_cursor == CURSOR_BLANK) {
2405 wl_pointer_set_cursor(input->pointer, input->display->serial,
2406 NULL, 0, 0);
2407 return;
2408 }
2409
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002410 if (input->current_cursor == CURSOR_UNSET)
2411 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002412 cursor = input->display->cursors[input->current_cursor];
2413 if (!cursor)
2414 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002415
2416 /* FIXME We don't have the current time on the first call so we set
2417 * the animation start to the time of the first frame callback. */
2418 if (time == 0)
2419 input->cursor_anim_start = 0;
2420 else if (input->cursor_anim_start == 0)
2421 input->cursor_anim_start = time;
2422
2423 if (time == 0 || input->cursor_anim_start == 0)
2424 i = 0;
2425 else
2426 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2427
2428 input_set_pointer_image_index(input, i);
2429
2430 if (cursor->image_count == 1)
2431 return;
2432
2433 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2434 wl_callback_add_listener(input->cursor_frame_cb,
2435 &pointer_surface_listener, input);
2436}
2437
2438static const struct wl_callback_listener pointer_surface_listener = {
2439 pointer_surface_frame_callback
2440};
2441
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002442void
2443input_set_pointer_image(struct input *input, int pointer)
2444{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002445 int force = 0;
2446
2447 if (input->pointer_enter_serial > input->cursor_serial)
2448 force = 1;
2449
2450 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002451 return;
2452
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002453 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002454 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002455 if (!input->cursor_frame_cb)
2456 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002457 else if (force) {
2458 /* The current frame callback may be stuck if, for instance,
2459 * the set cursor request was processed by the server after
2460 * this client lost the focus. In this case the cursor surface
2461 * might not be mapped and the frame callback wouldn't ever
2462 * complete. Send a set_cursor and attach to try to map the
2463 * cursor surface again so that the callback will finish */
2464 input_set_pointer_image_index(input, 0);
2465 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002466}
2467
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002468struct wl_data_device *
2469input_get_data_device(struct input *input)
2470{
2471 return input->data_device;
2472}
2473
2474void
2475input_set_selection(struct input *input,
2476 struct wl_data_source *source, uint32_t time)
2477{
2478 wl_data_device_set_selection(input->data_device, source, time);
2479}
2480
2481void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002482input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002483{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002484 wl_data_offer_accept(input->drag_offer->offer,
2485 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002486}
2487
2488static void
2489offer_io_func(struct task *task, uint32_t events)
2490{
2491 struct data_offer *offer =
2492 container_of(task, struct data_offer, io_task);
2493 unsigned int len;
2494 char buffer[4096];
2495
2496 len = read(offer->fd, buffer, sizeof buffer);
2497 offer->func(buffer, len,
2498 offer->x, offer->y, offer->user_data);
2499
2500 if (len == 0) {
2501 close(offer->fd);
2502 data_offer_destroy(offer);
2503 }
2504}
2505
2506static void
2507data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2508 data_func_t func, void *user_data)
2509{
2510 int p[2];
2511
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002512 if (pipe2(p, O_CLOEXEC) == -1)
2513 return;
2514
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002515 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2516 close(p[1]);
2517
2518 offer->io_task.run = offer_io_func;
2519 offer->fd = p[0];
2520 offer->func = func;
2521 offer->refcount++;
2522 offer->user_data = user_data;
2523
2524 display_watch_fd(offer->input->display,
2525 offer->fd, EPOLLIN, &offer->io_task);
2526}
2527
2528void
2529input_receive_drag_data(struct input *input, const char *mime_type,
2530 data_func_t func, void *data)
2531{
2532 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2533 input->drag_offer->x = input->sx;
2534 input->drag_offer->y = input->sy;
2535}
2536
2537int
2538input_receive_selection_data(struct input *input, const char *mime_type,
2539 data_func_t func, void *data)
2540{
2541 char **p;
2542
2543 if (input->selection_offer == NULL)
2544 return -1;
2545
2546 for (p = input->selection_offer->types.data; *p; p++)
2547 if (strcmp(mime_type, *p) == 0)
2548 break;
2549
2550 if (*p == NULL)
2551 return -1;
2552
2553 data_offer_receive_data(input->selection_offer,
2554 mime_type, func, data);
2555 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002556}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002557
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002558int
2559input_receive_selection_data_to_fd(struct input *input,
2560 const char *mime_type, int fd)
2561{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002562 if (input->selection_offer)
2563 wl_data_offer_receive(input->selection_offer->offer,
2564 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002565
2566 return 0;
2567}
2568
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002569void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002570window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002571{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002572 if (!window->shell_surface)
2573 return;
2574
Daniel Stone37816df2012-05-16 18:45:18 +01002575 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002576}
2577
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002578static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002579idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002580{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002581 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002582
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002583 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002584 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002585 widget_set_allocation(widget,
2586 window->pending_allocation.x,
2587 window->pending_allocation.y,
2588 window->pending_allocation.width,
2589 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002590
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002591 if (window->input_region) {
2592 wl_region_destroy(window->input_region);
2593 window->input_region = NULL;
2594 }
2595
2596 if (window->opaque_region) {
2597 wl_region_destroy(window->opaque_region);
2598 window->opaque_region = NULL;
2599 }
2600
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002601 if (widget->resize_handler)
2602 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002603 widget->allocation.width,
2604 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002605 widget->user_data);
2606
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002607 if (window->allocation.width != widget->allocation.width ||
2608 window->allocation.height != widget->allocation.height) {
2609 window->allocation = widget->allocation;
2610 window_schedule_redraw(window);
2611 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002612}
2613
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002614void
2615window_schedule_resize(struct window *window, int width, int height)
2616{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002617 window->pending_allocation.x = 0;
2618 window->pending_allocation.y = 0;
2619 window->pending_allocation.width = width;
2620 window->pending_allocation.height = height;
2621
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002622 if (window->min_allocation.width == 0)
2623 window->min_allocation = window->pending_allocation;
2624 if (window->pending_allocation.width < window->min_allocation.width)
2625 window->pending_allocation.width = window->min_allocation.width;
2626 if (window->pending_allocation.height < window->min_allocation.height)
2627 window->pending_allocation.height = window->min_allocation.height;
2628
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002629 window->resize_needed = 1;
2630 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002631}
2632
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002633void
2634widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2635{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002636 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002637}
2638
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002639static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002640handle_ping(void *data, struct wl_shell_surface *shell_surface,
2641 uint32_t serial)
2642{
2643 wl_shell_surface_pong(shell_surface, serial);
2644}
2645
2646static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002647handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002648 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002649{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002650 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002651
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002652 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002653 return;
2654
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002655 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002656 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002657}
2658
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002659static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002660menu_destroy(struct menu *menu)
2661{
2662 widget_destroy(menu->widget);
2663 window_destroy(menu->window);
2664 free(menu);
2665}
2666
2667static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002668handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2669{
2670 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002671 struct menu *menu = window->widget->user_data;
2672
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002673 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002674 * device. Or just use wl_callback. And this really needs to
2675 * be a window vfunc that the menu can set. And we need the
2676 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002677
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002678 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002679 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002680 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002681}
2682
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002683static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002684 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002685 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002686 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002687};
2688
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002689void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002690window_get_allocation(struct window *window,
2691 struct rectangle *allocation)
2692{
2693 *allocation = window->allocation;
2694}
2695
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002696static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002697widget_redraw(struct widget *widget)
2698{
2699 struct widget *child;
2700
2701 if (widget->redraw_handler)
2702 widget->redraw_handler(widget, widget->user_data);
2703 wl_list_for_each(child, &widget->child_list, link)
2704 widget_redraw(child);
2705}
2706
2707static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002708frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2709{
2710 struct window *window = data;
2711
2712 wl_callback_destroy(callback);
2713 window->redraw_scheduled = 0;
2714 if (window->redraw_needed)
2715 window_schedule_redraw(window);
2716}
2717
2718static const struct wl_callback_listener listener = {
2719 frame_callback
2720};
2721
2722static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002723idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002724{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002725 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002726 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002727
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002728 if (window->resize_needed)
2729 idle_resize(window);
2730
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002731 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002732 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002733 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002734 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002735 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002736
2737 callback = wl_surface_frame(window->surface);
2738 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002739}
2740
2741void
2742window_schedule_redraw(struct window *window)
2743{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002744 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002745 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002746 window->redraw_task.run = idle_redraw;
2747 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002748 window->redraw_scheduled = 1;
2749 }
2750}
2751
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002752void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002753window_set_fullscreen(struct window *window, int fullscreen)
2754{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002755 if (!window->display->shell)
2756 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002757
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002758 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002759 return;
2760
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002761 if (fullscreen) {
2762 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002763 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002764 wl_shell_surface_set_fullscreen(window->shell_surface,
2765 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2766 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002767 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002768 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002769 wl_shell_surface_set_toplevel(window->shell_surface);
2770 window_schedule_resize(window,
2771 window->saved_allocation.width,
2772 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002773 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002774}
2775
2776void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002777window_set_maximized(struct window *window, int maximized)
2778{
2779 if (!window->display->shell)
2780 return;
2781
2782 if ((window->type == TYPE_MAXIMIZED) == maximized)
2783 return;
2784
2785 if (window->type == TYPE_TOPLEVEL) {
2786 window->saved_allocation = window->allocation;
2787 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2788 window->type = TYPE_MAXIMIZED;
2789 } else {
2790 wl_shell_surface_set_toplevel(window->shell_surface);
2791 window->type = TYPE_TOPLEVEL;
2792 window_schedule_resize(window,
2793 window->saved_allocation.width,
2794 window->saved_allocation.height);
2795 }
2796}
2797
2798void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002799window_set_user_data(struct window *window, void *data)
2800{
2801 window->user_data = data;
2802}
2803
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002804void *
2805window_get_user_data(struct window *window)
2806{
2807 return window->user_data;
2808}
2809
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002810void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002811window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002812 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002813{
2814 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002815}
2816
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002817void
2818window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002819 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002820{
2821 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002822}
2823
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002824void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002825window_set_data_handler(struct window *window, window_data_handler_t handler)
2826{
2827 window->data_handler = handler;
2828}
2829
2830void
2831window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2832{
2833 window->drop_handler = handler;
2834}
2835
2836void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002837window_set_close_handler(struct window *window,
2838 window_close_handler_t handler)
2839{
2840 window->close_handler = handler;
2841}
2842
2843void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002844window_set_fullscreen_handler(struct window *window,
2845 window_fullscreen_handler_t handler)
2846{
2847 window->fullscreen_handler = handler;
2848}
2849
2850void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002851window_set_title(struct window *window, const char *title)
2852{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002853 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002854 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002855 if (window->shell_surface)
2856 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002857}
2858
2859const char *
2860window_get_title(struct window *window)
2861{
2862 return window->title;
2863}
2864
2865void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002866window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2867{
2868 struct text_cursor_position *text_cursor_position =
2869 window->display->text_cursor_position;
2870
Scott Moreau9295ce02012-06-01 12:46:10 -06002871 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002872 return;
2873
2874 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002875 window->surface,
2876 wl_fixed_from_int(x),
2877 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002878}
2879
2880void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002881window_damage(struct window *window, int32_t x, int32_t y,
2882 int32_t width, int32_t height)
2883{
2884 wl_surface_damage(window->surface, x, y, width, height);
2885}
2886
Casey Dahlin9074db52012-04-19 22:50:09 -04002887static void
2888surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002889 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002890{
Rob Bradford7507b572012-05-15 17:55:34 +01002891 struct window *window = data;
2892 struct output *output;
2893 struct output *output_found = NULL;
2894 struct window_output *window_output;
2895
2896 wl_list_for_each(output, &window->display->output_list, link) {
2897 if (output->output == wl_output) {
2898 output_found = output;
2899 break;
2900 }
2901 }
2902
2903 if (!output_found)
2904 return;
2905
2906 window_output = malloc (sizeof *window_output);
2907 window_output->output = output_found;
2908
2909 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002910}
2911
2912static void
2913surface_leave(void *data,
2914 struct wl_surface *wl_surface, struct wl_output *output)
2915{
Rob Bradford7507b572012-05-15 17:55:34 +01002916 struct window *window = data;
2917 struct window_output *window_output;
2918 struct window_output *window_output_found = NULL;
2919
2920 wl_list_for_each(window_output, &window->window_output_list, link) {
2921 if (window_output->output->output == output) {
2922 window_output_found = window_output;
2923 break;
2924 }
2925 }
2926
2927 if (window_output_found) {
2928 wl_list_remove(&window_output_found->link);
2929 free(window_output_found);
2930 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002931}
2932
2933static const struct wl_surface_listener surface_listener = {
2934 surface_enter,
2935 surface_leave
2936};
2937
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002938static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002939window_create_internal(struct display *display,
2940 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002941{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002942 struct window *window;
2943
2944 window = malloc(sizeof *window);
2945 if (window == NULL)
2946 return NULL;
2947
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002948 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002949 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002950 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002951 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002952 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002953 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002954 window->shell_surface =
2955 wl_shell_get_shell_surface(display->shell,
2956 window->surface);
2957 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002958 window->allocation.x = 0;
2959 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002960 window->allocation.width = 0;
2961 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002962 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002963 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002964 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002965 window->input_region = NULL;
2966 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002967
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002968 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002969#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002970 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002971#else
2972 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2973#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002974 else
2975 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002976
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002977 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002978 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002979 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002980
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002981 if (window->shell_surface) {
2982 wl_shell_surface_set_user_data(window->shell_surface, window);
2983 wl_shell_surface_add_listener(window->shell_surface,
2984 &shell_surface_listener, window);
2985 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002986
Rob Bradford7507b572012-05-15 17:55:34 +01002987 wl_list_init (&window->window_output_list);
2988
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002989 return window;
2990}
2991
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002992struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002993window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002994{
2995 struct window *window;
2996
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002997 window = window_create_internal(display, NULL, TYPE_NONE);
2998 if (!window)
2999 return NULL;
3000
3001 return window;
3002}
3003
3004struct window *
3005window_create_custom(struct display *display)
3006{
3007 struct window *window;
3008
3009 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003010 if (!window)
3011 return NULL;
3012
3013 return window;
3014}
3015
3016struct window *
3017window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003018 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003019{
3020 struct window *window;
3021
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003022 window = window_create_internal(parent->display,
3023 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003024 if (!window)
3025 return NULL;
3026
3027 window->x = x;
3028 window->y = y;
3029
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003030 if (display->shell)
3031 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003032 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003033 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003034
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003035 return window;
3036}
3037
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003038static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003039menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003040{
3041 int next;
3042
3043 next = (sy - 8) / 20;
3044 if (menu->current != next) {
3045 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003046 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003047 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003048}
3049
3050static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003051menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003052 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003053 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003054{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003055 struct menu *menu = data;
3056
3057 if (widget == menu->widget)
3058 menu_set_item(data, y);
3059
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003060 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003061}
3062
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003063static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003064menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003065 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003066{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003067 struct menu *menu = data;
3068
3069 if (widget == menu->widget)
3070 menu_set_item(data, y);
3071
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003072 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003073}
3074
3075static void
3076menu_leave_handler(struct widget *widget, struct input *input, void *data)
3077{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003078 struct menu *menu = data;
3079
3080 if (widget == menu->widget)
3081 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003082}
3083
3084static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003085menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003086 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003087 uint32_t button, enum wl_pointer_button_state state,
3088 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003089
3090{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003091 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003092
Daniel Stone4dbadb12012-05-30 16:31:51 +01003093 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3094 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003095 /* Either relase after press-drag-release or
3096 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003097 menu->func(menu->window->parent,
3098 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003099 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003100 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003101 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003102}
3103
3104static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003105menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003106{
3107 cairo_t *cr;
3108 const int32_t r = 3, margin = 3;
3109 struct menu *menu = data;
3110 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003111 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003112
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003113 cr = cairo_create(window->cairo_surface);
3114 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3115 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3116 cairo_paint(cr);
3117
3118 width = window->allocation.width;
3119 height = window->allocation.height;
3120 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003121
3122 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003123 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3124 cairo_fill(cr);
3125
3126 for (i = 0; i < menu->count; i++) {
3127 if (i == menu->current) {
3128 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3129 cairo_rectangle(cr, margin, i * 20 + margin,
3130 width - 2 * margin, 20);
3131 cairo_fill(cr);
3132 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3133 cairo_move_to(cr, 10, i * 20 + 16);
3134 cairo_show_text(cr, menu->entries[i]);
3135 } else {
3136 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3137 cairo_move_to(cr, 10, i * 20 + 16);
3138 cairo_show_text(cr, menu->entries[i]);
3139 }
3140 }
3141
3142 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003143}
3144
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003145void
3146window_show_menu(struct display *display,
3147 struct input *input, uint32_t time, struct window *parent,
3148 int32_t x, int32_t y,
3149 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003150{
3151 struct window *window;
3152 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003153 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003154
3155 menu = malloc(sizeof *menu);
3156 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003157 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003158
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003159 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003160 if (!window) {
3161 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003162 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003163 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003164
3165 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003166 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003167 menu->entries = entries;
3168 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003169 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003170 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003171 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003172 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003173 window->type = TYPE_MENU;
3174 window->x = x;
3175 window->y = y;
3176
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003177 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003178 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003179 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003180 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003181 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003182
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003183 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003184 widget_set_enter_handler(menu->widget, menu_enter_handler);
3185 widget_set_leave_handler(menu->widget, menu_leave_handler);
3186 widget_set_motion_handler(menu->widget, menu_motion_handler);
3187 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003188
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003189 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003190 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003191}
3192
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003193void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003194window_set_buffer_type(struct window *window, enum window_buffer_type type)
3195{
3196 window->buffer_type = type;
3197}
3198
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003199
3200static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003201display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003202 struct wl_output *wl_output,
3203 int x, int y,
3204 int physical_width,
3205 int physical_height,
3206 int subpixel,
3207 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003208 const char *model,
3209 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003210{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003211 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003212
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003213 output->allocation.x = x;
3214 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003215}
3216
3217static void
3218display_handle_mode(void *data,
3219 struct wl_output *wl_output,
3220 uint32_t flags,
3221 int width,
3222 int height,
3223 int refresh)
3224{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003225 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003226 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003227
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003228 if (flags & WL_OUTPUT_MODE_CURRENT) {
3229 output->allocation.width = width;
3230 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003231 if (display->output_configure_handler)
3232 (*display->output_configure_handler)(
3233 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003234 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003235}
3236
3237static const struct wl_output_listener output_listener = {
3238 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003239 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003240};
3241
3242static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003243display_add_output(struct display *d, uint32_t id)
3244{
3245 struct output *output;
3246
3247 output = malloc(sizeof *output);
3248 if (output == NULL)
3249 return;
3250
3251 memset(output, 0, sizeof *output);
3252 output->display = d;
3253 output->output =
3254 wl_display_bind(d->display, id, &wl_output_interface);
3255 wl_list_insert(d->output_list.prev, &output->link);
3256
3257 wl_output_add_listener(output->output, &output_listener, output);
3258}
3259
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003260static void
3261output_destroy(struct output *output)
3262{
3263 if (output->destroy_handler)
3264 (*output->destroy_handler)(output, output->user_data);
3265
3266 wl_output_destroy(output->output);
3267 wl_list_remove(&output->link);
3268 free(output);
3269}
3270
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003271void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003272display_set_output_configure_handler(struct display *display,
3273 display_output_handler_t handler)
3274{
3275 struct output *output;
3276
3277 display->output_configure_handler = handler;
3278 if (!handler)
3279 return;
3280
3281 wl_list_for_each(output, &display->output_list, link)
3282 (*display->output_configure_handler)(output,
3283 display->user_data);
3284}
3285
3286void
3287output_set_user_data(struct output *output, void *data)
3288{
3289 output->user_data = data;
3290}
3291
3292void *
3293output_get_user_data(struct output *output)
3294{
3295 return output->user_data;
3296}
3297
3298void
3299output_set_destroy_handler(struct output *output,
3300 display_output_handler_t handler)
3301{
3302 output->destroy_handler = handler;
3303 /* FIXME: implement this, once we have way to remove outputs */
3304}
3305
3306void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003307output_get_allocation(struct output *output, struct rectangle *allocation)
3308{
3309 *allocation = output->allocation;
3310}
3311
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003312struct wl_output *
3313output_get_wl_output(struct output *output)
3314{
3315 return output->output;
3316}
3317
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003318static void
Daniel Stone97f68542012-05-30 16:32:01 +01003319fini_xkb(struct input *input)
3320{
3321 xkb_state_unref(input->xkb.state);
3322 xkb_map_unref(input->xkb.keymap);
3323}
3324
3325static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003326display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003327{
3328 struct input *input;
3329
3330 input = malloc(sizeof *input);
3331 if (input == NULL)
3332 return;
3333
3334 memset(input, 0, sizeof *input);
3335 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003336 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003337 input->pointer_focus = NULL;
3338 input->keyboard_focus = NULL;
3339 wl_list_insert(d->input_list.prev, &input->link);
3340
Daniel Stone37816df2012-05-16 18:45:18 +01003341 wl_seat_add_listener(input->seat, &seat_listener, input);
3342 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003343
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003344 input->data_device =
3345 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003346 input->seat);
3347 wl_data_device_add_listener(input->data_device, &data_device_listener,
3348 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003349
3350 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003351
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003352 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3353 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003354 input->repeat_task.run = keyboard_repeat_func;
3355 display_watch_fd(d, input->repeat_timer_fd,
3356 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003357}
3358
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003359static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003360input_destroy(struct input *input)
3361{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003362 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003363 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003364
3365 if (input->drag_offer)
3366 data_offer_destroy(input->drag_offer);
3367
3368 if (input->selection_offer)
3369 data_offer_destroy(input->selection_offer);
3370
3371 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003372 fini_xkb(input);
3373
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003374 wl_surface_destroy(input->pointer_surface);
3375
Pekka Paalanene1207c72011-12-16 12:02:09 +02003376 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003377 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003378 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003379 free(input);
3380}
3381
3382static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003383display_handle_global(struct wl_display *display, uint32_t id,
3384 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003385{
3386 struct display *d = data;
3387
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003388 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003389 d->compositor =
3390 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003391 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003392 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003393 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003394 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003395 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003396 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003397 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003398 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003399 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3400 d->data_device_manager =
3401 wl_display_bind(display, id,
3402 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003403 } else if (strcmp(interface, "text_cursor_position") == 0) {
3404 d->text_cursor_position =
3405 wl_display_bind(display, id,
3406 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003407 }
3408}
3409
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003410#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003411static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003412init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003413{
3414 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003415 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003416
Rob Clark6396ed32012-03-11 19:48:41 -05003417#ifdef USE_CAIRO_GLESV2
3418# define GL_BIT EGL_OPENGL_ES2_BIT
3419#else
3420# define GL_BIT EGL_OPENGL_BIT
3421#endif
3422
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003423 static const EGLint argb_cfg_attribs[] = {
3424 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003425 EGL_RED_SIZE, 1,
3426 EGL_GREEN_SIZE, 1,
3427 EGL_BLUE_SIZE, 1,
3428 EGL_ALPHA_SIZE, 1,
3429 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003430 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003431 EGL_NONE
3432 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003433
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003434#ifdef USE_CAIRO_GLESV2
3435 static const EGLint context_attribs[] = {
3436 EGL_CONTEXT_CLIENT_VERSION, 2,
3437 EGL_NONE
3438 };
3439 EGLint api = EGL_OPENGL_ES_API;
3440#else
3441 EGLint *context_attribs = NULL;
3442 EGLint api = EGL_OPENGL_API;
3443#endif
3444
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003445 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003446 if (!eglInitialize(d->dpy, &major, &minor)) {
3447 fprintf(stderr, "failed to initialize display\n");
3448 return -1;
3449 }
3450
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003451 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003452 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3453 return -1;
3454 }
3455
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003456 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3457 &d->argb_config, 1, &n) || n != 1) {
3458 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003459 return -1;
3460 }
3461
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003462 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003463 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003464 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003465 fprintf(stderr, "failed to create context\n");
3466 return -1;
3467 }
3468
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003469 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003470 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003471 return -1;
3472 }
3473
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003474#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003475 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3476 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3477 fprintf(stderr, "failed to get cairo egl argb device\n");
3478 return -1;
3479 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003480#endif
3481
3482 return 0;
3483}
3484
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003485static void
3486fini_egl(struct display *display)
3487{
3488#ifdef HAVE_CAIRO_EGL
3489 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003490#endif
3491
3492 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3493 EGL_NO_CONTEXT);
3494
3495 eglTerminate(display->dpy);
3496 eglReleaseThread();
3497}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003498#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003499
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003500static int
3501event_mask_update(uint32_t mask, void *data)
3502{
3503 struct display *d = data;
3504
3505 d->mask = mask;
3506
3507 return 0;
3508}
3509
3510static void
3511handle_display_data(struct task *task, uint32_t events)
3512{
3513 struct display *display =
3514 container_of(task, struct display, display_task);
3515
3516 wl_display_iterate(display->display, display->mask);
3517}
3518
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003519struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003520display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003521{
3522 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003523
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003524 d = malloc(sizeof *d);
3525 if (d == NULL)
3526 return NULL;
3527
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003528 memset(d, 0, sizeof *d);
3529
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003530 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003531 if (d->display == NULL) {
3532 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003533 return NULL;
3534 }
3535
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003536 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003537 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3538 d->display_task.run = handle_display_data;
3539 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3540
3541 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003542 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003543 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003544
Daniel Stone97f68542012-05-30 16:32:01 +01003545 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003546 if (d->xkb_context == NULL) {
3547 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003548 return NULL;
3549 }
3550
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003551 /* Set up listener so we'll catch all events. */
3552 wl_display_add_global_listener(d->display,
3553 display_handle_global, d);
3554
3555 /* Process connection events. */
3556 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003557#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003558 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003559 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003560#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003561
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003562 d->image_target_texture_2d =
3563 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3564 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3565 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3566
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003567 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003568
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003569 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003570
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003571 wl_list_init(&d->window_list);
3572
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003573 return d;
3574}
3575
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003576static void
3577display_destroy_outputs(struct display *display)
3578{
3579 struct output *tmp;
3580 struct output *output;
3581
3582 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3583 output_destroy(output);
3584}
3585
Pekka Paalanene1207c72011-12-16 12:02:09 +02003586static void
3587display_destroy_inputs(struct display *display)
3588{
3589 struct input *tmp;
3590 struct input *input;
3591
3592 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3593 input_destroy(input);
3594}
3595
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003596void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003597display_destroy(struct display *display)
3598{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003599 if (!wl_list_empty(&display->window_list))
3600 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3601
3602 if (!wl_list_empty(&display->deferred_list))
3603 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3604
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003605 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003606 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003607
Daniel Stone97f68542012-05-30 16:32:01 +01003608 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003609
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003610 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003611 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003612
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003613#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003614 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003615#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003616
Pekka Paalanenc2052982011-12-16 11:41:32 +02003617 if (display->shell)
3618 wl_shell_destroy(display->shell);
3619
3620 if (display->shm)
3621 wl_shm_destroy(display->shm);
3622
3623 if (display->data_device_manager)
3624 wl_data_device_manager_destroy(display->data_device_manager);
3625
3626 wl_compositor_destroy(display->compositor);
3627
3628 close(display->epoll_fd);
3629
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003630 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003631 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003632 free(display);
3633}
3634
3635void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003636display_set_user_data(struct display *display, void *data)
3637{
3638 display->user_data = data;
3639}
3640
3641void *
3642display_get_user_data(struct display *display)
3643{
3644 return display->user_data;
3645}
3646
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003647struct wl_display *
3648display_get_display(struct display *display)
3649{
3650 return display->display;
3651}
3652
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003653struct output *
3654display_get_output(struct display *display)
3655{
3656 return container_of(display->output_list.next, struct output, link);
3657}
3658
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003659struct wl_compositor *
3660display_get_compositor(struct display *display)
3661{
3662 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003663}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003664
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003665uint32_t
3666display_get_serial(struct display *display)
3667{
3668 return display->serial;
3669}
3670
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003671EGLDisplay
3672display_get_egl_display(struct display *d)
3673{
3674 return d->dpy;
3675}
3676
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003677struct wl_data_source *
3678display_create_data_source(struct display *display)
3679{
3680 return wl_data_device_manager_create_data_source(display->data_device_manager);
3681}
3682
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003683EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003684display_get_argb_egl_config(struct display *d)
3685{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003686 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003687}
3688
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003689struct wl_shell *
3690display_get_shell(struct display *display)
3691{
3692 return display->shell;
3693}
3694
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003695int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003696display_acquire_window_surface(struct display *display,
3697 struct window *window,
3698 EGLContext ctx)
3699{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003700#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003701 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003702 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003703
3704 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003705 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003706 device = cairo_surface_get_device(window->cairo_surface);
3707 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003708 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003709
Benjamin Franzke0c991632011-09-27 21:57:31 +02003710 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003711 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003712 ctx = display->argb_ctx;
3713 else
3714 assert(0);
3715 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003716
3717 data = cairo_surface_get_user_data(window->cairo_surface,
3718 &surface_data_key);
3719
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003720 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003721 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003722 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3723 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003724
3725 return 0;
3726#else
3727 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003728#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003729}
3730
3731void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003732display_release_window_surface(struct display *display,
3733 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003734{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003735#ifdef HAVE_CAIRO_EGL
3736 cairo_device_t *device;
3737
3738 device = cairo_surface_get_device(window->cairo_surface);
3739 if (!device)
3740 return;
3741
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003742 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003743 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003744 cairo_device_release(device);
3745#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003746}
3747
3748void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003749display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003750{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003751 wl_list_insert(&display->deferred_list, &task->link);
3752}
3753
3754void
3755display_watch_fd(struct display *display,
3756 int fd, uint32_t events, struct task *task)
3757{
3758 struct epoll_event ep;
3759
3760 ep.events = events;
3761 ep.data.ptr = task;
3762 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3763}
3764
3765void
3766display_run(struct display *display)
3767{
3768 struct task *task;
3769 struct epoll_event ep[16];
3770 int i, count;
3771
Pekka Paalanen826d7952011-12-15 10:14:07 +02003772 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003773 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003774 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003775
3776 while (!wl_list_empty(&display->deferred_list)) {
3777 task = container_of(display->deferred_list.next,
3778 struct task, link);
3779 wl_list_remove(&task->link);
3780 task->run(task, 0);
3781 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003782
3783 if (!display->running)
3784 break;
3785
3786 wl_display_flush(display->display);
3787
3788 count = epoll_wait(display->epoll_fd,
3789 ep, ARRAY_LENGTH(ep), -1);
3790 for (i = 0; i < count; i++) {
3791 task = ep[i].data.ptr;
3792 task->run(task, ep[i].events);
3793 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003794 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003795}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003796
3797void
3798display_exit(struct display *display)
3799{
3800 display->running = 0;
3801}