blob: 4e86f061162d10fc06fb99260614fb0ded7e3788 [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{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100649 char *config_file;
650 char *theme = NULL;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300651 unsigned int i;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100652 struct config_key shell_keys[] = {
653 { "cursor-theme", CONFIG_KEY_STRING, &theme },
654 };
655 struct config_section cs[] = {
656 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
657 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300658
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100659 config_file = config_file_path("weston.ini");
660 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
661 free(config_file);
662
663 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300664 display->cursors =
665 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
666
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300667 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300668 display->cursors[i] =
669 wl_cursor_theme_get_cursor(display->cursor_theme,
670 cursors[i]);
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300671 if (!display->cursors[i])
672 fprintf(stderr, "could not load cursor '%s'\n",
673 cursors[i]);
674 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300675}
676
677static void
678destroy_cursors(struct display *display)
679{
680 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800681 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300682}
683
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300684struct wl_cursor_image *
685display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400686{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300687 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300688 wl_cursor_theme_get_cursor(display->cursor_theme,
689 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400690
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300691 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400692}
693
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400694static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200695window_get_resize_dx_dy(struct window *window, int *x, int *y)
696{
697 if (window->resize_edges & WINDOW_RESIZING_LEFT)
698 *x = window->server_allocation.width - window->allocation.width;
699 else
700 *x = 0;
701
702 if (window->resize_edges & WINDOW_RESIZING_TOP)
703 *y = window->server_allocation.height -
704 window->allocation.height;
705 else
706 *y = 0;
707
708 window->resize_edges = 0;
709}
710
711static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500712window_attach_surface(struct window *window)
713{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400714 struct display *display = window->display;
715 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000716#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100717 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000718#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500719 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100720
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400721 if (window->type == TYPE_NONE) {
722 window->type = TYPE_TOPLEVEL;
723 if (display->shell)
724 wl_shell_surface_set_toplevel(window->shell_surface);
725 }
726
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000728#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100729 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
730 data = cairo_surface_get_user_data(window->cairo_surface,
731 &surface_data_key);
732
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733 cairo_gl_surface_swapbuffers(window->cairo_surface);
734 wl_egl_window_get_attached_size(data->window,
735 &window->server_allocation.width,
736 &window->server_allocation.height);
737 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000738#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100739 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 buffer =
741 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400742 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100743
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200744 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100745 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400746 wl_surface_damage(window->surface, 0, 0,
747 window->allocation.width,
748 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100749 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400750 cairo_surface_destroy(window->cairo_surface);
751 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100752 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000753 default:
754 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100755 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500756
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500757 if (window->input_region) {
758 wl_surface_set_input_region(window->surface,
759 window->input_region);
760 wl_region_destroy(window->input_region);
761 window->input_region = NULL;
762 }
763
764 if (window->opaque_region) {
765 wl_surface_set_opaque_region(window->surface,
766 window->opaque_region);
767 wl_region_destroy(window->opaque_region);
768 window->opaque_region = NULL;
769 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500770}
771
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500772void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400773window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500774{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400775 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100776 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500777}
778
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400779void
780window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400781{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500782 cairo_surface_reference(surface);
783
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400784 if (window->cairo_surface != NULL)
785 cairo_surface_destroy(window->cairo_surface);
786
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500787 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400788}
789
Benjamin Franzke22d54812011-07-16 19:50:32 +0000790#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100791static void
792window_resize_cairo_window_surface(struct window *window)
793{
794 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200795 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100796
797 data = cairo_surface_get_user_data(window->cairo_surface,
798 &surface_data_key);
799
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200800 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100801 wl_egl_window_resize(data->window,
802 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200803 window->allocation.height,
804 x,y);
805
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100806 cairo_gl_surface_set_size(window->cairo_surface,
807 window->allocation.width,
808 window->allocation.height);
809}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000810#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400812struct display *
813window_get_display(struct window *window)
814{
815 return window->display;
816}
817
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400818void
819window_create_surface(struct window *window)
820{
821 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400822 uint32_t flags = 0;
823
824 if (!window->transparent)
825 flags = SURFACE_OPAQUE;
826
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400827 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500828#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
830 if (window->cairo_surface) {
831 window_resize_cairo_window_surface(window);
832 return;
833 }
834 surface = display_create_surface(window->display,
835 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400836 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400838#endif
839 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400840 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400841 &window->allocation,
842 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400843 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800844 default:
845 surface = NULL;
846 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400847 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400848
849 window_set_surface(window, surface);
850 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400851}
852
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200853static void frame_destroy(struct frame *frame);
854
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400855void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500856window_destroy(struct window *window)
857{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200858 struct display *display = window->display;
859 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100860 struct window_output *window_output;
861 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200862
863 if (window->redraw_scheduled)
864 wl_list_remove(&window->redraw_task.link);
865
866 wl_list_for_each(input, &display->input_list, link) {
867 if (input->pointer_focus == window)
868 input->pointer_focus = NULL;
869 if (input->keyboard_focus == window)
870 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500871 if (input->focus_widget &&
872 input->focus_widget->window == window)
873 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200874 }
875
Rob Bradford7507b572012-05-15 17:55:34 +0100876 wl_list_for_each_safe(window_output, window_output_tmp,
877 &window->window_output_list, link) {
878 free (window_output);
879 }
880
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500881 if (window->input_region)
882 wl_region_destroy(window->input_region);
883 if (window->opaque_region)
884 wl_region_destroy(window->opaque_region);
885
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200886 if (window->frame)
887 frame_destroy(window->frame);
888
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200889 if (window->shell_surface)
890 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500891 wl_surface_destroy(window->surface);
892 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200893
894 if (window->cairo_surface != NULL)
895 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200896
897 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500898 free(window);
899}
900
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500901static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500902widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400903{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500904 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400905
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500906 wl_list_for_each(child, &widget->child_list, link) {
907 target = widget_find_widget(child, x, y);
908 if (target)
909 return target;
910 }
911
912 if (widget->allocation.x <= x &&
913 x < widget->allocation.x + widget->allocation.width &&
914 widget->allocation.y <= y &&
915 y < widget->allocation.y + widget->allocation.height) {
916 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400917 }
918
919 return NULL;
920}
921
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500922static struct widget *
923widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400924{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500925 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400926
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500927 widget = malloc(sizeof *widget);
928 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500929 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500930 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500931 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500932 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500933 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300934 widget->tooltip = NULL;
935 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500936
937 return widget;
938}
939
940struct widget *
941window_add_widget(struct window *window, void *data)
942{
943 window->widget = widget_create(window, data);
944 wl_list_init(&window->widget->link);
945
946 return window->widget;
947}
948
949struct widget *
950widget_add_widget(struct widget *parent, void *data)
951{
952 struct widget *widget;
953
954 widget = widget_create(parent->window, data);
955 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400956
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500957 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400958}
959
960void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500961widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400962{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200963 struct display *display = widget->window->display;
964 struct input *input;
965
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300966 if (widget->tooltip) {
967 free(widget->tooltip);
968 widget->tooltip = NULL;
969 }
970
Pekka Paalanene156fb62012-01-19 13:51:38 +0200971 wl_list_for_each(input, &display->input_list, link) {
972 if (input->focus_widget == widget)
973 input->focus_widget = NULL;
974 }
975
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500976 wl_list_remove(&widget->link);
977 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400978}
979
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400980void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500981widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400982{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500983 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400984}
985
986void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500987widget_set_size(struct widget *widget, int32_t width, int32_t height)
988{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500989 widget->allocation.width = width;
990 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500991}
992
993void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500994widget_set_allocation(struct widget *widget,
995 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500997 widget->allocation.x = x;
998 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200999 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001000}
1001
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001002void
1003widget_set_transparent(struct widget *widget, int transparent)
1004{
1005 widget->opaque = !transparent;
1006}
1007
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001008void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001009widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001010{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001011 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001012}
1013
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001014void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001015widget_set_resize_handler(struct widget *widget,
1016 widget_resize_handler_t handler)
1017{
1018 widget->resize_handler = handler;
1019}
1020
1021void
1022widget_set_redraw_handler(struct widget *widget,
1023 widget_redraw_handler_t handler)
1024{
1025 widget->redraw_handler = handler;
1026}
1027
1028void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001029widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001030{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001031 widget->enter_handler = handler;
1032}
1033
1034void
1035widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1036{
1037 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001038}
1039
1040void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001041widget_set_motion_handler(struct widget *widget,
1042 widget_motion_handler_t handler)
1043{
1044 widget->motion_handler = handler;
1045}
1046
1047void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001048widget_set_button_handler(struct widget *widget,
1049 widget_button_handler_t handler)
1050{
1051 widget->button_handler = handler;
1052}
1053
1054void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001055widget_schedule_redraw(struct widget *widget)
1056{
1057 window_schedule_redraw(widget->window);
1058}
1059
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001060cairo_surface_t *
1061window_get_surface(struct window *window)
1062{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001063 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001064}
1065
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001066struct wl_surface *
1067window_get_wl_surface(struct window *window)
1068{
1069 return window->surface;
1070}
1071
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001072struct wl_shell_surface *
1073window_get_wl_shell_surface(struct window *window)
1074{
1075 return window->shell_surface;
1076}
1077
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001078static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001079tooltip_redraw_handler(struct widget *widget, void *data)
1080{
1081 cairo_t *cr;
1082 const int32_t r = 3;
1083 struct tooltip *tooltip = data;
1084 int32_t width, height;
1085 struct window *window = widget->window;
1086
1087 cr = cairo_create(window->cairo_surface);
1088 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1089 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1090 cairo_paint(cr);
1091
1092 width = window->allocation.width;
1093 height = window->allocation.height;
1094 rounded_rect(cr, 0, 0, width, height, r);
1095
1096 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1097 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1098 cairo_fill(cr);
1099
1100 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1101 cairo_move_to(cr, 10, 16);
1102 cairo_show_text(cr, tooltip->entry);
1103 cairo_destroy(cr);
1104}
1105
1106static cairo_text_extents_t
1107get_text_extents(struct tooltip *tooltip)
1108{
1109 struct window *window;
1110 cairo_t *cr;
1111 cairo_text_extents_t extents;
1112
1113 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1114 * created yet */
1115 window = tooltip->widget->window->parent;
1116 cr = cairo_create(window->cairo_surface);
1117 cairo_text_extents(cr, tooltip->entry, &extents);
1118 cairo_destroy(cr);
1119
1120 return extents;
1121}
1122
1123static int
1124window_create_tooltip(struct tooltip *tooltip)
1125{
1126 struct widget *parent = tooltip->parent;
1127 struct display *display = parent->window->display;
1128 struct window *window;
1129 const int offset_y = 27;
1130 const int margin = 3;
1131 cairo_text_extents_t extents;
1132
1133 if (tooltip->widget)
1134 return 0;
1135
1136 window = window_create_transient(display, parent->window, tooltip->x,
1137 tooltip->y + offset_y,
1138 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1139 if (!window)
1140 return -1;
1141
1142 tooltip->window = window;
1143 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1144
1145 extents = get_text_extents(tooltip);
1146 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1147 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1148
1149 return 0;
1150}
1151
1152void
1153widget_destroy_tooltip(struct widget *parent)
1154{
1155 struct tooltip *tooltip = parent->tooltip;
1156
1157 parent->tooltip_count = 0;
1158 if (!tooltip)
1159 return;
1160
1161 if (tooltip->widget) {
1162 widget_destroy(tooltip->widget);
1163 window_destroy(tooltip->window);
1164 tooltip->widget = NULL;
1165 tooltip->window = NULL;
1166 }
1167
1168 close(tooltip->tooltip_fd);
1169 free(tooltip->entry);
1170 free(tooltip);
1171 parent->tooltip = NULL;
1172}
1173
1174static void
1175tooltip_func(struct task *task, uint32_t events)
1176{
1177 struct tooltip *tooltip =
1178 container_of(task, struct tooltip, tooltip_task);
1179 uint64_t exp;
1180
Martin Olsson8df662a2012-07-08 03:03:47 +02001181 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1182 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001183 window_create_tooltip(tooltip);
1184}
1185
1186#define TOOLTIP_TIMEOUT 500
1187static int
1188tooltip_timer_reset(struct tooltip *tooltip)
1189{
1190 struct itimerspec its;
1191
1192 its.it_interval.tv_sec = 0;
1193 its.it_interval.tv_nsec = 0;
1194 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1195 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1196 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1197 fprintf(stderr, "could not set timerfd\n: %m");
1198 return -1;
1199 }
1200
1201 return 0;
1202}
1203
1204int
1205widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1206{
1207 struct tooltip *tooltip = parent->tooltip;
1208
1209 parent->tooltip_count++;
1210 if (tooltip) {
1211 tooltip->x = x;
1212 tooltip->y = y;
1213 tooltip_timer_reset(tooltip);
1214 return 0;
1215 }
1216
1217 /* the handler might be triggered too fast via input device motion, so
1218 * we need this check here to make sure tooltip is fully initialized */
1219 if (parent->tooltip_count > 1)
1220 return 0;
1221
1222 tooltip = malloc(sizeof *tooltip);
1223 if (!tooltip)
1224 return -1;
1225
1226 parent->tooltip = tooltip;
1227 tooltip->parent = parent;
1228 tooltip->widget = NULL;
1229 tooltip->window = NULL;
1230 tooltip->x = x;
1231 tooltip->y = y;
1232 tooltip->entry = strdup(entry);
1233 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1234 if (tooltip->tooltip_fd < 0) {
1235 fprintf(stderr, "could not create timerfd\n: %m");
1236 return -1;
1237 }
1238
1239 tooltip->tooltip_task.run = tooltip_func;
1240 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1241 EPOLLIN, &tooltip->tooltip_task);
1242 tooltip_timer_reset(tooltip);
1243
1244 return 0;
1245}
1246
1247static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001248frame_resize_handler(struct widget *widget,
1249 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001250{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251 struct frame *frame = data;
1252 struct widget *child = frame->child;
1253 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001254 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001255 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001256 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001257 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001259 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001260
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001261 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001262 decoration_width = (t->width + t->margin) * 2;
1263 decoration_height = t->width +
1264 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001265
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001266 allocation.x = t->width + t->margin;
1267 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001268 allocation.width = width - decoration_width;
1269 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001270
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001271 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001272
1273 wl_list_for_each(button, &frame->buttons_list, link)
1274 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001275 } else {
1276 decoration_width = 0;
1277 decoration_height = 0;
1278
1279 allocation.x = 0;
1280 allocation.y = 0;
1281 allocation.width = width;
1282 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001283 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001284
1285 wl_list_for_each(button, &frame->buttons_list, link)
1286 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001287 }
1288
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001289 widget_set_allocation(child, allocation.x, allocation.y,
1290 allocation.width, allocation.height);
1291
1292 if (child->resize_handler)
1293 child->resize_handler(child,
1294 allocation.width,
1295 allocation.height,
1296 child->user_data);
1297
Scott Moreauf7e498c2012-05-14 11:39:29 -06001298 width = child->allocation.width + decoration_width;
1299 height = child->allocation.height + decoration_height;
1300
Kristian Høgsberg023be102012-07-31 11:59:12 -04001301 if (widget->window->type != TYPE_FULLSCREEN) {
1302 widget->window->input_region =
1303 wl_compositor_create_region(display->compositor);
1304 wl_region_add(widget->window->input_region,
1305 t->margin, t->margin,
1306 width - 2 * t->margin,
1307 height - 2 * t->margin);
1308 }
1309
Scott Moreauf7e498c2012-05-14 11:39:29 -06001310 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001311
1312 if (child->opaque) {
1313 widget->window->opaque_region =
1314 wl_compositor_create_region(display->compositor);
1315 wl_region_add(widget->window->opaque_region,
1316 opaque_margin, opaque_margin,
1317 widget->allocation.width - 2 * opaque_margin,
1318 widget->allocation.height - 2 * opaque_margin);
1319 }
Martin Minarik1998b152012-05-10 02:04:35 +02001320
1321 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001322 x_r = frame->widget->allocation.width - t->width - t->margin;
1323 x_l = t->width + t->margin;
1324 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001325 wl_list_for_each(button, &frame->buttons_list, link) {
1326 const int button_padding = 4;
1327 w = cairo_image_surface_get_width(button->icon);
1328 h = cairo_image_surface_get_height(button->icon);
1329
1330 if (button->decoration == FRAME_BUTTON_FANCY)
1331 w += 10;
1332
1333 if (button->align == FRAME_BUTTON_LEFT) {
1334 widget_set_allocation(button->widget,
1335 x_l, y , w + 1, h + 1);
1336 x_l += w;
1337 x_l += button_padding;
1338 } else {
1339 x_r -= w;
1340 widget_set_allocation(button->widget,
1341 x_r, y , w + 1, h + 1);
1342 x_r -= button_padding;
1343 }
1344 }
1345}
1346
1347static int
1348frame_button_enter_handler(struct widget *widget,
1349 struct input *input, float x, float y, void *data)
1350{
1351 struct frame_button *frame_button = data;
1352
1353 widget_schedule_redraw(frame_button->widget);
1354 frame_button->state = FRAME_BUTTON_OVER;
1355
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001356 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001357}
1358
1359static void
1360frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1361{
1362 struct frame_button *frame_button = data;
1363
1364 widget_schedule_redraw(frame_button->widget);
1365 frame_button->state = FRAME_BUTTON_DEFAULT;
1366}
1367
1368static void
1369frame_button_button_handler(struct widget *widget,
1370 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001371 uint32_t button,
1372 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001373{
1374 struct frame_button *frame_button = data;
1375 struct window *window = widget->window;
1376
1377 if (button != BTN_LEFT)
1378 return;
1379
1380 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001381 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001382 frame_button->state = FRAME_BUTTON_ACTIVE;
1383 widget_schedule_redraw(frame_button->widget);
1384
1385 if (frame_button->type == FRAME_BUTTON_ICON)
1386 window_show_frame_menu(window, input, time);
1387 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001388 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001389 frame_button->state = FRAME_BUTTON_DEFAULT;
1390 widget_schedule_redraw(frame_button->widget);
1391 break;
1392 }
1393
1394 switch (frame_button->type) {
1395 case FRAME_BUTTON_CLOSE:
1396 if (window->close_handler)
1397 window->close_handler(window->parent,
1398 window->user_data);
1399 else
1400 display_exit(window->display);
1401 break;
1402 case FRAME_BUTTON_MINIMIZE:
1403 fprintf(stderr,"Minimize stub\n");
1404 break;
1405 case FRAME_BUTTON_MAXIMIZE:
1406 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1407 break;
1408 default:
1409 /* Unknown operation */
1410 break;
1411 }
1412}
1413
1414static void
1415frame_button_redraw_handler(struct widget *widget, void *data)
1416{
1417 struct frame_button *frame_button = data;
1418 cairo_t *cr;
1419 int width, height, x, y;
1420 struct window *window = widget->window;
1421
1422 x = widget->allocation.x;
1423 y = widget->allocation.y;
1424 width = widget->allocation.width;
1425 height = widget->allocation.height;
1426
1427 if (!width)
1428 return;
1429 if (!height)
1430 return;
1431 if (widget->opaque)
1432 return;
1433
1434 cr = cairo_create(window->cairo_surface);
1435
1436 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1437 cairo_set_line_width(cr, 1);
1438
1439 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1440 cairo_rectangle (cr, x, y, 25, 16);
1441
1442 cairo_stroke_preserve(cr);
1443
1444 switch (frame_button->state) {
1445 case FRAME_BUTTON_DEFAULT:
1446 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1447 break;
1448 case FRAME_BUTTON_OVER:
1449 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1450 break;
1451 case FRAME_BUTTON_ACTIVE:
1452 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1453 break;
1454 }
1455
1456 cairo_fill (cr);
1457
1458 x += 4;
1459 }
1460
1461 cairo_set_source_surface(cr, frame_button->icon, x, y);
1462 cairo_paint(cr);
1463
1464 cairo_destroy(cr);
1465}
1466
1467static struct widget *
1468frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1469 enum frame_button_align align, enum frame_button_decoration style)
1470{
1471 struct frame_button *frame_button;
1472 const char *icon = data;
1473
1474 frame_button = malloc (sizeof *frame_button);
1475 memset(frame_button, 0, sizeof *frame_button);
1476
1477 frame_button->icon = cairo_image_surface_create_from_png(icon);
1478 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1479 frame_button->frame = frame;
1480 frame_button->type = type;
1481 frame_button->align = align;
1482 frame_button->decoration = style;
1483
1484 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1485
1486 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1487 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1488 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1489 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1490 return frame_button->widget;
1491}
1492
1493static void
1494frame_button_destroy(struct frame_button *frame_button)
1495{
1496 widget_destroy(frame_button->widget);
1497 wl_list_remove(&frame_button->link);
1498 cairo_surface_destroy(frame_button->icon);
1499 free(frame_button);
1500
1501 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001502}
1503
1504static void
1505frame_redraw_handler(struct widget *widget, void *data)
1506{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001507 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001508 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001509 struct theme *t = window->display->theme;
1510 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001511
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001512 if (window->type == TYPE_FULLSCREEN)
1513 return;
1514
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001515 cr = cairo_create(window->cairo_surface);
1516
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001517 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001518 flags |= THEME_FRAME_ACTIVE;
1519 theme_render_frame(t, cr, widget->allocation.width,
1520 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001521
1522 cairo_destroy(cr);
1523}
1524
1525static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001526frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001527{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001529 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001530
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001531 location = theme_get_location(t, input->sx, input->sy,
1532 frame->widget->allocation.width,
1533 frame->widget->allocation.height);
1534
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001535 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001536 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001537 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001538 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001539 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001540 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001541 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001542 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001543 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001544 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001545 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001546 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001547 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001548 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001549 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001550 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001551 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001552 case THEME_LOCATION_EXTERIOR:
1553 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001554 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001555 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001556 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001557}
1558
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001559static void
1560frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001561{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001562 switch (index) {
1563 case 0: /* close */
1564 if (window->close_handler)
1565 window->close_handler(window->parent,
1566 window->user_data);
1567 else
1568 display_exit(window->display);
1569 break;
1570 case 1: /* fullscreen */
1571 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001572 if (window->fullscreen_handler)
1573 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001574 break;
1575 case 2: /* rotate */
1576 case 3: /* scale */
1577 break;
1578 }
1579}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001580
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001581void
1582window_show_frame_menu(struct window *window,
1583 struct input *input, uint32_t time)
1584{
1585 int32_t x, y;
1586
1587 static const char *entries[] = {
1588 "Close", "Fullscreen", "Rotate", "Scale"
1589 };
1590
1591 input_get_position(input, &x, &y);
1592 window_show_menu(window->display, input, time, window,
1593 x - 10, y - 10, frame_menu_func, entries, 4);
1594}
1595
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001596static int
1597frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001598 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001599{
1600 return frame_get_pointer_image_for_location(data, input);
1601}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001602
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603static int
1604frame_motion_handler(struct widget *widget,
1605 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001606 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001607{
1608 return frame_get_pointer_image_for_location(data, input);
1609}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001610
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001611static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001612frame_button_handler(struct widget *widget,
1613 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001614 uint32_t button, enum wl_pointer_button_state state,
1615 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001616
1617{
1618 struct frame *frame = data;
1619 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001620 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001621 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001622
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001623 location = theme_get_location(display->theme, input->sx, input->sy,
1624 frame->widget->allocation.width,
1625 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001626
Daniel Stone4dbadb12012-05-30 16:31:51 +01001627 if (window->display->shell && button == BTN_LEFT &&
1628 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001629 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001630 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001631 if (!window->shell_surface)
1632 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001633 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001634 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001635 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001636 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001637 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001638 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001639 case THEME_LOCATION_RESIZING_TOP:
1640 case THEME_LOCATION_RESIZING_BOTTOM:
1641 case THEME_LOCATION_RESIZING_LEFT:
1642 case THEME_LOCATION_RESIZING_RIGHT:
1643 case THEME_LOCATION_RESIZING_TOP_LEFT:
1644 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1645 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1646 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001647 if (!window->shell_surface)
1648 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001649 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001650
1651 if (!display->dpy) {
1652 /* If we're using shm, allocate a big
1653 pool to create buffers out of while
1654 we resize. We should probably base
1655 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001656 window->pool =
1657 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001658 }
1659
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001660 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001661 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001662 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001663 break;
1664 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001665 } else if (button == BTN_RIGHT &&
1666 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001667 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001668 }
1669}
1670
1671struct widget *
1672frame_create(struct window *window, void *data)
1673{
1674 struct frame *frame;
1675
1676 frame = malloc(sizeof *frame);
1677 memset(frame, 0, sizeof *frame);
1678
1679 frame->widget = window_add_widget(window, frame);
1680 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001681
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001682 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1683 widget_set_resize_handler(frame->widget, frame_resize_handler);
1684 widget_set_enter_handler(frame->widget, frame_enter_handler);
1685 widget_set_motion_handler(frame->widget, frame_motion_handler);
1686 widget_set_button_handler(frame->widget, frame_button_handler);
1687
Martin Minarik1998b152012-05-10 02:04:35 +02001688 /* Create empty list for frame buttons */
1689 wl_list_init(&frame->buttons_list);
1690
1691 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1692 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1693
1694 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1695 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1696
1697 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1698 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1699
1700 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1701 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1702
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001703 window->frame = frame;
1704
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001705 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001706}
1707
Kristian Høgsberga1627922012-06-20 17:30:03 -04001708void
1709frame_set_child_size(struct widget *widget, int child_width, int child_height)
1710{
1711 struct display *display = widget->window->display;
1712 struct theme *t = display->theme;
1713 int decoration_width, decoration_height;
1714 int width, height;
1715
1716 if (widget->window->type != TYPE_FULLSCREEN) {
1717 decoration_width = (t->width + t->margin) * 2;
1718 decoration_height = t->width +
1719 t->titlebar_height + t->margin * 2;
1720
1721 width = child_width + decoration_width;
1722 height = child_height + decoration_height;
1723 } else {
1724 width = child_width;
1725 height = child_height;
1726 }
1727
1728 window_schedule_resize(widget->window, width, height);
1729}
1730
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001731static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001732frame_destroy(struct frame *frame)
1733{
Martin Minarik1998b152012-05-10 02:04:35 +02001734 struct frame_button *button, *tmp;
1735
1736 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1737 frame_button_destroy(button);
1738
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001739 /* frame->child must be destroyed by the application */
1740 widget_destroy(frame->widget);
1741 free(frame);
1742}
1743
1744static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001745input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001746 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001747{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001748 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001749 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001750
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001751 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001752 return;
1753
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001754 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001755 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001756 widget = old;
1757 if (input->grab)
1758 widget = input->grab;
1759 if (widget->leave_handler)
1760 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001761 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001762 }
1763
1764 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001765 widget = focus;
1766 if (input->grab)
1767 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001768 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001769 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001770 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001771 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001772
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001773 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001774 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001775}
1776
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001777void
1778input_grab(struct input *input, struct widget *widget, uint32_t button)
1779{
1780 input->grab = widget;
1781 input->grab_button = button;
1782}
1783
1784void
1785input_ungrab(struct input *input)
1786{
1787 struct widget *widget;
1788
1789 input->grab = NULL;
1790 if (input->pointer_focus) {
1791 widget = widget_find_widget(input->pointer_focus->widget,
1792 input->sx, input->sy);
1793 input_set_focus_widget(input, widget, input->sx, input->sy);
1794 }
1795}
1796
1797static void
1798input_remove_pointer_focus(struct input *input)
1799{
1800 struct window *window = input->pointer_focus;
1801
1802 if (!window)
1803 return;
1804
1805 input_set_focus_widget(input, NULL, 0, 0);
1806
1807 input->pointer_focus = NULL;
1808 input->current_cursor = CURSOR_UNSET;
1809}
1810
1811static void
1812pointer_handle_enter(void *data, struct wl_pointer *pointer,
1813 uint32_t serial, struct wl_surface *surface,
1814 wl_fixed_t sx_w, wl_fixed_t sy_w)
1815{
1816 struct input *input = data;
1817 struct window *window;
1818 struct widget *widget;
1819 float sx = wl_fixed_to_double(sx_w);
1820 float sy = wl_fixed_to_double(sy_w);
1821
1822 if (!surface) {
1823 /* enter event for a window we've just destroyed */
1824 return;
1825 }
1826
1827 input->display->serial = serial;
1828 input->pointer_enter_serial = serial;
1829 input->pointer_focus = wl_surface_get_user_data(surface);
1830 window = input->pointer_focus;
1831
1832 if (window->pool) {
1833 shm_pool_destroy(window->pool);
1834 window->pool = NULL;
1835 /* Schedule a redraw to free the pool */
1836 window_schedule_redraw(window);
1837 }
1838
1839 input->sx = sx;
1840 input->sy = sy;
1841
1842 widget = widget_find_widget(window->widget, sx, sy);
1843 input_set_focus_widget(input, widget, sx, sy);
1844}
1845
1846static void
1847pointer_handle_leave(void *data, struct wl_pointer *pointer,
1848 uint32_t serial, struct wl_surface *surface)
1849{
1850 struct input *input = data;
1851
1852 input->display->serial = serial;
1853 input_remove_pointer_focus(input);
1854}
1855
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001856static void
Daniel Stone37816df2012-05-16 18:45:18 +01001857pointer_handle_motion(void *data, struct wl_pointer *pointer,
1858 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001859{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001860 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001861 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001862 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001863 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001864 float sx = wl_fixed_to_double(sx_w);
1865 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001866
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001867 input->sx = sx;
1868 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001869
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001870 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001871 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001872 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001873 }
1874
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001875 if (input->grab)
1876 widget = input->grab;
1877 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001878 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001879 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001880 cursor = widget->motion_handler(input->focus_widget,
1881 input, time, sx, sy,
1882 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001883
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001884 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001885}
1886
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001887static void
Daniel Stone37816df2012-05-16 18:45:18 +01001888pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001889 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001890{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001891 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001892 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001893 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001894
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001895 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001896 if (input->focus_widget && input->grab == NULL &&
1897 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001898 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001899
Neil Roberts6b28aad2012-01-23 19:11:18 +00001900 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001901 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001902 (*widget->button_handler)(widget,
1903 input, time,
1904 button, state,
1905 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001906
Daniel Stone4dbadb12012-05-30 16:31:51 +01001907 if (input->grab && input->grab_button == button &&
1908 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001909 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001910}
1911
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001912static void
Daniel Stone37816df2012-05-16 18:45:18 +01001913pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001914 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001915{
1916}
1917
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001918static const struct wl_pointer_listener pointer_listener = {
1919 pointer_handle_enter,
1920 pointer_handle_leave,
1921 pointer_handle_motion,
1922 pointer_handle_button,
1923 pointer_handle_axis,
1924};
1925
1926static void
1927input_remove_keyboard_focus(struct input *input)
1928{
1929 struct window *window = input->keyboard_focus;
1930 struct itimerspec its;
1931
1932 its.it_interval.tv_sec = 0;
1933 its.it_interval.tv_nsec = 0;
1934 its.it_value.tv_sec = 0;
1935 its.it_value.tv_nsec = 0;
1936 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1937
1938 if (!window)
1939 return;
1940
1941 window->keyboard_device = NULL;
1942 if (window->keyboard_focus_handler)
1943 (*window->keyboard_focus_handler)(window, NULL,
1944 window->user_data);
1945
1946 input->keyboard_focus = NULL;
1947}
1948
1949static void
1950keyboard_repeat_func(struct task *task, uint32_t events)
1951{
1952 struct input *input =
1953 container_of(task, struct input, repeat_task);
1954 struct window *window = input->keyboard_focus;
1955 uint64_t exp;
1956
1957 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1958 /* If we change the timer between the fd becoming
1959 * readable and getting here, there'll be nothing to
1960 * read and we get EAGAIN. */
1961 return;
1962
1963 if (window && window->key_handler) {
1964 (*window->key_handler)(window, input, input->repeat_time,
1965 input->repeat_key, input->repeat_sym,
1966 WL_KEYBOARD_KEY_STATE_PRESSED,
1967 window->user_data);
1968 }
1969}
1970
1971static void
1972keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1973 uint32_t format, int fd, uint32_t size)
1974{
1975 struct input *input = data;
1976 char *map_str;
1977
1978 if (!data) {
1979 close(fd);
1980 return;
1981 }
1982
1983 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1984 close(fd);
1985 return;
1986 }
1987
1988 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1989 if (map_str == MAP_FAILED) {
1990 close(fd);
1991 return;
1992 }
1993
1994 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
1995 map_str,
1996 XKB_KEYMAP_FORMAT_TEXT_V1,
1997 0);
1998 munmap(map_str, size);
1999 close(fd);
2000
2001 if (!input->xkb.keymap) {
2002 fprintf(stderr, "failed to compile keymap\n");
2003 return;
2004 }
2005
2006 input->xkb.state = xkb_state_new(input->xkb.keymap);
2007 if (!input->xkb.state) {
2008 fprintf(stderr, "failed to create XKB state\n");
2009 xkb_map_unref(input->xkb.keymap);
2010 input->xkb.keymap = NULL;
2011 return;
2012 }
2013
2014 input->xkb.control_mask =
2015 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2016 input->xkb.alt_mask =
2017 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2018 input->xkb.shift_mask =
2019 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2020}
2021
2022static void
2023keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2024 uint32_t serial, struct wl_surface *surface,
2025 struct wl_array *keys)
2026{
2027 struct input *input = data;
2028 struct window *window;
2029
2030 input->display->serial = serial;
2031 input->keyboard_focus = wl_surface_get_user_data(surface);
2032
2033 window = input->keyboard_focus;
2034 window->keyboard_device = input;
2035 if (window->keyboard_focus_handler)
2036 (*window->keyboard_focus_handler)(window,
2037 window->keyboard_device,
2038 window->user_data);
2039}
2040
2041static void
2042keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2043 uint32_t serial, struct wl_surface *surface)
2044{
2045 struct input *input = data;
2046
2047 input->display->serial = serial;
2048 input_remove_keyboard_focus(input);
2049}
2050
Scott Moreau210d0792012-03-22 10:47:01 -06002051static void
Daniel Stone37816df2012-05-16 18:45:18 +01002052keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002053 uint32_t serial, uint32_t time, uint32_t key,
2054 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002055{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002056 struct input *input = data;
2057 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002058 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002059 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002060 const xkb_keysym_t *syms;
2061 xkb_keysym_t sym;
2062 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002063 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002064
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002065 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002066 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002067 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002068 return;
2069
Daniel Stone97f68542012-05-30 16:32:01 +01002070 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002071
Daniel Stone97f68542012-05-30 16:32:01 +01002072 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002073 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002074 XKB_STATE_LATCHED);
2075 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002076 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002077 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002078 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002079 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002080 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002081 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002082
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002083 sym = XKB_KEY_NoSymbol;
2084 if (num_syms == 1)
2085 sym = syms[0];
2086
2087 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002088 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002089 window_set_maximized(window,
2090 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002091 } else if (sym == XKB_KEY_F11 &&
2092 window->fullscreen_handler &&
2093 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2094 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002095 } else if (sym == XKB_KEY_F4 &&
2096 input->modifiers == MOD_ALT_MASK &&
2097 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2098 if (window->close_handler)
2099 window->close_handler(window->parent,
2100 window->user_data);
2101 else
2102 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002103 } else if (window->key_handler) {
2104 (*window->key_handler)(window, input, time, key,
2105 sym, state, window->user_data);
2106 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002107
2108 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2109 key == input->repeat_key) {
2110 its.it_interval.tv_sec = 0;
2111 its.it_interval.tv_nsec = 0;
2112 its.it_value.tv_sec = 0;
2113 its.it_value.tv_nsec = 0;
2114 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2115 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2116 input->repeat_sym = sym;
2117 input->repeat_key = key;
2118 input->repeat_time = time;
2119 its.it_interval.tv_sec = 0;
2120 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2121 its.it_value.tv_sec = 0;
2122 its.it_value.tv_nsec = 400 * 1000 * 1000;
2123 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2124 }
2125}
2126
2127static void
Daniel Stone351eb612012-05-31 15:27:47 -04002128keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2129 uint32_t serial, uint32_t mods_depressed,
2130 uint32_t mods_latched, uint32_t mods_locked,
2131 uint32_t group)
2132{
2133 struct input *input = data;
2134
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002135 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2136 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002137}
2138
Daniel Stone37816df2012-05-16 18:45:18 +01002139static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002140 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002141 keyboard_handle_enter,
2142 keyboard_handle_leave,
2143 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002144 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002145};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002146
2147static void
Daniel Stone37816df2012-05-16 18:45:18 +01002148seat_handle_capabilities(void *data, struct wl_seat *seat,
2149 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002150{
Daniel Stone37816df2012-05-16 18:45:18 +01002151 struct input *input = data;
2152
2153 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2154 input->pointer = wl_seat_get_pointer(seat);
2155 wl_pointer_set_user_data(input->pointer, input);
2156 wl_pointer_add_listener(input->pointer, &pointer_listener,
2157 input);
2158 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2159 wl_pointer_destroy(input->pointer);
2160 input->pointer = NULL;
2161 }
2162
2163 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2164 input->keyboard = wl_seat_get_keyboard(seat);
2165 wl_keyboard_set_user_data(input->keyboard, input);
2166 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2167 input);
2168 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2169 wl_keyboard_destroy(input->keyboard);
2170 input->keyboard = NULL;
2171 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002172}
2173
Daniel Stone37816df2012-05-16 18:45:18 +01002174static const struct wl_seat_listener seat_listener = {
2175 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002176};
2177
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002178void
2179input_get_position(struct input *input, int32_t *x, int32_t *y)
2180{
2181 *x = input->sx;
2182 *y = input->sy;
2183}
2184
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002185struct display *
2186input_get_display(struct input *input)
2187{
2188 return input->display;
2189}
2190
Daniel Stone37816df2012-05-16 18:45:18 +01002191struct wl_seat *
2192input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002193{
Daniel Stone37816df2012-05-16 18:45:18 +01002194 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002195}
2196
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002197uint32_t
2198input_get_modifiers(struct input *input)
2199{
2200 return input->modifiers;
2201}
2202
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002203struct widget *
2204input_get_focus_widget(struct input *input)
2205{
2206 return input->focus_widget;
2207}
2208
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002209struct data_offer {
2210 struct wl_data_offer *offer;
2211 struct input *input;
2212 struct wl_array types;
2213 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002214
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002215 struct task io_task;
2216 int fd;
2217 data_func_t func;
2218 int32_t x, y;
2219 void *user_data;
2220};
2221
2222static void
2223data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2224{
2225 struct data_offer *offer = data;
2226 char **p;
2227
2228 p = wl_array_add(&offer->types, sizeof *p);
2229 *p = strdup(type);
2230}
2231
2232static const struct wl_data_offer_listener data_offer_listener = {
2233 data_offer_offer,
2234};
2235
2236static void
2237data_offer_destroy(struct data_offer *offer)
2238{
2239 char **p;
2240
2241 offer->refcount--;
2242 if (offer->refcount == 0) {
2243 wl_data_offer_destroy(offer->offer);
2244 for (p = offer->types.data; *p; p++)
2245 free(*p);
2246 wl_array_release(&offer->types);
2247 free(offer);
2248 }
2249}
2250
2251static void
2252data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002253 struct wl_data_device *data_device,
2254 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002255{
2256 struct data_offer *offer;
2257
2258 offer = malloc(sizeof *offer);
2259
2260 wl_array_init(&offer->types);
2261 offer->refcount = 1;
2262 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002263 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002264 wl_data_offer_add_listener(offer->offer,
2265 &data_offer_listener, offer);
2266}
2267
2268static void
2269data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002270 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002271 wl_fixed_t x_w, wl_fixed_t y_w,
2272 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002273{
2274 struct input *input = data;
2275 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002276 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002277 float x = wl_fixed_to_double(x_w);
2278 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002279 char **p;
2280
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002281 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002282 window = wl_surface_get_user_data(surface);
2283 input->pointer_focus = window;
2284
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002285 if (offer) {
2286 input->drag_offer = wl_data_offer_get_user_data(offer);
2287
2288 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2289 *p = NULL;
2290
2291 types_data = input->drag_offer->types.data;
2292 } else {
2293 input->drag_offer = NULL;
2294 types_data = NULL;
2295 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002296
2297 window = input->pointer_focus;
2298 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002299 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002300 window->user_data);
2301}
2302
2303static void
2304data_device_leave(void *data, struct wl_data_device *data_device)
2305{
2306 struct input *input = data;
2307
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002308 if (input->drag_offer) {
2309 data_offer_destroy(input->drag_offer);
2310 input->drag_offer = NULL;
2311 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002312}
2313
2314static void
2315data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002316 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002317{
2318 struct input *input = data;
2319 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002320 float x = wl_fixed_to_double(x_w);
2321 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002322 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002323
2324 input->sx = x;
2325 input->sy = y;
2326
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002327 if (input->drag_offer)
2328 types_data = input->drag_offer->types.data;
2329 else
2330 types_data = NULL;
2331
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002332 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002333 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002334 window->user_data);
2335}
2336
2337static void
2338data_device_drop(void *data, struct wl_data_device *data_device)
2339{
2340 struct input *input = data;
2341 struct window *window = input->pointer_focus;
2342
2343 if (window->drop_handler)
2344 window->drop_handler(window, input,
2345 input->sx, input->sy, window->user_data);
2346}
2347
2348static void
2349data_device_selection(void *data,
2350 struct wl_data_device *wl_data_device,
2351 struct wl_data_offer *offer)
2352{
2353 struct input *input = data;
2354 char **p;
2355
2356 if (input->selection_offer)
2357 data_offer_destroy(input->selection_offer);
2358
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002359 if (offer) {
2360 input->selection_offer = wl_data_offer_get_user_data(offer);
2361 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2362 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002363 } else {
2364 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002365 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002366}
2367
2368static const struct wl_data_device_listener data_device_listener = {
2369 data_device_data_offer,
2370 data_device_enter,
2371 data_device_leave,
2372 data_device_motion,
2373 data_device_drop,
2374 data_device_selection
2375};
2376
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002377static void
2378input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002379{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002380 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002381 struct wl_cursor *cursor;
2382 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002383
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002384 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002385 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002386 return;
2387
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002388 if (index >= (int) cursor->image_count) {
2389 fprintf(stderr, "cursor index out of range\n");
2390 return;
2391 }
2392
2393 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002394 buffer = wl_cursor_image_get_buffer(image);
2395 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002396 return;
2397
Kristian Høgsbergae277372012-08-01 09:41:08 -04002398 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002399 input->pointer_surface,
2400 image->hotspot_x, image->hotspot_y);
2401 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2402 wl_surface_damage(input->pointer_surface, 0, 0,
2403 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002404}
2405
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002406static const struct wl_callback_listener pointer_surface_listener;
2407
2408static void
2409pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2410 uint32_t time)
2411{
2412 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002413 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002414 int i;
2415
2416 if (callback) {
2417 assert(callback == input->cursor_frame_cb);
2418 wl_callback_destroy(callback);
2419 input->cursor_frame_cb = NULL;
2420 }
2421
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002422 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002423 wl_pointer_set_cursor(input->pointer,
2424 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002425 NULL, 0, 0);
2426 return;
2427 }
2428
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002429 if (input->current_cursor == CURSOR_UNSET)
2430 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002431 cursor = input->display->cursors[input->current_cursor];
2432 if (!cursor)
2433 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002434
2435 /* FIXME We don't have the current time on the first call so we set
2436 * the animation start to the time of the first frame callback. */
2437 if (time == 0)
2438 input->cursor_anim_start = 0;
2439 else if (input->cursor_anim_start == 0)
2440 input->cursor_anim_start = time;
2441
2442 if (time == 0 || input->cursor_anim_start == 0)
2443 i = 0;
2444 else
2445 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2446
2447 input_set_pointer_image_index(input, i);
2448
2449 if (cursor->image_count == 1)
2450 return;
2451
2452 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2453 wl_callback_add_listener(input->cursor_frame_cb,
2454 &pointer_surface_listener, input);
2455}
2456
2457static const struct wl_callback_listener pointer_surface_listener = {
2458 pointer_surface_frame_callback
2459};
2460
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002461void
2462input_set_pointer_image(struct input *input, int pointer)
2463{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002464 int force = 0;
2465
2466 if (input->pointer_enter_serial > input->cursor_serial)
2467 force = 1;
2468
2469 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002470 return;
2471
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002472 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002473 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002474 if (!input->cursor_frame_cb)
2475 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002476 else if (force) {
2477 /* The current frame callback may be stuck if, for instance,
2478 * the set cursor request was processed by the server after
2479 * this client lost the focus. In this case the cursor surface
2480 * might not be mapped and the frame callback wouldn't ever
2481 * complete. Send a set_cursor and attach to try to map the
2482 * cursor surface again so that the callback will finish */
2483 input_set_pointer_image_index(input, 0);
2484 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002485}
2486
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002487struct wl_data_device *
2488input_get_data_device(struct input *input)
2489{
2490 return input->data_device;
2491}
2492
2493void
2494input_set_selection(struct input *input,
2495 struct wl_data_source *source, uint32_t time)
2496{
2497 wl_data_device_set_selection(input->data_device, source, time);
2498}
2499
2500void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002501input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002502{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002503 wl_data_offer_accept(input->drag_offer->offer,
2504 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002505}
2506
2507static void
2508offer_io_func(struct task *task, uint32_t events)
2509{
2510 struct data_offer *offer =
2511 container_of(task, struct data_offer, io_task);
2512 unsigned int len;
2513 char buffer[4096];
2514
2515 len = read(offer->fd, buffer, sizeof buffer);
2516 offer->func(buffer, len,
2517 offer->x, offer->y, offer->user_data);
2518
2519 if (len == 0) {
2520 close(offer->fd);
2521 data_offer_destroy(offer);
2522 }
2523}
2524
2525static void
2526data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2527 data_func_t func, void *user_data)
2528{
2529 int p[2];
2530
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002531 if (pipe2(p, O_CLOEXEC) == -1)
2532 return;
2533
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002534 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2535 close(p[1]);
2536
2537 offer->io_task.run = offer_io_func;
2538 offer->fd = p[0];
2539 offer->func = func;
2540 offer->refcount++;
2541 offer->user_data = user_data;
2542
2543 display_watch_fd(offer->input->display,
2544 offer->fd, EPOLLIN, &offer->io_task);
2545}
2546
2547void
2548input_receive_drag_data(struct input *input, const char *mime_type,
2549 data_func_t func, void *data)
2550{
2551 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2552 input->drag_offer->x = input->sx;
2553 input->drag_offer->y = input->sy;
2554}
2555
2556int
2557input_receive_selection_data(struct input *input, const char *mime_type,
2558 data_func_t func, void *data)
2559{
2560 char **p;
2561
2562 if (input->selection_offer == NULL)
2563 return -1;
2564
2565 for (p = input->selection_offer->types.data; *p; p++)
2566 if (strcmp(mime_type, *p) == 0)
2567 break;
2568
2569 if (*p == NULL)
2570 return -1;
2571
2572 data_offer_receive_data(input->selection_offer,
2573 mime_type, func, data);
2574 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002575}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002576
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002577int
2578input_receive_selection_data_to_fd(struct input *input,
2579 const char *mime_type, int fd)
2580{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002581 if (input->selection_offer)
2582 wl_data_offer_receive(input->selection_offer->offer,
2583 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002584
2585 return 0;
2586}
2587
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002588void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002589window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002590{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002591 if (!window->shell_surface)
2592 return;
2593
Daniel Stone37816df2012-05-16 18:45:18 +01002594 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002595}
2596
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002597static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002598idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002599{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002600 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002601
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002602 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002603 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002604 widget_set_allocation(widget,
2605 window->pending_allocation.x,
2606 window->pending_allocation.y,
2607 window->pending_allocation.width,
2608 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002609
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002610 if (window->input_region) {
2611 wl_region_destroy(window->input_region);
2612 window->input_region = NULL;
2613 }
2614
2615 if (window->opaque_region) {
2616 wl_region_destroy(window->opaque_region);
2617 window->opaque_region = NULL;
2618 }
2619
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002620 if (widget->resize_handler)
2621 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002622 widget->allocation.width,
2623 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002624 widget->user_data);
2625
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002626 if (window->allocation.width != widget->allocation.width ||
2627 window->allocation.height != widget->allocation.height) {
2628 window->allocation = widget->allocation;
2629 window_schedule_redraw(window);
2630 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002631}
2632
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002633void
2634window_schedule_resize(struct window *window, int width, int height)
2635{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002636 window->pending_allocation.x = 0;
2637 window->pending_allocation.y = 0;
2638 window->pending_allocation.width = width;
2639 window->pending_allocation.height = height;
2640
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002641 if (window->min_allocation.width == 0)
2642 window->min_allocation = window->pending_allocation;
2643 if (window->pending_allocation.width < window->min_allocation.width)
2644 window->pending_allocation.width = window->min_allocation.width;
2645 if (window->pending_allocation.height < window->min_allocation.height)
2646 window->pending_allocation.height = window->min_allocation.height;
2647
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002648 window->resize_needed = 1;
2649 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002650}
2651
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002652void
2653widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2654{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002655 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002656}
2657
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002658static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002659handle_ping(void *data, struct wl_shell_surface *shell_surface,
2660 uint32_t serial)
2661{
2662 wl_shell_surface_pong(shell_surface, serial);
2663}
2664
2665static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002666handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002667 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002668{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002669 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002670
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002671 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002672 return;
2673
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002674 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002675 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002676}
2677
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002678static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002679menu_destroy(struct menu *menu)
2680{
2681 widget_destroy(menu->widget);
2682 window_destroy(menu->window);
2683 free(menu);
2684}
2685
2686static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002687handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2688{
2689 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002690 struct menu *menu = window->widget->user_data;
2691
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002692 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002693 * device. Or just use wl_callback. And this really needs to
2694 * be a window vfunc that the menu can set. And we need the
2695 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002696
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002697 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002698 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002699 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002700}
2701
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002702static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002703 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002704 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002705 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002706};
2707
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002708void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002709window_get_allocation(struct window *window,
2710 struct rectangle *allocation)
2711{
2712 *allocation = window->allocation;
2713}
2714
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002715static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002716widget_redraw(struct widget *widget)
2717{
2718 struct widget *child;
2719
2720 if (widget->redraw_handler)
2721 widget->redraw_handler(widget, widget->user_data);
2722 wl_list_for_each(child, &widget->child_list, link)
2723 widget_redraw(child);
2724}
2725
2726static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002727frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2728{
2729 struct window *window = data;
2730
2731 wl_callback_destroy(callback);
2732 window->redraw_scheduled = 0;
2733 if (window->redraw_needed)
2734 window_schedule_redraw(window);
2735}
2736
2737static const struct wl_callback_listener listener = {
2738 frame_callback
2739};
2740
2741static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002742idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002743{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002744 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002745 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002746
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002747 if (window->resize_needed)
2748 idle_resize(window);
2749
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002750 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002751 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002752 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002753 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002754 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002755
2756 callback = wl_surface_frame(window->surface);
2757 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002758}
2759
2760void
2761window_schedule_redraw(struct window *window)
2762{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002763 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002764 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002765 window->redraw_task.run = idle_redraw;
2766 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002767 window->redraw_scheduled = 1;
2768 }
2769}
2770
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002771void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002772window_set_fullscreen(struct window *window, int fullscreen)
2773{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002774 if (!window->display->shell)
2775 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002776
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002777 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002778 return;
2779
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002780 if (fullscreen) {
2781 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002782 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002783 wl_shell_surface_set_fullscreen(window->shell_surface,
2784 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2785 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002786 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002787 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002788 wl_shell_surface_set_toplevel(window->shell_surface);
2789 window_schedule_resize(window,
2790 window->saved_allocation.width,
2791 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002792 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002793}
2794
2795void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002796window_set_maximized(struct window *window, int maximized)
2797{
2798 if (!window->display->shell)
2799 return;
2800
2801 if ((window->type == TYPE_MAXIMIZED) == maximized)
2802 return;
2803
2804 if (window->type == TYPE_TOPLEVEL) {
2805 window->saved_allocation = window->allocation;
2806 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2807 window->type = TYPE_MAXIMIZED;
2808 } else {
2809 wl_shell_surface_set_toplevel(window->shell_surface);
2810 window->type = TYPE_TOPLEVEL;
2811 window_schedule_resize(window,
2812 window->saved_allocation.width,
2813 window->saved_allocation.height);
2814 }
2815}
2816
2817void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002818window_set_user_data(struct window *window, void *data)
2819{
2820 window->user_data = data;
2821}
2822
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002823void *
2824window_get_user_data(struct window *window)
2825{
2826 return window->user_data;
2827}
2828
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002829void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002830window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002831 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002832{
2833 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002834}
2835
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002836void
2837window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002838 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002839{
2840 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002841}
2842
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002843void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002844window_set_data_handler(struct window *window, window_data_handler_t handler)
2845{
2846 window->data_handler = handler;
2847}
2848
2849void
2850window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2851{
2852 window->drop_handler = handler;
2853}
2854
2855void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002856window_set_close_handler(struct window *window,
2857 window_close_handler_t handler)
2858{
2859 window->close_handler = handler;
2860}
2861
2862void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002863window_set_fullscreen_handler(struct window *window,
2864 window_fullscreen_handler_t handler)
2865{
2866 window->fullscreen_handler = handler;
2867}
2868
2869void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002870window_set_title(struct window *window, const char *title)
2871{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002872 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002873 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002874 if (window->shell_surface)
2875 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002876}
2877
2878const char *
2879window_get_title(struct window *window)
2880{
2881 return window->title;
2882}
2883
2884void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002885window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2886{
2887 struct text_cursor_position *text_cursor_position =
2888 window->display->text_cursor_position;
2889
Scott Moreau9295ce02012-06-01 12:46:10 -06002890 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002891 return;
2892
2893 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002894 window->surface,
2895 wl_fixed_from_int(x),
2896 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002897}
2898
2899void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002900window_damage(struct window *window, int32_t x, int32_t y,
2901 int32_t width, int32_t height)
2902{
2903 wl_surface_damage(window->surface, x, y, width, height);
2904}
2905
Casey Dahlin9074db52012-04-19 22:50:09 -04002906static void
2907surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002908 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002909{
Rob Bradford7507b572012-05-15 17:55:34 +01002910 struct window *window = data;
2911 struct output *output;
2912 struct output *output_found = NULL;
2913 struct window_output *window_output;
2914
2915 wl_list_for_each(output, &window->display->output_list, link) {
2916 if (output->output == wl_output) {
2917 output_found = output;
2918 break;
2919 }
2920 }
2921
2922 if (!output_found)
2923 return;
2924
2925 window_output = malloc (sizeof *window_output);
2926 window_output->output = output_found;
2927
2928 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002929}
2930
2931static void
2932surface_leave(void *data,
2933 struct wl_surface *wl_surface, struct wl_output *output)
2934{
Rob Bradford7507b572012-05-15 17:55:34 +01002935 struct window *window = data;
2936 struct window_output *window_output;
2937 struct window_output *window_output_found = NULL;
2938
2939 wl_list_for_each(window_output, &window->window_output_list, link) {
2940 if (window_output->output->output == output) {
2941 window_output_found = window_output;
2942 break;
2943 }
2944 }
2945
2946 if (window_output_found) {
2947 wl_list_remove(&window_output_found->link);
2948 free(window_output_found);
2949 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002950}
2951
2952static const struct wl_surface_listener surface_listener = {
2953 surface_enter,
2954 surface_leave
2955};
2956
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002957static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002958window_create_internal(struct display *display,
2959 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002960{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002961 struct window *window;
2962
2963 window = malloc(sizeof *window);
2964 if (window == NULL)
2965 return NULL;
2966
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002967 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002968 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002969 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002970 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002971 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002972 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002973 window->shell_surface =
2974 wl_shell_get_shell_surface(display->shell,
2975 window->surface);
2976 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002977 window->allocation.x = 0;
2978 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002979 window->allocation.width = 0;
2980 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002981 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002982 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002983 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002984 window->input_region = NULL;
2985 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002986
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002987 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002988#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002989 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002990#else
2991 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2992#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002993 else
2994 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002995
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002996 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002997 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002998 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002999
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003000 if (window->shell_surface) {
3001 wl_shell_surface_set_user_data(window->shell_surface, window);
3002 wl_shell_surface_add_listener(window->shell_surface,
3003 &shell_surface_listener, window);
3004 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003005
Rob Bradford7507b572012-05-15 17:55:34 +01003006 wl_list_init (&window->window_output_list);
3007
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003008 return window;
3009}
3010
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003011struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003012window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003013{
3014 struct window *window;
3015
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003016 window = window_create_internal(display, NULL, TYPE_NONE);
3017 if (!window)
3018 return NULL;
3019
3020 return window;
3021}
3022
3023struct window *
3024window_create_custom(struct display *display)
3025{
3026 struct window *window;
3027
3028 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003029 if (!window)
3030 return NULL;
3031
3032 return window;
3033}
3034
3035struct window *
3036window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003037 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003038{
3039 struct window *window;
3040
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003041 window = window_create_internal(parent->display,
3042 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003043 if (!window)
3044 return NULL;
3045
3046 window->x = x;
3047 window->y = y;
3048
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003049 if (display->shell)
3050 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003051 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003052 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003053
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003054 return window;
3055}
3056
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003057static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003058menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003059{
3060 int next;
3061
3062 next = (sy - 8) / 20;
3063 if (menu->current != next) {
3064 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003065 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003066 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003067}
3068
3069static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003070menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003071 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003072 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øgsbergbbedd7e2011-12-19 15:40:10 -05003080}
3081
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003082static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003083menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003084 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003085{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003086 struct menu *menu = data;
3087
3088 if (widget == menu->widget)
3089 menu_set_item(data, y);
3090
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003091 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003092}
3093
3094static void
3095menu_leave_handler(struct widget *widget, struct input *input, void *data)
3096{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003097 struct menu *menu = data;
3098
3099 if (widget == menu->widget)
3100 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003101}
3102
3103static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003104menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003105 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003106 uint32_t button, enum wl_pointer_button_state state,
3107 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003108
3109{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003110 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003111
Daniel Stone4dbadb12012-05-30 16:31:51 +01003112 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3113 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003114 /* Either relase after press-drag-release or
3115 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003116 menu->func(menu->window->parent,
3117 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003118 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003119 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003120 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003121}
3122
3123static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003124menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003125{
3126 cairo_t *cr;
3127 const int32_t r = 3, margin = 3;
3128 struct menu *menu = data;
3129 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003130 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003131
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003132 cr = cairo_create(window->cairo_surface);
3133 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3134 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3135 cairo_paint(cr);
3136
3137 width = window->allocation.width;
3138 height = window->allocation.height;
3139 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003140
3141 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003142 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3143 cairo_fill(cr);
3144
3145 for (i = 0; i < menu->count; i++) {
3146 if (i == menu->current) {
3147 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3148 cairo_rectangle(cr, margin, i * 20 + margin,
3149 width - 2 * margin, 20);
3150 cairo_fill(cr);
3151 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3152 cairo_move_to(cr, 10, i * 20 + 16);
3153 cairo_show_text(cr, menu->entries[i]);
3154 } else {
3155 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3156 cairo_move_to(cr, 10, i * 20 + 16);
3157 cairo_show_text(cr, menu->entries[i]);
3158 }
3159 }
3160
3161 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003162}
3163
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003164void
3165window_show_menu(struct display *display,
3166 struct input *input, uint32_t time, struct window *parent,
3167 int32_t x, int32_t y,
3168 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003169{
3170 struct window *window;
3171 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003172 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003173
3174 menu = malloc(sizeof *menu);
3175 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003176 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003177
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003178 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003179 if (!window) {
3180 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003181 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003182 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003183
3184 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003185 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003186 menu->entries = entries;
3187 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003188 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003189 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003190 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003191 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003192 window->type = TYPE_MENU;
3193 window->x = x;
3194 window->y = y;
3195
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003196 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003197 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003198 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003199 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003200 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003201
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003202 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003203 widget_set_enter_handler(menu->widget, menu_enter_handler);
3204 widget_set_leave_handler(menu->widget, menu_leave_handler);
3205 widget_set_motion_handler(menu->widget, menu_motion_handler);
3206 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003207
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003208 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003209 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003210}
3211
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003212void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003213window_set_buffer_type(struct window *window, enum window_buffer_type type)
3214{
3215 window->buffer_type = type;
3216}
3217
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003218
3219static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003220display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003221 struct wl_output *wl_output,
3222 int x, int y,
3223 int physical_width,
3224 int physical_height,
3225 int subpixel,
3226 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003227 const char *model,
3228 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003229{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003230 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003231
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003232 output->allocation.x = x;
3233 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003234}
3235
3236static void
3237display_handle_mode(void *data,
3238 struct wl_output *wl_output,
3239 uint32_t flags,
3240 int width,
3241 int height,
3242 int refresh)
3243{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003244 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003245 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003246
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003247 if (flags & WL_OUTPUT_MODE_CURRENT) {
3248 output->allocation.width = width;
3249 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003250 if (display->output_configure_handler)
3251 (*display->output_configure_handler)(
3252 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003253 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003254}
3255
3256static const struct wl_output_listener output_listener = {
3257 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003258 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003259};
3260
3261static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003262display_add_output(struct display *d, uint32_t id)
3263{
3264 struct output *output;
3265
3266 output = malloc(sizeof *output);
3267 if (output == NULL)
3268 return;
3269
3270 memset(output, 0, sizeof *output);
3271 output->display = d;
3272 output->output =
3273 wl_display_bind(d->display, id, &wl_output_interface);
3274 wl_list_insert(d->output_list.prev, &output->link);
3275
3276 wl_output_add_listener(output->output, &output_listener, output);
3277}
3278
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003279static void
3280output_destroy(struct output *output)
3281{
3282 if (output->destroy_handler)
3283 (*output->destroy_handler)(output, output->user_data);
3284
3285 wl_output_destroy(output->output);
3286 wl_list_remove(&output->link);
3287 free(output);
3288}
3289
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003290void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003291display_set_output_configure_handler(struct display *display,
3292 display_output_handler_t handler)
3293{
3294 struct output *output;
3295
3296 display->output_configure_handler = handler;
3297 if (!handler)
3298 return;
3299
3300 wl_list_for_each(output, &display->output_list, link)
3301 (*display->output_configure_handler)(output,
3302 display->user_data);
3303}
3304
3305void
3306output_set_user_data(struct output *output, void *data)
3307{
3308 output->user_data = data;
3309}
3310
3311void *
3312output_get_user_data(struct output *output)
3313{
3314 return output->user_data;
3315}
3316
3317void
3318output_set_destroy_handler(struct output *output,
3319 display_output_handler_t handler)
3320{
3321 output->destroy_handler = handler;
3322 /* FIXME: implement this, once we have way to remove outputs */
3323}
3324
3325void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003326output_get_allocation(struct output *output, struct rectangle *allocation)
3327{
3328 *allocation = output->allocation;
3329}
3330
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003331struct wl_output *
3332output_get_wl_output(struct output *output)
3333{
3334 return output->output;
3335}
3336
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003337static void
Daniel Stone97f68542012-05-30 16:32:01 +01003338fini_xkb(struct input *input)
3339{
3340 xkb_state_unref(input->xkb.state);
3341 xkb_map_unref(input->xkb.keymap);
3342}
3343
3344static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003345display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003346{
3347 struct input *input;
3348
3349 input = malloc(sizeof *input);
3350 if (input == NULL)
3351 return;
3352
3353 memset(input, 0, sizeof *input);
3354 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003355 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003356 input->pointer_focus = NULL;
3357 input->keyboard_focus = NULL;
3358 wl_list_insert(d->input_list.prev, &input->link);
3359
Daniel Stone37816df2012-05-16 18:45:18 +01003360 wl_seat_add_listener(input->seat, &seat_listener, input);
3361 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003362
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003363 input->data_device =
3364 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003365 input->seat);
3366 wl_data_device_add_listener(input->data_device, &data_device_listener,
3367 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003368
3369 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003370
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003371 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3372 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003373 input->repeat_task.run = keyboard_repeat_func;
3374 display_watch_fd(d, input->repeat_timer_fd,
3375 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003376}
3377
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003378static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003379input_destroy(struct input *input)
3380{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003381 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003382 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003383
3384 if (input->drag_offer)
3385 data_offer_destroy(input->drag_offer);
3386
3387 if (input->selection_offer)
3388 data_offer_destroy(input->selection_offer);
3389
3390 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003391 fini_xkb(input);
3392
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003393 wl_surface_destroy(input->pointer_surface);
3394
Pekka Paalanene1207c72011-12-16 12:02:09 +02003395 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003396 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003397 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003398 free(input);
3399}
3400
3401static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003402display_handle_global(struct wl_display *display, uint32_t id,
3403 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003404{
3405 struct display *d = data;
3406
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003407 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003408 d->compositor =
3409 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003410 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003411 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003412 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003413 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003414 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003415 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003416 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003417 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003418 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3419 d->data_device_manager =
3420 wl_display_bind(display, id,
3421 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003422 } else if (strcmp(interface, "text_cursor_position") == 0) {
3423 d->text_cursor_position =
3424 wl_display_bind(display, id,
3425 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003426 }
3427}
3428
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003429#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003430static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003431init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003432{
3433 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003434 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003435
Rob Clark6396ed32012-03-11 19:48:41 -05003436#ifdef USE_CAIRO_GLESV2
3437# define GL_BIT EGL_OPENGL_ES2_BIT
3438#else
3439# define GL_BIT EGL_OPENGL_BIT
3440#endif
3441
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003442 static const EGLint argb_cfg_attribs[] = {
3443 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003444 EGL_RED_SIZE, 1,
3445 EGL_GREEN_SIZE, 1,
3446 EGL_BLUE_SIZE, 1,
3447 EGL_ALPHA_SIZE, 1,
3448 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003449 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003450 EGL_NONE
3451 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003452
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003453#ifdef USE_CAIRO_GLESV2
3454 static const EGLint context_attribs[] = {
3455 EGL_CONTEXT_CLIENT_VERSION, 2,
3456 EGL_NONE
3457 };
3458 EGLint api = EGL_OPENGL_ES_API;
3459#else
3460 EGLint *context_attribs = NULL;
3461 EGLint api = EGL_OPENGL_API;
3462#endif
3463
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003464 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003465 if (!eglInitialize(d->dpy, &major, &minor)) {
3466 fprintf(stderr, "failed to initialize display\n");
3467 return -1;
3468 }
3469
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003470 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003471 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3472 return -1;
3473 }
3474
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003475 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3476 &d->argb_config, 1, &n) || n != 1) {
3477 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003478 return -1;
3479 }
3480
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003481 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003482 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003483 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003484 fprintf(stderr, "failed to create context\n");
3485 return -1;
3486 }
3487
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003488 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003489 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003490 return -1;
3491 }
3492
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003493#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003494 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3495 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3496 fprintf(stderr, "failed to get cairo egl argb device\n");
3497 return -1;
3498 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003499#endif
3500
3501 return 0;
3502}
3503
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003504static void
3505fini_egl(struct display *display)
3506{
3507#ifdef HAVE_CAIRO_EGL
3508 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003509#endif
3510
3511 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3512 EGL_NO_CONTEXT);
3513
3514 eglTerminate(display->dpy);
3515 eglReleaseThread();
3516}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003517#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003518
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003519static int
3520event_mask_update(uint32_t mask, void *data)
3521{
3522 struct display *d = data;
3523
3524 d->mask = mask;
3525
3526 return 0;
3527}
3528
3529static void
3530handle_display_data(struct task *task, uint32_t events)
3531{
3532 struct display *display =
3533 container_of(task, struct display, display_task);
3534
3535 wl_display_iterate(display->display, display->mask);
3536}
3537
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003538struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003539display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003540{
3541 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003542
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003543 d = malloc(sizeof *d);
3544 if (d == NULL)
3545 return NULL;
3546
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003547 memset(d, 0, sizeof *d);
3548
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003549 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003550 if (d->display == NULL) {
3551 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003552 return NULL;
3553 }
3554
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003555 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003556 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3557 d->display_task.run = handle_display_data;
3558 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3559
3560 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003561 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003562 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003563
Daniel Stone97f68542012-05-30 16:32:01 +01003564 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003565 if (d->xkb_context == NULL) {
3566 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003567 return NULL;
3568 }
3569
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003570 /* Set up listener so we'll catch all events. */
3571 wl_display_add_global_listener(d->display,
3572 display_handle_global, d);
3573
3574 /* Process connection events. */
3575 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003576#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003577 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003578 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003579#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003580
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003581 d->image_target_texture_2d =
3582 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3583 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3584 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3585
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003586 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003587
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003588 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003589
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003590 wl_list_init(&d->window_list);
3591
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003592 return d;
3593}
3594
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003595static void
3596display_destroy_outputs(struct display *display)
3597{
3598 struct output *tmp;
3599 struct output *output;
3600
3601 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3602 output_destroy(output);
3603}
3604
Pekka Paalanene1207c72011-12-16 12:02:09 +02003605static void
3606display_destroy_inputs(struct display *display)
3607{
3608 struct input *tmp;
3609 struct input *input;
3610
3611 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3612 input_destroy(input);
3613}
3614
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003615void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003616display_destroy(struct display *display)
3617{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003618 if (!wl_list_empty(&display->window_list))
3619 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3620
3621 if (!wl_list_empty(&display->deferred_list))
3622 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3623
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003624 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003625 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003626
Daniel Stone97f68542012-05-30 16:32:01 +01003627 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003628
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003629 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003630 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003631
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003632#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003633 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003634#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003635
Pekka Paalanenc2052982011-12-16 11:41:32 +02003636 if (display->shell)
3637 wl_shell_destroy(display->shell);
3638
3639 if (display->shm)
3640 wl_shm_destroy(display->shm);
3641
3642 if (display->data_device_manager)
3643 wl_data_device_manager_destroy(display->data_device_manager);
3644
3645 wl_compositor_destroy(display->compositor);
3646
3647 close(display->epoll_fd);
3648
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003649 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003650 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003651 free(display);
3652}
3653
3654void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003655display_set_user_data(struct display *display, void *data)
3656{
3657 display->user_data = data;
3658}
3659
3660void *
3661display_get_user_data(struct display *display)
3662{
3663 return display->user_data;
3664}
3665
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003666struct wl_display *
3667display_get_display(struct display *display)
3668{
3669 return display->display;
3670}
3671
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003672struct output *
3673display_get_output(struct display *display)
3674{
3675 return container_of(display->output_list.next, struct output, link);
3676}
3677
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003678struct wl_compositor *
3679display_get_compositor(struct display *display)
3680{
3681 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003682}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003683
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003684uint32_t
3685display_get_serial(struct display *display)
3686{
3687 return display->serial;
3688}
3689
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003690EGLDisplay
3691display_get_egl_display(struct display *d)
3692{
3693 return d->dpy;
3694}
3695
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003696struct wl_data_source *
3697display_create_data_source(struct display *display)
3698{
3699 return wl_data_device_manager_create_data_source(display->data_device_manager);
3700}
3701
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003702EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003703display_get_argb_egl_config(struct display *d)
3704{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003705 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003706}
3707
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003708struct wl_shell *
3709display_get_shell(struct display *display)
3710{
3711 return display->shell;
3712}
3713
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003714int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003715display_acquire_window_surface(struct display *display,
3716 struct window *window,
3717 EGLContext ctx)
3718{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003719#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003720 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003721 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003722
3723 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003724 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003725 device = cairo_surface_get_device(window->cairo_surface);
3726 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003727 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003728
Benjamin Franzke0c991632011-09-27 21:57:31 +02003729 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003730 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003731 ctx = display->argb_ctx;
3732 else
3733 assert(0);
3734 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003735
3736 data = cairo_surface_get_user_data(window->cairo_surface,
3737 &surface_data_key);
3738
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003739 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003740 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003741 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3742 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003743
3744 return 0;
3745#else
3746 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003747#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003748}
3749
3750void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003751display_release_window_surface(struct display *display,
3752 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003753{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003754#ifdef HAVE_CAIRO_EGL
3755 cairo_device_t *device;
3756
3757 device = cairo_surface_get_device(window->cairo_surface);
3758 if (!device)
3759 return;
3760
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003761 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003762 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003763 cairo_device_release(device);
3764#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003765}
3766
3767void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003768display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003769{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003770 wl_list_insert(&display->deferred_list, &task->link);
3771}
3772
3773void
3774display_watch_fd(struct display *display,
3775 int fd, uint32_t events, struct task *task)
3776{
3777 struct epoll_event ep;
3778
3779 ep.events = events;
3780 ep.data.ptr = task;
3781 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3782}
3783
3784void
3785display_run(struct display *display)
3786{
3787 struct task *task;
3788 struct epoll_event ep[16];
3789 int i, count;
3790
Pekka Paalanen826d7952011-12-15 10:14:07 +02003791 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003792 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003793 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003794
3795 while (!wl_list_empty(&display->deferred_list)) {
3796 task = container_of(display->deferred_list.next,
3797 struct task, link);
3798 wl_list_remove(&task->link);
3799 task->run(task, 0);
3800 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003801
3802 if (!display->running)
3803 break;
3804
3805 wl_display_flush(display->display);
3806
3807 count = epoll_wait(display->epoll_fd,
3808 ep, ARRAY_LENGTH(ep), -1);
3809 for (i = 0; i < count; i++) {
3810 task = ep[i].data.ptr;
3811 task->run(task, ep[i].events);
3812 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003813 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003814}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003815
3816void
3817display_exit(struct display *display)
3818{
3819 display->running = 0;
3820}