blob: c20221c7b92d96d3241d81d432b9a3ba38761a77 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060064#include "text-cursor-position-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030065#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070069struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030070
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050073 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040074 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040075 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040076 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060077 struct text_cursor_position *text_cursor_position;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050079 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020081 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040082 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040083
84 int display_fd;
85 uint32_t mask;
86 struct task display_task;
87
88 int epoll_fd;
89 struct wl_list deferred_list;
90
Pekka Paalanen826d7952011-12-15 10:14:07 +020091 int running;
92
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040094 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050095 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040096
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040097 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030099 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300100 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400101
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500102 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
103 PFNEGLCREATEIMAGEKHRPROC create_image;
104 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200105
106 display_output_handler_t output_configure_handler;
107
108 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100109
110 struct xkb_context *xkb_context;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500111};
112
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400114 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400116 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500117 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500119 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_CUSTOM
121};
Rob Bradford7507b572012-05-15 17:55:34 +0100122
123struct window_output {
124 struct output *output;
125 struct wl_list link;
126};
127
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128struct window {
129 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500130 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100131 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500132 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200133 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500134 struct wl_region *input_region;
135 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500136 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400138 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500139 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500140 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400141 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400142 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400143 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400144 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400145 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400146 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400147 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400148 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400149 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500150
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400151 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500152
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700153 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400154
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500155 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500156 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400157 window_data_handler_t data_handler;
158 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500159 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400160 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200162 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500163 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500164
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500165 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400166 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500167};
168
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500169struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500170 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300171 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500172 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400173 struct wl_list link;
174 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500175 widget_resize_handler_t resize_handler;
176 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500177 widget_enter_handler_t enter_handler;
178 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500179 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500180 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400181 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500182 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300183 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400184};
185
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400186struct input {
187 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100188 struct wl_seat *seat;
189 struct wl_pointer *pointer;
190 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400191 struct window *pointer_focus;
192 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300193 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300194 uint32_t cursor_anim_start;
195 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300196 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400197 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400198 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400199 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400200 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400201 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400202
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500203 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500204 struct widget *grab;
205 uint32_t grab_button;
206
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400207 struct wl_data_device *data_device;
208 struct data_offer *drag_offer;
209 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100210
211 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100212 struct xkb_keymap *keymap;
213 struct xkb_state *state;
214 xkb_mod_mask_t control_mask;
215 xkb_mod_mask_t alt_mask;
216 xkb_mod_mask_t shift_mask;
217 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400218
219 struct task repeat_task;
220 int repeat_timer_fd;
221 uint32_t repeat_sym;
222 uint32_t repeat_key;
223 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400224};
225
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500226struct output {
227 struct display *display;
228 struct wl_output *output;
229 struct rectangle allocation;
230 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200231
232 display_output_handler_t destroy_handler;
233 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500234};
235
Martin Minarik1998b152012-05-10 02:04:35 +0200236enum frame_button_action {
237 FRAME_BUTTON_NULL = 0,
238 FRAME_BUTTON_ICON = 1,
239 FRAME_BUTTON_CLOSE = 2,
240 FRAME_BUTTON_MINIMIZE = 3,
241 FRAME_BUTTON_MAXIMIZE = 4,
242};
243
244enum frame_button_pointer {
245 FRAME_BUTTON_DEFAULT = 0,
246 FRAME_BUTTON_OVER = 1,
247 FRAME_BUTTON_ACTIVE = 2,
248};
249
250enum frame_button_align {
251 FRAME_BUTTON_RIGHT = 0,
252 FRAME_BUTTON_LEFT = 1,
253};
254
255enum frame_button_decoration {
256 FRAME_BUTTON_NONE = 0,
257 FRAME_BUTTON_FANCY = 1,
258};
259
260struct frame_button {
261 struct widget *widget;
262 struct frame *frame;
263 cairo_surface_t *icon;
264 enum frame_button_action type;
265 enum frame_button_pointer state;
266 struct wl_list link; /* buttons_list */
267 enum frame_button_align align;
268 enum frame_button_decoration decoration;
269};
270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500271struct frame {
272 struct widget *widget;
273 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200274 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500275};
276
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500277struct menu {
278 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500279 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500280 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500281 const char **entries;
282 uint32_t time;
283 int current;
284 int count;
285 menu_func_t func;
286};
287
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300288struct tooltip {
289 struct widget *parent;
290 struct window *window;
291 struct widget *widget;
292 char *entry;
293 struct task tooltip_task;
294 int tooltip_fd;
295 float x, y;
296};
297
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700298struct shm_pool {
299 struct wl_shm_pool *pool;
300 size_t size;
301 size_t used;
302 void *data;
303};
304
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400305enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300306 CURSOR_DEFAULT = 100,
307 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400308};
309
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500310enum window_location {
311 WINDOW_INTERIOR = 0,
312 WINDOW_RESIZING_TOP = 1,
313 WINDOW_RESIZING_BOTTOM = 2,
314 WINDOW_RESIZING_LEFT = 4,
315 WINDOW_RESIZING_TOP_LEFT = 5,
316 WINDOW_RESIZING_BOTTOM_LEFT = 6,
317 WINDOW_RESIZING_RIGHT = 8,
318 WINDOW_RESIZING_TOP_RIGHT = 9,
319 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
320 WINDOW_RESIZING_MASK = 15,
321 WINDOW_EXTERIOR = 16,
322 WINDOW_TITLEBAR = 17,
323 WINDOW_CLIENT_AREA = 18,
324};
325
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326static const cairo_user_data_key_t surface_data_key;
327struct surface_data {
328 struct wl_buffer *buffer;
329};
330
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500331#define MULT(_d,c,a,t) \
332 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
333
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500334#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100336struct egl_window_surface_data {
337 struct display *display;
338 struct wl_surface *surface;
339 struct wl_egl_window *window;
340 EGLSurface surf;
341};
342
343static void
344egl_window_surface_data_destroy(void *p)
345{
346 struct egl_window_surface_data *data = p;
347 struct display *d = data->display;
348
349 eglDestroySurface(d->dpy, data->surf);
350 wl_egl_window_destroy(data->window);
351 data->surface = NULL;
352
353 free(p);
354}
355
356static cairo_surface_t *
357display_create_egl_window_surface(struct display *display,
358 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400359 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100360 struct rectangle *rectangle)
361{
362 cairo_surface_t *cairo_surface;
363 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400364 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200365 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100366
367 data = malloc(sizeof *data);
368 if (data == NULL)
369 return NULL;
370
371 data->display = display;
372 data->surface = surface;
373
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500374 config = display->argb_config;
375 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400377 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400379 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100380
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400381 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500382 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383
Benjamin Franzke0c991632011-09-27 21:57:31 +0200384 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100385 data->surf,
386 rectangle->width,
387 rectangle->height);
388
389 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
390 data, egl_window_surface_data_destroy);
391
392 return cairo_surface;
393}
394
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395#endif
396
397struct wl_buffer *
398display_get_buffer_for_surface(struct display *display,
399 cairo_surface_t *surface)
400{
401 struct surface_data *data;
402
403 data = cairo_surface_get_user_data (surface, &surface_data_key);
404
405 return data->buffer;
406}
407
408struct shm_surface_data {
409 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700410 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411};
412
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500413static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700414shm_pool_destroy(struct shm_pool *pool);
415
416static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417shm_surface_data_destroy(void *p)
418{
419 struct shm_surface_data *data = p;
420
421 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700422 if (data->pool)
423 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300424
425 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426}
427
Kristian Høgsberg16626282012-04-03 11:21:27 -0400428static struct wl_shm_pool *
429make_shm_pool(struct display *display, int size, void **data)
430{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400431 struct wl_shm_pool *pool;
432 int fd;
433
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300434 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400435 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300436 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
437 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400438 return NULL;
439 }
440
441 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400442 if (*data == MAP_FAILED) {
443 fprintf(stderr, "mmap failed: %m\n");
444 close(fd);
445 return NULL;
446 }
447
448 pool = wl_shm_create_pool(display->shm, fd, size);
449
450 close(fd);
451
452 return pool;
453}
454
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700455static struct shm_pool *
456shm_pool_create(struct display *display, size_t size)
457{
458 struct shm_pool *pool = malloc(sizeof *pool);
459
460 if (!pool)
461 return NULL;
462
463 pool->pool = make_shm_pool(display, size, &pool->data);
464 if (!pool->pool) {
465 free(pool);
466 return NULL;
467 }
468
469 pool->size = size;
470 pool->used = 0;
471
472 return pool;
473}
474
475static void *
476shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
477{
478 if (pool->used + size > pool->size)
479 return NULL;
480
481 *offset = pool->used;
482 pool->used += size;
483
484 return (char *) pool->data + *offset;
485}
486
487/* destroy the pool. this does not unmap the memory though */
488static void
489shm_pool_destroy(struct shm_pool *pool)
490{
491 munmap(pool->data, pool->size);
492 wl_shm_pool_destroy(pool->pool);
493 free(pool);
494}
495
496/* Start allocating from the beginning of the pool again */
497static void
498shm_pool_reset(struct shm_pool *pool)
499{
500 pool->used = 0;
501}
502
503static int
504data_length_for_shm_surface(struct rectangle *rect)
505{
506 int stride;
507
508 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
509 rect->width);
510 return stride * rect->height;
511}
512
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500513static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700514display_create_shm_surface_from_pool(struct display *display,
515 struct rectangle *rectangle,
516 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517{
518 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400519 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400520 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700521 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400522 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523
524 data = malloc(sizeof *data);
525 if (data == NULL)
526 return NULL;
527
528 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
529 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700530 length = stride * rectangle->height;
531 data->pool = NULL;
532 map = shm_pool_allocate(pool, length, &offset);
533
534 if (!map) {
535 free(data);
536 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400537 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400539 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540 CAIRO_FORMAT_ARGB32,
541 rectangle->width,
542 rectangle->height,
543 stride);
544
545 cairo_surface_set_user_data (surface, &surface_data_key,
546 data, shm_surface_data_destroy);
547
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500549 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500551 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400552
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700553 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400554 rectangle->width,
555 rectangle->height,
556 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700558 return surface;
559}
560
561static cairo_surface_t *
562display_create_shm_surface(struct display *display,
563 struct rectangle *rectangle, uint32_t flags,
564 struct window *window)
565{
566 struct shm_surface_data *data;
567 struct shm_pool *pool;
568 cairo_surface_t *surface;
569
570 if (window && window->pool) {
571 shm_pool_reset(window->pool);
572 surface = display_create_shm_surface_from_pool(display,
573 rectangle,
574 flags,
575 window->pool);
576 if (surface)
577 return surface;
578 }
579
580 pool = shm_pool_create(display,
581 data_length_for_shm_surface(rectangle));
582 if (!pool)
583 return NULL;
584
585 surface =
586 display_create_shm_surface_from_pool(display, rectangle,
587 flags, pool);
588
589 if (!surface) {
590 shm_pool_destroy(pool);
591 return NULL;
592 }
593
594 /* make sure we destroy the pool when the surface is destroyed */
595 data = cairo_surface_get_user_data(surface, &surface_data_key);
596 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597
598 return surface;
599}
600
nobled7b87cb02011-02-01 18:51:47 +0000601static int
602check_size(struct rectangle *rect)
603{
604 if (rect->width && rect->height)
605 return 0;
606
607 fprintf(stderr, "tried to create surface of "
608 "width: %d, height: %d\n", rect->width, rect->height);
609 return -1;
610}
611
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400612cairo_surface_t *
613display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100614 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400615 struct rectangle *rectangle,
616 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617{
nobled7b87cb02011-02-01 18:51:47 +0000618 if (check_size(rectangle) < 0)
619 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500620#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300621 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400622 return display_create_egl_window_surface(display,
623 surface,
624 flags,
625 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400627 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628}
629
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300630static const char *cursors[] = {
631 "bottom_left_corner",
632 "bottom_right_corner",
633 "bottom_side",
634 "grabbing",
635 "left_ptr",
636 "left_side",
637 "right_side",
638 "top_left_corner",
639 "top_right_corner",
640 "top_side",
641 "xterm",
642 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400643 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300644};
645
646static void
647create_cursors(struct display *display)
648{
649 unsigned int i;
650
651 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
652 display->cursors =
653 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
654
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300655 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300656 display->cursors[i] =
657 wl_cursor_theme_get_cursor(display->cursor_theme,
658 cursors[i]);
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300659 if (!display->cursors[i])
660 fprintf(stderr, "could not load cursor '%s'\n",
661 cursors[i]);
662 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300663}
664
665static void
666destroy_cursors(struct display *display)
667{
668 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800669 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300670}
671
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300672struct wl_cursor_image *
673display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300675 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300676 wl_cursor_theme_get_cursor(display->cursor_theme,
677 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400678
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300679 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400680}
681
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400682static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200683window_get_resize_dx_dy(struct window *window, int *x, int *y)
684{
685 if (window->resize_edges & WINDOW_RESIZING_LEFT)
686 *x = window->server_allocation.width - window->allocation.width;
687 else
688 *x = 0;
689
690 if (window->resize_edges & WINDOW_RESIZING_TOP)
691 *y = window->server_allocation.height -
692 window->allocation.height;
693 else
694 *y = 0;
695
696 window->resize_edges = 0;
697}
698
699static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500700window_attach_surface(struct window *window)
701{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400702 struct display *display = window->display;
703 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000704#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100705 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000706#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500707 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100708
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400709 if (window->type == TYPE_NONE) {
710 window->type = TYPE_TOPLEVEL;
711 if (display->shell)
712 wl_shell_surface_set_toplevel(window->shell_surface);
713 }
714
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100715 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000716#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100717 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
718 data = cairo_surface_get_user_data(window->cairo_surface,
719 &surface_data_key);
720
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100721 cairo_gl_surface_swapbuffers(window->cairo_surface);
722 wl_egl_window_get_attached_size(data->window,
723 &window->server_allocation.width,
724 &window->server_allocation.height);
725 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000726#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728 buffer =
729 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400730 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200732 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400734 wl_surface_damage(window->surface, 0, 0,
735 window->allocation.width,
736 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100737 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400738 cairo_surface_destroy(window->cairo_surface);
739 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000741 default:
742 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100743 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500744
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500745 if (window->input_region) {
746 wl_surface_set_input_region(window->surface,
747 window->input_region);
748 wl_region_destroy(window->input_region);
749 window->input_region = NULL;
750 }
751
752 if (window->opaque_region) {
753 wl_surface_set_opaque_region(window->surface,
754 window->opaque_region);
755 wl_region_destroy(window->opaque_region);
756 window->opaque_region = NULL;
757 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500758}
759
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500760void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400761window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500762{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400763 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100764 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500765}
766
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400767void
768window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400769{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500770 cairo_surface_reference(surface);
771
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400772 if (window->cairo_surface != NULL)
773 cairo_surface_destroy(window->cairo_surface);
774
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500775 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400776}
777
Benjamin Franzke22d54812011-07-16 19:50:32 +0000778#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100779static void
780window_resize_cairo_window_surface(struct window *window)
781{
782 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200783 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784
785 data = cairo_surface_get_user_data(window->cairo_surface,
786 &surface_data_key);
787
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200788 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 wl_egl_window_resize(data->window,
790 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200791 window->allocation.height,
792 x,y);
793
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794 cairo_gl_surface_set_size(window->cairo_surface,
795 window->allocation.width,
796 window->allocation.height);
797}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000798#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100799
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400800struct display *
801window_get_display(struct window *window)
802{
803 return window->display;
804}
805
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400806void
807window_create_surface(struct window *window)
808{
809 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400810 uint32_t flags = 0;
811
812 if (!window->transparent)
813 flags = SURFACE_OPAQUE;
814
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400815 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500816#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
818 if (window->cairo_surface) {
819 window_resize_cairo_window_surface(window);
820 return;
821 }
822 surface = display_create_surface(window->display,
823 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400824 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826#endif
827 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400828 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400829 &window->allocation,
830 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800832 default:
833 surface = NULL;
834 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400836
837 window_set_surface(window, surface);
838 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400839}
840
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200841static void frame_destroy(struct frame *frame);
842
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400843void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500844window_destroy(struct window *window)
845{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200846 struct display *display = window->display;
847 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100848 struct window_output *window_output;
849 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200850
851 if (window->redraw_scheduled)
852 wl_list_remove(&window->redraw_task.link);
853
854 wl_list_for_each(input, &display->input_list, link) {
855 if (input->pointer_focus == window)
856 input->pointer_focus = NULL;
857 if (input->keyboard_focus == window)
858 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500859 if (input->focus_widget &&
860 input->focus_widget->window == window)
861 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200862 }
863
Rob Bradford7507b572012-05-15 17:55:34 +0100864 wl_list_for_each_safe(window_output, window_output_tmp,
865 &window->window_output_list, link) {
866 free (window_output);
867 }
868
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500869 if (window->input_region)
870 wl_region_destroy(window->input_region);
871 if (window->opaque_region)
872 wl_region_destroy(window->opaque_region);
873
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200874 if (window->frame)
875 frame_destroy(window->frame);
876
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200877 if (window->shell_surface)
878 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500879 wl_surface_destroy(window->surface);
880 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200881
882 if (window->cairo_surface != NULL)
883 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200884
885 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500886 free(window);
887}
888
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500889static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500890widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400891{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500892 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400893
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500894 wl_list_for_each(child, &widget->child_list, link) {
895 target = widget_find_widget(child, x, y);
896 if (target)
897 return target;
898 }
899
900 if (widget->allocation.x <= x &&
901 x < widget->allocation.x + widget->allocation.width &&
902 widget->allocation.y <= y &&
903 y < widget->allocation.y + widget->allocation.height) {
904 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400905 }
906
907 return NULL;
908}
909
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500910static struct widget *
911widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400912{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500913 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400914
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500915 widget = malloc(sizeof *widget);
916 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500917 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500918 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500919 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500920 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500921 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300922 widget->tooltip = NULL;
923 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500924
925 return widget;
926}
927
928struct widget *
929window_add_widget(struct window *window, void *data)
930{
931 window->widget = widget_create(window, data);
932 wl_list_init(&window->widget->link);
933
934 return window->widget;
935}
936
937struct widget *
938widget_add_widget(struct widget *parent, void *data)
939{
940 struct widget *widget;
941
942 widget = widget_create(parent->window, data);
943 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400944
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500945 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400946}
947
948void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500949widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400950{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200951 struct display *display = widget->window->display;
952 struct input *input;
953
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300954 if (widget->tooltip) {
955 free(widget->tooltip);
956 widget->tooltip = NULL;
957 }
958
Pekka Paalanene156fb62012-01-19 13:51:38 +0200959 wl_list_for_each(input, &display->input_list, link) {
960 if (input->focus_widget == widget)
961 input->focus_widget = NULL;
962 }
963
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500964 wl_list_remove(&widget->link);
965 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400966}
967
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400968void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500969widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400970{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500971 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400972}
973
974void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500975widget_set_size(struct widget *widget, int32_t width, int32_t height)
976{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500977 widget->allocation.width = width;
978 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500979}
980
981void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500982widget_set_allocation(struct widget *widget,
983 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400984{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500985 widget->allocation.x = x;
986 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200987 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400988}
989
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500990void
991widget_set_transparent(struct widget *widget, int transparent)
992{
993 widget->opaque = !transparent;
994}
995
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500997widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001000}
1001
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001002void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001003widget_set_resize_handler(struct widget *widget,
1004 widget_resize_handler_t handler)
1005{
1006 widget->resize_handler = handler;
1007}
1008
1009void
1010widget_set_redraw_handler(struct widget *widget,
1011 widget_redraw_handler_t handler)
1012{
1013 widget->redraw_handler = handler;
1014}
1015
1016void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001017widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001018{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001019 widget->enter_handler = handler;
1020}
1021
1022void
1023widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1024{
1025 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001026}
1027
1028void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001029widget_set_motion_handler(struct widget *widget,
1030 widget_motion_handler_t handler)
1031{
1032 widget->motion_handler = handler;
1033}
1034
1035void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001036widget_set_button_handler(struct widget *widget,
1037 widget_button_handler_t handler)
1038{
1039 widget->button_handler = handler;
1040}
1041
1042void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001043widget_schedule_redraw(struct widget *widget)
1044{
1045 window_schedule_redraw(widget->window);
1046}
1047
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001048cairo_surface_t *
1049window_get_surface(struct window *window)
1050{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001051 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001052}
1053
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001054struct wl_surface *
1055window_get_wl_surface(struct window *window)
1056{
1057 return window->surface;
1058}
1059
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001060struct wl_shell_surface *
1061window_get_wl_shell_surface(struct window *window)
1062{
1063 return window->shell_surface;
1064}
1065
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001066static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001067tooltip_redraw_handler(struct widget *widget, void *data)
1068{
1069 cairo_t *cr;
1070 const int32_t r = 3;
1071 struct tooltip *tooltip = data;
1072 int32_t width, height;
1073 struct window *window = widget->window;
1074
1075 cr = cairo_create(window->cairo_surface);
1076 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1077 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1078 cairo_paint(cr);
1079
1080 width = window->allocation.width;
1081 height = window->allocation.height;
1082 rounded_rect(cr, 0, 0, width, height, r);
1083
1084 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1085 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1086 cairo_fill(cr);
1087
1088 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1089 cairo_move_to(cr, 10, 16);
1090 cairo_show_text(cr, tooltip->entry);
1091 cairo_destroy(cr);
1092}
1093
1094static cairo_text_extents_t
1095get_text_extents(struct tooltip *tooltip)
1096{
1097 struct window *window;
1098 cairo_t *cr;
1099 cairo_text_extents_t extents;
1100
1101 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1102 * created yet */
1103 window = tooltip->widget->window->parent;
1104 cr = cairo_create(window->cairo_surface);
1105 cairo_text_extents(cr, tooltip->entry, &extents);
1106 cairo_destroy(cr);
1107
1108 return extents;
1109}
1110
1111static int
1112window_create_tooltip(struct tooltip *tooltip)
1113{
1114 struct widget *parent = tooltip->parent;
1115 struct display *display = parent->window->display;
1116 struct window *window;
1117 const int offset_y = 27;
1118 const int margin = 3;
1119 cairo_text_extents_t extents;
1120
1121 if (tooltip->widget)
1122 return 0;
1123
1124 window = window_create_transient(display, parent->window, tooltip->x,
1125 tooltip->y + offset_y,
1126 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1127 if (!window)
1128 return -1;
1129
1130 tooltip->window = window;
1131 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1132
1133 extents = get_text_extents(tooltip);
1134 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1135 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1136
1137 return 0;
1138}
1139
1140void
1141widget_destroy_tooltip(struct widget *parent)
1142{
1143 struct tooltip *tooltip = parent->tooltip;
1144
1145 parent->tooltip_count = 0;
1146 if (!tooltip)
1147 return;
1148
1149 if (tooltip->widget) {
1150 widget_destroy(tooltip->widget);
1151 window_destroy(tooltip->window);
1152 tooltip->widget = NULL;
1153 tooltip->window = NULL;
1154 }
1155
1156 close(tooltip->tooltip_fd);
1157 free(tooltip->entry);
1158 free(tooltip);
1159 parent->tooltip = NULL;
1160}
1161
1162static void
1163tooltip_func(struct task *task, uint32_t events)
1164{
1165 struct tooltip *tooltip =
1166 container_of(task, struct tooltip, tooltip_task);
1167 uint64_t exp;
1168
Martin Olsson8df662a2012-07-08 03:03:47 +02001169 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1170 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001171 window_create_tooltip(tooltip);
1172}
1173
1174#define TOOLTIP_TIMEOUT 500
1175static int
1176tooltip_timer_reset(struct tooltip *tooltip)
1177{
1178 struct itimerspec its;
1179
1180 its.it_interval.tv_sec = 0;
1181 its.it_interval.tv_nsec = 0;
1182 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1183 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1184 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1185 fprintf(stderr, "could not set timerfd\n: %m");
1186 return -1;
1187 }
1188
1189 return 0;
1190}
1191
1192int
1193widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1194{
1195 struct tooltip *tooltip = parent->tooltip;
1196
1197 parent->tooltip_count++;
1198 if (tooltip) {
1199 tooltip->x = x;
1200 tooltip->y = y;
1201 tooltip_timer_reset(tooltip);
1202 return 0;
1203 }
1204
1205 /* the handler might be triggered too fast via input device motion, so
1206 * we need this check here to make sure tooltip is fully initialized */
1207 if (parent->tooltip_count > 1)
1208 return 0;
1209
1210 tooltip = malloc(sizeof *tooltip);
1211 if (!tooltip)
1212 return -1;
1213
1214 parent->tooltip = tooltip;
1215 tooltip->parent = parent;
1216 tooltip->widget = NULL;
1217 tooltip->window = NULL;
1218 tooltip->x = x;
1219 tooltip->y = y;
1220 tooltip->entry = strdup(entry);
1221 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1222 if (tooltip->tooltip_fd < 0) {
1223 fprintf(stderr, "could not create timerfd\n: %m");
1224 return -1;
1225 }
1226
1227 tooltip->tooltip_task.run = tooltip_func;
1228 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1229 EPOLLIN, &tooltip->tooltip_task);
1230 tooltip_timer_reset(tooltip);
1231
1232 return 0;
1233}
1234
1235static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001236frame_resize_handler(struct widget *widget,
1237 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001238{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001239 struct frame *frame = data;
1240 struct widget *child = frame->child;
1241 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001242 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001243 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001244 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001245 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001246 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001247 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001248
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001249 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001250 decoration_width = (t->width + t->margin) * 2;
1251 decoration_height = t->width +
1252 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001253
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001254 allocation.x = t->width + t->margin;
1255 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001256 allocation.width = width - decoration_width;
1257 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001258
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001259 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001260
1261 wl_list_for_each(button, &frame->buttons_list, link)
1262 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001263 } else {
1264 decoration_width = 0;
1265 decoration_height = 0;
1266
1267 allocation.x = 0;
1268 allocation.y = 0;
1269 allocation.width = width;
1270 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001271 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001272
1273 wl_list_for_each(button, &frame->buttons_list, link)
1274 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001275 }
1276
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001277 widget_set_allocation(child, allocation.x, allocation.y,
1278 allocation.width, allocation.height);
1279
1280 if (child->resize_handler)
1281 child->resize_handler(child,
1282 allocation.width,
1283 allocation.height,
1284 child->user_data);
1285
Scott Moreauf7e498c2012-05-14 11:39:29 -06001286 width = child->allocation.width + decoration_width;
1287 height = child->allocation.height + decoration_height;
1288
Kristian Høgsberg023be102012-07-31 11:59:12 -04001289 if (widget->window->type != TYPE_FULLSCREEN) {
1290 widget->window->input_region =
1291 wl_compositor_create_region(display->compositor);
1292 wl_region_add(widget->window->input_region,
1293 t->margin, t->margin,
1294 width - 2 * t->margin,
1295 height - 2 * t->margin);
1296 }
1297
Scott Moreauf7e498c2012-05-14 11:39:29 -06001298 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001299
1300 if (child->opaque) {
1301 widget->window->opaque_region =
1302 wl_compositor_create_region(display->compositor);
1303 wl_region_add(widget->window->opaque_region,
1304 opaque_margin, opaque_margin,
1305 widget->allocation.width - 2 * opaque_margin,
1306 widget->allocation.height - 2 * opaque_margin);
1307 }
Martin Minarik1998b152012-05-10 02:04:35 +02001308
1309 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001310 x_r = frame->widget->allocation.width - t->width - t->margin;
1311 x_l = t->width + t->margin;
1312 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001313 wl_list_for_each(button, &frame->buttons_list, link) {
1314 const int button_padding = 4;
1315 w = cairo_image_surface_get_width(button->icon);
1316 h = cairo_image_surface_get_height(button->icon);
1317
1318 if (button->decoration == FRAME_BUTTON_FANCY)
1319 w += 10;
1320
1321 if (button->align == FRAME_BUTTON_LEFT) {
1322 widget_set_allocation(button->widget,
1323 x_l, y , w + 1, h + 1);
1324 x_l += w;
1325 x_l += button_padding;
1326 } else {
1327 x_r -= w;
1328 widget_set_allocation(button->widget,
1329 x_r, y , w + 1, h + 1);
1330 x_r -= button_padding;
1331 }
1332 }
1333}
1334
1335static int
1336frame_button_enter_handler(struct widget *widget,
1337 struct input *input, float x, float y, void *data)
1338{
1339 struct frame_button *frame_button = data;
1340
1341 widget_schedule_redraw(frame_button->widget);
1342 frame_button->state = FRAME_BUTTON_OVER;
1343
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001344 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001345}
1346
1347static void
1348frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1349{
1350 struct frame_button *frame_button = data;
1351
1352 widget_schedule_redraw(frame_button->widget);
1353 frame_button->state = FRAME_BUTTON_DEFAULT;
1354}
1355
1356static void
1357frame_button_button_handler(struct widget *widget,
1358 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001359 uint32_t button,
1360 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001361{
1362 struct frame_button *frame_button = data;
1363 struct window *window = widget->window;
1364
1365 if (button != BTN_LEFT)
1366 return;
1367
1368 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001369 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001370 frame_button->state = FRAME_BUTTON_ACTIVE;
1371 widget_schedule_redraw(frame_button->widget);
1372
1373 if (frame_button->type == FRAME_BUTTON_ICON)
1374 window_show_frame_menu(window, input, time);
1375 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001376 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001377 frame_button->state = FRAME_BUTTON_DEFAULT;
1378 widget_schedule_redraw(frame_button->widget);
1379 break;
1380 }
1381
1382 switch (frame_button->type) {
1383 case FRAME_BUTTON_CLOSE:
1384 if (window->close_handler)
1385 window->close_handler(window->parent,
1386 window->user_data);
1387 else
1388 display_exit(window->display);
1389 break;
1390 case FRAME_BUTTON_MINIMIZE:
1391 fprintf(stderr,"Minimize stub\n");
1392 break;
1393 case FRAME_BUTTON_MAXIMIZE:
1394 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1395 break;
1396 default:
1397 /* Unknown operation */
1398 break;
1399 }
1400}
1401
1402static void
1403frame_button_redraw_handler(struct widget *widget, void *data)
1404{
1405 struct frame_button *frame_button = data;
1406 cairo_t *cr;
1407 int width, height, x, y;
1408 struct window *window = widget->window;
1409
1410 x = widget->allocation.x;
1411 y = widget->allocation.y;
1412 width = widget->allocation.width;
1413 height = widget->allocation.height;
1414
1415 if (!width)
1416 return;
1417 if (!height)
1418 return;
1419 if (widget->opaque)
1420 return;
1421
1422 cr = cairo_create(window->cairo_surface);
1423
1424 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1425 cairo_set_line_width(cr, 1);
1426
1427 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1428 cairo_rectangle (cr, x, y, 25, 16);
1429
1430 cairo_stroke_preserve(cr);
1431
1432 switch (frame_button->state) {
1433 case FRAME_BUTTON_DEFAULT:
1434 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1435 break;
1436 case FRAME_BUTTON_OVER:
1437 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1438 break;
1439 case FRAME_BUTTON_ACTIVE:
1440 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1441 break;
1442 }
1443
1444 cairo_fill (cr);
1445
1446 x += 4;
1447 }
1448
1449 cairo_set_source_surface(cr, frame_button->icon, x, y);
1450 cairo_paint(cr);
1451
1452 cairo_destroy(cr);
1453}
1454
1455static struct widget *
1456frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1457 enum frame_button_align align, enum frame_button_decoration style)
1458{
1459 struct frame_button *frame_button;
1460 const char *icon = data;
1461
1462 frame_button = malloc (sizeof *frame_button);
1463 memset(frame_button, 0, sizeof *frame_button);
1464
1465 frame_button->icon = cairo_image_surface_create_from_png(icon);
1466 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1467 frame_button->frame = frame;
1468 frame_button->type = type;
1469 frame_button->align = align;
1470 frame_button->decoration = style;
1471
1472 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1473
1474 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1475 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1476 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1477 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1478 return frame_button->widget;
1479}
1480
1481static void
1482frame_button_destroy(struct frame_button *frame_button)
1483{
1484 widget_destroy(frame_button->widget);
1485 wl_list_remove(&frame_button->link);
1486 cairo_surface_destroy(frame_button->icon);
1487 free(frame_button);
1488
1489 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001490}
1491
1492static void
1493frame_redraw_handler(struct widget *widget, void *data)
1494{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001496 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001497 struct theme *t = window->display->theme;
1498 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001500 if (window->type == TYPE_FULLSCREEN)
1501 return;
1502
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001503 cr = cairo_create(window->cairo_surface);
1504
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001505 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001506 flags |= THEME_FRAME_ACTIVE;
1507 theme_render_frame(t, cr, widget->allocation.width,
1508 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001509
1510 cairo_destroy(cr);
1511}
1512
1513static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001514frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001515{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001516 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001517 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001518
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001519 location = theme_get_location(t, input->sx, input->sy,
1520 frame->widget->allocation.width,
1521 frame->widget->allocation.height);
1522
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001523 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001525 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001527 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001531 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001532 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001533 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001534 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001535 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001536 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001537 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001538 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001539 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001540 case THEME_LOCATION_EXTERIOR:
1541 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001542 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001543 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001544 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001545}
1546
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001547static void
1548frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001549{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001550 switch (index) {
1551 case 0: /* close */
1552 if (window->close_handler)
1553 window->close_handler(window->parent,
1554 window->user_data);
1555 else
1556 display_exit(window->display);
1557 break;
1558 case 1: /* fullscreen */
1559 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001560 if (window->fullscreen_handler)
1561 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001562 break;
1563 case 2: /* rotate */
1564 case 3: /* scale */
1565 break;
1566 }
1567}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001568
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001569void
1570window_show_frame_menu(struct window *window,
1571 struct input *input, uint32_t time)
1572{
1573 int32_t x, y;
1574
1575 static const char *entries[] = {
1576 "Close", "Fullscreen", "Rotate", "Scale"
1577 };
1578
1579 input_get_position(input, &x, &y);
1580 window_show_menu(window->display, input, time, window,
1581 x - 10, y - 10, frame_menu_func, entries, 4);
1582}
1583
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001584static int
1585frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001586 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001587{
1588 return frame_get_pointer_image_for_location(data, input);
1589}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001590
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001591static int
1592frame_motion_handler(struct widget *widget,
1593 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001594 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001595{
1596 return frame_get_pointer_image_for_location(data, input);
1597}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001598
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001599static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001600frame_button_handler(struct widget *widget,
1601 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001602 uint32_t button, enum wl_pointer_button_state state,
1603 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001604
1605{
1606 struct frame *frame = data;
1607 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001608 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001609 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001610
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001611 location = theme_get_location(display->theme, input->sx, input->sy,
1612 frame->widget->allocation.width,
1613 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614
Daniel Stone4dbadb12012-05-30 16:31:51 +01001615 if (window->display->shell && button == BTN_LEFT &&
1616 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001617 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001618 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001619 if (!window->shell_surface)
1620 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001621 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001622 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001623 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001624 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001625 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001626 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001627 case THEME_LOCATION_RESIZING_TOP:
1628 case THEME_LOCATION_RESIZING_BOTTOM:
1629 case THEME_LOCATION_RESIZING_LEFT:
1630 case THEME_LOCATION_RESIZING_RIGHT:
1631 case THEME_LOCATION_RESIZING_TOP_LEFT:
1632 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1633 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1634 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001635 if (!window->shell_surface)
1636 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001637 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001638
1639 if (!display->dpy) {
1640 /* If we're using shm, allocate a big
1641 pool to create buffers out of while
1642 we resize. We should probably base
1643 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001644 window->pool =
1645 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001646 }
1647
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001648 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001649 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001650 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001651 break;
1652 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001653 } else if (button == BTN_RIGHT &&
1654 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001655 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001656 }
1657}
1658
1659struct widget *
1660frame_create(struct window *window, void *data)
1661{
1662 struct frame *frame;
1663
1664 frame = malloc(sizeof *frame);
1665 memset(frame, 0, sizeof *frame);
1666
1667 frame->widget = window_add_widget(window, frame);
1668 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001669
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001670 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1671 widget_set_resize_handler(frame->widget, frame_resize_handler);
1672 widget_set_enter_handler(frame->widget, frame_enter_handler);
1673 widget_set_motion_handler(frame->widget, frame_motion_handler);
1674 widget_set_button_handler(frame->widget, frame_button_handler);
1675
Martin Minarik1998b152012-05-10 02:04:35 +02001676 /* Create empty list for frame buttons */
1677 wl_list_init(&frame->buttons_list);
1678
1679 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1680 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1681
1682 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1683 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1684
1685 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1686 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1687
1688 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1689 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1690
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001691 window->frame = frame;
1692
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001693 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001694}
1695
Kristian Høgsberga1627922012-06-20 17:30:03 -04001696void
1697frame_set_child_size(struct widget *widget, int child_width, int child_height)
1698{
1699 struct display *display = widget->window->display;
1700 struct theme *t = display->theme;
1701 int decoration_width, decoration_height;
1702 int width, height;
1703
1704 if (widget->window->type != TYPE_FULLSCREEN) {
1705 decoration_width = (t->width + t->margin) * 2;
1706 decoration_height = t->width +
1707 t->titlebar_height + t->margin * 2;
1708
1709 width = child_width + decoration_width;
1710 height = child_height + decoration_height;
1711 } else {
1712 width = child_width;
1713 height = child_height;
1714 }
1715
1716 window_schedule_resize(widget->window, width, height);
1717}
1718
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001719static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001720frame_destroy(struct frame *frame)
1721{
Martin Minarik1998b152012-05-10 02:04:35 +02001722 struct frame_button *button, *tmp;
1723
1724 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1725 frame_button_destroy(button);
1726
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001727 /* frame->child must be destroyed by the application */
1728 widget_destroy(frame->widget);
1729 free(frame);
1730}
1731
1732static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001733input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001734 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001735{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001736 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001737 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001738
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001739 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001740 return;
1741
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001742 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001743 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001744 widget = old;
1745 if (input->grab)
1746 widget = input->grab;
1747 if (widget->leave_handler)
1748 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001749 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001750 }
1751
1752 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001753 widget = focus;
1754 if (input->grab)
1755 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001756 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001757 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001758 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001759 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001760
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001761 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001762 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001763}
1764
1765static void
Daniel Stone37816df2012-05-16 18:45:18 +01001766pointer_handle_motion(void *data, struct wl_pointer *pointer,
1767 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001768{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001769 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001770 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001771 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001772 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001773 float sx = wl_fixed_to_double(sx_w);
1774 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001775
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001776 input->sx = sx;
1777 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001778
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001779 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001780 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001781 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001782 }
1783
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001784 if (input->grab)
1785 widget = input->grab;
1786 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001787 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001788 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001789 cursor = widget->motion_handler(input->focus_widget,
1790 input, time, sx, sy,
1791 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001792
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001793 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001794}
1795
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001796void
1797input_grab(struct input *input, struct widget *widget, uint32_t button)
1798{
1799 input->grab = widget;
1800 input->grab_button = button;
1801}
1802
1803void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001804input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001805{
1806 struct widget *widget;
1807
1808 input->grab = NULL;
1809 if (input->pointer_focus) {
1810 widget = widget_find_widget(input->pointer_focus->widget,
1811 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001812 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001813 }
1814}
1815
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001816static void
Daniel Stone37816df2012-05-16 18:45:18 +01001817pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001818 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001819{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001820 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001821 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001822 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001823
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001824 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001825 if (input->focus_widget && input->grab == NULL &&
1826 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001827 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001828
Neil Roberts6b28aad2012-01-23 19:11:18 +00001829 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001830 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001831 (*widget->button_handler)(widget,
1832 input, time,
1833 button, state,
1834 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001835
Daniel Stone4dbadb12012-05-30 16:31:51 +01001836 if (input->grab && input->grab_button == button &&
1837 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001838 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001839}
1840
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001841static void
Daniel Stone37816df2012-05-16 18:45:18 +01001842pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001843 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001844{
1845}
1846
1847static void
Daniel Stone37816df2012-05-16 18:45:18 +01001848keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001849 uint32_t serial, uint32_t time, uint32_t key,
1850 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001851{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001852 struct input *input = data;
1853 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001854 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001855 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001856 const xkb_keysym_t *syms;
1857 xkb_keysym_t sym;
1858 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001859 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001860
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001861 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001862 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001863 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001864 return;
1865
Daniel Stone97f68542012-05-30 16:32:01 +01001866 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001867
Daniel Stone97f68542012-05-30 16:32:01 +01001868 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001869 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001870 XKB_STATE_LATCHED);
1871 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001872 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001873 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001874 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001875 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001876 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001877 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001878
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001879 sym = XKB_KEY_NoSymbol;
1880 if (num_syms == 1)
1881 sym = syms[0];
1882
1883 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001884 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001885 window_set_maximized(window,
1886 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001887 } else if (sym == XKB_KEY_F11 &&
1888 window->fullscreen_handler &&
1889 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1890 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04001891 } else if (sym == XKB_KEY_F4 &&
1892 input->modifiers == MOD_ALT_MASK &&
1893 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1894 if (window->close_handler)
1895 window->close_handler(window->parent,
1896 window->user_data);
1897 else
1898 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001899 } else if (window->key_handler) {
1900 (*window->key_handler)(window, input, time, key,
1901 sym, state, window->user_data);
1902 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001903
1904 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
1905 key == input->repeat_key) {
1906 its.it_interval.tv_sec = 0;
1907 its.it_interval.tv_nsec = 0;
1908 its.it_value.tv_sec = 0;
1909 its.it_value.tv_nsec = 0;
1910 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1911 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1912 input->repeat_sym = sym;
1913 input->repeat_key = key;
1914 input->repeat_time = time;
1915 its.it_interval.tv_sec = 0;
1916 its.it_interval.tv_nsec = 25 * 1000 * 1000;
1917 its.it_value.tv_sec = 0;
1918 its.it_value.tv_nsec = 400 * 1000 * 1000;
1919 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1920 }
1921}
1922
1923static void
1924keyboard_repeat_func(struct task *task, uint32_t events)
1925{
1926 struct input *input =
1927 container_of(task, struct input, repeat_task);
1928 struct window *window = input->keyboard_focus;
1929 uint64_t exp;
1930
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04001931 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1932 /* If we change the timer between the fd becoming
1933 * readable and getting here, there'll be nothing to
1934 * read and we get EAGAIN. */
1935 return;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001936
Kristian Høgsbergbd0cd762012-06-20 15:17:18 -04001937 if (window && window->key_handler) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001938 (*window->key_handler)(window, input, input->repeat_time,
1939 input->repeat_key, input->repeat_sym,
1940 WL_KEYBOARD_KEY_STATE_PRESSED,
1941 window->user_data);
1942 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001943}
1944
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001945static void
Daniel Stone351eb612012-05-31 15:27:47 -04001946keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1947 uint32_t serial, uint32_t mods_depressed,
1948 uint32_t mods_latched, uint32_t mods_locked,
1949 uint32_t group)
1950{
1951 struct input *input = data;
1952
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001953 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1954 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001955}
1956
1957static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001958input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001959{
1960 struct window *window = input->pointer_focus;
1961
1962 if (!window)
1963 return;
1964
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001965 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001966
Pekka Paalanene1207c72011-12-16 12:02:09 +02001967 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001968 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001969}
1970
1971static void
Daniel Stone37816df2012-05-16 18:45:18 +01001972pointer_handle_enter(void *data, struct wl_pointer *pointer,
1973 uint32_t serial, struct wl_surface *surface,
1974 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001975{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001976 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001977 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001978 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001979 float sx = wl_fixed_to_double(sx_w);
1980 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001981
Martin Minarik1998b152012-05-10 02:04:35 +02001982 if (!surface) {
1983 /* enter event for a window we've just destroyed */
1984 return;
1985 }
1986
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001987 input->display->serial = serial;
1988 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001989 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001990 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001991
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001992 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001993 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001994 window->pool = NULL;
1995 /* Schedule a redraw to free the pool */
1996 window_schedule_redraw(window);
1997 }
1998
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001999 input->sx = sx;
2000 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002001
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002002 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002003 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002004}
Kristian Høgsberg59826582011-01-20 11:56:57 -05002005
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002006static void
Daniel Stone37816df2012-05-16 18:45:18 +01002007pointer_handle_leave(void *data, struct wl_pointer *pointer,
2008 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002009{
2010 struct input *input = data;
2011
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002012 input->display->serial = serial;
2013 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002014}
2015
2016static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002017input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02002018{
2019 struct window *window = input->keyboard_focus;
Kristian Høgsbergd3c69c22012-06-20 22:41:59 -04002020 struct itimerspec its;
2021
2022 its.it_interval.tv_sec = 0;
2023 its.it_interval.tv_nsec = 0;
2024 its.it_value.tv_sec = 0;
2025 its.it_value.tv_nsec = 0;
2026 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002027
2028 if (!window)
2029 return;
2030
2031 window->keyboard_device = NULL;
2032 if (window->keyboard_focus_handler)
2033 (*window->keyboard_focus_handler)(window, NULL,
2034 window->user_data);
2035
2036 input->keyboard_focus = NULL;
2037}
2038
2039static void
Daniel Stone37816df2012-05-16 18:45:18 +01002040keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2041 uint32_t serial, struct wl_surface *surface,
2042 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002043{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002044 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02002045 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002046
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002047 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002048 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002049
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002050 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002051 window->keyboard_device = input;
2052 if (window->keyboard_focus_handler)
2053 (*window->keyboard_focus_handler)(window,
2054 window->keyboard_device,
2055 window->user_data);
2056}
2057
2058static void
Daniel Stone37816df2012-05-16 18:45:18 +01002059keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2060 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002061{
2062 struct input *input = data;
2063
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002064 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002065 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002066}
2067
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002068static void
2069keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2070 uint32_t format, int fd, uint32_t size)
2071{
2072 struct input *input = data;
2073 char *map_str;
2074
2075 if (!data) {
2076 close(fd);
2077 return;
2078 }
2079
2080 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2081 close(fd);
2082 return;
2083 }
2084
2085 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2086 if (map_str == MAP_FAILED) {
2087 close(fd);
2088 return;
2089 }
2090
2091 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2092 map_str,
2093 XKB_KEYMAP_FORMAT_TEXT_V1,
2094 0);
2095 munmap(map_str, size);
2096 close(fd);
2097
2098 if (!input->xkb.keymap) {
2099 fprintf(stderr, "failed to compile keymap\n");
2100 return;
2101 }
2102
2103 input->xkb.state = xkb_state_new(input->xkb.keymap);
2104 if (!input->xkb.state) {
2105 fprintf(stderr, "failed to create XKB state\n");
2106 xkb_map_unref(input->xkb.keymap);
2107 input->xkb.keymap = NULL;
2108 return;
2109 }
2110
2111 input->xkb.control_mask =
2112 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2113 input->xkb.alt_mask =
2114 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2115 input->xkb.shift_mask =
2116 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2117}
2118
Daniel Stone37816df2012-05-16 18:45:18 +01002119static const struct wl_pointer_listener pointer_listener = {
2120 pointer_handle_enter,
2121 pointer_handle_leave,
2122 pointer_handle_motion,
2123 pointer_handle_button,
2124 pointer_handle_axis,
2125};
2126
2127static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002128 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002129 keyboard_handle_enter,
2130 keyboard_handle_leave,
2131 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002132 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002133};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002134
2135static void
Daniel Stone37816df2012-05-16 18:45:18 +01002136seat_handle_capabilities(void *data, struct wl_seat *seat,
2137 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002138{
Daniel Stone37816df2012-05-16 18:45:18 +01002139 struct input *input = data;
2140
2141 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2142 input->pointer = wl_seat_get_pointer(seat);
2143 wl_pointer_set_user_data(input->pointer, input);
2144 wl_pointer_add_listener(input->pointer, &pointer_listener,
2145 input);
2146 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2147 wl_pointer_destroy(input->pointer);
2148 input->pointer = NULL;
2149 }
2150
2151 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2152 input->keyboard = wl_seat_get_keyboard(seat);
2153 wl_keyboard_set_user_data(input->keyboard, input);
2154 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2155 input);
2156 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2157 wl_keyboard_destroy(input->keyboard);
2158 input->keyboard = NULL;
2159 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002160}
2161
Daniel Stone37816df2012-05-16 18:45:18 +01002162static const struct wl_seat_listener seat_listener = {
2163 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002164};
2165
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002166void
2167input_get_position(struct input *input, int32_t *x, int32_t *y)
2168{
2169 *x = input->sx;
2170 *y = input->sy;
2171}
2172
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002173struct display *
2174input_get_display(struct input *input)
2175{
2176 return input->display;
2177}
2178
Daniel Stone37816df2012-05-16 18:45:18 +01002179struct wl_seat *
2180input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002181{
Daniel Stone37816df2012-05-16 18:45:18 +01002182 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002183}
2184
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002185uint32_t
2186input_get_modifiers(struct input *input)
2187{
2188 return input->modifiers;
2189}
2190
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002191struct widget *
2192input_get_focus_widget(struct input *input)
2193{
2194 return input->focus_widget;
2195}
2196
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002197struct data_offer {
2198 struct wl_data_offer *offer;
2199 struct input *input;
2200 struct wl_array types;
2201 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002202
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002203 struct task io_task;
2204 int fd;
2205 data_func_t func;
2206 int32_t x, y;
2207 void *user_data;
2208};
2209
2210static void
2211data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2212{
2213 struct data_offer *offer = data;
2214 char **p;
2215
2216 p = wl_array_add(&offer->types, sizeof *p);
2217 *p = strdup(type);
2218}
2219
2220static const struct wl_data_offer_listener data_offer_listener = {
2221 data_offer_offer,
2222};
2223
2224static void
2225data_offer_destroy(struct data_offer *offer)
2226{
2227 char **p;
2228
2229 offer->refcount--;
2230 if (offer->refcount == 0) {
2231 wl_data_offer_destroy(offer->offer);
2232 for (p = offer->types.data; *p; p++)
2233 free(*p);
2234 wl_array_release(&offer->types);
2235 free(offer);
2236 }
2237}
2238
2239static void
2240data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002241 struct wl_data_device *data_device,
2242 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002243{
2244 struct data_offer *offer;
2245
2246 offer = malloc(sizeof *offer);
2247
2248 wl_array_init(&offer->types);
2249 offer->refcount = 1;
2250 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002251 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002252 wl_data_offer_add_listener(offer->offer,
2253 &data_offer_listener, offer);
2254}
2255
2256static void
2257data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002258 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002259 wl_fixed_t x_w, wl_fixed_t y_w,
2260 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002261{
2262 struct input *input = data;
2263 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002264 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002265 float x = wl_fixed_to_double(x_w);
2266 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002267 char **p;
2268
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002269 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002270 window = wl_surface_get_user_data(surface);
2271 input->pointer_focus = window;
2272
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002273 if (offer) {
2274 input->drag_offer = wl_data_offer_get_user_data(offer);
2275
2276 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2277 *p = NULL;
2278
2279 types_data = input->drag_offer->types.data;
2280 } else {
2281 input->drag_offer = NULL;
2282 types_data = NULL;
2283 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002284
2285 window = input->pointer_focus;
2286 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002287 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002288 window->user_data);
2289}
2290
2291static void
2292data_device_leave(void *data, struct wl_data_device *data_device)
2293{
2294 struct input *input = data;
2295
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002296 if (input->drag_offer) {
2297 data_offer_destroy(input->drag_offer);
2298 input->drag_offer = NULL;
2299 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002300}
2301
2302static void
2303data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002304 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002305{
2306 struct input *input = data;
2307 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002308 float x = wl_fixed_to_double(x_w);
2309 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002310 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002311
2312 input->sx = x;
2313 input->sy = y;
2314
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002315 if (input->drag_offer)
2316 types_data = input->drag_offer->types.data;
2317 else
2318 types_data = NULL;
2319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002320 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002321 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002322 window->user_data);
2323}
2324
2325static void
2326data_device_drop(void *data, struct wl_data_device *data_device)
2327{
2328 struct input *input = data;
2329 struct window *window = input->pointer_focus;
2330
2331 if (window->drop_handler)
2332 window->drop_handler(window, input,
2333 input->sx, input->sy, window->user_data);
2334}
2335
2336static void
2337data_device_selection(void *data,
2338 struct wl_data_device *wl_data_device,
2339 struct wl_data_offer *offer)
2340{
2341 struct input *input = data;
2342 char **p;
2343
2344 if (input->selection_offer)
2345 data_offer_destroy(input->selection_offer);
2346
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002347 if (offer) {
2348 input->selection_offer = wl_data_offer_get_user_data(offer);
2349 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2350 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002351 } else {
2352 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002353 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002354}
2355
2356static const struct wl_data_device_listener data_device_listener = {
2357 data_device_data_offer,
2358 data_device_enter,
2359 data_device_leave,
2360 data_device_motion,
2361 data_device_drop,
2362 data_device_selection
2363};
2364
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002365static void
2366input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002367{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002368 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002369 struct wl_cursor *cursor;
2370 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002371
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002372 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002373 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002374 return;
2375
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002376 if (index >= (int) cursor->image_count) {
2377 fprintf(stderr, "cursor index out of range\n");
2378 return;
2379 }
2380
2381 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002382 buffer = wl_cursor_image_get_buffer(image);
2383 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002384 return;
2385
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002386 wl_pointer_set_cursor(input->pointer, input->display->serial,
2387 input->pointer_surface,
2388 image->hotspot_x, image->hotspot_y);
2389 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2390 wl_surface_damage(input->pointer_surface, 0, 0,
2391 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002392}
2393
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002394static const struct wl_callback_listener pointer_surface_listener;
2395
2396static void
2397pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2398 uint32_t time)
2399{
2400 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002401 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002402 int i;
2403
2404 if (callback) {
2405 assert(callback == input->cursor_frame_cb);
2406 wl_callback_destroy(callback);
2407 input->cursor_frame_cb = NULL;
2408 }
2409
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002410 if (input->current_cursor == CURSOR_BLANK) {
2411 wl_pointer_set_cursor(input->pointer, input->display->serial,
2412 NULL, 0, 0);
2413 return;
2414 }
2415
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002416 if (input->current_cursor == CURSOR_UNSET)
2417 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002418 cursor = input->display->cursors[input->current_cursor];
2419 if (!cursor)
2420 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002421
2422 /* FIXME We don't have the current time on the first call so we set
2423 * the animation start to the time of the first frame callback. */
2424 if (time == 0)
2425 input->cursor_anim_start = 0;
2426 else if (input->cursor_anim_start == 0)
2427 input->cursor_anim_start = time;
2428
2429 if (time == 0 || input->cursor_anim_start == 0)
2430 i = 0;
2431 else
2432 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2433
2434 input_set_pointer_image_index(input, i);
2435
2436 if (cursor->image_count == 1)
2437 return;
2438
2439 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2440 wl_callback_add_listener(input->cursor_frame_cb,
2441 &pointer_surface_listener, input);
2442}
2443
2444static const struct wl_callback_listener pointer_surface_listener = {
2445 pointer_surface_frame_callback
2446};
2447
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002448void
2449input_set_pointer_image(struct input *input, int pointer)
2450{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002451 int force = 0;
2452
2453 if (input->pointer_enter_serial > input->cursor_serial)
2454 force = 1;
2455
2456 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002457 return;
2458
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002459 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002460 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002461 if (!input->cursor_frame_cb)
2462 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002463 else if (force) {
2464 /* The current frame callback may be stuck if, for instance,
2465 * the set cursor request was processed by the server after
2466 * this client lost the focus. In this case the cursor surface
2467 * might not be mapped and the frame callback wouldn't ever
2468 * complete. Send a set_cursor and attach to try to map the
2469 * cursor surface again so that the callback will finish */
2470 input_set_pointer_image_index(input, 0);
2471 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002472}
2473
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002474struct wl_data_device *
2475input_get_data_device(struct input *input)
2476{
2477 return input->data_device;
2478}
2479
2480void
2481input_set_selection(struct input *input,
2482 struct wl_data_source *source, uint32_t time)
2483{
2484 wl_data_device_set_selection(input->data_device, source, time);
2485}
2486
2487void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002488input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002489{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002490 wl_data_offer_accept(input->drag_offer->offer,
2491 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002492}
2493
2494static void
2495offer_io_func(struct task *task, uint32_t events)
2496{
2497 struct data_offer *offer =
2498 container_of(task, struct data_offer, io_task);
2499 unsigned int len;
2500 char buffer[4096];
2501
2502 len = read(offer->fd, buffer, sizeof buffer);
2503 offer->func(buffer, len,
2504 offer->x, offer->y, offer->user_data);
2505
2506 if (len == 0) {
2507 close(offer->fd);
2508 data_offer_destroy(offer);
2509 }
2510}
2511
2512static void
2513data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2514 data_func_t func, void *user_data)
2515{
2516 int p[2];
2517
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002518 if (pipe2(p, O_CLOEXEC) == -1)
2519 return;
2520
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002521 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2522 close(p[1]);
2523
2524 offer->io_task.run = offer_io_func;
2525 offer->fd = p[0];
2526 offer->func = func;
2527 offer->refcount++;
2528 offer->user_data = user_data;
2529
2530 display_watch_fd(offer->input->display,
2531 offer->fd, EPOLLIN, &offer->io_task);
2532}
2533
2534void
2535input_receive_drag_data(struct input *input, const char *mime_type,
2536 data_func_t func, void *data)
2537{
2538 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2539 input->drag_offer->x = input->sx;
2540 input->drag_offer->y = input->sy;
2541}
2542
2543int
2544input_receive_selection_data(struct input *input, const char *mime_type,
2545 data_func_t func, void *data)
2546{
2547 char **p;
2548
2549 if (input->selection_offer == NULL)
2550 return -1;
2551
2552 for (p = input->selection_offer->types.data; *p; p++)
2553 if (strcmp(mime_type, *p) == 0)
2554 break;
2555
2556 if (*p == NULL)
2557 return -1;
2558
2559 data_offer_receive_data(input->selection_offer,
2560 mime_type, func, data);
2561 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002562}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002563
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002564int
2565input_receive_selection_data_to_fd(struct input *input,
2566 const char *mime_type, int fd)
2567{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002568 if (input->selection_offer)
2569 wl_data_offer_receive(input->selection_offer->offer,
2570 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002571
2572 return 0;
2573}
2574
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002575void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002576window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002577{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002578 if (!window->shell_surface)
2579 return;
2580
Daniel Stone37816df2012-05-16 18:45:18 +01002581 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002582}
2583
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002584static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002585idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002586{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002587 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002588
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002589 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002590 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002591 widget_set_allocation(widget,
2592 window->pending_allocation.x,
2593 window->pending_allocation.y,
2594 window->pending_allocation.width,
2595 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002596
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002597 if (window->input_region) {
2598 wl_region_destroy(window->input_region);
2599 window->input_region = NULL;
2600 }
2601
2602 if (window->opaque_region) {
2603 wl_region_destroy(window->opaque_region);
2604 window->opaque_region = NULL;
2605 }
2606
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002607 if (widget->resize_handler)
2608 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002609 widget->allocation.width,
2610 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002611 widget->user_data);
2612
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002613 if (window->allocation.width != widget->allocation.width ||
2614 window->allocation.height != widget->allocation.height) {
2615 window->allocation = widget->allocation;
2616 window_schedule_redraw(window);
2617 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002618}
2619
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002620void
2621window_schedule_resize(struct window *window, int width, int height)
2622{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002623 window->pending_allocation.x = 0;
2624 window->pending_allocation.y = 0;
2625 window->pending_allocation.width = width;
2626 window->pending_allocation.height = height;
2627
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002628 if (window->min_allocation.width == 0)
2629 window->min_allocation = window->pending_allocation;
2630 if (window->pending_allocation.width < window->min_allocation.width)
2631 window->pending_allocation.width = window->min_allocation.width;
2632 if (window->pending_allocation.height < window->min_allocation.height)
2633 window->pending_allocation.height = window->min_allocation.height;
2634
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002635 window->resize_needed = 1;
2636 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002637}
2638
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002639void
2640widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2641{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002642 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002643}
2644
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002645static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002646handle_ping(void *data, struct wl_shell_surface *shell_surface,
2647 uint32_t serial)
2648{
2649 wl_shell_surface_pong(shell_surface, serial);
2650}
2651
2652static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002653handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002654 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002655{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002656 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002657
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002658 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002659 return;
2660
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002661 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002662 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002663}
2664
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002665static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002666menu_destroy(struct menu *menu)
2667{
2668 widget_destroy(menu->widget);
2669 window_destroy(menu->window);
2670 free(menu);
2671}
2672
2673static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002674handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2675{
2676 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002677 struct menu *menu = window->widget->user_data;
2678
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002679 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002680 * device. Or just use wl_callback. And this really needs to
2681 * be a window vfunc that the menu can set. And we need the
2682 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002683
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002684 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002685 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002686 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002687}
2688
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002689static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002690 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002691 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002692 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002693};
2694
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002695void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002696window_get_allocation(struct window *window,
2697 struct rectangle *allocation)
2698{
2699 *allocation = window->allocation;
2700}
2701
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002702static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002703widget_redraw(struct widget *widget)
2704{
2705 struct widget *child;
2706
2707 if (widget->redraw_handler)
2708 widget->redraw_handler(widget, widget->user_data);
2709 wl_list_for_each(child, &widget->child_list, link)
2710 widget_redraw(child);
2711}
2712
2713static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002714frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2715{
2716 struct window *window = data;
2717
2718 wl_callback_destroy(callback);
2719 window->redraw_scheduled = 0;
2720 if (window->redraw_needed)
2721 window_schedule_redraw(window);
2722}
2723
2724static const struct wl_callback_listener listener = {
2725 frame_callback
2726};
2727
2728static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002729idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002730{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002731 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002732 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002733
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002734 if (window->resize_needed)
2735 idle_resize(window);
2736
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002737 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002738 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002739 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002740 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002741 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002742
2743 callback = wl_surface_frame(window->surface);
2744 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002745}
2746
2747void
2748window_schedule_redraw(struct window *window)
2749{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002750 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002751 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002752 window->redraw_task.run = idle_redraw;
2753 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002754 window->redraw_scheduled = 1;
2755 }
2756}
2757
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002758void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002759window_set_fullscreen(struct window *window, int fullscreen)
2760{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002761 if (!window->display->shell)
2762 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002763
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002764 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002765 return;
2766
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002767 if (fullscreen) {
2768 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002769 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002770 wl_shell_surface_set_fullscreen(window->shell_surface,
2771 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2772 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002773 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002774 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002775 wl_shell_surface_set_toplevel(window->shell_surface);
2776 window_schedule_resize(window,
2777 window->saved_allocation.width,
2778 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002779 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002780}
2781
2782void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002783window_set_maximized(struct window *window, int maximized)
2784{
2785 if (!window->display->shell)
2786 return;
2787
2788 if ((window->type == TYPE_MAXIMIZED) == maximized)
2789 return;
2790
2791 if (window->type == TYPE_TOPLEVEL) {
2792 window->saved_allocation = window->allocation;
2793 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2794 window->type = TYPE_MAXIMIZED;
2795 } else {
2796 wl_shell_surface_set_toplevel(window->shell_surface);
2797 window->type = TYPE_TOPLEVEL;
2798 window_schedule_resize(window,
2799 window->saved_allocation.width,
2800 window->saved_allocation.height);
2801 }
2802}
2803
2804void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002805window_set_user_data(struct window *window, void *data)
2806{
2807 window->user_data = data;
2808}
2809
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002810void *
2811window_get_user_data(struct window *window)
2812{
2813 return window->user_data;
2814}
2815
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002816void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002817window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002818 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002819{
2820 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002821}
2822
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002823void
2824window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002825 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002826{
2827 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002828}
2829
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002830void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002831window_set_data_handler(struct window *window, window_data_handler_t handler)
2832{
2833 window->data_handler = handler;
2834}
2835
2836void
2837window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2838{
2839 window->drop_handler = handler;
2840}
2841
2842void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002843window_set_close_handler(struct window *window,
2844 window_close_handler_t handler)
2845{
2846 window->close_handler = handler;
2847}
2848
2849void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002850window_set_fullscreen_handler(struct window *window,
2851 window_fullscreen_handler_t handler)
2852{
2853 window->fullscreen_handler = handler;
2854}
2855
2856void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002857window_set_title(struct window *window, const char *title)
2858{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002859 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002860 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002861 if (window->shell_surface)
2862 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002863}
2864
2865const char *
2866window_get_title(struct window *window)
2867{
2868 return window->title;
2869}
2870
2871void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002872window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2873{
2874 struct text_cursor_position *text_cursor_position =
2875 window->display->text_cursor_position;
2876
Scott Moreau9295ce02012-06-01 12:46:10 -06002877 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002878 return;
2879
2880 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002881 window->surface,
2882 wl_fixed_from_int(x),
2883 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002884}
2885
2886void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002887window_damage(struct window *window, int32_t x, int32_t y,
2888 int32_t width, int32_t height)
2889{
2890 wl_surface_damage(window->surface, x, y, width, height);
2891}
2892
Casey Dahlin9074db52012-04-19 22:50:09 -04002893static void
2894surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002895 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002896{
Rob Bradford7507b572012-05-15 17:55:34 +01002897 struct window *window = data;
2898 struct output *output;
2899 struct output *output_found = NULL;
2900 struct window_output *window_output;
2901
2902 wl_list_for_each(output, &window->display->output_list, link) {
2903 if (output->output == wl_output) {
2904 output_found = output;
2905 break;
2906 }
2907 }
2908
2909 if (!output_found)
2910 return;
2911
2912 window_output = malloc (sizeof *window_output);
2913 window_output->output = output_found;
2914
2915 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002916}
2917
2918static void
2919surface_leave(void *data,
2920 struct wl_surface *wl_surface, struct wl_output *output)
2921{
Rob Bradford7507b572012-05-15 17:55:34 +01002922 struct window *window = data;
2923 struct window_output *window_output;
2924 struct window_output *window_output_found = NULL;
2925
2926 wl_list_for_each(window_output, &window->window_output_list, link) {
2927 if (window_output->output->output == output) {
2928 window_output_found = window_output;
2929 break;
2930 }
2931 }
2932
2933 if (window_output_found) {
2934 wl_list_remove(&window_output_found->link);
2935 free(window_output_found);
2936 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002937}
2938
2939static const struct wl_surface_listener surface_listener = {
2940 surface_enter,
2941 surface_leave
2942};
2943
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002944static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002945window_create_internal(struct display *display,
2946 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002947{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002948 struct window *window;
2949
2950 window = malloc(sizeof *window);
2951 if (window == NULL)
2952 return NULL;
2953
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002954 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002955 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002956 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002957 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002958 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002959 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002960 window->shell_surface =
2961 wl_shell_get_shell_surface(display->shell,
2962 window->surface);
2963 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002964 window->allocation.x = 0;
2965 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002966 window->allocation.width = 0;
2967 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002968 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002969 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002970 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002971 window->input_region = NULL;
2972 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002973
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002974 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002975#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002976 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002977#else
2978 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2979#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002980 else
2981 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002982
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002983 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002984 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002985 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002986
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002987 if (window->shell_surface) {
2988 wl_shell_surface_set_user_data(window->shell_surface, window);
2989 wl_shell_surface_add_listener(window->shell_surface,
2990 &shell_surface_listener, window);
2991 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002992
Rob Bradford7507b572012-05-15 17:55:34 +01002993 wl_list_init (&window->window_output_list);
2994
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002995 return window;
2996}
2997
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002998struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002999window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003000{
3001 struct window *window;
3002
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003003 window = window_create_internal(display, NULL, TYPE_NONE);
3004 if (!window)
3005 return NULL;
3006
3007 return window;
3008}
3009
3010struct window *
3011window_create_custom(struct display *display)
3012{
3013 struct window *window;
3014
3015 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003016 if (!window)
3017 return NULL;
3018
3019 return window;
3020}
3021
3022struct window *
3023window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003024 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003025{
3026 struct window *window;
3027
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003028 window = window_create_internal(parent->display,
3029 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003030 if (!window)
3031 return NULL;
3032
3033 window->x = x;
3034 window->y = y;
3035
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003036 if (display->shell)
3037 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003038 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003039 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003040
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003041 return window;
3042}
3043
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003044static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003045menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003046{
3047 int next;
3048
3049 next = (sy - 8) / 20;
3050 if (menu->current != next) {
3051 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003052 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003053 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003054}
3055
3056static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003057menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003058 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003059 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003060{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003061 struct menu *menu = data;
3062
3063 if (widget == menu->widget)
3064 menu_set_item(data, y);
3065
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003066 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003067}
3068
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003069static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003070menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003071 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003072{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003073 struct menu *menu = data;
3074
3075 if (widget == menu->widget)
3076 menu_set_item(data, y);
3077
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003078 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003079}
3080
3081static void
3082menu_leave_handler(struct widget *widget, struct input *input, void *data)
3083{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003084 struct menu *menu = data;
3085
3086 if (widget == menu->widget)
3087 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003088}
3089
3090static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003091menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003092 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003093 uint32_t button, enum wl_pointer_button_state state,
3094 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003095
3096{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003097 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003098
Daniel Stone4dbadb12012-05-30 16:31:51 +01003099 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3100 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003101 /* Either relase after press-drag-release or
3102 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003103 menu->func(menu->window->parent,
3104 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003105 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003106 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003107 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003108}
3109
3110static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003111menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003112{
3113 cairo_t *cr;
3114 const int32_t r = 3, margin = 3;
3115 struct menu *menu = data;
3116 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003117 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003118
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003119 cr = cairo_create(window->cairo_surface);
3120 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3121 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3122 cairo_paint(cr);
3123
3124 width = window->allocation.width;
3125 height = window->allocation.height;
3126 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003127
3128 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003129 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3130 cairo_fill(cr);
3131
3132 for (i = 0; i < menu->count; i++) {
3133 if (i == menu->current) {
3134 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3135 cairo_rectangle(cr, margin, i * 20 + margin,
3136 width - 2 * margin, 20);
3137 cairo_fill(cr);
3138 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3139 cairo_move_to(cr, 10, i * 20 + 16);
3140 cairo_show_text(cr, menu->entries[i]);
3141 } else {
3142 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3143 cairo_move_to(cr, 10, i * 20 + 16);
3144 cairo_show_text(cr, menu->entries[i]);
3145 }
3146 }
3147
3148 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003149}
3150
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003151void
3152window_show_menu(struct display *display,
3153 struct input *input, uint32_t time, struct window *parent,
3154 int32_t x, int32_t y,
3155 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003156{
3157 struct window *window;
3158 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003159 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003160
3161 menu = malloc(sizeof *menu);
3162 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003163 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003164
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003165 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003166 if (!window) {
3167 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003168 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003169 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003170
3171 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003172 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003173 menu->entries = entries;
3174 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003175 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003176 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003177 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003178 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003179 window->type = TYPE_MENU;
3180 window->x = x;
3181 window->y = y;
3182
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003183 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003184 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003185 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003186 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003187 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003188
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003189 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003190 widget_set_enter_handler(menu->widget, menu_enter_handler);
3191 widget_set_leave_handler(menu->widget, menu_leave_handler);
3192 widget_set_motion_handler(menu->widget, menu_motion_handler);
3193 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003194
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003195 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003196 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003197}
3198
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003199void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003200window_set_buffer_type(struct window *window, enum window_buffer_type type)
3201{
3202 window->buffer_type = type;
3203}
3204
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003205
3206static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003207display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003208 struct wl_output *wl_output,
3209 int x, int y,
3210 int physical_width,
3211 int physical_height,
3212 int subpixel,
3213 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003214 const char *model,
3215 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003216{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003217 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003218
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003219 output->allocation.x = x;
3220 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003221}
3222
3223static void
3224display_handle_mode(void *data,
3225 struct wl_output *wl_output,
3226 uint32_t flags,
3227 int width,
3228 int height,
3229 int refresh)
3230{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003231 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003232 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003233
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003234 if (flags & WL_OUTPUT_MODE_CURRENT) {
3235 output->allocation.width = width;
3236 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003237 if (display->output_configure_handler)
3238 (*display->output_configure_handler)(
3239 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003240 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003241}
3242
3243static const struct wl_output_listener output_listener = {
3244 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003245 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003246};
3247
3248static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003249display_add_output(struct display *d, uint32_t id)
3250{
3251 struct output *output;
3252
3253 output = malloc(sizeof *output);
3254 if (output == NULL)
3255 return;
3256
3257 memset(output, 0, sizeof *output);
3258 output->display = d;
3259 output->output =
3260 wl_display_bind(d->display, id, &wl_output_interface);
3261 wl_list_insert(d->output_list.prev, &output->link);
3262
3263 wl_output_add_listener(output->output, &output_listener, output);
3264}
3265
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003266static void
3267output_destroy(struct output *output)
3268{
3269 if (output->destroy_handler)
3270 (*output->destroy_handler)(output, output->user_data);
3271
3272 wl_output_destroy(output->output);
3273 wl_list_remove(&output->link);
3274 free(output);
3275}
3276
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003277void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003278display_set_output_configure_handler(struct display *display,
3279 display_output_handler_t handler)
3280{
3281 struct output *output;
3282
3283 display->output_configure_handler = handler;
3284 if (!handler)
3285 return;
3286
3287 wl_list_for_each(output, &display->output_list, link)
3288 (*display->output_configure_handler)(output,
3289 display->user_data);
3290}
3291
3292void
3293output_set_user_data(struct output *output, void *data)
3294{
3295 output->user_data = data;
3296}
3297
3298void *
3299output_get_user_data(struct output *output)
3300{
3301 return output->user_data;
3302}
3303
3304void
3305output_set_destroy_handler(struct output *output,
3306 display_output_handler_t handler)
3307{
3308 output->destroy_handler = handler;
3309 /* FIXME: implement this, once we have way to remove outputs */
3310}
3311
3312void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003313output_get_allocation(struct output *output, struct rectangle *allocation)
3314{
3315 *allocation = output->allocation;
3316}
3317
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003318struct wl_output *
3319output_get_wl_output(struct output *output)
3320{
3321 return output->output;
3322}
3323
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003324static void
Daniel Stone97f68542012-05-30 16:32:01 +01003325fini_xkb(struct input *input)
3326{
3327 xkb_state_unref(input->xkb.state);
3328 xkb_map_unref(input->xkb.keymap);
3329}
3330
3331static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003332display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003333{
3334 struct input *input;
3335
3336 input = malloc(sizeof *input);
3337 if (input == NULL)
3338 return;
3339
3340 memset(input, 0, sizeof *input);
3341 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003342 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003343 input->pointer_focus = NULL;
3344 input->keyboard_focus = NULL;
3345 wl_list_insert(d->input_list.prev, &input->link);
3346
Daniel Stone37816df2012-05-16 18:45:18 +01003347 wl_seat_add_listener(input->seat, &seat_listener, input);
3348 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003349
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003350 input->data_device =
3351 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003352 input->seat);
3353 wl_data_device_add_listener(input->data_device, &data_device_listener,
3354 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003355
3356 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003357
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003358 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3359 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003360 input->repeat_task.run = keyboard_repeat_func;
3361 display_watch_fd(d, input->repeat_timer_fd,
3362 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003363}
3364
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003365static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003366input_destroy(struct input *input)
3367{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003368 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003369 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003370
3371 if (input->drag_offer)
3372 data_offer_destroy(input->drag_offer);
3373
3374 if (input->selection_offer)
3375 data_offer_destroy(input->selection_offer);
3376
3377 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003378 fini_xkb(input);
3379
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003380 wl_surface_destroy(input->pointer_surface);
3381
Pekka Paalanene1207c72011-12-16 12:02:09 +02003382 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003383 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003384 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003385 free(input);
3386}
3387
3388static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003389display_handle_global(struct wl_display *display, uint32_t id,
3390 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003391{
3392 struct display *d = data;
3393
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003394 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003395 d->compositor =
3396 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003397 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003398 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003399 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003400 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003401 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003402 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003403 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003404 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003405 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3406 d->data_device_manager =
3407 wl_display_bind(display, id,
3408 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003409 } else if (strcmp(interface, "text_cursor_position") == 0) {
3410 d->text_cursor_position =
3411 wl_display_bind(display, id,
3412 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003413 }
3414}
3415
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003416#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003417static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003418init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003419{
3420 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003421 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003422
Rob Clark6396ed32012-03-11 19:48:41 -05003423#ifdef USE_CAIRO_GLESV2
3424# define GL_BIT EGL_OPENGL_ES2_BIT
3425#else
3426# define GL_BIT EGL_OPENGL_BIT
3427#endif
3428
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003429 static const EGLint argb_cfg_attribs[] = {
3430 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003431 EGL_RED_SIZE, 1,
3432 EGL_GREEN_SIZE, 1,
3433 EGL_BLUE_SIZE, 1,
3434 EGL_ALPHA_SIZE, 1,
3435 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003436 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003437 EGL_NONE
3438 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003439
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003440#ifdef USE_CAIRO_GLESV2
3441 static const EGLint context_attribs[] = {
3442 EGL_CONTEXT_CLIENT_VERSION, 2,
3443 EGL_NONE
3444 };
3445 EGLint api = EGL_OPENGL_ES_API;
3446#else
3447 EGLint *context_attribs = NULL;
3448 EGLint api = EGL_OPENGL_API;
3449#endif
3450
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003451 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003452 if (!eglInitialize(d->dpy, &major, &minor)) {
3453 fprintf(stderr, "failed to initialize display\n");
3454 return -1;
3455 }
3456
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003457 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003458 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3459 return -1;
3460 }
3461
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003462 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3463 &d->argb_config, 1, &n) || n != 1) {
3464 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003465 return -1;
3466 }
3467
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003468 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003469 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003470 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003471 fprintf(stderr, "failed to create context\n");
3472 return -1;
3473 }
3474
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003475 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003476 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003477 return -1;
3478 }
3479
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003480#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003481 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3482 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3483 fprintf(stderr, "failed to get cairo egl argb device\n");
3484 return -1;
3485 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003486#endif
3487
3488 return 0;
3489}
3490
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003491static void
3492fini_egl(struct display *display)
3493{
3494#ifdef HAVE_CAIRO_EGL
3495 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003496#endif
3497
3498 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3499 EGL_NO_CONTEXT);
3500
3501 eglTerminate(display->dpy);
3502 eglReleaseThread();
3503}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003504#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003505
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003506static int
3507event_mask_update(uint32_t mask, void *data)
3508{
3509 struct display *d = data;
3510
3511 d->mask = mask;
3512
3513 return 0;
3514}
3515
3516static void
3517handle_display_data(struct task *task, uint32_t events)
3518{
3519 struct display *display =
3520 container_of(task, struct display, display_task);
3521
3522 wl_display_iterate(display->display, display->mask);
3523}
3524
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003525struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003526display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003527{
3528 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003529
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003530 d = malloc(sizeof *d);
3531 if (d == NULL)
3532 return NULL;
3533
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003534 memset(d, 0, sizeof *d);
3535
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003536 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003537 if (d->display == NULL) {
3538 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003539 return NULL;
3540 }
3541
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003542 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003543 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3544 d->display_task.run = handle_display_data;
3545 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3546
3547 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003548 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003549 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003550
Daniel Stone97f68542012-05-30 16:32:01 +01003551 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003552 if (d->xkb_context == NULL) {
3553 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003554 return NULL;
3555 }
3556
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003557 /* Set up listener so we'll catch all events. */
3558 wl_display_add_global_listener(d->display,
3559 display_handle_global, d);
3560
3561 /* Process connection events. */
3562 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003563#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003564 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003565 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003566#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003567
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003568 d->image_target_texture_2d =
3569 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3570 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3571 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3572
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003573 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003574
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003575 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003576
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003577 wl_list_init(&d->window_list);
3578
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003579 return d;
3580}
3581
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003582static void
3583display_destroy_outputs(struct display *display)
3584{
3585 struct output *tmp;
3586 struct output *output;
3587
3588 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3589 output_destroy(output);
3590}
3591
Pekka Paalanene1207c72011-12-16 12:02:09 +02003592static void
3593display_destroy_inputs(struct display *display)
3594{
3595 struct input *tmp;
3596 struct input *input;
3597
3598 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3599 input_destroy(input);
3600}
3601
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003602void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003603display_destroy(struct display *display)
3604{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003605 if (!wl_list_empty(&display->window_list))
3606 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3607
3608 if (!wl_list_empty(&display->deferred_list))
3609 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3610
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003611 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003612 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003613
Daniel Stone97f68542012-05-30 16:32:01 +01003614 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003615
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003616 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003617 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003618
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003619#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003620 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003621#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003622
Pekka Paalanenc2052982011-12-16 11:41:32 +02003623 if (display->shell)
3624 wl_shell_destroy(display->shell);
3625
3626 if (display->shm)
3627 wl_shm_destroy(display->shm);
3628
3629 if (display->data_device_manager)
3630 wl_data_device_manager_destroy(display->data_device_manager);
3631
3632 wl_compositor_destroy(display->compositor);
3633
3634 close(display->epoll_fd);
3635
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003636 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003637 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003638 free(display);
3639}
3640
3641void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003642display_set_user_data(struct display *display, void *data)
3643{
3644 display->user_data = data;
3645}
3646
3647void *
3648display_get_user_data(struct display *display)
3649{
3650 return display->user_data;
3651}
3652
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003653struct wl_display *
3654display_get_display(struct display *display)
3655{
3656 return display->display;
3657}
3658
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003659struct output *
3660display_get_output(struct display *display)
3661{
3662 return container_of(display->output_list.next, struct output, link);
3663}
3664
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003665struct wl_compositor *
3666display_get_compositor(struct display *display)
3667{
3668 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003669}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003670
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003671uint32_t
3672display_get_serial(struct display *display)
3673{
3674 return display->serial;
3675}
3676
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003677EGLDisplay
3678display_get_egl_display(struct display *d)
3679{
3680 return d->dpy;
3681}
3682
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003683struct wl_data_source *
3684display_create_data_source(struct display *display)
3685{
3686 return wl_data_device_manager_create_data_source(display->data_device_manager);
3687}
3688
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003689EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003690display_get_argb_egl_config(struct display *d)
3691{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003692 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003693}
3694
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003695struct wl_shell *
3696display_get_shell(struct display *display)
3697{
3698 return display->shell;
3699}
3700
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003701int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003702display_acquire_window_surface(struct display *display,
3703 struct window *window,
3704 EGLContext ctx)
3705{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003706#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003707 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003708 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003709
3710 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003711 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003712 device = cairo_surface_get_device(window->cairo_surface);
3713 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003714 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003715
Benjamin Franzke0c991632011-09-27 21:57:31 +02003716 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003717 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003718 ctx = display->argb_ctx;
3719 else
3720 assert(0);
3721 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003722
3723 data = cairo_surface_get_user_data(window->cairo_surface,
3724 &surface_data_key);
3725
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003726 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003727 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003728 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3729 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003730
3731 return 0;
3732#else
3733 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003734#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003735}
3736
3737void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003738display_release_window_surface(struct display *display,
3739 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003740{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003741#ifdef HAVE_CAIRO_EGL
3742 cairo_device_t *device;
3743
3744 device = cairo_surface_get_device(window->cairo_surface);
3745 if (!device)
3746 return;
3747
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003748 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003749 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003750 cairo_device_release(device);
3751#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003752}
3753
3754void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003755display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003756{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003757 wl_list_insert(&display->deferred_list, &task->link);
3758}
3759
3760void
3761display_watch_fd(struct display *display,
3762 int fd, uint32_t events, struct task *task)
3763{
3764 struct epoll_event ep;
3765
3766 ep.events = events;
3767 ep.data.ptr = task;
3768 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3769}
3770
3771void
3772display_run(struct display *display)
3773{
3774 struct task *task;
3775 struct epoll_event ep[16];
3776 int i, count;
3777
Pekka Paalanen826d7952011-12-15 10:14:07 +02003778 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003779 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003780 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003781
3782 while (!wl_list_empty(&display->deferred_list)) {
3783 task = container_of(display->deferred_list.next,
3784 struct task, link);
3785 wl_list_remove(&task->link);
3786 task->run(task, 0);
3787 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003788
3789 if (!display->running)
3790 break;
3791
3792 wl_display_flush(display->display);
3793
3794 count = epoll_wait(display->epoll_fd,
3795 ep, ARRAY_LENGTH(ep), -1);
3796 for (i = 0; i < count; i++) {
3797 task = ep[i].data.ptr;
3798 task->run(task, ep[i].events);
3799 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003800 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003801}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003802
3803void
3804display_exit(struct display *display)
3805{
3806 display->running = 0;
3807}