blob: 7c309554434de742316c6d6a6e16b7badee319f9 [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
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300655 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300656 display->cursors[i] =
657 wl_cursor_theme_get_cursor(display->cursor_theme,
658 cursors[i]);
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300659 if (!display->cursors[i])
660 fprintf(stderr, "could not load cursor '%s'\n",
661 cursors[i]);
662 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300663}
664
665static void
666destroy_cursors(struct display *display)
667{
668 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800669 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300670}
671
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300672struct wl_cursor_image *
673display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300675 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300676 wl_cursor_theme_get_cursor(display->cursor_theme,
677 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400678
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300679 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400680}
681
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400682static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200683window_get_resize_dx_dy(struct window *window, int *x, int *y)
684{
685 if (window->resize_edges & WINDOW_RESIZING_LEFT)
686 *x = window->server_allocation.width - window->allocation.width;
687 else
688 *x = 0;
689
690 if (window->resize_edges & WINDOW_RESIZING_TOP)
691 *y = window->server_allocation.height -
692 window->allocation.height;
693 else
694 *y = 0;
695
696 window->resize_edges = 0;
697}
698
699static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500700window_attach_surface(struct window *window)
701{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400702 struct display *display = window->display;
703 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000704#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100705 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000706#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500707 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100708
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400709 if (window->type == TYPE_NONE) {
710 window->type = TYPE_TOPLEVEL;
711 if (display->shell)
712 wl_shell_surface_set_toplevel(window->shell_surface);
713 }
714
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100715 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000716#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100717 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
718 data = cairo_surface_get_user_data(window->cairo_surface,
719 &surface_data_key);
720
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100721 cairo_gl_surface_swapbuffers(window->cairo_surface);
722 wl_egl_window_get_attached_size(data->window,
723 &window->server_allocation.width,
724 &window->server_allocation.height);
725 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000726#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728 buffer =
729 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400730 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200732 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400734 wl_surface_damage(window->surface, 0, 0,
735 window->allocation.width,
736 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100737 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400738 cairo_surface_destroy(window->cairo_surface);
739 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000741 default:
742 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100743 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500744
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500745 if (window->input_region) {
746 wl_surface_set_input_region(window->surface,
747 window->input_region);
748 wl_region_destroy(window->input_region);
749 window->input_region = NULL;
750 }
751
752 if (window->opaque_region) {
753 wl_surface_set_opaque_region(window->surface,
754 window->opaque_region);
755 wl_region_destroy(window->opaque_region);
756 window->opaque_region = NULL;
757 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500758}
759
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500760void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400761window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500762{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400763 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100764 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500765}
766
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400767void
768window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400769{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500770 cairo_surface_reference(surface);
771
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400772 if (window->cairo_surface != NULL)
773 cairo_surface_destroy(window->cairo_surface);
774
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500775 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400776}
777
Benjamin Franzke22d54812011-07-16 19:50:32 +0000778#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100779static void
780window_resize_cairo_window_surface(struct window *window)
781{
782 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200783 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784
785 data = cairo_surface_get_user_data(window->cairo_surface,
786 &surface_data_key);
787
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200788 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 wl_egl_window_resize(data->window,
790 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200791 window->allocation.height,
792 x,y);
793
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794 cairo_gl_surface_set_size(window->cairo_surface,
795 window->allocation.width,
796 window->allocation.height);
797}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000798#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100799
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400800struct display *
801window_get_display(struct window *window)
802{
803 return window->display;
804}
805
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400806void
807window_create_surface(struct window *window)
808{
809 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400810 uint32_t flags = 0;
811
812 if (!window->transparent)
813 flags = SURFACE_OPAQUE;
814
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400815 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500816#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
818 if (window->cairo_surface) {
819 window_resize_cairo_window_surface(window);
820 return;
821 }
822 surface = display_create_surface(window->display,
823 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400824 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826#endif
827 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400828 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400829 &window->allocation,
830 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800832 default:
833 surface = NULL;
834 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400836
837 window_set_surface(window, surface);
838 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400839}
840
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200841static void frame_destroy(struct frame *frame);
842
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400843void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500844window_destroy(struct window *window)
845{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200846 struct display *display = window->display;
847 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100848 struct window_output *window_output;
849 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200850
851 if (window->redraw_scheduled)
852 wl_list_remove(&window->redraw_task.link);
853
854 wl_list_for_each(input, &display->input_list, link) {
855 if (input->pointer_focus == window)
856 input->pointer_focus = NULL;
857 if (input->keyboard_focus == window)
858 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500859 if (input->focus_widget &&
860 input->focus_widget->window == window)
861 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200862 }
863
Rob Bradford7507b572012-05-15 17:55:34 +0100864 wl_list_for_each_safe(window_output, window_output_tmp,
865 &window->window_output_list, link) {
866 free (window_output);
867 }
868
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500869 if (window->input_region)
870 wl_region_destroy(window->input_region);
871 if (window->opaque_region)
872 wl_region_destroy(window->opaque_region);
873
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200874 if (window->frame)
875 frame_destroy(window->frame);
876
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200877 if (window->shell_surface)
878 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500879 wl_surface_destroy(window->surface);
880 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200881
882 if (window->cairo_surface != NULL)
883 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200884
885 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500886 free(window);
887}
888
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500889static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500890widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400891{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500892 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400893
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500894 wl_list_for_each(child, &widget->child_list, link) {
895 target = widget_find_widget(child, x, y);
896 if (target)
897 return target;
898 }
899
900 if (widget->allocation.x <= x &&
901 x < widget->allocation.x + widget->allocation.width &&
902 widget->allocation.y <= y &&
903 y < widget->allocation.y + widget->allocation.height) {
904 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400905 }
906
907 return NULL;
908}
909
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500910static struct widget *
911widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400912{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500913 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400914
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500915 widget = malloc(sizeof *widget);
916 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500917 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500918 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500919 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500920 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500921 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300922 widget->tooltip = NULL;
923 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500924
925 return widget;
926}
927
928struct widget *
929window_add_widget(struct window *window, void *data)
930{
931 window->widget = widget_create(window, data);
932 wl_list_init(&window->widget->link);
933
934 return window->widget;
935}
936
937struct widget *
938widget_add_widget(struct widget *parent, void *data)
939{
940 struct widget *widget;
941
942 widget = widget_create(parent->window, data);
943 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400944
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500945 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400946}
947
948void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500949widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400950{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200951 struct display *display = widget->window->display;
952 struct input *input;
953
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300954 if (widget->tooltip) {
955 free(widget->tooltip);
956 widget->tooltip = NULL;
957 }
958
Pekka Paalanene156fb62012-01-19 13:51:38 +0200959 wl_list_for_each(input, &display->input_list, link) {
960 if (input->focus_widget == widget)
961 input->focus_widget = NULL;
962 }
963
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500964 wl_list_remove(&widget->link);
965 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400966}
967
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400968void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500969widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400970{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500971 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400972}
973
974void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500975widget_set_size(struct widget *widget, int32_t width, int32_t height)
976{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500977 widget->allocation.width = width;
978 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500979}
980
981void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500982widget_set_allocation(struct widget *widget,
983 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400984{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500985 widget->allocation.x = x;
986 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200987 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400988}
989
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500990void
991widget_set_transparent(struct widget *widget, int transparent)
992{
993 widget->opaque = !transparent;
994}
995
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500997widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001000}
1001
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001002void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001003widget_set_resize_handler(struct widget *widget,
1004 widget_resize_handler_t handler)
1005{
1006 widget->resize_handler = handler;
1007}
1008
1009void
1010widget_set_redraw_handler(struct widget *widget,
1011 widget_redraw_handler_t handler)
1012{
1013 widget->redraw_handler = handler;
1014}
1015
1016void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001017widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001018{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001019 widget->enter_handler = handler;
1020}
1021
1022void
1023widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1024{
1025 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001026}
1027
1028void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001029widget_set_motion_handler(struct widget *widget,
1030 widget_motion_handler_t handler)
1031{
1032 widget->motion_handler = handler;
1033}
1034
1035void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001036widget_set_button_handler(struct widget *widget,
1037 widget_button_handler_t handler)
1038{
1039 widget->button_handler = handler;
1040}
1041
1042void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001043widget_schedule_redraw(struct widget *widget)
1044{
1045 window_schedule_redraw(widget->window);
1046}
1047
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001048cairo_surface_t *
1049window_get_surface(struct window *window)
1050{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001051 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001052}
1053
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001054struct wl_surface *
1055window_get_wl_surface(struct window *window)
1056{
1057 return window->surface;
1058}
1059
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001060struct wl_shell_surface *
1061window_get_wl_shell_surface(struct window *window)
1062{
1063 return window->shell_surface;
1064}
1065
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001066static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001067tooltip_redraw_handler(struct widget *widget, void *data)
1068{
1069 cairo_t *cr;
1070 const int32_t r = 3;
1071 struct tooltip *tooltip = data;
1072 int32_t width, height;
1073 struct window *window = widget->window;
1074
1075 cr = cairo_create(window->cairo_surface);
1076 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1077 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1078 cairo_paint(cr);
1079
1080 width = window->allocation.width;
1081 height = window->allocation.height;
1082 rounded_rect(cr, 0, 0, width, height, r);
1083
1084 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1085 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1086 cairo_fill(cr);
1087
1088 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1089 cairo_move_to(cr, 10, 16);
1090 cairo_show_text(cr, tooltip->entry);
1091 cairo_destroy(cr);
1092}
1093
1094static cairo_text_extents_t
1095get_text_extents(struct tooltip *tooltip)
1096{
1097 struct window *window;
1098 cairo_t *cr;
1099 cairo_text_extents_t extents;
1100
1101 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1102 * created yet */
1103 window = tooltip->widget->window->parent;
1104 cr = cairo_create(window->cairo_surface);
1105 cairo_text_extents(cr, tooltip->entry, &extents);
1106 cairo_destroy(cr);
1107
1108 return extents;
1109}
1110
1111static int
1112window_create_tooltip(struct tooltip *tooltip)
1113{
1114 struct widget *parent = tooltip->parent;
1115 struct display *display = parent->window->display;
1116 struct window *window;
1117 const int offset_y = 27;
1118 const int margin = 3;
1119 cairo_text_extents_t extents;
1120
1121 if (tooltip->widget)
1122 return 0;
1123
1124 window = window_create_transient(display, parent->window, tooltip->x,
1125 tooltip->y + offset_y,
1126 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1127 if (!window)
1128 return -1;
1129
1130 tooltip->window = window;
1131 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1132
1133 extents = get_text_extents(tooltip);
1134 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1135 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1136
1137 return 0;
1138}
1139
1140void
1141widget_destroy_tooltip(struct widget *parent)
1142{
1143 struct tooltip *tooltip = parent->tooltip;
1144
1145 parent->tooltip_count = 0;
1146 if (!tooltip)
1147 return;
1148
1149 if (tooltip->widget) {
1150 widget_destroy(tooltip->widget);
1151 window_destroy(tooltip->window);
1152 tooltip->widget = NULL;
1153 tooltip->window = NULL;
1154 }
1155
1156 close(tooltip->tooltip_fd);
1157 free(tooltip->entry);
1158 free(tooltip);
1159 parent->tooltip = NULL;
1160}
1161
1162static void
1163tooltip_func(struct task *task, uint32_t events)
1164{
1165 struct tooltip *tooltip =
1166 container_of(task, struct tooltip, tooltip_task);
1167 uint64_t exp;
1168
Martin Olsson8df662a2012-07-08 03:03:47 +02001169 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1170 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001171 window_create_tooltip(tooltip);
1172}
1173
1174#define TOOLTIP_TIMEOUT 500
1175static int
1176tooltip_timer_reset(struct tooltip *tooltip)
1177{
1178 struct itimerspec its;
1179
1180 its.it_interval.tv_sec = 0;
1181 its.it_interval.tv_nsec = 0;
1182 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1183 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1184 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1185 fprintf(stderr, "could not set timerfd\n: %m");
1186 return -1;
1187 }
1188
1189 return 0;
1190}
1191
1192int
1193widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1194{
1195 struct tooltip *tooltip = parent->tooltip;
1196
1197 parent->tooltip_count++;
1198 if (tooltip) {
1199 tooltip->x = x;
1200 tooltip->y = y;
1201 tooltip_timer_reset(tooltip);
1202 return 0;
1203 }
1204
1205 /* the handler might be triggered too fast via input device motion, so
1206 * we need this check here to make sure tooltip is fully initialized */
1207 if (parent->tooltip_count > 1)
1208 return 0;
1209
1210 tooltip = malloc(sizeof *tooltip);
1211 if (!tooltip)
1212 return -1;
1213
1214 parent->tooltip = tooltip;
1215 tooltip->parent = parent;
1216 tooltip->widget = NULL;
1217 tooltip->window = NULL;
1218 tooltip->x = x;
1219 tooltip->y = y;
1220 tooltip->entry = strdup(entry);
1221 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1222 if (tooltip->tooltip_fd < 0) {
1223 fprintf(stderr, "could not create timerfd\n: %m");
1224 return -1;
1225 }
1226
1227 tooltip->tooltip_task.run = tooltip_func;
1228 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1229 EPOLLIN, &tooltip->tooltip_task);
1230 tooltip_timer_reset(tooltip);
1231
1232 return 0;
1233}
1234
1235static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001236frame_resize_handler(struct widget *widget,
1237 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001238{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001239 struct frame *frame = data;
1240 struct widget *child = frame->child;
1241 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001242 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001243 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001244 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001245 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001246 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001247 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001248
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001249 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001250 decoration_width = (t->width + t->margin) * 2;
1251 decoration_height = t->width +
1252 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001253
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001254 allocation.x = t->width + t->margin;
1255 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001256 allocation.width = width - decoration_width;
1257 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001258
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001259 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001260
1261 wl_list_for_each(button, &frame->buttons_list, link)
1262 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001263 } else {
1264 decoration_width = 0;
1265 decoration_height = 0;
1266
1267 allocation.x = 0;
1268 allocation.y = 0;
1269 allocation.width = width;
1270 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001271 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001272
1273 wl_list_for_each(button, &frame->buttons_list, link)
1274 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001275 }
1276
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001277 widget_set_allocation(child, allocation.x, allocation.y,
1278 allocation.width, allocation.height);
1279
1280 if (child->resize_handler)
1281 child->resize_handler(child,
1282 allocation.width,
1283 allocation.height,
1284 child->user_data);
1285
Scott Moreauf7e498c2012-05-14 11:39:29 -06001286 width = child->allocation.width + decoration_width;
1287 height = child->allocation.height + decoration_height;
1288
Kristian Høgsberg023be102012-07-31 11:59:12 -04001289 if (widget->window->type != TYPE_FULLSCREEN) {
1290 widget->window->input_region =
1291 wl_compositor_create_region(display->compositor);
1292 wl_region_add(widget->window->input_region,
1293 t->margin, t->margin,
1294 width - 2 * t->margin,
1295 height - 2 * t->margin);
1296 }
1297
Scott Moreauf7e498c2012-05-14 11:39:29 -06001298 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001299
1300 if (child->opaque) {
1301 widget->window->opaque_region =
1302 wl_compositor_create_region(display->compositor);
1303 wl_region_add(widget->window->opaque_region,
1304 opaque_margin, opaque_margin,
1305 widget->allocation.width - 2 * opaque_margin,
1306 widget->allocation.height - 2 * opaque_margin);
1307 }
Martin Minarik1998b152012-05-10 02:04:35 +02001308
1309 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001310 x_r = frame->widget->allocation.width - t->width - t->margin;
1311 x_l = t->width + t->margin;
1312 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001313 wl_list_for_each(button, &frame->buttons_list, link) {
1314 const int button_padding = 4;
1315 w = cairo_image_surface_get_width(button->icon);
1316 h = cairo_image_surface_get_height(button->icon);
1317
1318 if (button->decoration == FRAME_BUTTON_FANCY)
1319 w += 10;
1320
1321 if (button->align == FRAME_BUTTON_LEFT) {
1322 widget_set_allocation(button->widget,
1323 x_l, y , w + 1, h + 1);
1324 x_l += w;
1325 x_l += button_padding;
1326 } else {
1327 x_r -= w;
1328 widget_set_allocation(button->widget,
1329 x_r, y , w + 1, h + 1);
1330 x_r -= button_padding;
1331 }
1332 }
1333}
1334
1335static int
1336frame_button_enter_handler(struct widget *widget,
1337 struct input *input, float x, float y, void *data)
1338{
1339 struct frame_button *frame_button = data;
1340
1341 widget_schedule_redraw(frame_button->widget);
1342 frame_button->state = FRAME_BUTTON_OVER;
1343
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001344 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001345}
1346
1347static void
1348frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1349{
1350 struct frame_button *frame_button = data;
1351
1352 widget_schedule_redraw(frame_button->widget);
1353 frame_button->state = FRAME_BUTTON_DEFAULT;
1354}
1355
1356static void
1357frame_button_button_handler(struct widget *widget,
1358 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001359 uint32_t button,
1360 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001361{
1362 struct frame_button *frame_button = data;
1363 struct window *window = widget->window;
1364
1365 if (button != BTN_LEFT)
1366 return;
1367
1368 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001369 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001370 frame_button->state = FRAME_BUTTON_ACTIVE;
1371 widget_schedule_redraw(frame_button->widget);
1372
1373 if (frame_button->type == FRAME_BUTTON_ICON)
1374 window_show_frame_menu(window, input, time);
1375 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001376 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001377 frame_button->state = FRAME_BUTTON_DEFAULT;
1378 widget_schedule_redraw(frame_button->widget);
1379 break;
1380 }
1381
1382 switch (frame_button->type) {
1383 case FRAME_BUTTON_CLOSE:
1384 if (window->close_handler)
1385 window->close_handler(window->parent,
1386 window->user_data);
1387 else
1388 display_exit(window->display);
1389 break;
1390 case FRAME_BUTTON_MINIMIZE:
1391 fprintf(stderr,"Minimize stub\n");
1392 break;
1393 case FRAME_BUTTON_MAXIMIZE:
1394 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1395 break;
1396 default:
1397 /* Unknown operation */
1398 break;
1399 }
1400}
1401
1402static void
1403frame_button_redraw_handler(struct widget *widget, void *data)
1404{
1405 struct frame_button *frame_button = data;
1406 cairo_t *cr;
1407 int width, height, x, y;
1408 struct window *window = widget->window;
1409
1410 x = widget->allocation.x;
1411 y = widget->allocation.y;
1412 width = widget->allocation.width;
1413 height = widget->allocation.height;
1414
1415 if (!width)
1416 return;
1417 if (!height)
1418 return;
1419 if (widget->opaque)
1420 return;
1421
1422 cr = cairo_create(window->cairo_surface);
1423
1424 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1425 cairo_set_line_width(cr, 1);
1426
1427 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1428 cairo_rectangle (cr, x, y, 25, 16);
1429
1430 cairo_stroke_preserve(cr);
1431
1432 switch (frame_button->state) {
1433 case FRAME_BUTTON_DEFAULT:
1434 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1435 break;
1436 case FRAME_BUTTON_OVER:
1437 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1438 break;
1439 case FRAME_BUTTON_ACTIVE:
1440 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1441 break;
1442 }
1443
1444 cairo_fill (cr);
1445
1446 x += 4;
1447 }
1448
1449 cairo_set_source_surface(cr, frame_button->icon, x, y);
1450 cairo_paint(cr);
1451
1452 cairo_destroy(cr);
1453}
1454
1455static struct widget *
1456frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1457 enum frame_button_align align, enum frame_button_decoration style)
1458{
1459 struct frame_button *frame_button;
1460 const char *icon = data;
1461
1462 frame_button = malloc (sizeof *frame_button);
1463 memset(frame_button, 0, sizeof *frame_button);
1464
1465 frame_button->icon = cairo_image_surface_create_from_png(icon);
1466 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1467 frame_button->frame = frame;
1468 frame_button->type = type;
1469 frame_button->align = align;
1470 frame_button->decoration = style;
1471
1472 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1473
1474 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1475 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1476 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1477 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1478 return frame_button->widget;
1479}
1480
1481static void
1482frame_button_destroy(struct frame_button *frame_button)
1483{
1484 widget_destroy(frame_button->widget);
1485 wl_list_remove(&frame_button->link);
1486 cairo_surface_destroy(frame_button->icon);
1487 free(frame_button);
1488
1489 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001490}
1491
1492static void
1493frame_redraw_handler(struct widget *widget, void *data)
1494{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001496 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001497 struct theme *t = window->display->theme;
1498 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001500 if (window->type == TYPE_FULLSCREEN)
1501 return;
1502
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001503 cr = cairo_create(window->cairo_surface);
1504
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001505 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001506 flags |= THEME_FRAME_ACTIVE;
1507 theme_render_frame(t, cr, widget->allocation.width,
1508 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001509
1510 cairo_destroy(cr);
1511}
1512
1513static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001514frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001515{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001516 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001517 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001518
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001519 location = theme_get_location(t, input->sx, input->sy,
1520 frame->widget->allocation.width,
1521 frame->widget->allocation.height);
1522
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001523 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001525 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001527 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001531 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001532 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001533 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001534 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001535 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001536 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001537 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001538 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001539 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001540 case THEME_LOCATION_EXTERIOR:
1541 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001542 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001543 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001544 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001545}
1546
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001547static void
1548frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001549{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001550 switch (index) {
1551 case 0: /* close */
1552 if (window->close_handler)
1553 window->close_handler(window->parent,
1554 window->user_data);
1555 else
1556 display_exit(window->display);
1557 break;
1558 case 1: /* fullscreen */
1559 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001560 if (window->fullscreen_handler)
1561 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001562 break;
1563 case 2: /* rotate */
1564 case 3: /* scale */
1565 break;
1566 }
1567}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001568
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001569void
1570window_show_frame_menu(struct window *window,
1571 struct input *input, uint32_t time)
1572{
1573 int32_t x, y;
1574
1575 static const char *entries[] = {
1576 "Close", "Fullscreen", "Rotate", "Scale"
1577 };
1578
1579 input_get_position(input, &x, &y);
1580 window_show_menu(window->display, input, time, window,
1581 x - 10, y - 10, frame_menu_func, entries, 4);
1582}
1583
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001584static int
1585frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001586 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001587{
1588 return frame_get_pointer_image_for_location(data, input);
1589}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001590
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001591static int
1592frame_motion_handler(struct widget *widget,
1593 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001594 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001595{
1596 return frame_get_pointer_image_for_location(data, input);
1597}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001598
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001599static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001600frame_button_handler(struct widget *widget,
1601 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001602 uint32_t button, enum wl_pointer_button_state state,
1603 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001604
1605{
1606 struct frame *frame = data;
1607 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001608 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001609 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001610
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001611 location = theme_get_location(display->theme, input->sx, input->sy,
1612 frame->widget->allocation.width,
1613 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614
Daniel Stone4dbadb12012-05-30 16:31:51 +01001615 if (window->display->shell && button == BTN_LEFT &&
1616 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001617 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001618 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001619 if (!window->shell_surface)
1620 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001621 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001622 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001623 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001624 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001625 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001626 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001627 case THEME_LOCATION_RESIZING_TOP:
1628 case THEME_LOCATION_RESIZING_BOTTOM:
1629 case THEME_LOCATION_RESIZING_LEFT:
1630 case THEME_LOCATION_RESIZING_RIGHT:
1631 case THEME_LOCATION_RESIZING_TOP_LEFT:
1632 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1633 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1634 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001635 if (!window->shell_surface)
1636 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001637 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001638
1639 if (!display->dpy) {
1640 /* If we're using shm, allocate a big
1641 pool to create buffers out of while
1642 we resize. We should probably base
1643 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001644 window->pool =
1645 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001646 }
1647
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001648 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001649 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001650 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001651 break;
1652 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001653 } else if (button == BTN_RIGHT &&
1654 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001655 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001656 }
1657}
1658
1659struct widget *
1660frame_create(struct window *window, void *data)
1661{
1662 struct frame *frame;
1663
1664 frame = malloc(sizeof *frame);
1665 memset(frame, 0, sizeof *frame);
1666
1667 frame->widget = window_add_widget(window, frame);
1668 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001669
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001670 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1671 widget_set_resize_handler(frame->widget, frame_resize_handler);
1672 widget_set_enter_handler(frame->widget, frame_enter_handler);
1673 widget_set_motion_handler(frame->widget, frame_motion_handler);
1674 widget_set_button_handler(frame->widget, frame_button_handler);
1675
Martin Minarik1998b152012-05-10 02:04:35 +02001676 /* Create empty list for frame buttons */
1677 wl_list_init(&frame->buttons_list);
1678
1679 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1680 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1681
1682 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1683 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1684
1685 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1686 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1687
1688 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1689 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1690
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001691 window->frame = frame;
1692
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001693 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001694}
1695
Kristian Høgsberga1627922012-06-20 17:30:03 -04001696void
1697frame_set_child_size(struct widget *widget, int child_width, int child_height)
1698{
1699 struct display *display = widget->window->display;
1700 struct theme *t = display->theme;
1701 int decoration_width, decoration_height;
1702 int width, height;
1703
1704 if (widget->window->type != TYPE_FULLSCREEN) {
1705 decoration_width = (t->width + t->margin) * 2;
1706 decoration_height = t->width +
1707 t->titlebar_height + t->margin * 2;
1708
1709 width = child_width + decoration_width;
1710 height = child_height + decoration_height;
1711 } else {
1712 width = child_width;
1713 height = child_height;
1714 }
1715
1716 window_schedule_resize(widget->window, width, height);
1717}
1718
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001719static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001720frame_destroy(struct frame *frame)
1721{
Martin Minarik1998b152012-05-10 02:04:35 +02001722 struct frame_button *button, *tmp;
1723
1724 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1725 frame_button_destroy(button);
1726
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001727 /* frame->child must be destroyed by the application */
1728 widget_destroy(frame->widget);
1729 free(frame);
1730}
1731
1732static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001733input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001734 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001735{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001736 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001737 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001738
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001739 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001740 return;
1741
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001742 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001743 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001744 widget = old;
1745 if (input->grab)
1746 widget = input->grab;
1747 if (widget->leave_handler)
1748 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001749 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001750 }
1751
1752 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001753 widget = focus;
1754 if (input->grab)
1755 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001756 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001757 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001758 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001759 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001760
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001761 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001762 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001763}
1764
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001765void
1766input_grab(struct input *input, struct widget *widget, uint32_t button)
1767{
1768 input->grab = widget;
1769 input->grab_button = button;
1770}
1771
1772void
1773input_ungrab(struct input *input)
1774{
1775 struct widget *widget;
1776
1777 input->grab = NULL;
1778 if (input->pointer_focus) {
1779 widget = widget_find_widget(input->pointer_focus->widget,
1780 input->sx, input->sy);
1781 input_set_focus_widget(input, widget, input->sx, input->sy);
1782 }
1783}
1784
1785static void
1786input_remove_pointer_focus(struct input *input)
1787{
1788 struct window *window = input->pointer_focus;
1789
1790 if (!window)
1791 return;
1792
1793 input_set_focus_widget(input, NULL, 0, 0);
1794
1795 input->pointer_focus = NULL;
1796 input->current_cursor = CURSOR_UNSET;
1797}
1798
1799static void
1800pointer_handle_enter(void *data, struct wl_pointer *pointer,
1801 uint32_t serial, struct wl_surface *surface,
1802 wl_fixed_t sx_w, wl_fixed_t sy_w)
1803{
1804 struct input *input = data;
1805 struct window *window;
1806 struct widget *widget;
1807 float sx = wl_fixed_to_double(sx_w);
1808 float sy = wl_fixed_to_double(sy_w);
1809
1810 if (!surface) {
1811 /* enter event for a window we've just destroyed */
1812 return;
1813 }
1814
1815 input->display->serial = serial;
1816 input->pointer_enter_serial = serial;
1817 input->pointer_focus = wl_surface_get_user_data(surface);
1818 window = input->pointer_focus;
1819
1820 if (window->pool) {
1821 shm_pool_destroy(window->pool);
1822 window->pool = NULL;
1823 /* Schedule a redraw to free the pool */
1824 window_schedule_redraw(window);
1825 }
1826
1827 input->sx = sx;
1828 input->sy = sy;
1829
1830 widget = widget_find_widget(window->widget, sx, sy);
1831 input_set_focus_widget(input, widget, sx, sy);
1832}
1833
1834static void
1835pointer_handle_leave(void *data, struct wl_pointer *pointer,
1836 uint32_t serial, struct wl_surface *surface)
1837{
1838 struct input *input = data;
1839
1840 input->display->serial = serial;
1841 input_remove_pointer_focus(input);
1842}
1843
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001844static void
Daniel Stone37816df2012-05-16 18:45:18 +01001845pointer_handle_motion(void *data, struct wl_pointer *pointer,
1846 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001847{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001848 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001849 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001850 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001851 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001852 float sx = wl_fixed_to_double(sx_w);
1853 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001854
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001855 input->sx = sx;
1856 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001857
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001858 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001859 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001860 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001861 }
1862
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001863 if (input->grab)
1864 widget = input->grab;
1865 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001866 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001867 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001868 cursor = widget->motion_handler(input->focus_widget,
1869 input, time, sx, sy,
1870 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001871
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001872 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001873}
1874
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001875static void
Daniel Stone37816df2012-05-16 18:45:18 +01001876pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001877 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001878{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001879 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001880 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001881 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001882
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001883 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001884 if (input->focus_widget && input->grab == NULL &&
1885 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001886 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001887
Neil Roberts6b28aad2012-01-23 19:11:18 +00001888 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001889 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001890 (*widget->button_handler)(widget,
1891 input, time,
1892 button, state,
1893 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001894
Daniel Stone4dbadb12012-05-30 16:31:51 +01001895 if (input->grab && input->grab_button == button &&
1896 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001897 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001898}
1899
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001900static void
Daniel Stone37816df2012-05-16 18:45:18 +01001901pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001902 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001903{
1904}
1905
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001906static const struct wl_pointer_listener pointer_listener = {
1907 pointer_handle_enter,
1908 pointer_handle_leave,
1909 pointer_handle_motion,
1910 pointer_handle_button,
1911 pointer_handle_axis,
1912};
1913
1914static void
1915input_remove_keyboard_focus(struct input *input)
1916{
1917 struct window *window = input->keyboard_focus;
1918 struct itimerspec its;
1919
1920 its.it_interval.tv_sec = 0;
1921 its.it_interval.tv_nsec = 0;
1922 its.it_value.tv_sec = 0;
1923 its.it_value.tv_nsec = 0;
1924 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1925
1926 if (!window)
1927 return;
1928
1929 window->keyboard_device = NULL;
1930 if (window->keyboard_focus_handler)
1931 (*window->keyboard_focus_handler)(window, NULL,
1932 window->user_data);
1933
1934 input->keyboard_focus = NULL;
1935}
1936
1937static void
1938keyboard_repeat_func(struct task *task, uint32_t events)
1939{
1940 struct input *input =
1941 container_of(task, struct input, repeat_task);
1942 struct window *window = input->keyboard_focus;
1943 uint64_t exp;
1944
1945 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1946 /* If we change the timer between the fd becoming
1947 * readable and getting here, there'll be nothing to
1948 * read and we get EAGAIN. */
1949 return;
1950
1951 if (window && window->key_handler) {
1952 (*window->key_handler)(window, input, input->repeat_time,
1953 input->repeat_key, input->repeat_sym,
1954 WL_KEYBOARD_KEY_STATE_PRESSED,
1955 window->user_data);
1956 }
1957}
1958
1959static void
1960keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1961 uint32_t format, int fd, uint32_t size)
1962{
1963 struct input *input = data;
1964 char *map_str;
1965
1966 if (!data) {
1967 close(fd);
1968 return;
1969 }
1970
1971 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1972 close(fd);
1973 return;
1974 }
1975
1976 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1977 if (map_str == MAP_FAILED) {
1978 close(fd);
1979 return;
1980 }
1981
1982 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
1983 map_str,
1984 XKB_KEYMAP_FORMAT_TEXT_V1,
1985 0);
1986 munmap(map_str, size);
1987 close(fd);
1988
1989 if (!input->xkb.keymap) {
1990 fprintf(stderr, "failed to compile keymap\n");
1991 return;
1992 }
1993
1994 input->xkb.state = xkb_state_new(input->xkb.keymap);
1995 if (!input->xkb.state) {
1996 fprintf(stderr, "failed to create XKB state\n");
1997 xkb_map_unref(input->xkb.keymap);
1998 input->xkb.keymap = NULL;
1999 return;
2000 }
2001
2002 input->xkb.control_mask =
2003 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2004 input->xkb.alt_mask =
2005 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2006 input->xkb.shift_mask =
2007 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2008}
2009
2010static void
2011keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2012 uint32_t serial, struct wl_surface *surface,
2013 struct wl_array *keys)
2014{
2015 struct input *input = data;
2016 struct window *window;
2017
2018 input->display->serial = serial;
2019 input->keyboard_focus = wl_surface_get_user_data(surface);
2020
2021 window = input->keyboard_focus;
2022 window->keyboard_device = input;
2023 if (window->keyboard_focus_handler)
2024 (*window->keyboard_focus_handler)(window,
2025 window->keyboard_device,
2026 window->user_data);
2027}
2028
2029static void
2030keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2031 uint32_t serial, struct wl_surface *surface)
2032{
2033 struct input *input = data;
2034
2035 input->display->serial = serial;
2036 input_remove_keyboard_focus(input);
2037}
2038
Scott Moreau210d0792012-03-22 10:47:01 -06002039static void
Daniel Stone37816df2012-05-16 18:45:18 +01002040keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002041 uint32_t serial, uint32_t time, uint32_t key,
2042 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002043{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002044 struct input *input = data;
2045 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002046 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002047 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002048 const xkb_keysym_t *syms;
2049 xkb_keysym_t sym;
2050 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002051 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002052
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002053 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002054 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002055 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002056 return;
2057
Daniel Stone97f68542012-05-30 16:32:01 +01002058 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002059
Daniel Stone97f68542012-05-30 16:32:01 +01002060 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002061 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002062 XKB_STATE_LATCHED);
2063 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002064 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002065 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002066 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002067 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002068 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002069 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002070
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002071 sym = XKB_KEY_NoSymbol;
2072 if (num_syms == 1)
2073 sym = syms[0];
2074
2075 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002076 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002077 window_set_maximized(window,
2078 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002079 } else if (sym == XKB_KEY_F11 &&
2080 window->fullscreen_handler &&
2081 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2082 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002083 } else if (sym == XKB_KEY_F4 &&
2084 input->modifiers == MOD_ALT_MASK &&
2085 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2086 if (window->close_handler)
2087 window->close_handler(window->parent,
2088 window->user_data);
2089 else
2090 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002091 } else if (window->key_handler) {
2092 (*window->key_handler)(window, input, time, key,
2093 sym, state, window->user_data);
2094 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002095
2096 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2097 key == input->repeat_key) {
2098 its.it_interval.tv_sec = 0;
2099 its.it_interval.tv_nsec = 0;
2100 its.it_value.tv_sec = 0;
2101 its.it_value.tv_nsec = 0;
2102 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2103 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2104 input->repeat_sym = sym;
2105 input->repeat_key = key;
2106 input->repeat_time = time;
2107 its.it_interval.tv_sec = 0;
2108 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2109 its.it_value.tv_sec = 0;
2110 its.it_value.tv_nsec = 400 * 1000 * 1000;
2111 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2112 }
2113}
2114
2115static void
Daniel Stone351eb612012-05-31 15:27:47 -04002116keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2117 uint32_t serial, uint32_t mods_depressed,
2118 uint32_t mods_latched, uint32_t mods_locked,
2119 uint32_t group)
2120{
2121 struct input *input = data;
2122
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002123 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2124 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002125}
2126
Daniel Stone37816df2012-05-16 18:45:18 +01002127static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002128 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002129 keyboard_handle_enter,
2130 keyboard_handle_leave,
2131 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002132 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002133};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002134
2135static void
Daniel Stone37816df2012-05-16 18:45:18 +01002136seat_handle_capabilities(void *data, struct wl_seat *seat,
2137 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002138{
Daniel Stone37816df2012-05-16 18:45:18 +01002139 struct input *input = data;
2140
2141 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2142 input->pointer = wl_seat_get_pointer(seat);
2143 wl_pointer_set_user_data(input->pointer, input);
2144 wl_pointer_add_listener(input->pointer, &pointer_listener,
2145 input);
2146 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2147 wl_pointer_destroy(input->pointer);
2148 input->pointer = NULL;
2149 }
2150
2151 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2152 input->keyboard = wl_seat_get_keyboard(seat);
2153 wl_keyboard_set_user_data(input->keyboard, input);
2154 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2155 input);
2156 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2157 wl_keyboard_destroy(input->keyboard);
2158 input->keyboard = NULL;
2159 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002160}
2161
Daniel Stone37816df2012-05-16 18:45:18 +01002162static const struct wl_seat_listener seat_listener = {
2163 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002164};
2165
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002166void
2167input_get_position(struct input *input, int32_t *x, int32_t *y)
2168{
2169 *x = input->sx;
2170 *y = input->sy;
2171}
2172
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002173struct display *
2174input_get_display(struct input *input)
2175{
2176 return input->display;
2177}
2178
Daniel Stone37816df2012-05-16 18:45:18 +01002179struct wl_seat *
2180input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002181{
Daniel Stone37816df2012-05-16 18:45:18 +01002182 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002183}
2184
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002185uint32_t
2186input_get_modifiers(struct input *input)
2187{
2188 return input->modifiers;
2189}
2190
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002191struct widget *
2192input_get_focus_widget(struct input *input)
2193{
2194 return input->focus_widget;
2195}
2196
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002197struct data_offer {
2198 struct wl_data_offer *offer;
2199 struct input *input;
2200 struct wl_array types;
2201 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002202
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002203 struct task io_task;
2204 int fd;
2205 data_func_t func;
2206 int32_t x, y;
2207 void *user_data;
2208};
2209
2210static void
2211data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2212{
2213 struct data_offer *offer = data;
2214 char **p;
2215
2216 p = wl_array_add(&offer->types, sizeof *p);
2217 *p = strdup(type);
2218}
2219
2220static const struct wl_data_offer_listener data_offer_listener = {
2221 data_offer_offer,
2222};
2223
2224static void
2225data_offer_destroy(struct data_offer *offer)
2226{
2227 char **p;
2228
2229 offer->refcount--;
2230 if (offer->refcount == 0) {
2231 wl_data_offer_destroy(offer->offer);
2232 for (p = offer->types.data; *p; p++)
2233 free(*p);
2234 wl_array_release(&offer->types);
2235 free(offer);
2236 }
2237}
2238
2239static void
2240data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002241 struct wl_data_device *data_device,
2242 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002243{
2244 struct data_offer *offer;
2245
2246 offer = malloc(sizeof *offer);
2247
2248 wl_array_init(&offer->types);
2249 offer->refcount = 1;
2250 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002251 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002252 wl_data_offer_add_listener(offer->offer,
2253 &data_offer_listener, offer);
2254}
2255
2256static void
2257data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002258 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002259 wl_fixed_t x_w, wl_fixed_t y_w,
2260 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002261{
2262 struct input *input = data;
2263 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002264 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002265 float x = wl_fixed_to_double(x_w);
2266 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002267 char **p;
2268
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002269 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002270 window = wl_surface_get_user_data(surface);
2271 input->pointer_focus = window;
2272
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002273 if (offer) {
2274 input->drag_offer = wl_data_offer_get_user_data(offer);
2275
2276 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2277 *p = NULL;
2278
2279 types_data = input->drag_offer->types.data;
2280 } else {
2281 input->drag_offer = NULL;
2282 types_data = NULL;
2283 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002284
2285 window = input->pointer_focus;
2286 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002287 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002288 window->user_data);
2289}
2290
2291static void
2292data_device_leave(void *data, struct wl_data_device *data_device)
2293{
2294 struct input *input = data;
2295
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002296 if (input->drag_offer) {
2297 data_offer_destroy(input->drag_offer);
2298 input->drag_offer = NULL;
2299 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002300}
2301
2302static void
2303data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002304 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002305{
2306 struct input *input = data;
2307 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002308 float x = wl_fixed_to_double(x_w);
2309 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002310 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002311
2312 input->sx = x;
2313 input->sy = y;
2314
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002315 if (input->drag_offer)
2316 types_data = input->drag_offer->types.data;
2317 else
2318 types_data = NULL;
2319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002320 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002321 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002322 window->user_data);
2323}
2324
2325static void
2326data_device_drop(void *data, struct wl_data_device *data_device)
2327{
2328 struct input *input = data;
2329 struct window *window = input->pointer_focus;
2330
2331 if (window->drop_handler)
2332 window->drop_handler(window, input,
2333 input->sx, input->sy, window->user_data);
2334}
2335
2336static void
2337data_device_selection(void *data,
2338 struct wl_data_device *wl_data_device,
2339 struct wl_data_offer *offer)
2340{
2341 struct input *input = data;
2342 char **p;
2343
2344 if (input->selection_offer)
2345 data_offer_destroy(input->selection_offer);
2346
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002347 if (offer) {
2348 input->selection_offer = wl_data_offer_get_user_data(offer);
2349 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2350 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002351 } else {
2352 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002353 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002354}
2355
2356static const struct wl_data_device_listener data_device_listener = {
2357 data_device_data_offer,
2358 data_device_enter,
2359 data_device_leave,
2360 data_device_motion,
2361 data_device_drop,
2362 data_device_selection
2363};
2364
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002365static void
2366input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002367{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002368 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002369 struct wl_cursor *cursor;
2370 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002371
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002372 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002373 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002374 return;
2375
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002376 if (index >= (int) cursor->image_count) {
2377 fprintf(stderr, "cursor index out of range\n");
2378 return;
2379 }
2380
2381 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002382 buffer = wl_cursor_image_get_buffer(image);
2383 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002384 return;
2385
Kristian Høgsbergae277372012-08-01 09:41:08 -04002386 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002387 input->pointer_surface,
2388 image->hotspot_x, image->hotspot_y);
2389 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2390 wl_surface_damage(input->pointer_surface, 0, 0,
2391 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002392}
2393
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002394static const struct wl_callback_listener pointer_surface_listener;
2395
2396static void
2397pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2398 uint32_t time)
2399{
2400 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002401 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002402 int i;
2403
2404 if (callback) {
2405 assert(callback == input->cursor_frame_cb);
2406 wl_callback_destroy(callback);
2407 input->cursor_frame_cb = NULL;
2408 }
2409
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002410 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002411 wl_pointer_set_cursor(input->pointer,
2412 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002413 NULL, 0, 0);
2414 return;
2415 }
2416
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002417 if (input->current_cursor == CURSOR_UNSET)
2418 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002419 cursor = input->display->cursors[input->current_cursor];
2420 if (!cursor)
2421 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002422
2423 /* FIXME We don't have the current time on the first call so we set
2424 * the animation start to the time of the first frame callback. */
2425 if (time == 0)
2426 input->cursor_anim_start = 0;
2427 else if (input->cursor_anim_start == 0)
2428 input->cursor_anim_start = time;
2429
2430 if (time == 0 || input->cursor_anim_start == 0)
2431 i = 0;
2432 else
2433 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2434
2435 input_set_pointer_image_index(input, i);
2436
2437 if (cursor->image_count == 1)
2438 return;
2439
2440 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2441 wl_callback_add_listener(input->cursor_frame_cb,
2442 &pointer_surface_listener, input);
2443}
2444
2445static const struct wl_callback_listener pointer_surface_listener = {
2446 pointer_surface_frame_callback
2447};
2448
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002449void
2450input_set_pointer_image(struct input *input, int pointer)
2451{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002452 int force = 0;
2453
2454 if (input->pointer_enter_serial > input->cursor_serial)
2455 force = 1;
2456
2457 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002458 return;
2459
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002460 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002461 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002462 if (!input->cursor_frame_cb)
2463 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002464 else if (force) {
2465 /* The current frame callback may be stuck if, for instance,
2466 * the set cursor request was processed by the server after
2467 * this client lost the focus. In this case the cursor surface
2468 * might not be mapped and the frame callback wouldn't ever
2469 * complete. Send a set_cursor and attach to try to map the
2470 * cursor surface again so that the callback will finish */
2471 input_set_pointer_image_index(input, 0);
2472 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002473}
2474
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002475struct wl_data_device *
2476input_get_data_device(struct input *input)
2477{
2478 return input->data_device;
2479}
2480
2481void
2482input_set_selection(struct input *input,
2483 struct wl_data_source *source, uint32_t time)
2484{
2485 wl_data_device_set_selection(input->data_device, source, time);
2486}
2487
2488void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002489input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002490{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002491 wl_data_offer_accept(input->drag_offer->offer,
2492 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002493}
2494
2495static void
2496offer_io_func(struct task *task, uint32_t events)
2497{
2498 struct data_offer *offer =
2499 container_of(task, struct data_offer, io_task);
2500 unsigned int len;
2501 char buffer[4096];
2502
2503 len = read(offer->fd, buffer, sizeof buffer);
2504 offer->func(buffer, len,
2505 offer->x, offer->y, offer->user_data);
2506
2507 if (len == 0) {
2508 close(offer->fd);
2509 data_offer_destroy(offer);
2510 }
2511}
2512
2513static void
2514data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2515 data_func_t func, void *user_data)
2516{
2517 int p[2];
2518
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002519 if (pipe2(p, O_CLOEXEC) == -1)
2520 return;
2521
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002522 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2523 close(p[1]);
2524
2525 offer->io_task.run = offer_io_func;
2526 offer->fd = p[0];
2527 offer->func = func;
2528 offer->refcount++;
2529 offer->user_data = user_data;
2530
2531 display_watch_fd(offer->input->display,
2532 offer->fd, EPOLLIN, &offer->io_task);
2533}
2534
2535void
2536input_receive_drag_data(struct input *input, const char *mime_type,
2537 data_func_t func, void *data)
2538{
2539 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2540 input->drag_offer->x = input->sx;
2541 input->drag_offer->y = input->sy;
2542}
2543
2544int
2545input_receive_selection_data(struct input *input, const char *mime_type,
2546 data_func_t func, void *data)
2547{
2548 char **p;
2549
2550 if (input->selection_offer == NULL)
2551 return -1;
2552
2553 for (p = input->selection_offer->types.data; *p; p++)
2554 if (strcmp(mime_type, *p) == 0)
2555 break;
2556
2557 if (*p == NULL)
2558 return -1;
2559
2560 data_offer_receive_data(input->selection_offer,
2561 mime_type, func, data);
2562 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002563}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002564
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002565int
2566input_receive_selection_data_to_fd(struct input *input,
2567 const char *mime_type, int fd)
2568{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002569 if (input->selection_offer)
2570 wl_data_offer_receive(input->selection_offer->offer,
2571 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002572
2573 return 0;
2574}
2575
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002576void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002577window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002578{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002579 if (!window->shell_surface)
2580 return;
2581
Daniel Stone37816df2012-05-16 18:45:18 +01002582 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002583}
2584
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002585static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002586idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002587{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002588 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002589
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002590 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002591 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002592 widget_set_allocation(widget,
2593 window->pending_allocation.x,
2594 window->pending_allocation.y,
2595 window->pending_allocation.width,
2596 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002597
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002598 if (window->input_region) {
2599 wl_region_destroy(window->input_region);
2600 window->input_region = NULL;
2601 }
2602
2603 if (window->opaque_region) {
2604 wl_region_destroy(window->opaque_region);
2605 window->opaque_region = NULL;
2606 }
2607
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002608 if (widget->resize_handler)
2609 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002610 widget->allocation.width,
2611 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002612 widget->user_data);
2613
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002614 if (window->allocation.width != widget->allocation.width ||
2615 window->allocation.height != widget->allocation.height) {
2616 window->allocation = widget->allocation;
2617 window_schedule_redraw(window);
2618 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002619}
2620
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002621void
2622window_schedule_resize(struct window *window, int width, int height)
2623{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002624 window->pending_allocation.x = 0;
2625 window->pending_allocation.y = 0;
2626 window->pending_allocation.width = width;
2627 window->pending_allocation.height = height;
2628
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002629 if (window->min_allocation.width == 0)
2630 window->min_allocation = window->pending_allocation;
2631 if (window->pending_allocation.width < window->min_allocation.width)
2632 window->pending_allocation.width = window->min_allocation.width;
2633 if (window->pending_allocation.height < window->min_allocation.height)
2634 window->pending_allocation.height = window->min_allocation.height;
2635
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002636 window->resize_needed = 1;
2637 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002638}
2639
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002640void
2641widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2642{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002643 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002644}
2645
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002646static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002647handle_ping(void *data, struct wl_shell_surface *shell_surface,
2648 uint32_t serial)
2649{
2650 wl_shell_surface_pong(shell_surface, serial);
2651}
2652
2653static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002654handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002655 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002656{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002657 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002658
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002659 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002660 return;
2661
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002662 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002663 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002664}
2665
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002666static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002667menu_destroy(struct menu *menu)
2668{
2669 widget_destroy(menu->widget);
2670 window_destroy(menu->window);
2671 free(menu);
2672}
2673
2674static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002675handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2676{
2677 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002678 struct menu *menu = window->widget->user_data;
2679
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002680 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002681 * device. Or just use wl_callback. And this really needs to
2682 * be a window vfunc that the menu can set. And we need the
2683 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002684
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002685 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002686 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002687 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002688}
2689
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002690static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002691 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002692 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002693 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002694};
2695
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002696void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002697window_get_allocation(struct window *window,
2698 struct rectangle *allocation)
2699{
2700 *allocation = window->allocation;
2701}
2702
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002703static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002704widget_redraw(struct widget *widget)
2705{
2706 struct widget *child;
2707
2708 if (widget->redraw_handler)
2709 widget->redraw_handler(widget, widget->user_data);
2710 wl_list_for_each(child, &widget->child_list, link)
2711 widget_redraw(child);
2712}
2713
2714static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002715frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2716{
2717 struct window *window = data;
2718
2719 wl_callback_destroy(callback);
2720 window->redraw_scheduled = 0;
2721 if (window->redraw_needed)
2722 window_schedule_redraw(window);
2723}
2724
2725static const struct wl_callback_listener listener = {
2726 frame_callback
2727};
2728
2729static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002730idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002731{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002732 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002733 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002734
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002735 if (window->resize_needed)
2736 idle_resize(window);
2737
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002738 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002739 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002740 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002741 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002742 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002743
2744 callback = wl_surface_frame(window->surface);
2745 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002746}
2747
2748void
2749window_schedule_redraw(struct window *window)
2750{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002751 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002752 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002753 window->redraw_task.run = idle_redraw;
2754 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002755 window->redraw_scheduled = 1;
2756 }
2757}
2758
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002759void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002760window_set_fullscreen(struct window *window, int fullscreen)
2761{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002762 if (!window->display->shell)
2763 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002764
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002765 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002766 return;
2767
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002768 if (fullscreen) {
2769 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002770 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002771 wl_shell_surface_set_fullscreen(window->shell_surface,
2772 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2773 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002774 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002775 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002776 wl_shell_surface_set_toplevel(window->shell_surface);
2777 window_schedule_resize(window,
2778 window->saved_allocation.width,
2779 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002780 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002781}
2782
2783void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002784window_set_maximized(struct window *window, int maximized)
2785{
2786 if (!window->display->shell)
2787 return;
2788
2789 if ((window->type == TYPE_MAXIMIZED) == maximized)
2790 return;
2791
2792 if (window->type == TYPE_TOPLEVEL) {
2793 window->saved_allocation = window->allocation;
2794 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2795 window->type = TYPE_MAXIMIZED;
2796 } else {
2797 wl_shell_surface_set_toplevel(window->shell_surface);
2798 window->type = TYPE_TOPLEVEL;
2799 window_schedule_resize(window,
2800 window->saved_allocation.width,
2801 window->saved_allocation.height);
2802 }
2803}
2804
2805void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002806window_set_user_data(struct window *window, void *data)
2807{
2808 window->user_data = data;
2809}
2810
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002811void *
2812window_get_user_data(struct window *window)
2813{
2814 return window->user_data;
2815}
2816
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002817void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002818window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002819 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002820{
2821 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002822}
2823
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002824void
2825window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002826 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002827{
2828 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002829}
2830
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002831void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002832window_set_data_handler(struct window *window, window_data_handler_t handler)
2833{
2834 window->data_handler = handler;
2835}
2836
2837void
2838window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2839{
2840 window->drop_handler = handler;
2841}
2842
2843void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002844window_set_close_handler(struct window *window,
2845 window_close_handler_t handler)
2846{
2847 window->close_handler = handler;
2848}
2849
2850void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002851window_set_fullscreen_handler(struct window *window,
2852 window_fullscreen_handler_t handler)
2853{
2854 window->fullscreen_handler = handler;
2855}
2856
2857void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002858window_set_title(struct window *window, const char *title)
2859{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002860 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002861 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002862 if (window->shell_surface)
2863 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002864}
2865
2866const char *
2867window_get_title(struct window *window)
2868{
2869 return window->title;
2870}
2871
2872void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002873window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2874{
2875 struct text_cursor_position *text_cursor_position =
2876 window->display->text_cursor_position;
2877
Scott Moreau9295ce02012-06-01 12:46:10 -06002878 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002879 return;
2880
2881 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002882 window->surface,
2883 wl_fixed_from_int(x),
2884 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002885}
2886
2887void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002888window_damage(struct window *window, int32_t x, int32_t y,
2889 int32_t width, int32_t height)
2890{
2891 wl_surface_damage(window->surface, x, y, width, height);
2892}
2893
Casey Dahlin9074db52012-04-19 22:50:09 -04002894static void
2895surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002896 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002897{
Rob Bradford7507b572012-05-15 17:55:34 +01002898 struct window *window = data;
2899 struct output *output;
2900 struct output *output_found = NULL;
2901 struct window_output *window_output;
2902
2903 wl_list_for_each(output, &window->display->output_list, link) {
2904 if (output->output == wl_output) {
2905 output_found = output;
2906 break;
2907 }
2908 }
2909
2910 if (!output_found)
2911 return;
2912
2913 window_output = malloc (sizeof *window_output);
2914 window_output->output = output_found;
2915
2916 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002917}
2918
2919static void
2920surface_leave(void *data,
2921 struct wl_surface *wl_surface, struct wl_output *output)
2922{
Rob Bradford7507b572012-05-15 17:55:34 +01002923 struct window *window = data;
2924 struct window_output *window_output;
2925 struct window_output *window_output_found = NULL;
2926
2927 wl_list_for_each(window_output, &window->window_output_list, link) {
2928 if (window_output->output->output == output) {
2929 window_output_found = window_output;
2930 break;
2931 }
2932 }
2933
2934 if (window_output_found) {
2935 wl_list_remove(&window_output_found->link);
2936 free(window_output_found);
2937 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002938}
2939
2940static const struct wl_surface_listener surface_listener = {
2941 surface_enter,
2942 surface_leave
2943};
2944
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002945static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002946window_create_internal(struct display *display,
2947 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002948{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002949 struct window *window;
2950
2951 window = malloc(sizeof *window);
2952 if (window == NULL)
2953 return NULL;
2954
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002955 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002956 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002957 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002958 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002959 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002960 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002961 window->shell_surface =
2962 wl_shell_get_shell_surface(display->shell,
2963 window->surface);
2964 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002965 window->allocation.x = 0;
2966 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002967 window->allocation.width = 0;
2968 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002969 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002970 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002971 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002972 window->input_region = NULL;
2973 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002974
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002975 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002976#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002977 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002978#else
2979 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2980#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002981 else
2982 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002983
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002984 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002985 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002986 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002987
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002988 if (window->shell_surface) {
2989 wl_shell_surface_set_user_data(window->shell_surface, window);
2990 wl_shell_surface_add_listener(window->shell_surface,
2991 &shell_surface_listener, window);
2992 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002993
Rob Bradford7507b572012-05-15 17:55:34 +01002994 wl_list_init (&window->window_output_list);
2995
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002996 return window;
2997}
2998
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002999struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003000window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003001{
3002 struct window *window;
3003
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003004 window = window_create_internal(display, NULL, TYPE_NONE);
3005 if (!window)
3006 return NULL;
3007
3008 return window;
3009}
3010
3011struct window *
3012window_create_custom(struct display *display)
3013{
3014 struct window *window;
3015
3016 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003017 if (!window)
3018 return NULL;
3019
3020 return window;
3021}
3022
3023struct window *
3024window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003025 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003026{
3027 struct window *window;
3028
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003029 window = window_create_internal(parent->display,
3030 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003031 if (!window)
3032 return NULL;
3033
3034 window->x = x;
3035 window->y = y;
3036
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003037 if (display->shell)
3038 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003039 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003040 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003041
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003042 return window;
3043}
3044
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003045static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003046menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003047{
3048 int next;
3049
3050 next = (sy - 8) / 20;
3051 if (menu->current != next) {
3052 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003053 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003054 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003055}
3056
3057static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003058menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003059 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003060 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003061{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003062 struct menu *menu = data;
3063
3064 if (widget == menu->widget)
3065 menu_set_item(data, y);
3066
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003067 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003068}
3069
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003070static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003071menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003072 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003073{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003074 struct menu *menu = data;
3075
3076 if (widget == menu->widget)
3077 menu_set_item(data, y);
3078
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003079 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003080}
3081
3082static void
3083menu_leave_handler(struct widget *widget, struct input *input, void *data)
3084{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003085 struct menu *menu = data;
3086
3087 if (widget == menu->widget)
3088 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003089}
3090
3091static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003092menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003093 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003094 uint32_t button, enum wl_pointer_button_state state,
3095 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003096
3097{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003098 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003099
Daniel Stone4dbadb12012-05-30 16:31:51 +01003100 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3101 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003102 /* Either relase after press-drag-release or
3103 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003104 menu->func(menu->window->parent,
3105 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003106 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003107 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003108 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003109}
3110
3111static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003112menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003113{
3114 cairo_t *cr;
3115 const int32_t r = 3, margin = 3;
3116 struct menu *menu = data;
3117 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003118 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003119
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003120 cr = cairo_create(window->cairo_surface);
3121 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3122 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3123 cairo_paint(cr);
3124
3125 width = window->allocation.width;
3126 height = window->allocation.height;
3127 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003128
3129 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003130 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3131 cairo_fill(cr);
3132
3133 for (i = 0; i < menu->count; i++) {
3134 if (i == menu->current) {
3135 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3136 cairo_rectangle(cr, margin, i * 20 + margin,
3137 width - 2 * margin, 20);
3138 cairo_fill(cr);
3139 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3140 cairo_move_to(cr, 10, i * 20 + 16);
3141 cairo_show_text(cr, menu->entries[i]);
3142 } else {
3143 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3144 cairo_move_to(cr, 10, i * 20 + 16);
3145 cairo_show_text(cr, menu->entries[i]);
3146 }
3147 }
3148
3149 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003150}
3151
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003152void
3153window_show_menu(struct display *display,
3154 struct input *input, uint32_t time, struct window *parent,
3155 int32_t x, int32_t y,
3156 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003157{
3158 struct window *window;
3159 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003160 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003161
3162 menu = malloc(sizeof *menu);
3163 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003164 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003165
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003166 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003167 if (!window) {
3168 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003169 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003170 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003171
3172 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003173 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003174 menu->entries = entries;
3175 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003176 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003177 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003178 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003179 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003180 window->type = TYPE_MENU;
3181 window->x = x;
3182 window->y = y;
3183
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003184 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003185 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003186 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003187 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003188 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003189
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003190 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003191 widget_set_enter_handler(menu->widget, menu_enter_handler);
3192 widget_set_leave_handler(menu->widget, menu_leave_handler);
3193 widget_set_motion_handler(menu->widget, menu_motion_handler);
3194 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003195
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003196 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003197 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003198}
3199
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003200void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003201window_set_buffer_type(struct window *window, enum window_buffer_type type)
3202{
3203 window->buffer_type = type;
3204}
3205
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003206
3207static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003208display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003209 struct wl_output *wl_output,
3210 int x, int y,
3211 int physical_width,
3212 int physical_height,
3213 int subpixel,
3214 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003215 const char *model,
3216 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003217{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003218 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003219
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003220 output->allocation.x = x;
3221 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003222}
3223
3224static void
3225display_handle_mode(void *data,
3226 struct wl_output *wl_output,
3227 uint32_t flags,
3228 int width,
3229 int height,
3230 int refresh)
3231{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003232 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003233 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003234
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003235 if (flags & WL_OUTPUT_MODE_CURRENT) {
3236 output->allocation.width = width;
3237 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003238 if (display->output_configure_handler)
3239 (*display->output_configure_handler)(
3240 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003241 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003242}
3243
3244static const struct wl_output_listener output_listener = {
3245 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003246 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003247};
3248
3249static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003250display_add_output(struct display *d, uint32_t id)
3251{
3252 struct output *output;
3253
3254 output = malloc(sizeof *output);
3255 if (output == NULL)
3256 return;
3257
3258 memset(output, 0, sizeof *output);
3259 output->display = d;
3260 output->output =
3261 wl_display_bind(d->display, id, &wl_output_interface);
3262 wl_list_insert(d->output_list.prev, &output->link);
3263
3264 wl_output_add_listener(output->output, &output_listener, output);
3265}
3266
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003267static void
3268output_destroy(struct output *output)
3269{
3270 if (output->destroy_handler)
3271 (*output->destroy_handler)(output, output->user_data);
3272
3273 wl_output_destroy(output->output);
3274 wl_list_remove(&output->link);
3275 free(output);
3276}
3277
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003278void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003279display_set_output_configure_handler(struct display *display,
3280 display_output_handler_t handler)
3281{
3282 struct output *output;
3283
3284 display->output_configure_handler = handler;
3285 if (!handler)
3286 return;
3287
3288 wl_list_for_each(output, &display->output_list, link)
3289 (*display->output_configure_handler)(output,
3290 display->user_data);
3291}
3292
3293void
3294output_set_user_data(struct output *output, void *data)
3295{
3296 output->user_data = data;
3297}
3298
3299void *
3300output_get_user_data(struct output *output)
3301{
3302 return output->user_data;
3303}
3304
3305void
3306output_set_destroy_handler(struct output *output,
3307 display_output_handler_t handler)
3308{
3309 output->destroy_handler = handler;
3310 /* FIXME: implement this, once we have way to remove outputs */
3311}
3312
3313void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003314output_get_allocation(struct output *output, struct rectangle *allocation)
3315{
3316 *allocation = output->allocation;
3317}
3318
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003319struct wl_output *
3320output_get_wl_output(struct output *output)
3321{
3322 return output->output;
3323}
3324
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003325static void
Daniel Stone97f68542012-05-30 16:32:01 +01003326fini_xkb(struct input *input)
3327{
3328 xkb_state_unref(input->xkb.state);
3329 xkb_map_unref(input->xkb.keymap);
3330}
3331
3332static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003333display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003334{
3335 struct input *input;
3336
3337 input = malloc(sizeof *input);
3338 if (input == NULL)
3339 return;
3340
3341 memset(input, 0, sizeof *input);
3342 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003343 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003344 input->pointer_focus = NULL;
3345 input->keyboard_focus = NULL;
3346 wl_list_insert(d->input_list.prev, &input->link);
3347
Daniel Stone37816df2012-05-16 18:45:18 +01003348 wl_seat_add_listener(input->seat, &seat_listener, input);
3349 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003350
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003351 input->data_device =
3352 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003353 input->seat);
3354 wl_data_device_add_listener(input->data_device, &data_device_listener,
3355 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003356
3357 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003358
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003359 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3360 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003361 input->repeat_task.run = keyboard_repeat_func;
3362 display_watch_fd(d, input->repeat_timer_fd,
3363 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003364}
3365
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003366static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003367input_destroy(struct input *input)
3368{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003369 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003370 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003371
3372 if (input->drag_offer)
3373 data_offer_destroy(input->drag_offer);
3374
3375 if (input->selection_offer)
3376 data_offer_destroy(input->selection_offer);
3377
3378 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003379 fini_xkb(input);
3380
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003381 wl_surface_destroy(input->pointer_surface);
3382
Pekka Paalanene1207c72011-12-16 12:02:09 +02003383 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003384 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003385 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003386 free(input);
3387}
3388
3389static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003390display_handle_global(struct wl_display *display, uint32_t id,
3391 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003392{
3393 struct display *d = data;
3394
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003395 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003396 d->compositor =
3397 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003398 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003399 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003400 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003401 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003402 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003403 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003404 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003405 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003406 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3407 d->data_device_manager =
3408 wl_display_bind(display, id,
3409 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003410 } else if (strcmp(interface, "text_cursor_position") == 0) {
3411 d->text_cursor_position =
3412 wl_display_bind(display, id,
3413 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003414 }
3415}
3416
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003417#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003418static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003419init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003420{
3421 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003422 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003423
Rob Clark6396ed32012-03-11 19:48:41 -05003424#ifdef USE_CAIRO_GLESV2
3425# define GL_BIT EGL_OPENGL_ES2_BIT
3426#else
3427# define GL_BIT EGL_OPENGL_BIT
3428#endif
3429
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003430 static const EGLint argb_cfg_attribs[] = {
3431 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003432 EGL_RED_SIZE, 1,
3433 EGL_GREEN_SIZE, 1,
3434 EGL_BLUE_SIZE, 1,
3435 EGL_ALPHA_SIZE, 1,
3436 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003437 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003438 EGL_NONE
3439 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003440
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003441#ifdef USE_CAIRO_GLESV2
3442 static const EGLint context_attribs[] = {
3443 EGL_CONTEXT_CLIENT_VERSION, 2,
3444 EGL_NONE
3445 };
3446 EGLint api = EGL_OPENGL_ES_API;
3447#else
3448 EGLint *context_attribs = NULL;
3449 EGLint api = EGL_OPENGL_API;
3450#endif
3451
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003452 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003453 if (!eglInitialize(d->dpy, &major, &minor)) {
3454 fprintf(stderr, "failed to initialize display\n");
3455 return -1;
3456 }
3457
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003458 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003459 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3460 return -1;
3461 }
3462
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003463 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3464 &d->argb_config, 1, &n) || n != 1) {
3465 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003466 return -1;
3467 }
3468
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003469 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003470 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003471 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003472 fprintf(stderr, "failed to create context\n");
3473 return -1;
3474 }
3475
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003476 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003477 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003478 return -1;
3479 }
3480
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003481#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003482 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3483 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3484 fprintf(stderr, "failed to get cairo egl argb device\n");
3485 return -1;
3486 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003487#endif
3488
3489 return 0;
3490}
3491
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003492static void
3493fini_egl(struct display *display)
3494{
3495#ifdef HAVE_CAIRO_EGL
3496 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003497#endif
3498
3499 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3500 EGL_NO_CONTEXT);
3501
3502 eglTerminate(display->dpy);
3503 eglReleaseThread();
3504}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003505#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003506
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003507static int
3508event_mask_update(uint32_t mask, void *data)
3509{
3510 struct display *d = data;
3511
3512 d->mask = mask;
3513
3514 return 0;
3515}
3516
3517static void
3518handle_display_data(struct task *task, uint32_t events)
3519{
3520 struct display *display =
3521 container_of(task, struct display, display_task);
3522
3523 wl_display_iterate(display->display, display->mask);
3524}
3525
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003526struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003527display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003528{
3529 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003530
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003531 d = malloc(sizeof *d);
3532 if (d == NULL)
3533 return NULL;
3534
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003535 memset(d, 0, sizeof *d);
3536
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003537 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003538 if (d->display == NULL) {
3539 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003540 return NULL;
3541 }
3542
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003543 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003544 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3545 d->display_task.run = handle_display_data;
3546 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3547
3548 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003549 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003550 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003551
Daniel Stone97f68542012-05-30 16:32:01 +01003552 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003553 if (d->xkb_context == NULL) {
3554 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003555 return NULL;
3556 }
3557
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003558 /* Set up listener so we'll catch all events. */
3559 wl_display_add_global_listener(d->display,
3560 display_handle_global, d);
3561
3562 /* Process connection events. */
3563 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003564#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003565 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003566 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003567#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003568
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003569 d->image_target_texture_2d =
3570 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3571 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3572 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3573
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003574 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003575
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003576 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003577
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003578 wl_list_init(&d->window_list);
3579
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003580 return d;
3581}
3582
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003583static void
3584display_destroy_outputs(struct display *display)
3585{
3586 struct output *tmp;
3587 struct output *output;
3588
3589 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3590 output_destroy(output);
3591}
3592
Pekka Paalanene1207c72011-12-16 12:02:09 +02003593static void
3594display_destroy_inputs(struct display *display)
3595{
3596 struct input *tmp;
3597 struct input *input;
3598
3599 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3600 input_destroy(input);
3601}
3602
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003603void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003604display_destroy(struct display *display)
3605{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003606 if (!wl_list_empty(&display->window_list))
3607 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3608
3609 if (!wl_list_empty(&display->deferred_list))
3610 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3611
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003612 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003613 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003614
Daniel Stone97f68542012-05-30 16:32:01 +01003615 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003616
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003617 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003618 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003619
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003620#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003621 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003622#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003623
Pekka Paalanenc2052982011-12-16 11:41:32 +02003624 if (display->shell)
3625 wl_shell_destroy(display->shell);
3626
3627 if (display->shm)
3628 wl_shm_destroy(display->shm);
3629
3630 if (display->data_device_manager)
3631 wl_data_device_manager_destroy(display->data_device_manager);
3632
3633 wl_compositor_destroy(display->compositor);
3634
3635 close(display->epoll_fd);
3636
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003637 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003638 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003639 free(display);
3640}
3641
3642void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003643display_set_user_data(struct display *display, void *data)
3644{
3645 display->user_data = data;
3646}
3647
3648void *
3649display_get_user_data(struct display *display)
3650{
3651 return display->user_data;
3652}
3653
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003654struct wl_display *
3655display_get_display(struct display *display)
3656{
3657 return display->display;
3658}
3659
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003660struct output *
3661display_get_output(struct display *display)
3662{
3663 return container_of(display->output_list.next, struct output, link);
3664}
3665
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003666struct wl_compositor *
3667display_get_compositor(struct display *display)
3668{
3669 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003670}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003671
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003672uint32_t
3673display_get_serial(struct display *display)
3674{
3675 return display->serial;
3676}
3677
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003678EGLDisplay
3679display_get_egl_display(struct display *d)
3680{
3681 return d->dpy;
3682}
3683
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003684struct wl_data_source *
3685display_create_data_source(struct display *display)
3686{
3687 return wl_data_device_manager_create_data_source(display->data_device_manager);
3688}
3689
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003690EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003691display_get_argb_egl_config(struct display *d)
3692{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003693 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003694}
3695
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003696struct wl_shell *
3697display_get_shell(struct display *display)
3698{
3699 return display->shell;
3700}
3701
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003702int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003703display_acquire_window_surface(struct display *display,
3704 struct window *window,
3705 EGLContext ctx)
3706{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003707#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003708 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003709 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003710
3711 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003712 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003713 device = cairo_surface_get_device(window->cairo_surface);
3714 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003715 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003716
Benjamin Franzke0c991632011-09-27 21:57:31 +02003717 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003718 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003719 ctx = display->argb_ctx;
3720 else
3721 assert(0);
3722 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003723
3724 data = cairo_surface_get_user_data(window->cairo_surface,
3725 &surface_data_key);
3726
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003727 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003728 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003729 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3730 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003731
3732 return 0;
3733#else
3734 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003735#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003736}
3737
3738void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003739display_release_window_surface(struct display *display,
3740 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003741{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003742#ifdef HAVE_CAIRO_EGL
3743 cairo_device_t *device;
3744
3745 device = cairo_surface_get_device(window->cairo_surface);
3746 if (!device)
3747 return;
3748
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003749 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003750 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003751 cairo_device_release(device);
3752#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003753}
3754
3755void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003756display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003757{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003758 wl_list_insert(&display->deferred_list, &task->link);
3759}
3760
3761void
3762display_watch_fd(struct display *display,
3763 int fd, uint32_t events, struct task *task)
3764{
3765 struct epoll_event ep;
3766
3767 ep.events = events;
3768 ep.data.ptr = task;
3769 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3770}
3771
3772void
3773display_run(struct display *display)
3774{
3775 struct task *task;
3776 struct epoll_event ep[16];
3777 int i, count;
3778
Pekka Paalanen826d7952011-12-15 10:14:07 +02003779 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003780 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003781 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003782
3783 while (!wl_list_empty(&display->deferred_list)) {
3784 task = container_of(display->deferred_list.next,
3785 struct task, link);
3786 wl_list_remove(&task->link);
3787 task->run(task, 0);
3788 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003789
3790 if (!display->running)
3791 break;
3792
3793 wl_display_flush(display->display);
3794
3795 count = epoll_wait(display->epoll_fd,
3796 ep, ARRAY_LENGTH(ep), -1);
3797 for (i = 0; i < count; i++) {
3798 task = ep[i].data.ptr;
3799 task->run(task, ep[i].events);
3800 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003801 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003802}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003803
3804void
3805display_exit(struct display *display)
3806{
3807 display->running = 0;
3808}