blob: 3a746abbcd04ee42af83b0dd4694c1310a0eeafa [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060064#include "text-cursor-position-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030065#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070069struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030070
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050073 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040074 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040075 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040076 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060077 struct text_cursor_position *text_cursor_position;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050079 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020081 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040082 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040083
84 int display_fd;
85 uint32_t mask;
86 struct task display_task;
87
88 int epoll_fd;
89 struct wl_list deferred_list;
90
Pekka Paalanen826d7952011-12-15 10:14:07 +020091 int running;
92
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040094 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050095 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040096
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040097 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030099 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300100 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400101
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500102 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
103 PFNEGLCREATEIMAGEKHRPROC create_image;
104 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200105
106 display_output_handler_t output_configure_handler;
107
108 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100109
110 struct xkb_context *xkb_context;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500111};
112
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400114 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400116 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500117 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500119 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_CUSTOM
121};
Rob Bradford7507b572012-05-15 17:55:34 +0100122
123struct window_output {
124 struct output *output;
125 struct wl_list link;
126};
127
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128struct window {
129 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500130 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100131 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500132 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200133 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500134 struct wl_region *input_region;
135 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500136 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400138 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500139 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500140 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400141 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400142 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400143 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400144 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400145 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400146 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400147 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400148 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400149 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500150
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400151 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500152
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700153 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400154
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500155 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500156 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400157 window_data_handler_t data_handler;
158 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500159 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400160 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200162 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500163 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500164
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500165 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400166 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500167};
168
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500169struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500170 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300171 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500172 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400173 struct wl_list link;
174 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500175 widget_resize_handler_t resize_handler;
176 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500177 widget_enter_handler_t enter_handler;
178 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500179 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500180 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400181 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500182 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300183 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400184};
185
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400186struct input {
187 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100188 struct wl_seat *seat;
189 struct wl_pointer *pointer;
190 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400191 struct window *pointer_focus;
192 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300193 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300194 uint32_t cursor_anim_start;
195 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300196 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400197 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400198 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400199 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400200 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400201 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400202
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500203 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500204 struct widget *grab;
205 uint32_t grab_button;
206
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400207 struct wl_data_device *data_device;
208 struct data_offer *drag_offer;
209 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100210
211 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100212 struct xkb_keymap *keymap;
213 struct xkb_state *state;
214 xkb_mod_mask_t control_mask;
215 xkb_mod_mask_t alt_mask;
216 xkb_mod_mask_t shift_mask;
217 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400218
219 struct task repeat_task;
220 int repeat_timer_fd;
221 uint32_t repeat_sym;
222 uint32_t repeat_key;
223 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400224};
225
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500226struct output {
227 struct display *display;
228 struct wl_output *output;
229 struct rectangle allocation;
230 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200231
232 display_output_handler_t destroy_handler;
233 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500234};
235
Martin Minarik1998b152012-05-10 02:04:35 +0200236enum frame_button_action {
237 FRAME_BUTTON_NULL = 0,
238 FRAME_BUTTON_ICON = 1,
239 FRAME_BUTTON_CLOSE = 2,
240 FRAME_BUTTON_MINIMIZE = 3,
241 FRAME_BUTTON_MAXIMIZE = 4,
242};
243
244enum frame_button_pointer {
245 FRAME_BUTTON_DEFAULT = 0,
246 FRAME_BUTTON_OVER = 1,
247 FRAME_BUTTON_ACTIVE = 2,
248};
249
250enum frame_button_align {
251 FRAME_BUTTON_RIGHT = 0,
252 FRAME_BUTTON_LEFT = 1,
253};
254
255enum frame_button_decoration {
256 FRAME_BUTTON_NONE = 0,
257 FRAME_BUTTON_FANCY = 1,
258};
259
260struct frame_button {
261 struct widget *widget;
262 struct frame *frame;
263 cairo_surface_t *icon;
264 enum frame_button_action type;
265 enum frame_button_pointer state;
266 struct wl_list link; /* buttons_list */
267 enum frame_button_align align;
268 enum frame_button_decoration decoration;
269};
270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500271struct frame {
272 struct widget *widget;
273 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200274 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500275};
276
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500277struct menu {
278 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500279 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500280 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500281 const char **entries;
282 uint32_t time;
283 int current;
284 int count;
285 menu_func_t func;
286};
287
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300288struct tooltip {
289 struct widget *parent;
290 struct window *window;
291 struct widget *widget;
292 char *entry;
293 struct task tooltip_task;
294 int tooltip_fd;
295 float x, y;
296};
297
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700298struct shm_pool {
299 struct wl_shm_pool *pool;
300 size_t size;
301 size_t used;
302 void *data;
303};
304
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400305enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300306 CURSOR_DEFAULT = 100,
307 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400308};
309
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500310enum window_location {
311 WINDOW_INTERIOR = 0,
312 WINDOW_RESIZING_TOP = 1,
313 WINDOW_RESIZING_BOTTOM = 2,
314 WINDOW_RESIZING_LEFT = 4,
315 WINDOW_RESIZING_TOP_LEFT = 5,
316 WINDOW_RESIZING_BOTTOM_LEFT = 6,
317 WINDOW_RESIZING_RIGHT = 8,
318 WINDOW_RESIZING_TOP_RIGHT = 9,
319 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
320 WINDOW_RESIZING_MASK = 15,
321 WINDOW_EXTERIOR = 16,
322 WINDOW_TITLEBAR = 17,
323 WINDOW_CLIENT_AREA = 18,
324};
325
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326static const cairo_user_data_key_t surface_data_key;
327struct surface_data {
328 struct wl_buffer *buffer;
329};
330
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500331#define MULT(_d,c,a,t) \
332 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
333
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500334#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100336struct egl_window_surface_data {
337 struct display *display;
338 struct wl_surface *surface;
339 struct wl_egl_window *window;
340 EGLSurface surf;
341};
342
343static void
344egl_window_surface_data_destroy(void *p)
345{
346 struct egl_window_surface_data *data = p;
347 struct display *d = data->display;
348
349 eglDestroySurface(d->dpy, data->surf);
350 wl_egl_window_destroy(data->window);
351 data->surface = NULL;
352
353 free(p);
354}
355
356static cairo_surface_t *
357display_create_egl_window_surface(struct display *display,
358 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400359 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100360 struct rectangle *rectangle)
361{
362 cairo_surface_t *cairo_surface;
363 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400364 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200365 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100366
367 data = malloc(sizeof *data);
368 if (data == NULL)
369 return NULL;
370
371 data->display = display;
372 data->surface = surface;
373
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500374 config = display->argb_config;
375 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400377 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400379 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100380
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400381 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500382 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383
Benjamin Franzke0c991632011-09-27 21:57:31 +0200384 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100385 data->surf,
386 rectangle->width,
387 rectangle->height);
388
389 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
390 data, egl_window_surface_data_destroy);
391
392 return cairo_surface;
393}
394
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395#endif
396
397struct wl_buffer *
398display_get_buffer_for_surface(struct display *display,
399 cairo_surface_t *surface)
400{
401 struct surface_data *data;
402
403 data = cairo_surface_get_user_data (surface, &surface_data_key);
404
405 return data->buffer;
406}
407
408struct shm_surface_data {
409 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700410 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411};
412
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500413static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700414shm_pool_destroy(struct shm_pool *pool);
415
416static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417shm_surface_data_destroy(void *p)
418{
419 struct shm_surface_data *data = p;
420
421 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700422 if (data->pool)
423 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300424
425 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426}
427
Kristian Høgsberg16626282012-04-03 11:21:27 -0400428static struct wl_shm_pool *
429make_shm_pool(struct display *display, int size, void **data)
430{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400431 struct wl_shm_pool *pool;
432 int fd;
433
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300434 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400435 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300436 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
437 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400438 return NULL;
439 }
440
441 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400442 if (*data == MAP_FAILED) {
443 fprintf(stderr, "mmap failed: %m\n");
444 close(fd);
445 return NULL;
446 }
447
448 pool = wl_shm_create_pool(display->shm, fd, size);
449
450 close(fd);
451
452 return pool;
453}
454
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700455static struct shm_pool *
456shm_pool_create(struct display *display, size_t size)
457{
458 struct shm_pool *pool = malloc(sizeof *pool);
459
460 if (!pool)
461 return NULL;
462
463 pool->pool = make_shm_pool(display, size, &pool->data);
464 if (!pool->pool) {
465 free(pool);
466 return NULL;
467 }
468
469 pool->size = size;
470 pool->used = 0;
471
472 return pool;
473}
474
475static void *
476shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
477{
478 if (pool->used + size > pool->size)
479 return NULL;
480
481 *offset = pool->used;
482 pool->used += size;
483
484 return (char *) pool->data + *offset;
485}
486
487/* destroy the pool. this does not unmap the memory though */
488static void
489shm_pool_destroy(struct shm_pool *pool)
490{
491 munmap(pool->data, pool->size);
492 wl_shm_pool_destroy(pool->pool);
493 free(pool);
494}
495
496/* Start allocating from the beginning of the pool again */
497static void
498shm_pool_reset(struct shm_pool *pool)
499{
500 pool->used = 0;
501}
502
503static int
504data_length_for_shm_surface(struct rectangle *rect)
505{
506 int stride;
507
508 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
509 rect->width);
510 return stride * rect->height;
511}
512
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500513static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700514display_create_shm_surface_from_pool(struct display *display,
515 struct rectangle *rectangle,
516 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517{
518 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400519 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400520 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700521 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400522 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523
524 data = malloc(sizeof *data);
525 if (data == NULL)
526 return NULL;
527
528 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
529 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700530 length = stride * rectangle->height;
531 data->pool = NULL;
532 map = shm_pool_allocate(pool, length, &offset);
533
534 if (!map) {
535 free(data);
536 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400537 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400539 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540 CAIRO_FORMAT_ARGB32,
541 rectangle->width,
542 rectangle->height,
543 stride);
544
545 cairo_surface_set_user_data (surface, &surface_data_key,
546 data, shm_surface_data_destroy);
547
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500549 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500551 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400552
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700553 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400554 rectangle->width,
555 rectangle->height,
556 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700558 return surface;
559}
560
561static cairo_surface_t *
562display_create_shm_surface(struct display *display,
563 struct rectangle *rectangle, uint32_t flags,
564 struct window *window)
565{
566 struct shm_surface_data *data;
567 struct shm_pool *pool;
568 cairo_surface_t *surface;
569
570 if (window && window->pool) {
571 shm_pool_reset(window->pool);
572 surface = display_create_shm_surface_from_pool(display,
573 rectangle,
574 flags,
575 window->pool);
576 if (surface)
577 return surface;
578 }
579
580 pool = shm_pool_create(display,
581 data_length_for_shm_surface(rectangle));
582 if (!pool)
583 return NULL;
584
585 surface =
586 display_create_shm_surface_from_pool(display, rectangle,
587 flags, pool);
588
589 if (!surface) {
590 shm_pool_destroy(pool);
591 return NULL;
592 }
593
594 /* make sure we destroy the pool when the surface is destroyed */
595 data = cairo_surface_get_user_data(surface, &surface_data_key);
596 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597
598 return surface;
599}
600
nobled7b87cb02011-02-01 18:51:47 +0000601static int
602check_size(struct rectangle *rect)
603{
604 if (rect->width && rect->height)
605 return 0;
606
607 fprintf(stderr, "tried to create surface of "
608 "width: %d, height: %d\n", rect->width, rect->height);
609 return -1;
610}
611
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400612cairo_surface_t *
613display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100614 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400615 struct rectangle *rectangle,
616 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617{
nobled7b87cb02011-02-01 18:51:47 +0000618 if (check_size(rectangle) < 0)
619 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500620#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300621 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400622 return display_create_egl_window_surface(display,
623 surface,
624 flags,
625 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400627 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628}
629
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300630static const char *cursors[] = {
631 "bottom_left_corner",
632 "bottom_right_corner",
633 "bottom_side",
634 "grabbing",
635 "left_ptr",
636 "left_side",
637 "right_side",
638 "top_left_corner",
639 "top_right_corner",
640 "top_side",
641 "xterm",
642 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400643 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300644};
645
646static void
647create_cursors(struct display *display)
648{
649 unsigned int i;
650
651 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
652 display->cursors =
653 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
654
655 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
656 display->cursors[i] =
657 wl_cursor_theme_get_cursor(display->cursor_theme,
658 cursors[i]);
659}
660
661static void
662destroy_cursors(struct display *display)
663{
664 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800665 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300666}
667
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300668struct wl_cursor_image *
669display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300671 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300672 wl_cursor_theme_get_cursor(display->cursor_theme,
673 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300675 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400676}
677
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400678static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200679window_get_resize_dx_dy(struct window *window, int *x, int *y)
680{
681 if (window->resize_edges & WINDOW_RESIZING_LEFT)
682 *x = window->server_allocation.width - window->allocation.width;
683 else
684 *x = 0;
685
686 if (window->resize_edges & WINDOW_RESIZING_TOP)
687 *y = window->server_allocation.height -
688 window->allocation.height;
689 else
690 *y = 0;
691
692 window->resize_edges = 0;
693}
694
695static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500696window_attach_surface(struct window *window)
697{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400698 struct display *display = window->display;
699 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000700#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100701 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000702#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500703 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100704
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400705 if (window->type == TYPE_NONE) {
706 window->type = TYPE_TOPLEVEL;
707 if (display->shell)
708 wl_shell_surface_set_toplevel(window->shell_surface);
709 }
710
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100711 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000712#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100713 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
714 data = cairo_surface_get_user_data(window->cairo_surface,
715 &surface_data_key);
716
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100717 cairo_gl_surface_swapbuffers(window->cairo_surface);
718 wl_egl_window_get_attached_size(data->window,
719 &window->server_allocation.width,
720 &window->server_allocation.height);
721 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000722#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100723 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100724 buffer =
725 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400726 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200728 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100729 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400730 wl_surface_damage(window->surface, 0, 0,
731 window->allocation.width,
732 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400734 cairo_surface_destroy(window->cairo_surface);
735 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000737 default:
738 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100739 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500740
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500741 if (window->input_region) {
742 wl_surface_set_input_region(window->surface,
743 window->input_region);
744 wl_region_destroy(window->input_region);
745 window->input_region = NULL;
746 }
747
748 if (window->opaque_region) {
749 wl_surface_set_opaque_region(window->surface,
750 window->opaque_region);
751 wl_region_destroy(window->opaque_region);
752 window->opaque_region = NULL;
753 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500754}
755
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500756void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400757window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500758{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400759 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100760 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500761}
762
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400763void
764window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400765{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500766 cairo_surface_reference(surface);
767
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400768 if (window->cairo_surface != NULL)
769 cairo_surface_destroy(window->cairo_surface);
770
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500771 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400772}
773
Benjamin Franzke22d54812011-07-16 19:50:32 +0000774#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100775static void
776window_resize_cairo_window_surface(struct window *window)
777{
778 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200779 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100780
781 data = cairo_surface_get_user_data(window->cairo_surface,
782 &surface_data_key);
783
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200784 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100785 wl_egl_window_resize(data->window,
786 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200787 window->allocation.height,
788 x,y);
789
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100790 cairo_gl_surface_set_size(window->cairo_surface,
791 window->allocation.width,
792 window->allocation.height);
793}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000794#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100795
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400796struct display *
797window_get_display(struct window *window)
798{
799 return window->display;
800}
801
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400802void
803window_create_surface(struct window *window)
804{
805 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400806 uint32_t flags = 0;
807
808 if (!window->transparent)
809 flags = SURFACE_OPAQUE;
810
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400811 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500812#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100813 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
814 if (window->cairo_surface) {
815 window_resize_cairo_window_surface(window);
816 return;
817 }
818 surface = display_create_surface(window->display,
819 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400820 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400822#endif
823 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400824 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400825 &window->allocation,
826 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400827 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800828 default:
829 surface = NULL;
830 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400832
833 window_set_surface(window, surface);
834 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835}
836
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200837static void frame_destroy(struct frame *frame);
838
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400839void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500840window_destroy(struct window *window)
841{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200842 struct display *display = window->display;
843 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100844 struct window_output *window_output;
845 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200846
847 if (window->redraw_scheduled)
848 wl_list_remove(&window->redraw_task.link);
849
850 wl_list_for_each(input, &display->input_list, link) {
851 if (input->pointer_focus == window)
852 input->pointer_focus = NULL;
853 if (input->keyboard_focus == window)
854 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500855 if (input->focus_widget &&
856 input->focus_widget->window == window)
857 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200858 }
859
Rob Bradford7507b572012-05-15 17:55:34 +0100860 wl_list_for_each_safe(window_output, window_output_tmp,
861 &window->window_output_list, link) {
862 free (window_output);
863 }
864
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500865 if (window->input_region)
866 wl_region_destroy(window->input_region);
867 if (window->opaque_region)
868 wl_region_destroy(window->opaque_region);
869
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200870 if (window->frame)
871 frame_destroy(window->frame);
872
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200873 if (window->shell_surface)
874 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500875 wl_surface_destroy(window->surface);
876 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200877
878 if (window->cairo_surface != NULL)
879 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200880
881 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500882 free(window);
883}
884
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500885static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500886widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400887{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500888 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400889
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500890 wl_list_for_each(child, &widget->child_list, link) {
891 target = widget_find_widget(child, x, y);
892 if (target)
893 return target;
894 }
895
896 if (widget->allocation.x <= x &&
897 x < widget->allocation.x + widget->allocation.width &&
898 widget->allocation.y <= y &&
899 y < widget->allocation.y + widget->allocation.height) {
900 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400901 }
902
903 return NULL;
904}
905
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500906static struct widget *
907widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400908{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500909 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400910
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500911 widget = malloc(sizeof *widget);
912 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500913 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500914 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500915 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500916 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500917 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300918 widget->tooltip = NULL;
919 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500920
921 return widget;
922}
923
924struct widget *
925window_add_widget(struct window *window, void *data)
926{
927 window->widget = widget_create(window, data);
928 wl_list_init(&window->widget->link);
929
930 return window->widget;
931}
932
933struct widget *
934widget_add_widget(struct widget *parent, void *data)
935{
936 struct widget *widget;
937
938 widget = widget_create(parent->window, data);
939 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400940
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500941 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400942}
943
944void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500945widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400946{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200947 struct display *display = widget->window->display;
948 struct input *input;
949
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300950 if (widget->tooltip) {
951 free(widget->tooltip);
952 widget->tooltip = NULL;
953 }
954
Pekka Paalanene156fb62012-01-19 13:51:38 +0200955 wl_list_for_each(input, &display->input_list, link) {
956 if (input->focus_widget == widget)
957 input->focus_widget = NULL;
958 }
959
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500960 wl_list_remove(&widget->link);
961 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400962}
963
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400964void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500965widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400966{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500967 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400968}
969
970void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500971widget_set_size(struct widget *widget, int32_t width, int32_t height)
972{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500973 widget->allocation.width = width;
974 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500975}
976
977void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500978widget_set_allocation(struct widget *widget,
979 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400980{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500981 widget->allocation.x = x;
982 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200983 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400984}
985
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500986void
987widget_set_transparent(struct widget *widget, int transparent)
988{
989 widget->opaque = !transparent;
990}
991
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400992void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500993widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400994{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500995 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996}
997
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500998void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500999widget_set_resize_handler(struct widget *widget,
1000 widget_resize_handler_t handler)
1001{
1002 widget->resize_handler = handler;
1003}
1004
1005void
1006widget_set_redraw_handler(struct widget *widget,
1007 widget_redraw_handler_t handler)
1008{
1009 widget->redraw_handler = handler;
1010}
1011
1012void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001013widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001014{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001015 widget->enter_handler = handler;
1016}
1017
1018void
1019widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1020{
1021 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001022}
1023
1024void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001025widget_set_motion_handler(struct widget *widget,
1026 widget_motion_handler_t handler)
1027{
1028 widget->motion_handler = handler;
1029}
1030
1031void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001032widget_set_button_handler(struct widget *widget,
1033 widget_button_handler_t handler)
1034{
1035 widget->button_handler = handler;
1036}
1037
1038void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001039widget_schedule_redraw(struct widget *widget)
1040{
1041 window_schedule_redraw(widget->window);
1042}
1043
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001044cairo_surface_t *
1045window_get_surface(struct window *window)
1046{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001047 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001048}
1049
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001050struct wl_surface *
1051window_get_wl_surface(struct window *window)
1052{
1053 return window->surface;
1054}
1055
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001056struct wl_shell_surface *
1057window_get_wl_shell_surface(struct window *window)
1058{
1059 return window->shell_surface;
1060}
1061
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001062static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001063tooltip_redraw_handler(struct widget *widget, void *data)
1064{
1065 cairo_t *cr;
1066 const int32_t r = 3;
1067 struct tooltip *tooltip = data;
1068 int32_t width, height;
1069 struct window *window = widget->window;
1070
1071 cr = cairo_create(window->cairo_surface);
1072 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1073 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1074 cairo_paint(cr);
1075
1076 width = window->allocation.width;
1077 height = window->allocation.height;
1078 rounded_rect(cr, 0, 0, width, height, r);
1079
1080 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1081 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1082 cairo_fill(cr);
1083
1084 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1085 cairo_move_to(cr, 10, 16);
1086 cairo_show_text(cr, tooltip->entry);
1087 cairo_destroy(cr);
1088}
1089
1090static cairo_text_extents_t
1091get_text_extents(struct tooltip *tooltip)
1092{
1093 struct window *window;
1094 cairo_t *cr;
1095 cairo_text_extents_t extents;
1096
1097 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1098 * created yet */
1099 window = tooltip->widget->window->parent;
1100 cr = cairo_create(window->cairo_surface);
1101 cairo_text_extents(cr, tooltip->entry, &extents);
1102 cairo_destroy(cr);
1103
1104 return extents;
1105}
1106
1107static int
1108window_create_tooltip(struct tooltip *tooltip)
1109{
1110 struct widget *parent = tooltip->parent;
1111 struct display *display = parent->window->display;
1112 struct window *window;
1113 const int offset_y = 27;
1114 const int margin = 3;
1115 cairo_text_extents_t extents;
1116
1117 if (tooltip->widget)
1118 return 0;
1119
1120 window = window_create_transient(display, parent->window, tooltip->x,
1121 tooltip->y + offset_y,
1122 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1123 if (!window)
1124 return -1;
1125
1126 tooltip->window = window;
1127 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1128
1129 extents = get_text_extents(tooltip);
1130 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1131 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1132
1133 return 0;
1134}
1135
1136void
1137widget_destroy_tooltip(struct widget *parent)
1138{
1139 struct tooltip *tooltip = parent->tooltip;
1140
1141 parent->tooltip_count = 0;
1142 if (!tooltip)
1143 return;
1144
1145 if (tooltip->widget) {
1146 widget_destroy(tooltip->widget);
1147 window_destroy(tooltip->window);
1148 tooltip->widget = NULL;
1149 tooltip->window = NULL;
1150 }
1151
1152 close(tooltip->tooltip_fd);
1153 free(tooltip->entry);
1154 free(tooltip);
1155 parent->tooltip = NULL;
1156}
1157
1158static void
1159tooltip_func(struct task *task, uint32_t events)
1160{
1161 struct tooltip *tooltip =
1162 container_of(task, struct tooltip, tooltip_task);
1163 uint64_t exp;
1164
Martin Olsson8df662a2012-07-08 03:03:47 +02001165 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1166 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001167 window_create_tooltip(tooltip);
1168}
1169
1170#define TOOLTIP_TIMEOUT 500
1171static int
1172tooltip_timer_reset(struct tooltip *tooltip)
1173{
1174 struct itimerspec its;
1175
1176 its.it_interval.tv_sec = 0;
1177 its.it_interval.tv_nsec = 0;
1178 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1179 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1180 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1181 fprintf(stderr, "could not set timerfd\n: %m");
1182 return -1;
1183 }
1184
1185 return 0;
1186}
1187
1188int
1189widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1190{
1191 struct tooltip *tooltip = parent->tooltip;
1192
1193 parent->tooltip_count++;
1194 if (tooltip) {
1195 tooltip->x = x;
1196 tooltip->y = y;
1197 tooltip_timer_reset(tooltip);
1198 return 0;
1199 }
1200
1201 /* the handler might be triggered too fast via input device motion, so
1202 * we need this check here to make sure tooltip is fully initialized */
1203 if (parent->tooltip_count > 1)
1204 return 0;
1205
1206 tooltip = malloc(sizeof *tooltip);
1207 if (!tooltip)
1208 return -1;
1209
1210 parent->tooltip = tooltip;
1211 tooltip->parent = parent;
1212 tooltip->widget = NULL;
1213 tooltip->window = NULL;
1214 tooltip->x = x;
1215 tooltip->y = y;
1216 tooltip->entry = strdup(entry);
1217 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1218 if (tooltip->tooltip_fd < 0) {
1219 fprintf(stderr, "could not create timerfd\n: %m");
1220 return -1;
1221 }
1222
1223 tooltip->tooltip_task.run = tooltip_func;
1224 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1225 EPOLLIN, &tooltip->tooltip_task);
1226 tooltip_timer_reset(tooltip);
1227
1228 return 0;
1229}
1230
1231static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001232frame_resize_handler(struct widget *widget,
1233 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001234{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001235 struct frame *frame = data;
1236 struct widget *child = frame->child;
1237 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001238 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001239 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001240 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001241 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001243 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001244
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001245 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001246 decoration_width = (t->width + t->margin) * 2;
1247 decoration_height = t->width +
1248 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001250 allocation.x = t->width + t->margin;
1251 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001252 allocation.width = width - decoration_width;
1253 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001254
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001255 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001256
1257 wl_list_for_each(button, &frame->buttons_list, link)
1258 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001259 } else {
1260 decoration_width = 0;
1261 decoration_height = 0;
1262
1263 allocation.x = 0;
1264 allocation.y = 0;
1265 allocation.width = width;
1266 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001267 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001268
1269 wl_list_for_each(button, &frame->buttons_list, link)
1270 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001271 }
1272
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001273 widget_set_allocation(child, allocation.x, allocation.y,
1274 allocation.width, allocation.height);
1275
1276 if (child->resize_handler)
1277 child->resize_handler(child,
1278 allocation.width,
1279 allocation.height,
1280 child->user_data);
1281
Scott Moreauf7e498c2012-05-14 11:39:29 -06001282 width = child->allocation.width + decoration_width;
1283 height = child->allocation.height + decoration_height;
1284
Kristian Høgsberg023be102012-07-31 11:59:12 -04001285 if (widget->window->type != TYPE_FULLSCREEN) {
1286 widget->window->input_region =
1287 wl_compositor_create_region(display->compositor);
1288 wl_region_add(widget->window->input_region,
1289 t->margin, t->margin,
1290 width - 2 * t->margin,
1291 height - 2 * t->margin);
1292 }
1293
Scott Moreauf7e498c2012-05-14 11:39:29 -06001294 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001295
1296 if (child->opaque) {
1297 widget->window->opaque_region =
1298 wl_compositor_create_region(display->compositor);
1299 wl_region_add(widget->window->opaque_region,
1300 opaque_margin, opaque_margin,
1301 widget->allocation.width - 2 * opaque_margin,
1302 widget->allocation.height - 2 * opaque_margin);
1303 }
Martin Minarik1998b152012-05-10 02:04:35 +02001304
1305 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001306 x_r = frame->widget->allocation.width - t->width - t->margin;
1307 x_l = t->width + t->margin;
1308 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001309 wl_list_for_each(button, &frame->buttons_list, link) {
1310 const int button_padding = 4;
1311 w = cairo_image_surface_get_width(button->icon);
1312 h = cairo_image_surface_get_height(button->icon);
1313
1314 if (button->decoration == FRAME_BUTTON_FANCY)
1315 w += 10;
1316
1317 if (button->align == FRAME_BUTTON_LEFT) {
1318 widget_set_allocation(button->widget,
1319 x_l, y , w + 1, h + 1);
1320 x_l += w;
1321 x_l += button_padding;
1322 } else {
1323 x_r -= w;
1324 widget_set_allocation(button->widget,
1325 x_r, y , w + 1, h + 1);
1326 x_r -= button_padding;
1327 }
1328 }
1329}
1330
1331static int
1332frame_button_enter_handler(struct widget *widget,
1333 struct input *input, float x, float y, void *data)
1334{
1335 struct frame_button *frame_button = data;
1336
1337 widget_schedule_redraw(frame_button->widget);
1338 frame_button->state = FRAME_BUTTON_OVER;
1339
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001340 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001341}
1342
1343static void
1344frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1345{
1346 struct frame_button *frame_button = data;
1347
1348 widget_schedule_redraw(frame_button->widget);
1349 frame_button->state = FRAME_BUTTON_DEFAULT;
1350}
1351
1352static void
1353frame_button_button_handler(struct widget *widget,
1354 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001355 uint32_t button,
1356 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001357{
1358 struct frame_button *frame_button = data;
1359 struct window *window = widget->window;
1360
1361 if (button != BTN_LEFT)
1362 return;
1363
1364 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001365 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001366 frame_button->state = FRAME_BUTTON_ACTIVE;
1367 widget_schedule_redraw(frame_button->widget);
1368
1369 if (frame_button->type == FRAME_BUTTON_ICON)
1370 window_show_frame_menu(window, input, time);
1371 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001372 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001373 frame_button->state = FRAME_BUTTON_DEFAULT;
1374 widget_schedule_redraw(frame_button->widget);
1375 break;
1376 }
1377
1378 switch (frame_button->type) {
1379 case FRAME_BUTTON_CLOSE:
1380 if (window->close_handler)
1381 window->close_handler(window->parent,
1382 window->user_data);
1383 else
1384 display_exit(window->display);
1385 break;
1386 case FRAME_BUTTON_MINIMIZE:
1387 fprintf(stderr,"Minimize stub\n");
1388 break;
1389 case FRAME_BUTTON_MAXIMIZE:
1390 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1391 break;
1392 default:
1393 /* Unknown operation */
1394 break;
1395 }
1396}
1397
1398static void
1399frame_button_redraw_handler(struct widget *widget, void *data)
1400{
1401 struct frame_button *frame_button = data;
1402 cairo_t *cr;
1403 int width, height, x, y;
1404 struct window *window = widget->window;
1405
1406 x = widget->allocation.x;
1407 y = widget->allocation.y;
1408 width = widget->allocation.width;
1409 height = widget->allocation.height;
1410
1411 if (!width)
1412 return;
1413 if (!height)
1414 return;
1415 if (widget->opaque)
1416 return;
1417
1418 cr = cairo_create(window->cairo_surface);
1419
1420 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1421 cairo_set_line_width(cr, 1);
1422
1423 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1424 cairo_rectangle (cr, x, y, 25, 16);
1425
1426 cairo_stroke_preserve(cr);
1427
1428 switch (frame_button->state) {
1429 case FRAME_BUTTON_DEFAULT:
1430 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1431 break;
1432 case FRAME_BUTTON_OVER:
1433 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1434 break;
1435 case FRAME_BUTTON_ACTIVE:
1436 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1437 break;
1438 }
1439
1440 cairo_fill (cr);
1441
1442 x += 4;
1443 }
1444
1445 cairo_set_source_surface(cr, frame_button->icon, x, y);
1446 cairo_paint(cr);
1447
1448 cairo_destroy(cr);
1449}
1450
1451static struct widget *
1452frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1453 enum frame_button_align align, enum frame_button_decoration style)
1454{
1455 struct frame_button *frame_button;
1456 const char *icon = data;
1457
1458 frame_button = malloc (sizeof *frame_button);
1459 memset(frame_button, 0, sizeof *frame_button);
1460
1461 frame_button->icon = cairo_image_surface_create_from_png(icon);
1462 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1463 frame_button->frame = frame;
1464 frame_button->type = type;
1465 frame_button->align = align;
1466 frame_button->decoration = style;
1467
1468 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1469
1470 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1471 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1472 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1473 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1474 return frame_button->widget;
1475}
1476
1477static void
1478frame_button_destroy(struct frame_button *frame_button)
1479{
1480 widget_destroy(frame_button->widget);
1481 wl_list_remove(&frame_button->link);
1482 cairo_surface_destroy(frame_button->icon);
1483 free(frame_button);
1484
1485 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001486}
1487
1488static void
1489frame_redraw_handler(struct widget *widget, void *data)
1490{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001491 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001492 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001493 struct theme *t = window->display->theme;
1494 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001496 if (window->type == TYPE_FULLSCREEN)
1497 return;
1498
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499 cr = cairo_create(window->cairo_surface);
1500
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001501 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001502 flags |= THEME_FRAME_ACTIVE;
1503 theme_render_frame(t, cr, widget->allocation.width,
1504 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001505
1506 cairo_destroy(cr);
1507}
1508
1509static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001510frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001511{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001512 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001513 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001514
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001515 location = theme_get_location(t, input->sx, input->sy,
1516 frame->widget->allocation.width,
1517 frame->widget->allocation.height);
1518
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001519 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001520 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001521 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001522 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001523 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001525 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001527 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001531 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001532 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001533 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001534 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001535 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001536 case THEME_LOCATION_EXTERIOR:
1537 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001538 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001539 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001540 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001541}
1542
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001543static void
1544frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001545{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001546 switch (index) {
1547 case 0: /* close */
1548 if (window->close_handler)
1549 window->close_handler(window->parent,
1550 window->user_data);
1551 else
1552 display_exit(window->display);
1553 break;
1554 case 1: /* fullscreen */
1555 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001556 if (window->fullscreen_handler)
1557 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001558 break;
1559 case 2: /* rotate */
1560 case 3: /* scale */
1561 break;
1562 }
1563}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001564
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001565void
1566window_show_frame_menu(struct window *window,
1567 struct input *input, uint32_t time)
1568{
1569 int32_t x, y;
1570
1571 static const char *entries[] = {
1572 "Close", "Fullscreen", "Rotate", "Scale"
1573 };
1574
1575 input_get_position(input, &x, &y);
1576 window_show_menu(window->display, input, time, window,
1577 x - 10, y - 10, frame_menu_func, entries, 4);
1578}
1579
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001580static int
1581frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001582 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001583{
1584 return frame_get_pointer_image_for_location(data, input);
1585}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001586
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001587static int
1588frame_motion_handler(struct widget *widget,
1589 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001590 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001591{
1592 return frame_get_pointer_image_for_location(data, input);
1593}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001594
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001595static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001596frame_button_handler(struct widget *widget,
1597 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001598 uint32_t button, enum wl_pointer_button_state state,
1599 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001600
1601{
1602 struct frame *frame = data;
1603 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001604 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001605 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001606
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001607 location = theme_get_location(display->theme, input->sx, input->sy,
1608 frame->widget->allocation.width,
1609 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610
Daniel Stone4dbadb12012-05-30 16:31:51 +01001611 if (window->display->shell && button == BTN_LEFT &&
1612 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001613 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001614 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001615 if (!window->shell_surface)
1616 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001617 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001618 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001619 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001620 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001621 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001622 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001623 case THEME_LOCATION_RESIZING_TOP:
1624 case THEME_LOCATION_RESIZING_BOTTOM:
1625 case THEME_LOCATION_RESIZING_LEFT:
1626 case THEME_LOCATION_RESIZING_RIGHT:
1627 case THEME_LOCATION_RESIZING_TOP_LEFT:
1628 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1629 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1630 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001631 if (!window->shell_surface)
1632 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001633 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001634
1635 if (!display->dpy) {
1636 /* If we're using shm, allocate a big
1637 pool to create buffers out of while
1638 we resize. We should probably base
1639 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001640 window->pool =
1641 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001642 }
1643
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001644 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001645 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001646 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001647 break;
1648 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001649 } else if (button == BTN_RIGHT &&
1650 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001651 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001652 }
1653}
1654
1655struct widget *
1656frame_create(struct window *window, void *data)
1657{
1658 struct frame *frame;
1659
1660 frame = malloc(sizeof *frame);
1661 memset(frame, 0, sizeof *frame);
1662
1663 frame->widget = window_add_widget(window, frame);
1664 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001665
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001666 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1667 widget_set_resize_handler(frame->widget, frame_resize_handler);
1668 widget_set_enter_handler(frame->widget, frame_enter_handler);
1669 widget_set_motion_handler(frame->widget, frame_motion_handler);
1670 widget_set_button_handler(frame->widget, frame_button_handler);
1671
Martin Minarik1998b152012-05-10 02:04:35 +02001672 /* Create empty list for frame buttons */
1673 wl_list_init(&frame->buttons_list);
1674
1675 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1676 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1677
1678 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1679 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1680
1681 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1682 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1683
1684 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1685 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1686
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001687 window->frame = frame;
1688
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001689 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001690}
1691
Kristian Høgsberga1627922012-06-20 17:30:03 -04001692void
1693frame_set_child_size(struct widget *widget, int child_width, int child_height)
1694{
1695 struct display *display = widget->window->display;
1696 struct theme *t = display->theme;
1697 int decoration_width, decoration_height;
1698 int width, height;
1699
1700 if (widget->window->type != TYPE_FULLSCREEN) {
1701 decoration_width = (t->width + t->margin) * 2;
1702 decoration_height = t->width +
1703 t->titlebar_height + t->margin * 2;
1704
1705 width = child_width + decoration_width;
1706 height = child_height + decoration_height;
1707 } else {
1708 width = child_width;
1709 height = child_height;
1710 }
1711
1712 window_schedule_resize(widget->window, width, height);
1713}
1714
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001715static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001716frame_destroy(struct frame *frame)
1717{
Martin Minarik1998b152012-05-10 02:04:35 +02001718 struct frame_button *button, *tmp;
1719
1720 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1721 frame_button_destroy(button);
1722
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001723 /* frame->child must be destroyed by the application */
1724 widget_destroy(frame->widget);
1725 free(frame);
1726}
1727
1728static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001729input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001730 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001731{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001732 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001733 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001734
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001735 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001736 return;
1737
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001738 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001739 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001740 widget = old;
1741 if (input->grab)
1742 widget = input->grab;
1743 if (widget->leave_handler)
1744 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001745 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001746 }
1747
1748 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001749 widget = focus;
1750 if (input->grab)
1751 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001752 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001753 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001754 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001755 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001756
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001757 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001758 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001759}
1760
1761static void
Daniel Stone37816df2012-05-16 18:45:18 +01001762pointer_handle_motion(void *data, struct wl_pointer *pointer,
1763 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001764{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001765 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001766 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001767 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001768 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001769 float sx = wl_fixed_to_double(sx_w);
1770 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001771
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001772 input->sx = sx;
1773 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001774
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001775 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001776 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001777 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001778 }
1779
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001780 if (input->grab)
1781 widget = input->grab;
1782 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001783 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001784 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001785 cursor = widget->motion_handler(input->focus_widget,
1786 input, time, sx, sy,
1787 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001788
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001789 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001790}
1791
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001792void
1793input_grab(struct input *input, struct widget *widget, uint32_t button)
1794{
1795 input->grab = widget;
1796 input->grab_button = button;
1797}
1798
1799void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001800input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001801{
1802 struct widget *widget;
1803
1804 input->grab = NULL;
1805 if (input->pointer_focus) {
1806 widget = widget_find_widget(input->pointer_focus->widget,
1807 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001808 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001809 }
1810}
1811
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001812static void
Daniel Stone37816df2012-05-16 18:45:18 +01001813pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001814 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001815{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001816 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001817 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001818 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001819
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001820 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001821 if (input->focus_widget && input->grab == NULL &&
1822 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001823 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001824
Neil Roberts6b28aad2012-01-23 19:11:18 +00001825 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001826 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001827 (*widget->button_handler)(widget,
1828 input, time,
1829 button, state,
1830 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001831
Daniel Stone4dbadb12012-05-30 16:31:51 +01001832 if (input->grab && input->grab_button == button &&
1833 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001834 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001835}
1836
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001837static void
Daniel Stone37816df2012-05-16 18:45:18 +01001838pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001839 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001840{
1841}
1842
1843static void
Daniel Stone37816df2012-05-16 18:45:18 +01001844keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001845 uint32_t serial, uint32_t time, uint32_t key,
1846 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001847{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001848 struct input *input = data;
1849 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001850 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001851 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001852 const xkb_keysym_t *syms;
1853 xkb_keysym_t sym;
1854 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001855 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001856
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001857 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001858 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001859 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001860 return;
1861
Daniel Stone97f68542012-05-30 16:32:01 +01001862 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001863
Daniel Stone97f68542012-05-30 16:32:01 +01001864 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001865 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001866 XKB_STATE_LATCHED);
1867 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001868 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001869 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001870 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001871 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001872 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001873 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001874
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001875 sym = XKB_KEY_NoSymbol;
1876 if (num_syms == 1)
1877 sym = syms[0];
1878
1879 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001880 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001881 window_set_maximized(window,
1882 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001883 } else if (sym == XKB_KEY_F11 &&
1884 window->fullscreen_handler &&
1885 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1886 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04001887 } else if (sym == XKB_KEY_F4 &&
1888 input->modifiers == MOD_ALT_MASK &&
1889 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1890 if (window->close_handler)
1891 window->close_handler(window->parent,
1892 window->user_data);
1893 else
1894 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001895 } else if (window->key_handler) {
1896 (*window->key_handler)(window, input, time, key,
1897 sym, state, window->user_data);
1898 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001899
1900 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
1901 key == input->repeat_key) {
1902 its.it_interval.tv_sec = 0;
1903 its.it_interval.tv_nsec = 0;
1904 its.it_value.tv_sec = 0;
1905 its.it_value.tv_nsec = 0;
1906 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1907 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1908 input->repeat_sym = sym;
1909 input->repeat_key = key;
1910 input->repeat_time = time;
1911 its.it_interval.tv_sec = 0;
1912 its.it_interval.tv_nsec = 25 * 1000 * 1000;
1913 its.it_value.tv_sec = 0;
1914 its.it_value.tv_nsec = 400 * 1000 * 1000;
1915 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1916 }
1917}
1918
1919static void
1920keyboard_repeat_func(struct task *task, uint32_t events)
1921{
1922 struct input *input =
1923 container_of(task, struct input, repeat_task);
1924 struct window *window = input->keyboard_focus;
1925 uint64_t exp;
1926
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04001927 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1928 /* If we change the timer between the fd becoming
1929 * readable and getting here, there'll be nothing to
1930 * read and we get EAGAIN. */
1931 return;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001932
Kristian Høgsbergbd0cd762012-06-20 15:17:18 -04001933 if (window && window->key_handler) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001934 (*window->key_handler)(window, input, input->repeat_time,
1935 input->repeat_key, input->repeat_sym,
1936 WL_KEYBOARD_KEY_STATE_PRESSED,
1937 window->user_data);
1938 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001939}
1940
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001941static void
Daniel Stone351eb612012-05-31 15:27:47 -04001942keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1943 uint32_t serial, uint32_t mods_depressed,
1944 uint32_t mods_latched, uint32_t mods_locked,
1945 uint32_t group)
1946{
1947 struct input *input = data;
1948
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001949 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1950 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001951}
1952
1953static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001954input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001955{
1956 struct window *window = input->pointer_focus;
1957
1958 if (!window)
1959 return;
1960
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001961 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001962
Pekka Paalanene1207c72011-12-16 12:02:09 +02001963 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001964 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001965}
1966
1967static void
Daniel Stone37816df2012-05-16 18:45:18 +01001968pointer_handle_enter(void *data, struct wl_pointer *pointer,
1969 uint32_t serial, struct wl_surface *surface,
1970 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001971{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001972 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001973 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001974 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001975 float sx = wl_fixed_to_double(sx_w);
1976 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001977
Martin Minarik1998b152012-05-10 02:04:35 +02001978 if (!surface) {
1979 /* enter event for a window we've just destroyed */
1980 return;
1981 }
1982
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001983 input->display->serial = serial;
1984 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001985 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001986 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001987
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001988 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001989 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001990 window->pool = NULL;
1991 /* Schedule a redraw to free the pool */
1992 window_schedule_redraw(window);
1993 }
1994
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001995 input->sx = sx;
1996 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001997
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001998 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001999 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002000}
Kristian Høgsberg59826582011-01-20 11:56:57 -05002001
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002002static void
Daniel Stone37816df2012-05-16 18:45:18 +01002003pointer_handle_leave(void *data, struct wl_pointer *pointer,
2004 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002005{
2006 struct input *input = data;
2007
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002008 input->display->serial = serial;
2009 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002010}
2011
2012static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002013input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02002014{
2015 struct window *window = input->keyboard_focus;
Kristian Høgsbergd3c69c22012-06-20 22:41:59 -04002016 struct itimerspec its;
2017
2018 its.it_interval.tv_sec = 0;
2019 its.it_interval.tv_nsec = 0;
2020 its.it_value.tv_sec = 0;
2021 its.it_value.tv_nsec = 0;
2022 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002023
2024 if (!window)
2025 return;
2026
2027 window->keyboard_device = NULL;
2028 if (window->keyboard_focus_handler)
2029 (*window->keyboard_focus_handler)(window, NULL,
2030 window->user_data);
2031
2032 input->keyboard_focus = NULL;
2033}
2034
2035static void
Daniel Stone37816df2012-05-16 18:45:18 +01002036keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2037 uint32_t serial, struct wl_surface *surface,
2038 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002039{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002040 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02002041 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002042
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002043 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002044 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002045
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002046 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002047 window->keyboard_device = input;
2048 if (window->keyboard_focus_handler)
2049 (*window->keyboard_focus_handler)(window,
2050 window->keyboard_device,
2051 window->user_data);
2052}
2053
2054static void
Daniel Stone37816df2012-05-16 18:45:18 +01002055keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2056 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002057{
2058 struct input *input = data;
2059
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002060 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002061 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002062}
2063
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002064static void
2065keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2066 uint32_t format, int fd, uint32_t size)
2067{
2068 struct input *input = data;
2069 char *map_str;
2070
2071 if (!data) {
2072 close(fd);
2073 return;
2074 }
2075
2076 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2077 close(fd);
2078 return;
2079 }
2080
2081 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2082 if (map_str == MAP_FAILED) {
2083 close(fd);
2084 return;
2085 }
2086
2087 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2088 map_str,
2089 XKB_KEYMAP_FORMAT_TEXT_V1,
2090 0);
2091 munmap(map_str, size);
2092 close(fd);
2093
2094 if (!input->xkb.keymap) {
2095 fprintf(stderr, "failed to compile keymap\n");
2096 return;
2097 }
2098
2099 input->xkb.state = xkb_state_new(input->xkb.keymap);
2100 if (!input->xkb.state) {
2101 fprintf(stderr, "failed to create XKB state\n");
2102 xkb_map_unref(input->xkb.keymap);
2103 input->xkb.keymap = NULL;
2104 return;
2105 }
2106
2107 input->xkb.control_mask =
2108 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2109 input->xkb.alt_mask =
2110 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2111 input->xkb.shift_mask =
2112 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2113}
2114
Daniel Stone37816df2012-05-16 18:45:18 +01002115static const struct wl_pointer_listener pointer_listener = {
2116 pointer_handle_enter,
2117 pointer_handle_leave,
2118 pointer_handle_motion,
2119 pointer_handle_button,
2120 pointer_handle_axis,
2121};
2122
2123static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002124 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002125 keyboard_handle_enter,
2126 keyboard_handle_leave,
2127 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002128 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002129};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002130
2131static void
Daniel Stone37816df2012-05-16 18:45:18 +01002132seat_handle_capabilities(void *data, struct wl_seat *seat,
2133 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002134{
Daniel Stone37816df2012-05-16 18:45:18 +01002135 struct input *input = data;
2136
2137 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2138 input->pointer = wl_seat_get_pointer(seat);
2139 wl_pointer_set_user_data(input->pointer, input);
2140 wl_pointer_add_listener(input->pointer, &pointer_listener,
2141 input);
2142 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2143 wl_pointer_destroy(input->pointer);
2144 input->pointer = NULL;
2145 }
2146
2147 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2148 input->keyboard = wl_seat_get_keyboard(seat);
2149 wl_keyboard_set_user_data(input->keyboard, input);
2150 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2151 input);
2152 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2153 wl_keyboard_destroy(input->keyboard);
2154 input->keyboard = NULL;
2155 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002156}
2157
Daniel Stone37816df2012-05-16 18:45:18 +01002158static const struct wl_seat_listener seat_listener = {
2159 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002160};
2161
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002162void
2163input_get_position(struct input *input, int32_t *x, int32_t *y)
2164{
2165 *x = input->sx;
2166 *y = input->sy;
2167}
2168
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002169struct display *
2170input_get_display(struct input *input)
2171{
2172 return input->display;
2173}
2174
Daniel Stone37816df2012-05-16 18:45:18 +01002175struct wl_seat *
2176input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002177{
Daniel Stone37816df2012-05-16 18:45:18 +01002178 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002179}
2180
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002181uint32_t
2182input_get_modifiers(struct input *input)
2183{
2184 return input->modifiers;
2185}
2186
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002187struct widget *
2188input_get_focus_widget(struct input *input)
2189{
2190 return input->focus_widget;
2191}
2192
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002193struct data_offer {
2194 struct wl_data_offer *offer;
2195 struct input *input;
2196 struct wl_array types;
2197 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002198
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002199 struct task io_task;
2200 int fd;
2201 data_func_t func;
2202 int32_t x, y;
2203 void *user_data;
2204};
2205
2206static void
2207data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2208{
2209 struct data_offer *offer = data;
2210 char **p;
2211
2212 p = wl_array_add(&offer->types, sizeof *p);
2213 *p = strdup(type);
2214}
2215
2216static const struct wl_data_offer_listener data_offer_listener = {
2217 data_offer_offer,
2218};
2219
2220static void
2221data_offer_destroy(struct data_offer *offer)
2222{
2223 char **p;
2224
2225 offer->refcount--;
2226 if (offer->refcount == 0) {
2227 wl_data_offer_destroy(offer->offer);
2228 for (p = offer->types.data; *p; p++)
2229 free(*p);
2230 wl_array_release(&offer->types);
2231 free(offer);
2232 }
2233}
2234
2235static void
2236data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002237 struct wl_data_device *data_device,
2238 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002239{
2240 struct data_offer *offer;
2241
2242 offer = malloc(sizeof *offer);
2243
2244 wl_array_init(&offer->types);
2245 offer->refcount = 1;
2246 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002247 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002248 wl_data_offer_add_listener(offer->offer,
2249 &data_offer_listener, offer);
2250}
2251
2252static void
2253data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002254 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002255 wl_fixed_t x_w, wl_fixed_t y_w,
2256 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002257{
2258 struct input *input = data;
2259 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002260 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002261 float x = wl_fixed_to_double(x_w);
2262 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002263 char **p;
2264
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002265 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002266 window = wl_surface_get_user_data(surface);
2267 input->pointer_focus = window;
2268
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002269 if (offer) {
2270 input->drag_offer = wl_data_offer_get_user_data(offer);
2271
2272 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2273 *p = NULL;
2274
2275 types_data = input->drag_offer->types.data;
2276 } else {
2277 input->drag_offer = NULL;
2278 types_data = NULL;
2279 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002280
2281 window = input->pointer_focus;
2282 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002283 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002284 window->user_data);
2285}
2286
2287static void
2288data_device_leave(void *data, struct wl_data_device *data_device)
2289{
2290 struct input *input = data;
2291
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002292 if (input->drag_offer) {
2293 data_offer_destroy(input->drag_offer);
2294 input->drag_offer = NULL;
2295 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002296}
2297
2298static void
2299data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002300 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002301{
2302 struct input *input = data;
2303 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002304 float x = wl_fixed_to_double(x_w);
2305 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002306 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002307
2308 input->sx = x;
2309 input->sy = y;
2310
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002311 if (input->drag_offer)
2312 types_data = input->drag_offer->types.data;
2313 else
2314 types_data = NULL;
2315
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002316 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002317 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002318 window->user_data);
2319}
2320
2321static void
2322data_device_drop(void *data, struct wl_data_device *data_device)
2323{
2324 struct input *input = data;
2325 struct window *window = input->pointer_focus;
2326
2327 if (window->drop_handler)
2328 window->drop_handler(window, input,
2329 input->sx, input->sy, window->user_data);
2330}
2331
2332static void
2333data_device_selection(void *data,
2334 struct wl_data_device *wl_data_device,
2335 struct wl_data_offer *offer)
2336{
2337 struct input *input = data;
2338 char **p;
2339
2340 if (input->selection_offer)
2341 data_offer_destroy(input->selection_offer);
2342
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002343 if (offer) {
2344 input->selection_offer = wl_data_offer_get_user_data(offer);
2345 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2346 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002347 } else {
2348 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002349 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002350}
2351
2352static const struct wl_data_device_listener data_device_listener = {
2353 data_device_data_offer,
2354 data_device_enter,
2355 data_device_leave,
2356 data_device_motion,
2357 data_device_drop,
2358 data_device_selection
2359};
2360
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002361static void
2362input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002363{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002364 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002365 struct wl_cursor *cursor;
2366 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002367
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002368 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002369 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002370 return;
2371
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002372 if (index >= (int) cursor->image_count) {
2373 fprintf(stderr, "cursor index out of range\n");
2374 return;
2375 }
2376
2377 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002378 buffer = wl_cursor_image_get_buffer(image);
2379 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002380 return;
2381
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002382 wl_pointer_set_cursor(input->pointer, input->display->serial,
2383 input->pointer_surface,
2384 image->hotspot_x, image->hotspot_y);
2385 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2386 wl_surface_damage(input->pointer_surface, 0, 0,
2387 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002388}
2389
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002390static const struct wl_callback_listener pointer_surface_listener;
2391
2392static void
2393pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2394 uint32_t time)
2395{
2396 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002397 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002398 int i;
2399
2400 if (callback) {
2401 assert(callback == input->cursor_frame_cb);
2402 wl_callback_destroy(callback);
2403 input->cursor_frame_cb = NULL;
2404 }
2405
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002406 if (input->current_cursor == CURSOR_BLANK) {
2407 wl_pointer_set_cursor(input->pointer, input->display->serial,
2408 NULL, 0, 0);
2409 return;
2410 }
2411
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002412 if (input->current_cursor == CURSOR_UNSET)
2413 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002414 cursor = input->display->cursors[input->current_cursor];
2415 if (!cursor)
2416 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002417
2418 /* FIXME We don't have the current time on the first call so we set
2419 * the animation start to the time of the first frame callback. */
2420 if (time == 0)
2421 input->cursor_anim_start = 0;
2422 else if (input->cursor_anim_start == 0)
2423 input->cursor_anim_start = time;
2424
2425 if (time == 0 || input->cursor_anim_start == 0)
2426 i = 0;
2427 else
2428 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2429
2430 input_set_pointer_image_index(input, i);
2431
2432 if (cursor->image_count == 1)
2433 return;
2434
2435 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2436 wl_callback_add_listener(input->cursor_frame_cb,
2437 &pointer_surface_listener, input);
2438}
2439
2440static const struct wl_callback_listener pointer_surface_listener = {
2441 pointer_surface_frame_callback
2442};
2443
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002444void
2445input_set_pointer_image(struct input *input, int pointer)
2446{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002447 int force = 0;
2448
2449 if (input->pointer_enter_serial > input->cursor_serial)
2450 force = 1;
2451
2452 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002453 return;
2454
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002455 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002456 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002457 if (!input->cursor_frame_cb)
2458 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002459 else if (force) {
2460 /* The current frame callback may be stuck if, for instance,
2461 * the set cursor request was processed by the server after
2462 * this client lost the focus. In this case the cursor surface
2463 * might not be mapped and the frame callback wouldn't ever
2464 * complete. Send a set_cursor and attach to try to map the
2465 * cursor surface again so that the callback will finish */
2466 input_set_pointer_image_index(input, 0);
2467 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002468}
2469
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002470struct wl_data_device *
2471input_get_data_device(struct input *input)
2472{
2473 return input->data_device;
2474}
2475
2476void
2477input_set_selection(struct input *input,
2478 struct wl_data_source *source, uint32_t time)
2479{
2480 wl_data_device_set_selection(input->data_device, source, time);
2481}
2482
2483void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002484input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002485{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002486 wl_data_offer_accept(input->drag_offer->offer,
2487 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002488}
2489
2490static void
2491offer_io_func(struct task *task, uint32_t events)
2492{
2493 struct data_offer *offer =
2494 container_of(task, struct data_offer, io_task);
2495 unsigned int len;
2496 char buffer[4096];
2497
2498 len = read(offer->fd, buffer, sizeof buffer);
2499 offer->func(buffer, len,
2500 offer->x, offer->y, offer->user_data);
2501
2502 if (len == 0) {
2503 close(offer->fd);
2504 data_offer_destroy(offer);
2505 }
2506}
2507
2508static void
2509data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2510 data_func_t func, void *user_data)
2511{
2512 int p[2];
2513
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002514 if (pipe2(p, O_CLOEXEC) == -1)
2515 return;
2516
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002517 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2518 close(p[1]);
2519
2520 offer->io_task.run = offer_io_func;
2521 offer->fd = p[0];
2522 offer->func = func;
2523 offer->refcount++;
2524 offer->user_data = user_data;
2525
2526 display_watch_fd(offer->input->display,
2527 offer->fd, EPOLLIN, &offer->io_task);
2528}
2529
2530void
2531input_receive_drag_data(struct input *input, const char *mime_type,
2532 data_func_t func, void *data)
2533{
2534 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2535 input->drag_offer->x = input->sx;
2536 input->drag_offer->y = input->sy;
2537}
2538
2539int
2540input_receive_selection_data(struct input *input, const char *mime_type,
2541 data_func_t func, void *data)
2542{
2543 char **p;
2544
2545 if (input->selection_offer == NULL)
2546 return -1;
2547
2548 for (p = input->selection_offer->types.data; *p; p++)
2549 if (strcmp(mime_type, *p) == 0)
2550 break;
2551
2552 if (*p == NULL)
2553 return -1;
2554
2555 data_offer_receive_data(input->selection_offer,
2556 mime_type, func, data);
2557 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002558}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002559
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002560int
2561input_receive_selection_data_to_fd(struct input *input,
2562 const char *mime_type, int fd)
2563{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002564 if (input->selection_offer)
2565 wl_data_offer_receive(input->selection_offer->offer,
2566 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002567
2568 return 0;
2569}
2570
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002571void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002572window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002573{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002574 if (!window->shell_surface)
2575 return;
2576
Daniel Stone37816df2012-05-16 18:45:18 +01002577 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002578}
2579
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002580static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002581idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002582{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002583 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002584
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002585 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002586 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002587 widget_set_allocation(widget,
2588 window->pending_allocation.x,
2589 window->pending_allocation.y,
2590 window->pending_allocation.width,
2591 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002592
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002593 if (window->input_region) {
2594 wl_region_destroy(window->input_region);
2595 window->input_region = NULL;
2596 }
2597
2598 if (window->opaque_region) {
2599 wl_region_destroy(window->opaque_region);
2600 window->opaque_region = NULL;
2601 }
2602
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002603 if (widget->resize_handler)
2604 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002605 widget->allocation.width,
2606 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002607 widget->user_data);
2608
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002609 if (window->allocation.width != widget->allocation.width ||
2610 window->allocation.height != widget->allocation.height) {
2611 window->allocation = widget->allocation;
2612 window_schedule_redraw(window);
2613 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002614}
2615
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002616void
2617window_schedule_resize(struct window *window, int width, int height)
2618{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002619 window->pending_allocation.x = 0;
2620 window->pending_allocation.y = 0;
2621 window->pending_allocation.width = width;
2622 window->pending_allocation.height = height;
2623
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002624 if (window->min_allocation.width == 0)
2625 window->min_allocation = window->pending_allocation;
2626 if (window->pending_allocation.width < window->min_allocation.width)
2627 window->pending_allocation.width = window->min_allocation.width;
2628 if (window->pending_allocation.height < window->min_allocation.height)
2629 window->pending_allocation.height = window->min_allocation.height;
2630
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002631 window->resize_needed = 1;
2632 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002633}
2634
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002635void
2636widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2637{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002638 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002639}
2640
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002641static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002642handle_ping(void *data, struct wl_shell_surface *shell_surface,
2643 uint32_t serial)
2644{
2645 wl_shell_surface_pong(shell_surface, serial);
2646}
2647
2648static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002649handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002650 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002651{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002652 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002653
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002654 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002655 return;
2656
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002657 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002658 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002659}
2660
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002661static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002662menu_destroy(struct menu *menu)
2663{
2664 widget_destroy(menu->widget);
2665 window_destroy(menu->window);
2666 free(menu);
2667}
2668
2669static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002670handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2671{
2672 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002673 struct menu *menu = window->widget->user_data;
2674
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002675 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002676 * device. Or just use wl_callback. And this really needs to
2677 * be a window vfunc that the menu can set. And we need the
2678 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002679
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002680 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002681 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002682 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002683}
2684
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002685static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002686 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002687 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002688 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002689};
2690
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002691void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002692window_get_allocation(struct window *window,
2693 struct rectangle *allocation)
2694{
2695 *allocation = window->allocation;
2696}
2697
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002698static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002699widget_redraw(struct widget *widget)
2700{
2701 struct widget *child;
2702
2703 if (widget->redraw_handler)
2704 widget->redraw_handler(widget, widget->user_data);
2705 wl_list_for_each(child, &widget->child_list, link)
2706 widget_redraw(child);
2707}
2708
2709static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002710frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2711{
2712 struct window *window = data;
2713
2714 wl_callback_destroy(callback);
2715 window->redraw_scheduled = 0;
2716 if (window->redraw_needed)
2717 window_schedule_redraw(window);
2718}
2719
2720static const struct wl_callback_listener listener = {
2721 frame_callback
2722};
2723
2724static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002725idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002726{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002727 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002728 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002729
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002730 if (window->resize_needed)
2731 idle_resize(window);
2732
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002733 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002734 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002735 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002736 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002737 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002738
2739 callback = wl_surface_frame(window->surface);
2740 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002741}
2742
2743void
2744window_schedule_redraw(struct window *window)
2745{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002746 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002747 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002748 window->redraw_task.run = idle_redraw;
2749 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002750 window->redraw_scheduled = 1;
2751 }
2752}
2753
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002754void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002755window_set_fullscreen(struct window *window, int fullscreen)
2756{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002757 if (!window->display->shell)
2758 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002759
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002760 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002761 return;
2762
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002763 if (fullscreen) {
2764 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002765 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002766 wl_shell_surface_set_fullscreen(window->shell_surface,
2767 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2768 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002769 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002770 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002771 wl_shell_surface_set_toplevel(window->shell_surface);
2772 window_schedule_resize(window,
2773 window->saved_allocation.width,
2774 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002775 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002776}
2777
2778void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002779window_set_maximized(struct window *window, int maximized)
2780{
2781 if (!window->display->shell)
2782 return;
2783
2784 if ((window->type == TYPE_MAXIMIZED) == maximized)
2785 return;
2786
2787 if (window->type == TYPE_TOPLEVEL) {
2788 window->saved_allocation = window->allocation;
2789 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2790 window->type = TYPE_MAXIMIZED;
2791 } else {
2792 wl_shell_surface_set_toplevel(window->shell_surface);
2793 window->type = TYPE_TOPLEVEL;
2794 window_schedule_resize(window,
2795 window->saved_allocation.width,
2796 window->saved_allocation.height);
2797 }
2798}
2799
2800void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002801window_set_user_data(struct window *window, void *data)
2802{
2803 window->user_data = data;
2804}
2805
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002806void *
2807window_get_user_data(struct window *window)
2808{
2809 return window->user_data;
2810}
2811
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002812void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002813window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002814 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002815{
2816 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002817}
2818
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002819void
2820window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002821 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002822{
2823 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002824}
2825
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002826void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002827window_set_data_handler(struct window *window, window_data_handler_t handler)
2828{
2829 window->data_handler = handler;
2830}
2831
2832void
2833window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2834{
2835 window->drop_handler = handler;
2836}
2837
2838void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002839window_set_close_handler(struct window *window,
2840 window_close_handler_t handler)
2841{
2842 window->close_handler = handler;
2843}
2844
2845void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002846window_set_fullscreen_handler(struct window *window,
2847 window_fullscreen_handler_t handler)
2848{
2849 window->fullscreen_handler = handler;
2850}
2851
2852void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002853window_set_title(struct window *window, const char *title)
2854{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002855 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002856 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002857 if (window->shell_surface)
2858 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002859}
2860
2861const char *
2862window_get_title(struct window *window)
2863{
2864 return window->title;
2865}
2866
2867void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002868window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2869{
2870 struct text_cursor_position *text_cursor_position =
2871 window->display->text_cursor_position;
2872
Scott Moreau9295ce02012-06-01 12:46:10 -06002873 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002874 return;
2875
2876 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002877 window->surface,
2878 wl_fixed_from_int(x),
2879 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002880}
2881
2882void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002883window_damage(struct window *window, int32_t x, int32_t y,
2884 int32_t width, int32_t height)
2885{
2886 wl_surface_damage(window->surface, x, y, width, height);
2887}
2888
Casey Dahlin9074db52012-04-19 22:50:09 -04002889static void
2890surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002891 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002892{
Rob Bradford7507b572012-05-15 17:55:34 +01002893 struct window *window = data;
2894 struct output *output;
2895 struct output *output_found = NULL;
2896 struct window_output *window_output;
2897
2898 wl_list_for_each(output, &window->display->output_list, link) {
2899 if (output->output == wl_output) {
2900 output_found = output;
2901 break;
2902 }
2903 }
2904
2905 if (!output_found)
2906 return;
2907
2908 window_output = malloc (sizeof *window_output);
2909 window_output->output = output_found;
2910
2911 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002912}
2913
2914static void
2915surface_leave(void *data,
2916 struct wl_surface *wl_surface, struct wl_output *output)
2917{
Rob Bradford7507b572012-05-15 17:55:34 +01002918 struct window *window = data;
2919 struct window_output *window_output;
2920 struct window_output *window_output_found = NULL;
2921
2922 wl_list_for_each(window_output, &window->window_output_list, link) {
2923 if (window_output->output->output == output) {
2924 window_output_found = window_output;
2925 break;
2926 }
2927 }
2928
2929 if (window_output_found) {
2930 wl_list_remove(&window_output_found->link);
2931 free(window_output_found);
2932 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002933}
2934
2935static const struct wl_surface_listener surface_listener = {
2936 surface_enter,
2937 surface_leave
2938};
2939
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002940static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002941window_create_internal(struct display *display,
2942 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002943{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002944 struct window *window;
2945
2946 window = malloc(sizeof *window);
2947 if (window == NULL)
2948 return NULL;
2949
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002950 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002951 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002952 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002953 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002954 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002955 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002956 window->shell_surface =
2957 wl_shell_get_shell_surface(display->shell,
2958 window->surface);
2959 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002960 window->allocation.x = 0;
2961 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002962 window->allocation.width = 0;
2963 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002964 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002965 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002966 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002967 window->input_region = NULL;
2968 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002969
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002970 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002971#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002972 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002973#else
2974 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2975#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002976 else
2977 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002978
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002979 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002980 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002981 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002982
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002983 if (window->shell_surface) {
2984 wl_shell_surface_set_user_data(window->shell_surface, window);
2985 wl_shell_surface_add_listener(window->shell_surface,
2986 &shell_surface_listener, window);
2987 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002988
Rob Bradford7507b572012-05-15 17:55:34 +01002989 wl_list_init (&window->window_output_list);
2990
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002991 return window;
2992}
2993
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002994struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002995window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002996{
2997 struct window *window;
2998
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002999 window = window_create_internal(display, NULL, TYPE_NONE);
3000 if (!window)
3001 return NULL;
3002
3003 return window;
3004}
3005
3006struct window *
3007window_create_custom(struct display *display)
3008{
3009 struct window *window;
3010
3011 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003012 if (!window)
3013 return NULL;
3014
3015 return window;
3016}
3017
3018struct window *
3019window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003020 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003021{
3022 struct window *window;
3023
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003024 window = window_create_internal(parent->display,
3025 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003026 if (!window)
3027 return NULL;
3028
3029 window->x = x;
3030 window->y = y;
3031
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003032 if (display->shell)
3033 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003034 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003035 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003036
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003037 return window;
3038}
3039
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003040static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003041menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003042{
3043 int next;
3044
3045 next = (sy - 8) / 20;
3046 if (menu->current != next) {
3047 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003048 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003049 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003050}
3051
3052static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003053menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003054 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003055 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003056{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003057 struct menu *menu = data;
3058
3059 if (widget == menu->widget)
3060 menu_set_item(data, y);
3061
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003062 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003063}
3064
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003065static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003066menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003067 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003068{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003069 struct menu *menu = data;
3070
3071 if (widget == menu->widget)
3072 menu_set_item(data, y);
3073
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003074 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003075}
3076
3077static void
3078menu_leave_handler(struct widget *widget, struct input *input, void *data)
3079{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003080 struct menu *menu = data;
3081
3082 if (widget == menu->widget)
3083 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003084}
3085
3086static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003087menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003088 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003089 uint32_t button, enum wl_pointer_button_state state,
3090 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003091
3092{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003093 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003094
Daniel Stone4dbadb12012-05-30 16:31:51 +01003095 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3096 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003097 /* Either relase after press-drag-release or
3098 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003099 menu->func(menu->window->parent,
3100 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003101 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003102 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003103 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003104}
3105
3106static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003107menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003108{
3109 cairo_t *cr;
3110 const int32_t r = 3, margin = 3;
3111 struct menu *menu = data;
3112 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003113 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003114
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003115 cr = cairo_create(window->cairo_surface);
3116 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3117 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3118 cairo_paint(cr);
3119
3120 width = window->allocation.width;
3121 height = window->allocation.height;
3122 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003123
3124 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003125 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3126 cairo_fill(cr);
3127
3128 for (i = 0; i < menu->count; i++) {
3129 if (i == menu->current) {
3130 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3131 cairo_rectangle(cr, margin, i * 20 + margin,
3132 width - 2 * margin, 20);
3133 cairo_fill(cr);
3134 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3135 cairo_move_to(cr, 10, i * 20 + 16);
3136 cairo_show_text(cr, menu->entries[i]);
3137 } else {
3138 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3139 cairo_move_to(cr, 10, i * 20 + 16);
3140 cairo_show_text(cr, menu->entries[i]);
3141 }
3142 }
3143
3144 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003145}
3146
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003147void
3148window_show_menu(struct display *display,
3149 struct input *input, uint32_t time, struct window *parent,
3150 int32_t x, int32_t y,
3151 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003152{
3153 struct window *window;
3154 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003155 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003156
3157 menu = malloc(sizeof *menu);
3158 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003159 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003160
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003161 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003162 if (!window) {
3163 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003164 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003165 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003166
3167 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003168 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003169 menu->entries = entries;
3170 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003171 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003172 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003173 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003174 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003175 window->type = TYPE_MENU;
3176 window->x = x;
3177 window->y = y;
3178
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003179 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003180 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003181 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003182 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003183 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003184
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003185 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003186 widget_set_enter_handler(menu->widget, menu_enter_handler);
3187 widget_set_leave_handler(menu->widget, menu_leave_handler);
3188 widget_set_motion_handler(menu->widget, menu_motion_handler);
3189 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003190
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003191 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003192 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003193}
3194
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003195void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003196window_set_buffer_type(struct window *window, enum window_buffer_type type)
3197{
3198 window->buffer_type = type;
3199}
3200
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003201
3202static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003203display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003204 struct wl_output *wl_output,
3205 int x, int y,
3206 int physical_width,
3207 int physical_height,
3208 int subpixel,
3209 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003210 const char *model,
3211 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003212{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003213 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003214
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003215 output->allocation.x = x;
3216 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003217}
3218
3219static void
3220display_handle_mode(void *data,
3221 struct wl_output *wl_output,
3222 uint32_t flags,
3223 int width,
3224 int height,
3225 int refresh)
3226{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003227 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003228 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003229
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003230 if (flags & WL_OUTPUT_MODE_CURRENT) {
3231 output->allocation.width = width;
3232 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003233 if (display->output_configure_handler)
3234 (*display->output_configure_handler)(
3235 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003236 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003237}
3238
3239static const struct wl_output_listener output_listener = {
3240 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003241 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003242};
3243
3244static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003245display_add_output(struct display *d, uint32_t id)
3246{
3247 struct output *output;
3248
3249 output = malloc(sizeof *output);
3250 if (output == NULL)
3251 return;
3252
3253 memset(output, 0, sizeof *output);
3254 output->display = d;
3255 output->output =
3256 wl_display_bind(d->display, id, &wl_output_interface);
3257 wl_list_insert(d->output_list.prev, &output->link);
3258
3259 wl_output_add_listener(output->output, &output_listener, output);
3260}
3261
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003262static void
3263output_destroy(struct output *output)
3264{
3265 if (output->destroy_handler)
3266 (*output->destroy_handler)(output, output->user_data);
3267
3268 wl_output_destroy(output->output);
3269 wl_list_remove(&output->link);
3270 free(output);
3271}
3272
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003273void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003274display_set_output_configure_handler(struct display *display,
3275 display_output_handler_t handler)
3276{
3277 struct output *output;
3278
3279 display->output_configure_handler = handler;
3280 if (!handler)
3281 return;
3282
3283 wl_list_for_each(output, &display->output_list, link)
3284 (*display->output_configure_handler)(output,
3285 display->user_data);
3286}
3287
3288void
3289output_set_user_data(struct output *output, void *data)
3290{
3291 output->user_data = data;
3292}
3293
3294void *
3295output_get_user_data(struct output *output)
3296{
3297 return output->user_data;
3298}
3299
3300void
3301output_set_destroy_handler(struct output *output,
3302 display_output_handler_t handler)
3303{
3304 output->destroy_handler = handler;
3305 /* FIXME: implement this, once we have way to remove outputs */
3306}
3307
3308void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003309output_get_allocation(struct output *output, struct rectangle *allocation)
3310{
3311 *allocation = output->allocation;
3312}
3313
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003314struct wl_output *
3315output_get_wl_output(struct output *output)
3316{
3317 return output->output;
3318}
3319
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003320static void
Daniel Stone97f68542012-05-30 16:32:01 +01003321fini_xkb(struct input *input)
3322{
3323 xkb_state_unref(input->xkb.state);
3324 xkb_map_unref(input->xkb.keymap);
3325}
3326
3327static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003328display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003329{
3330 struct input *input;
3331
3332 input = malloc(sizeof *input);
3333 if (input == NULL)
3334 return;
3335
3336 memset(input, 0, sizeof *input);
3337 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003338 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003339 input->pointer_focus = NULL;
3340 input->keyboard_focus = NULL;
3341 wl_list_insert(d->input_list.prev, &input->link);
3342
Daniel Stone37816df2012-05-16 18:45:18 +01003343 wl_seat_add_listener(input->seat, &seat_listener, input);
3344 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003345
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003346 input->data_device =
3347 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003348 input->seat);
3349 wl_data_device_add_listener(input->data_device, &data_device_listener,
3350 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003351
3352 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003353
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003354 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3355 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003356 input->repeat_task.run = keyboard_repeat_func;
3357 display_watch_fd(d, input->repeat_timer_fd,
3358 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003359}
3360
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003361static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003362input_destroy(struct input *input)
3363{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003364 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003365 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003366
3367 if (input->drag_offer)
3368 data_offer_destroy(input->drag_offer);
3369
3370 if (input->selection_offer)
3371 data_offer_destroy(input->selection_offer);
3372
3373 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003374 fini_xkb(input);
3375
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003376 wl_surface_destroy(input->pointer_surface);
3377
Pekka Paalanene1207c72011-12-16 12:02:09 +02003378 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003379 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003380 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003381 free(input);
3382}
3383
3384static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003385display_handle_global(struct wl_display *display, uint32_t id,
3386 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003387{
3388 struct display *d = data;
3389
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003390 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003391 d->compositor =
3392 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003393 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003394 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003395 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003396 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003397 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003398 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003399 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003400 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003401 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3402 d->data_device_manager =
3403 wl_display_bind(display, id,
3404 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003405 } else if (strcmp(interface, "text_cursor_position") == 0) {
3406 d->text_cursor_position =
3407 wl_display_bind(display, id,
3408 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003409 }
3410}
3411
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003412#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003413static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003414init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003415{
3416 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003417 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003418
Rob Clark6396ed32012-03-11 19:48:41 -05003419#ifdef USE_CAIRO_GLESV2
3420# define GL_BIT EGL_OPENGL_ES2_BIT
3421#else
3422# define GL_BIT EGL_OPENGL_BIT
3423#endif
3424
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003425 static const EGLint argb_cfg_attribs[] = {
3426 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003427 EGL_RED_SIZE, 1,
3428 EGL_GREEN_SIZE, 1,
3429 EGL_BLUE_SIZE, 1,
3430 EGL_ALPHA_SIZE, 1,
3431 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003432 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003433 EGL_NONE
3434 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003435
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003436#ifdef USE_CAIRO_GLESV2
3437 static const EGLint context_attribs[] = {
3438 EGL_CONTEXT_CLIENT_VERSION, 2,
3439 EGL_NONE
3440 };
3441 EGLint api = EGL_OPENGL_ES_API;
3442#else
3443 EGLint *context_attribs = NULL;
3444 EGLint api = EGL_OPENGL_API;
3445#endif
3446
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003447 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003448 if (!eglInitialize(d->dpy, &major, &minor)) {
3449 fprintf(stderr, "failed to initialize display\n");
3450 return -1;
3451 }
3452
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003453 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003454 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3455 return -1;
3456 }
3457
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003458 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3459 &d->argb_config, 1, &n) || n != 1) {
3460 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003461 return -1;
3462 }
3463
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003464 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003465 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003466 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003467 fprintf(stderr, "failed to create context\n");
3468 return -1;
3469 }
3470
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003471 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003472 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003473 return -1;
3474 }
3475
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003476#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003477 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3478 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3479 fprintf(stderr, "failed to get cairo egl argb device\n");
3480 return -1;
3481 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003482#endif
3483
3484 return 0;
3485}
3486
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003487static void
3488fini_egl(struct display *display)
3489{
3490#ifdef HAVE_CAIRO_EGL
3491 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003492#endif
3493
3494 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3495 EGL_NO_CONTEXT);
3496
3497 eglTerminate(display->dpy);
3498 eglReleaseThread();
3499}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003500#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003501
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003502static int
3503event_mask_update(uint32_t mask, void *data)
3504{
3505 struct display *d = data;
3506
3507 d->mask = mask;
3508
3509 return 0;
3510}
3511
3512static void
3513handle_display_data(struct task *task, uint32_t events)
3514{
3515 struct display *display =
3516 container_of(task, struct display, display_task);
3517
3518 wl_display_iterate(display->display, display->mask);
3519}
3520
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003521struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003522display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003523{
3524 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003525
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003526 d = malloc(sizeof *d);
3527 if (d == NULL)
3528 return NULL;
3529
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003530 memset(d, 0, sizeof *d);
3531
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003532 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003533 if (d->display == NULL) {
3534 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003535 return NULL;
3536 }
3537
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003538 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003539 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3540 d->display_task.run = handle_display_data;
3541 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3542
3543 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003544 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003545 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003546
Daniel Stone97f68542012-05-30 16:32:01 +01003547 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003548 if (d->xkb_context == NULL) {
3549 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003550 return NULL;
3551 }
3552
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003553 /* Set up listener so we'll catch all events. */
3554 wl_display_add_global_listener(d->display,
3555 display_handle_global, d);
3556
3557 /* Process connection events. */
3558 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003559#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003560 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003561 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003562#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003563
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003564 d->image_target_texture_2d =
3565 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3566 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3567 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3568
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003569 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003570
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003571 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003572
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003573 wl_list_init(&d->window_list);
3574
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003575 return d;
3576}
3577
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003578static void
3579display_destroy_outputs(struct display *display)
3580{
3581 struct output *tmp;
3582 struct output *output;
3583
3584 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3585 output_destroy(output);
3586}
3587
Pekka Paalanene1207c72011-12-16 12:02:09 +02003588static void
3589display_destroy_inputs(struct display *display)
3590{
3591 struct input *tmp;
3592 struct input *input;
3593
3594 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3595 input_destroy(input);
3596}
3597
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003598void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003599display_destroy(struct display *display)
3600{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003601 if (!wl_list_empty(&display->window_list))
3602 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3603
3604 if (!wl_list_empty(&display->deferred_list))
3605 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3606
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003607 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003608 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003609
Daniel Stone97f68542012-05-30 16:32:01 +01003610 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003611
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003612 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003613 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003614
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003615#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003616 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003617#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003618
Pekka Paalanenc2052982011-12-16 11:41:32 +02003619 if (display->shell)
3620 wl_shell_destroy(display->shell);
3621
3622 if (display->shm)
3623 wl_shm_destroy(display->shm);
3624
3625 if (display->data_device_manager)
3626 wl_data_device_manager_destroy(display->data_device_manager);
3627
3628 wl_compositor_destroy(display->compositor);
3629
3630 close(display->epoll_fd);
3631
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003632 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003633 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003634 free(display);
3635}
3636
3637void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003638display_set_user_data(struct display *display, void *data)
3639{
3640 display->user_data = data;
3641}
3642
3643void *
3644display_get_user_data(struct display *display)
3645{
3646 return display->user_data;
3647}
3648
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003649struct wl_display *
3650display_get_display(struct display *display)
3651{
3652 return display->display;
3653}
3654
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003655struct output *
3656display_get_output(struct display *display)
3657{
3658 return container_of(display->output_list.next, struct output, link);
3659}
3660
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003661struct wl_compositor *
3662display_get_compositor(struct display *display)
3663{
3664 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003665}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003666
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003667uint32_t
3668display_get_serial(struct display *display)
3669{
3670 return display->serial;
3671}
3672
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003673EGLDisplay
3674display_get_egl_display(struct display *d)
3675{
3676 return d->dpy;
3677}
3678
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003679struct wl_data_source *
3680display_create_data_source(struct display *display)
3681{
3682 return wl_data_device_manager_create_data_source(display->data_device_manager);
3683}
3684
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003685EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003686display_get_argb_egl_config(struct display *d)
3687{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003688 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003689}
3690
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003691struct wl_shell *
3692display_get_shell(struct display *display)
3693{
3694 return display->shell;
3695}
3696
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003697int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003698display_acquire_window_surface(struct display *display,
3699 struct window *window,
3700 EGLContext ctx)
3701{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003702#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003703 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003704 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003705
3706 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003707 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003708 device = cairo_surface_get_device(window->cairo_surface);
3709 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003710 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003711
Benjamin Franzke0c991632011-09-27 21:57:31 +02003712 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003713 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003714 ctx = display->argb_ctx;
3715 else
3716 assert(0);
3717 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003718
3719 data = cairo_surface_get_user_data(window->cairo_surface,
3720 &surface_data_key);
3721
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003722 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003723 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003724 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3725 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003726
3727 return 0;
3728#else
3729 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003730#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003731}
3732
3733void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003734display_release_window_surface(struct display *display,
3735 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003736{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003737#ifdef HAVE_CAIRO_EGL
3738 cairo_device_t *device;
3739
3740 device = cairo_surface_get_device(window->cairo_surface);
3741 if (!device)
3742 return;
3743
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003744 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003745 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003746 cairo_device_release(device);
3747#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003748}
3749
3750void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003751display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003752{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003753 wl_list_insert(&display->deferred_list, &task->link);
3754}
3755
3756void
3757display_watch_fd(struct display *display,
3758 int fd, uint32_t events, struct task *task)
3759{
3760 struct epoll_event ep;
3761
3762 ep.events = events;
3763 ep.data.ptr = task;
3764 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3765}
3766
3767void
3768display_run(struct display *display)
3769{
3770 struct task *task;
3771 struct epoll_event ep[16];
3772 int i, count;
3773
Pekka Paalanen826d7952011-12-15 10:14:07 +02003774 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003775 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003776 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003777
3778 while (!wl_list_empty(&display->deferred_list)) {
3779 task = container_of(display->deferred_list.next,
3780 struct task, link);
3781 wl_list_remove(&task->link);
3782 task->run(task, 0);
3783 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003784
3785 if (!display->running)
3786 break;
3787
3788 wl_display_flush(display->display);
3789
3790 count = epoll_wait(display->epoll_fd,
3791 ep, ARRAY_LENGTH(ep), -1);
3792 for (i = 0; i < count; i++) {
3793 task = ep[i].data.ptr;
3794 task->run(task, ep[i].events);
3795 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003796 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003797}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003798
3799void
3800display_exit(struct display *display)
3801{
3802 display->running = 0;
3803}