blob: 25a803627ec95269a6196dcd31c6837e3b6d0afc [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øgsberg0d1c0622012-01-31 15:30:47 -0500138 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500139 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400140 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400141 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400142 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400143 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400144 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400145 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400146 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400147 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400148 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500149
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400150 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500151
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700152 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400153
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500154 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500155 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400156 window_data_handler_t data_handler;
157 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500158 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400159
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200160 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500161 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500162
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500163 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400164 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500165};
166
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500167struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500168 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300169 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500170 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400171 struct wl_list link;
172 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500173 widget_resize_handler_t resize_handler;
174 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500175 widget_enter_handler_t enter_handler;
176 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500177 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500178 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500180 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300181 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400182};
183
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400184struct input {
185 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100186 struct wl_seat *seat;
187 struct wl_pointer *pointer;
188 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400189 struct window *pointer_focus;
190 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300191 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300192 uint32_t cursor_anim_start;
193 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300194 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400195 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400196 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400197 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400198 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400199
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500200 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500201 struct widget *grab;
202 uint32_t grab_button;
203
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400204 struct wl_data_device *data_device;
205 struct data_offer *drag_offer;
206 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100207
208 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100209 struct xkb_keymap *keymap;
210 struct xkb_state *state;
211 xkb_mod_mask_t control_mask;
212 xkb_mod_mask_t alt_mask;
213 xkb_mod_mask_t shift_mask;
214 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400215
216 struct task repeat_task;
217 int repeat_timer_fd;
218 uint32_t repeat_sym;
219 uint32_t repeat_key;
220 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400221};
222
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500223struct output {
224 struct display *display;
225 struct wl_output *output;
226 struct rectangle allocation;
227 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200228
229 display_output_handler_t destroy_handler;
230 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500231};
232
Martin Minarik1998b152012-05-10 02:04:35 +0200233enum frame_button_action {
234 FRAME_BUTTON_NULL = 0,
235 FRAME_BUTTON_ICON = 1,
236 FRAME_BUTTON_CLOSE = 2,
237 FRAME_BUTTON_MINIMIZE = 3,
238 FRAME_BUTTON_MAXIMIZE = 4,
239};
240
241enum frame_button_pointer {
242 FRAME_BUTTON_DEFAULT = 0,
243 FRAME_BUTTON_OVER = 1,
244 FRAME_BUTTON_ACTIVE = 2,
245};
246
247enum frame_button_align {
248 FRAME_BUTTON_RIGHT = 0,
249 FRAME_BUTTON_LEFT = 1,
250};
251
252enum frame_button_decoration {
253 FRAME_BUTTON_NONE = 0,
254 FRAME_BUTTON_FANCY = 1,
255};
256
257struct frame_button {
258 struct widget *widget;
259 struct frame *frame;
260 cairo_surface_t *icon;
261 enum frame_button_action type;
262 enum frame_button_pointer state;
263 struct wl_list link; /* buttons_list */
264 enum frame_button_align align;
265 enum frame_button_decoration decoration;
266};
267
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500268struct frame {
269 struct widget *widget;
270 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200271 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500272};
273
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500274struct menu {
275 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500276 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500277 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500278 const char **entries;
279 uint32_t time;
280 int current;
281 int count;
282 menu_func_t func;
283};
284
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300285struct tooltip {
286 struct widget *parent;
287 struct window *window;
288 struct widget *widget;
289 char *entry;
290 struct task tooltip_task;
291 int tooltip_fd;
292 float x, y;
293};
294
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700295struct shm_pool {
296 struct wl_shm_pool *pool;
297 size_t size;
298 size_t used;
299 void *data;
300};
301
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400302enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300303 CURSOR_DEFAULT = 100,
304 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400305};
306
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500307enum window_location {
308 WINDOW_INTERIOR = 0,
309 WINDOW_RESIZING_TOP = 1,
310 WINDOW_RESIZING_BOTTOM = 2,
311 WINDOW_RESIZING_LEFT = 4,
312 WINDOW_RESIZING_TOP_LEFT = 5,
313 WINDOW_RESIZING_BOTTOM_LEFT = 6,
314 WINDOW_RESIZING_RIGHT = 8,
315 WINDOW_RESIZING_TOP_RIGHT = 9,
316 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
317 WINDOW_RESIZING_MASK = 15,
318 WINDOW_EXTERIOR = 16,
319 WINDOW_TITLEBAR = 17,
320 WINDOW_CLIENT_AREA = 18,
321};
322
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400323const char *option_xkb_layout = "us";
324const char *option_xkb_variant = "";
325const char *option_xkb_options = "";
326
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400327static const struct weston_option xkb_options[] = {
328 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
329 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
330 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400331};
332
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333static const cairo_user_data_key_t surface_data_key;
334struct surface_data {
335 struct wl_buffer *buffer;
336};
337
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500338#define MULT(_d,c,a,t) \
339 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
340
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500341#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400342
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100343struct egl_window_surface_data {
344 struct display *display;
345 struct wl_surface *surface;
346 struct wl_egl_window *window;
347 EGLSurface surf;
348};
349
350static void
351egl_window_surface_data_destroy(void *p)
352{
353 struct egl_window_surface_data *data = p;
354 struct display *d = data->display;
355
356 eglDestroySurface(d->dpy, data->surf);
357 wl_egl_window_destroy(data->window);
358 data->surface = NULL;
359
360 free(p);
361}
362
363static cairo_surface_t *
364display_create_egl_window_surface(struct display *display,
365 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400366 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100367 struct rectangle *rectangle)
368{
369 cairo_surface_t *cairo_surface;
370 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400371 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200372 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100373
374 data = malloc(sizeof *data);
375 if (data == NULL)
376 return NULL;
377
378 data->display = display;
379 data->surface = surface;
380
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500381 config = display->argb_config;
382 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400383
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400384 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100385 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400386 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100387
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400388 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500389 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100390
Benjamin Franzke0c991632011-09-27 21:57:31 +0200391 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100392 data->surf,
393 rectangle->width,
394 rectangle->height);
395
396 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
397 data, egl_window_surface_data_destroy);
398
399 return cairo_surface;
400}
401
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402#endif
403
404struct wl_buffer *
405display_get_buffer_for_surface(struct display *display,
406 cairo_surface_t *surface)
407{
408 struct surface_data *data;
409
410 data = cairo_surface_get_user_data (surface, &surface_data_key);
411
412 return data->buffer;
413}
414
415struct shm_surface_data {
416 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700417 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418};
419
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500420static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700421shm_pool_destroy(struct shm_pool *pool);
422
423static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424shm_surface_data_destroy(void *p)
425{
426 struct shm_surface_data *data = p;
427
428 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700429 if (data->pool)
430 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300431
432 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400433}
434
Kristian Høgsberg16626282012-04-03 11:21:27 -0400435static struct wl_shm_pool *
436make_shm_pool(struct display *display, int size, void **data)
437{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400438 struct wl_shm_pool *pool;
439 int fd;
440
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300441 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400442 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300443 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
444 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400445 return NULL;
446 }
447
448 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400449 if (*data == MAP_FAILED) {
450 fprintf(stderr, "mmap failed: %m\n");
451 close(fd);
452 return NULL;
453 }
454
455 pool = wl_shm_create_pool(display->shm, fd, size);
456
457 close(fd);
458
459 return pool;
460}
461
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700462static struct shm_pool *
463shm_pool_create(struct display *display, size_t size)
464{
465 struct shm_pool *pool = malloc(sizeof *pool);
466
467 if (!pool)
468 return NULL;
469
470 pool->pool = make_shm_pool(display, size, &pool->data);
471 if (!pool->pool) {
472 free(pool);
473 return NULL;
474 }
475
476 pool->size = size;
477 pool->used = 0;
478
479 return pool;
480}
481
482static void *
483shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
484{
485 if (pool->used + size > pool->size)
486 return NULL;
487
488 *offset = pool->used;
489 pool->used += size;
490
491 return (char *) pool->data + *offset;
492}
493
494/* destroy the pool. this does not unmap the memory though */
495static void
496shm_pool_destroy(struct shm_pool *pool)
497{
498 munmap(pool->data, pool->size);
499 wl_shm_pool_destroy(pool->pool);
500 free(pool);
501}
502
503/* Start allocating from the beginning of the pool again */
504static void
505shm_pool_reset(struct shm_pool *pool)
506{
507 pool->used = 0;
508}
509
510static int
511data_length_for_shm_surface(struct rectangle *rect)
512{
513 int stride;
514
515 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
516 rect->width);
517 return stride * rect->height;
518}
519
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500520static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700521display_create_shm_surface_from_pool(struct display *display,
522 struct rectangle *rectangle,
523 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524{
525 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400526 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400527 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700528 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400529 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400530
531 data = malloc(sizeof *data);
532 if (data == NULL)
533 return NULL;
534
535 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
536 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700537 length = stride * rectangle->height;
538 data->pool = NULL;
539 map = shm_pool_allocate(pool, length, &offset);
540
541 if (!map) {
542 free(data);
543 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400544 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400545
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400546 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400547 CAIRO_FORMAT_ARGB32,
548 rectangle->width,
549 rectangle->height,
550 stride);
551
552 cairo_surface_set_user_data (surface, &surface_data_key,
553 data, shm_surface_data_destroy);
554
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400555 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500556 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400557 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500558 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400559
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700560 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400561 rectangle->width,
562 rectangle->height,
563 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400564
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700565 return surface;
566}
567
568static cairo_surface_t *
569display_create_shm_surface(struct display *display,
570 struct rectangle *rectangle, uint32_t flags,
571 struct window *window)
572{
573 struct shm_surface_data *data;
574 struct shm_pool *pool;
575 cairo_surface_t *surface;
576
577 if (window && window->pool) {
578 shm_pool_reset(window->pool);
579 surface = display_create_shm_surface_from_pool(display,
580 rectangle,
581 flags,
582 window->pool);
583 if (surface)
584 return surface;
585 }
586
587 pool = shm_pool_create(display,
588 data_length_for_shm_surface(rectangle));
589 if (!pool)
590 return NULL;
591
592 surface =
593 display_create_shm_surface_from_pool(display, rectangle,
594 flags, pool);
595
596 if (!surface) {
597 shm_pool_destroy(pool);
598 return NULL;
599 }
600
601 /* make sure we destroy the pool when the surface is destroyed */
602 data = cairo_surface_get_user_data(surface, &surface_data_key);
603 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400604
605 return surface;
606}
607
nobled7b87cb02011-02-01 18:51:47 +0000608static int
609check_size(struct rectangle *rect)
610{
611 if (rect->width && rect->height)
612 return 0;
613
614 fprintf(stderr, "tried to create surface of "
615 "width: %d, height: %d\n", rect->width, rect->height);
616 return -1;
617}
618
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400619cairo_surface_t *
620display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100621 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400622 struct rectangle *rectangle,
623 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400624{
nobled7b87cb02011-02-01 18:51:47 +0000625 if (check_size(rectangle) < 0)
626 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500627#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300628 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400629 return display_create_egl_window_surface(display,
630 surface,
631 flags,
632 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400634 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635}
636
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300637static const char *cursors[] = {
638 "bottom_left_corner",
639 "bottom_right_corner",
640 "bottom_side",
641 "grabbing",
642 "left_ptr",
643 "left_side",
644 "right_side",
645 "top_left_corner",
646 "top_right_corner",
647 "top_side",
648 "xterm",
649 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400650 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300651};
652
653static void
654create_cursors(struct display *display)
655{
656 unsigned int i;
657
658 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
659 display->cursors =
660 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
661
662 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
663 display->cursors[i] =
664 wl_cursor_theme_get_cursor(display->cursor_theme,
665 cursors[i]);
666}
667
668static void
669destroy_cursors(struct display *display)
670{
671 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800672 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300673}
674
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300675struct wl_cursor_image *
676display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400677{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300678 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300679 wl_cursor_theme_get_cursor(display->cursor_theme,
680 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400681
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300682 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400683}
684
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400685static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200686window_get_resize_dx_dy(struct window *window, int *x, int *y)
687{
688 if (window->resize_edges & WINDOW_RESIZING_LEFT)
689 *x = window->server_allocation.width - window->allocation.width;
690 else
691 *x = 0;
692
693 if (window->resize_edges & WINDOW_RESIZING_TOP)
694 *y = window->server_allocation.height -
695 window->allocation.height;
696 else
697 *y = 0;
698
699 window->resize_edges = 0;
700}
701
702static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500703window_attach_surface(struct window *window)
704{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400705 struct display *display = window->display;
706 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000707#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100708 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000709#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500710 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100711
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400712 if (window->type == TYPE_NONE) {
713 window->type = TYPE_TOPLEVEL;
714 if (display->shell)
715 wl_shell_surface_set_toplevel(window->shell_surface);
716 }
717
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100718 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000719#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100720 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
721 data = cairo_surface_get_user_data(window->cairo_surface,
722 &surface_data_key);
723
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100724 cairo_gl_surface_swapbuffers(window->cairo_surface);
725 wl_egl_window_get_attached_size(data->window,
726 &window->server_allocation.width,
727 &window->server_allocation.height);
728 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000729#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100730 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731 buffer =
732 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400733 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100734
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200735 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400737 wl_surface_damage(window->surface, 0, 0,
738 window->allocation.width,
739 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400741 cairo_surface_destroy(window->cairo_surface);
742 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100743 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000744 default:
745 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100746 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500747
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500748 if (window->input_region) {
749 wl_surface_set_input_region(window->surface,
750 window->input_region);
751 wl_region_destroy(window->input_region);
752 window->input_region = NULL;
753 }
754
755 if (window->opaque_region) {
756 wl_surface_set_opaque_region(window->surface,
757 window->opaque_region);
758 wl_region_destroy(window->opaque_region);
759 window->opaque_region = NULL;
760 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500761}
762
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500763void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400764window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500765{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400766 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100767 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500768}
769
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400770void
771window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400772{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500773 cairo_surface_reference(surface);
774
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400775 if (window->cairo_surface != NULL)
776 cairo_surface_destroy(window->cairo_surface);
777
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500778 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400779}
780
Benjamin Franzke22d54812011-07-16 19:50:32 +0000781#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100782static void
783window_resize_cairo_window_surface(struct window *window)
784{
785 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200786 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787
788 data = cairo_surface_get_user_data(window->cairo_surface,
789 &surface_data_key);
790
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200791 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792 wl_egl_window_resize(data->window,
793 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200794 window->allocation.height,
795 x,y);
796
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100797 cairo_gl_surface_set_size(window->cairo_surface,
798 window->allocation.width,
799 window->allocation.height);
800}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000801#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100802
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400803struct display *
804window_get_display(struct window *window)
805{
806 return window->display;
807}
808
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400809void
810window_create_surface(struct window *window)
811{
812 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400813 uint32_t flags = 0;
814
815 if (!window->transparent)
816 flags = SURFACE_OPAQUE;
817
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400818 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500819#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
821 if (window->cairo_surface) {
822 window_resize_cairo_window_surface(window);
823 return;
824 }
825 surface = display_create_surface(window->display,
826 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400827 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100828 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400829#endif
830 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400831 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400832 &window->allocation,
833 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800835 default:
836 surface = NULL;
837 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400838 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400839
840 window_set_surface(window, surface);
841 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400842}
843
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200844static void frame_destroy(struct frame *frame);
845
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400846void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500847window_destroy(struct window *window)
848{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200849 struct display *display = window->display;
850 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100851 struct window_output *window_output;
852 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200853
854 if (window->redraw_scheduled)
855 wl_list_remove(&window->redraw_task.link);
856
857 wl_list_for_each(input, &display->input_list, link) {
858 if (input->pointer_focus == window)
859 input->pointer_focus = NULL;
860 if (input->keyboard_focus == window)
861 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500862 if (input->focus_widget &&
863 input->focus_widget->window == window)
864 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200865 }
866
Rob Bradford7507b572012-05-15 17:55:34 +0100867 wl_list_for_each_safe(window_output, window_output_tmp,
868 &window->window_output_list, link) {
869 free (window_output);
870 }
871
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500872 if (window->input_region)
873 wl_region_destroy(window->input_region);
874 if (window->opaque_region)
875 wl_region_destroy(window->opaque_region);
876
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200877 if (window->frame)
878 frame_destroy(window->frame);
879
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200880 if (window->shell_surface)
881 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500882 wl_surface_destroy(window->surface);
883 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200884
885 if (window->cairo_surface != NULL)
886 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200887
888 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500889 free(window);
890}
891
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500892static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500893widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400894{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500895 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400896
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500897 wl_list_for_each(child, &widget->child_list, link) {
898 target = widget_find_widget(child, x, y);
899 if (target)
900 return target;
901 }
902
903 if (widget->allocation.x <= x &&
904 x < widget->allocation.x + widget->allocation.width &&
905 widget->allocation.y <= y &&
906 y < widget->allocation.y + widget->allocation.height) {
907 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400908 }
909
910 return NULL;
911}
912
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500913static struct widget *
914widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400915{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500916 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400917
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500918 widget = malloc(sizeof *widget);
919 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500920 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500921 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500922 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500923 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500924 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300925 widget->tooltip = NULL;
926 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500927
928 return widget;
929}
930
931struct widget *
932window_add_widget(struct window *window, void *data)
933{
934 window->widget = widget_create(window, data);
935 wl_list_init(&window->widget->link);
936
937 return window->widget;
938}
939
940struct widget *
941widget_add_widget(struct widget *parent, void *data)
942{
943 struct widget *widget;
944
945 widget = widget_create(parent->window, data);
946 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400947
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500948 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400949}
950
951void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500952widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400953{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200954 struct display *display = widget->window->display;
955 struct input *input;
956
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300957 if (widget->tooltip) {
958 free(widget->tooltip);
959 widget->tooltip = NULL;
960 }
961
Pekka Paalanene156fb62012-01-19 13:51:38 +0200962 wl_list_for_each(input, &display->input_list, link) {
963 if (input->focus_widget == widget)
964 input->focus_widget = NULL;
965 }
966
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500967 wl_list_remove(&widget->link);
968 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400969}
970
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400971void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500972widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400973{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500974 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400975}
976
977void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500978widget_set_size(struct widget *widget, int32_t width, int32_t height)
979{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500980 widget->allocation.width = width;
981 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500982}
983
984void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500985widget_set_allocation(struct widget *widget,
986 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400987{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500988 widget->allocation.x = x;
989 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200990 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400991}
992
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500993void
994widget_set_transparent(struct widget *widget, int transparent)
995{
996 widget->opaque = !transparent;
997}
998
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400999void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001000widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001001{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001002 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001003}
1004
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001005void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001006widget_set_resize_handler(struct widget *widget,
1007 widget_resize_handler_t handler)
1008{
1009 widget->resize_handler = handler;
1010}
1011
1012void
1013widget_set_redraw_handler(struct widget *widget,
1014 widget_redraw_handler_t handler)
1015{
1016 widget->redraw_handler = handler;
1017}
1018
1019void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001020widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001021{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001022 widget->enter_handler = handler;
1023}
1024
1025void
1026widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1027{
1028 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001029}
1030
1031void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001032widget_set_motion_handler(struct widget *widget,
1033 widget_motion_handler_t handler)
1034{
1035 widget->motion_handler = handler;
1036}
1037
1038void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001039widget_set_button_handler(struct widget *widget,
1040 widget_button_handler_t handler)
1041{
1042 widget->button_handler = handler;
1043}
1044
1045void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001046widget_schedule_redraw(struct widget *widget)
1047{
1048 window_schedule_redraw(widget->window);
1049}
1050
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001051cairo_surface_t *
1052window_get_surface(struct window *window)
1053{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001054 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001055}
1056
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001057struct wl_surface *
1058window_get_wl_surface(struct window *window)
1059{
1060 return window->surface;
1061}
1062
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001063struct wl_shell_surface *
1064window_get_wl_shell_surface(struct window *window)
1065{
1066 return window->shell_surface;
1067}
1068
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001069static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001070tooltip_redraw_handler(struct widget *widget, void *data)
1071{
1072 cairo_t *cr;
1073 const int32_t r = 3;
1074 struct tooltip *tooltip = data;
1075 int32_t width, height;
1076 struct window *window = widget->window;
1077
1078 cr = cairo_create(window->cairo_surface);
1079 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1080 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1081 cairo_paint(cr);
1082
1083 width = window->allocation.width;
1084 height = window->allocation.height;
1085 rounded_rect(cr, 0, 0, width, height, r);
1086
1087 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1088 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1089 cairo_fill(cr);
1090
1091 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1092 cairo_move_to(cr, 10, 16);
1093 cairo_show_text(cr, tooltip->entry);
1094 cairo_destroy(cr);
1095}
1096
1097static cairo_text_extents_t
1098get_text_extents(struct tooltip *tooltip)
1099{
1100 struct window *window;
1101 cairo_t *cr;
1102 cairo_text_extents_t extents;
1103
1104 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1105 * created yet */
1106 window = tooltip->widget->window->parent;
1107 cr = cairo_create(window->cairo_surface);
1108 cairo_text_extents(cr, tooltip->entry, &extents);
1109 cairo_destroy(cr);
1110
1111 return extents;
1112}
1113
1114static int
1115window_create_tooltip(struct tooltip *tooltip)
1116{
1117 struct widget *parent = tooltip->parent;
1118 struct display *display = parent->window->display;
1119 struct window *window;
1120 const int offset_y = 27;
1121 const int margin = 3;
1122 cairo_text_extents_t extents;
1123
1124 if (tooltip->widget)
1125 return 0;
1126
1127 window = window_create_transient(display, parent->window, tooltip->x,
1128 tooltip->y + offset_y,
1129 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1130 if (!window)
1131 return -1;
1132
1133 tooltip->window = window;
1134 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1135
1136 extents = get_text_extents(tooltip);
1137 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1138 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1139
1140 return 0;
1141}
1142
1143void
1144widget_destroy_tooltip(struct widget *parent)
1145{
1146 struct tooltip *tooltip = parent->tooltip;
1147
1148 parent->tooltip_count = 0;
1149 if (!tooltip)
1150 return;
1151
1152 if (tooltip->widget) {
1153 widget_destroy(tooltip->widget);
1154 window_destroy(tooltip->window);
1155 tooltip->widget = NULL;
1156 tooltip->window = NULL;
1157 }
1158
1159 close(tooltip->tooltip_fd);
1160 free(tooltip->entry);
1161 free(tooltip);
1162 parent->tooltip = NULL;
1163}
1164
1165static void
1166tooltip_func(struct task *task, uint32_t events)
1167{
1168 struct tooltip *tooltip =
1169 container_of(task, struct tooltip, tooltip_task);
1170 uint64_t exp;
1171
1172 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1173 window_create_tooltip(tooltip);
1174}
1175
1176#define TOOLTIP_TIMEOUT 500
1177static int
1178tooltip_timer_reset(struct tooltip *tooltip)
1179{
1180 struct itimerspec its;
1181
1182 its.it_interval.tv_sec = 0;
1183 its.it_interval.tv_nsec = 0;
1184 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1185 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1186 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1187 fprintf(stderr, "could not set timerfd\n: %m");
1188 return -1;
1189 }
1190
1191 return 0;
1192}
1193
1194int
1195widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1196{
1197 struct tooltip *tooltip = parent->tooltip;
1198
1199 parent->tooltip_count++;
1200 if (tooltip) {
1201 tooltip->x = x;
1202 tooltip->y = y;
1203 tooltip_timer_reset(tooltip);
1204 return 0;
1205 }
1206
1207 /* the handler might be triggered too fast via input device motion, so
1208 * we need this check here to make sure tooltip is fully initialized */
1209 if (parent->tooltip_count > 1)
1210 return 0;
1211
1212 tooltip = malloc(sizeof *tooltip);
1213 if (!tooltip)
1214 return -1;
1215
1216 parent->tooltip = tooltip;
1217 tooltip->parent = parent;
1218 tooltip->widget = NULL;
1219 tooltip->window = NULL;
1220 tooltip->x = x;
1221 tooltip->y = y;
1222 tooltip->entry = strdup(entry);
1223 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1224 if (tooltip->tooltip_fd < 0) {
1225 fprintf(stderr, "could not create timerfd\n: %m");
1226 return -1;
1227 }
1228
1229 tooltip->tooltip_task.run = tooltip_func;
1230 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1231 EPOLLIN, &tooltip->tooltip_task);
1232 tooltip_timer_reset(tooltip);
1233
1234 return 0;
1235}
1236
1237static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001238frame_resize_handler(struct widget *widget,
1239 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001240{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001241 struct frame *frame = data;
1242 struct widget *child = frame->child;
1243 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001244 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001245 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001246 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001247 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001248 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001249 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001250
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001251 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001252 decoration_width = (t->width + t->margin) * 2;
1253 decoration_height = t->width +
1254 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001255
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001256 allocation.x = t->width + t->margin;
1257 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001258 allocation.width = width - decoration_width;
1259 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001260
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001261 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001262
1263 wl_list_for_each(button, &frame->buttons_list, link)
1264 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001265 } else {
1266 decoration_width = 0;
1267 decoration_height = 0;
1268
1269 allocation.x = 0;
1270 allocation.y = 0;
1271 allocation.width = width;
1272 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001273 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001274
1275 wl_list_for_each(button, &frame->buttons_list, link)
1276 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001277 }
1278
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001279 widget_set_allocation(child, allocation.x, allocation.y,
1280 allocation.width, allocation.height);
1281
1282 if (child->resize_handler)
1283 child->resize_handler(child,
1284 allocation.width,
1285 allocation.height,
1286 child->user_data);
1287
Scott Moreauf7e498c2012-05-14 11:39:29 -06001288 width = child->allocation.width + decoration_width;
1289 height = child->allocation.height + decoration_height;
1290
1291 widget->window->input_region =
1292 wl_compositor_create_region(display->compositor);
1293 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001294 t->margin, t->margin,
1295 width - 2 * t->margin,
1296 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001297
1298 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001299
1300 if (child->opaque) {
1301 widget->window->opaque_region =
1302 wl_compositor_create_region(display->compositor);
1303 wl_region_add(widget->window->opaque_region,
1304 opaque_margin, opaque_margin,
1305 widget->allocation.width - 2 * opaque_margin,
1306 widget->allocation.height - 2 * opaque_margin);
1307 }
Martin Minarik1998b152012-05-10 02:04:35 +02001308
1309 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001310 x_r = frame->widget->allocation.width - t->width - t->margin;
1311 x_l = t->width + t->margin;
1312 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001313 wl_list_for_each(button, &frame->buttons_list, link) {
1314 const int button_padding = 4;
1315 w = cairo_image_surface_get_width(button->icon);
1316 h = cairo_image_surface_get_height(button->icon);
1317
1318 if (button->decoration == FRAME_BUTTON_FANCY)
1319 w += 10;
1320
1321 if (button->align == FRAME_BUTTON_LEFT) {
1322 widget_set_allocation(button->widget,
1323 x_l, y , w + 1, h + 1);
1324 x_l += w;
1325 x_l += button_padding;
1326 } else {
1327 x_r -= w;
1328 widget_set_allocation(button->widget,
1329 x_r, y , w + 1, h + 1);
1330 x_r -= button_padding;
1331 }
1332 }
1333}
1334
1335static int
1336frame_button_enter_handler(struct widget *widget,
1337 struct input *input, float x, float y, void *data)
1338{
1339 struct frame_button *frame_button = data;
1340
1341 widget_schedule_redraw(frame_button->widget);
1342 frame_button->state = FRAME_BUTTON_OVER;
1343
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001344 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001345}
1346
1347static void
1348frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1349{
1350 struct frame_button *frame_button = data;
1351
1352 widget_schedule_redraw(frame_button->widget);
1353 frame_button->state = FRAME_BUTTON_DEFAULT;
1354}
1355
1356static void
1357frame_button_button_handler(struct widget *widget,
1358 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001359 uint32_t button,
1360 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001361{
1362 struct frame_button *frame_button = data;
1363 struct window *window = widget->window;
1364
1365 if (button != BTN_LEFT)
1366 return;
1367
1368 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001369 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001370 frame_button->state = FRAME_BUTTON_ACTIVE;
1371 widget_schedule_redraw(frame_button->widget);
1372
1373 if (frame_button->type == FRAME_BUTTON_ICON)
1374 window_show_frame_menu(window, input, time);
1375 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001376 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001377 frame_button->state = FRAME_BUTTON_DEFAULT;
1378 widget_schedule_redraw(frame_button->widget);
1379 break;
1380 }
1381
1382 switch (frame_button->type) {
1383 case FRAME_BUTTON_CLOSE:
1384 if (window->close_handler)
1385 window->close_handler(window->parent,
1386 window->user_data);
1387 else
1388 display_exit(window->display);
1389 break;
1390 case FRAME_BUTTON_MINIMIZE:
1391 fprintf(stderr,"Minimize stub\n");
1392 break;
1393 case FRAME_BUTTON_MAXIMIZE:
1394 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1395 break;
1396 default:
1397 /* Unknown operation */
1398 break;
1399 }
1400}
1401
1402static void
1403frame_button_redraw_handler(struct widget *widget, void *data)
1404{
1405 struct frame_button *frame_button = data;
1406 cairo_t *cr;
1407 int width, height, x, y;
1408 struct window *window = widget->window;
1409
1410 x = widget->allocation.x;
1411 y = widget->allocation.y;
1412 width = widget->allocation.width;
1413 height = widget->allocation.height;
1414
1415 if (!width)
1416 return;
1417 if (!height)
1418 return;
1419 if (widget->opaque)
1420 return;
1421
1422 cr = cairo_create(window->cairo_surface);
1423
1424 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1425 cairo_set_line_width(cr, 1);
1426
1427 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1428 cairo_rectangle (cr, x, y, 25, 16);
1429
1430 cairo_stroke_preserve(cr);
1431
1432 switch (frame_button->state) {
1433 case FRAME_BUTTON_DEFAULT:
1434 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1435 break;
1436 case FRAME_BUTTON_OVER:
1437 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1438 break;
1439 case FRAME_BUTTON_ACTIVE:
1440 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1441 break;
1442 }
1443
1444 cairo_fill (cr);
1445
1446 x += 4;
1447 }
1448
1449 cairo_set_source_surface(cr, frame_button->icon, x, y);
1450 cairo_paint(cr);
1451
1452 cairo_destroy(cr);
1453}
1454
1455static struct widget *
1456frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1457 enum frame_button_align align, enum frame_button_decoration style)
1458{
1459 struct frame_button *frame_button;
1460 const char *icon = data;
1461
1462 frame_button = malloc (sizeof *frame_button);
1463 memset(frame_button, 0, sizeof *frame_button);
1464
1465 frame_button->icon = cairo_image_surface_create_from_png(icon);
1466 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1467 frame_button->frame = frame;
1468 frame_button->type = type;
1469 frame_button->align = align;
1470 frame_button->decoration = style;
1471
1472 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1473
1474 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1475 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1476 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1477 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1478 return frame_button->widget;
1479}
1480
1481static void
1482frame_button_destroy(struct frame_button *frame_button)
1483{
1484 widget_destroy(frame_button->widget);
1485 wl_list_remove(&frame_button->link);
1486 cairo_surface_destroy(frame_button->icon);
1487 free(frame_button);
1488
1489 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001490}
1491
1492static void
1493frame_redraw_handler(struct widget *widget, void *data)
1494{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001496 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001497 struct theme *t = window->display->theme;
1498 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001500 if (window->type == TYPE_FULLSCREEN)
1501 return;
1502
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001503 cr = cairo_create(window->cairo_surface);
1504
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001505 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001506 flags |= THEME_FRAME_ACTIVE;
1507 theme_render_frame(t, cr, widget->allocation.width,
1508 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001509
1510 cairo_destroy(cr);
1511}
1512
1513static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001514frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001515{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001516 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001517 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001518
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001519 location = theme_get_location(t, input->sx, input->sy,
1520 frame->widget->allocation.width,
1521 frame->widget->allocation.height);
1522
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001523 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001525 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001527 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001531 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001532 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001533 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001534 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001535 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001536 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001537 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001538 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001539 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001540 case THEME_LOCATION_EXTERIOR:
1541 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001542 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001543 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001544 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001545}
1546
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001547static void
1548frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001549{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001550 switch (index) {
1551 case 0: /* close */
1552 if (window->close_handler)
1553 window->close_handler(window->parent,
1554 window->user_data);
1555 else
1556 display_exit(window->display);
1557 break;
1558 case 1: /* fullscreen */
1559 /* we don't have a way to get out of fullscreen for now */
1560 window_set_fullscreen(window, 1);
1561 break;
1562 case 2: /* rotate */
1563 case 3: /* scale */
1564 break;
1565 }
1566}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001567
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001568void
1569window_show_frame_menu(struct window *window,
1570 struct input *input, uint32_t time)
1571{
1572 int32_t x, y;
1573
1574 static const char *entries[] = {
1575 "Close", "Fullscreen", "Rotate", "Scale"
1576 };
1577
1578 input_get_position(input, &x, &y);
1579 window_show_menu(window->display, input, time, window,
1580 x - 10, y - 10, frame_menu_func, entries, 4);
1581}
1582
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001583static int
1584frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001585 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001586{
1587 return frame_get_pointer_image_for_location(data, input);
1588}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001589
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001590static int
1591frame_motion_handler(struct widget *widget,
1592 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001593 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001594{
1595 return frame_get_pointer_image_for_location(data, input);
1596}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001597
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001598static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001599frame_button_handler(struct widget *widget,
1600 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001601 uint32_t button, enum wl_pointer_button_state state,
1602 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603
1604{
1605 struct frame *frame = data;
1606 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001607 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001608 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001609
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001610 location = theme_get_location(display->theme, input->sx, input->sy,
1611 frame->widget->allocation.width,
1612 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001613
Daniel Stone4dbadb12012-05-30 16:31:51 +01001614 if (window->display->shell && button == BTN_LEFT &&
1615 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001616 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001617 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001618 if (!window->shell_surface)
1619 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001620 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001621 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001622 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001623 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001624 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001625 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001626 case THEME_LOCATION_RESIZING_TOP:
1627 case THEME_LOCATION_RESIZING_BOTTOM:
1628 case THEME_LOCATION_RESIZING_LEFT:
1629 case THEME_LOCATION_RESIZING_RIGHT:
1630 case THEME_LOCATION_RESIZING_TOP_LEFT:
1631 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1632 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1633 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001634 if (!window->shell_surface)
1635 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001636 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001637
1638 if (!display->dpy) {
1639 /* If we're using shm, allocate a big
1640 pool to create buffers out of while
1641 we resize. We should probably base
1642 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001643 window->pool =
1644 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001645 }
1646
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001647 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001648 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001649 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001650 break;
1651 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001652 } else if (button == BTN_RIGHT &&
1653 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001654 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001655 }
1656}
1657
1658struct widget *
1659frame_create(struct window *window, void *data)
1660{
1661 struct frame *frame;
1662
1663 frame = malloc(sizeof *frame);
1664 memset(frame, 0, sizeof *frame);
1665
1666 frame->widget = window_add_widget(window, frame);
1667 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001668
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001669 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1670 widget_set_resize_handler(frame->widget, frame_resize_handler);
1671 widget_set_enter_handler(frame->widget, frame_enter_handler);
1672 widget_set_motion_handler(frame->widget, frame_motion_handler);
1673 widget_set_button_handler(frame->widget, frame_button_handler);
1674
Martin Minarik1998b152012-05-10 02:04:35 +02001675 /* Create empty list for frame buttons */
1676 wl_list_init(&frame->buttons_list);
1677
1678 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1679 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1680
1681 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1682 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1683
1684 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1685 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1686
1687 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1688 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1689
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001690 window->frame = frame;
1691
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001692 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001693}
1694
Kristian Høgsberga1627922012-06-20 17:30:03 -04001695void
1696frame_set_child_size(struct widget *widget, int child_width, int child_height)
1697{
1698 struct display *display = widget->window->display;
1699 struct theme *t = display->theme;
1700 int decoration_width, decoration_height;
1701 int width, height;
1702
1703 if (widget->window->type != TYPE_FULLSCREEN) {
1704 decoration_width = (t->width + t->margin) * 2;
1705 decoration_height = t->width +
1706 t->titlebar_height + t->margin * 2;
1707
1708 width = child_width + decoration_width;
1709 height = child_height + decoration_height;
1710 } else {
1711 width = child_width;
1712 height = child_height;
1713 }
1714
1715 window_schedule_resize(widget->window, width, height);
1716}
1717
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001718static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001719frame_destroy(struct frame *frame)
1720{
Martin Minarik1998b152012-05-10 02:04:35 +02001721 struct frame_button *button, *tmp;
1722
1723 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1724 frame_button_destroy(button);
1725
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001726 /* frame->child must be destroyed by the application */
1727 widget_destroy(frame->widget);
1728 free(frame);
1729}
1730
1731static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001732input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001733 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001734{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001735 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001736 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001737
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001738 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001739 return;
1740
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001741 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001742 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001743 widget = old;
1744 if (input->grab)
1745 widget = input->grab;
1746 if (widget->leave_handler)
1747 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001748 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001749 }
1750
1751 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001752 widget = focus;
1753 if (input->grab)
1754 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001755 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001756 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001757 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001758 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001759
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001760 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001761 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001762}
1763
1764static void
Daniel Stone37816df2012-05-16 18:45:18 +01001765pointer_handle_motion(void *data, struct wl_pointer *pointer,
1766 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001767{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001768 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001769 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001770 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001771 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001772 float sx = wl_fixed_to_double(sx_w);
1773 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001774
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001775 input->sx = sx;
1776 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001777
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001778 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001779 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001780 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001781 }
1782
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001783 if (input->grab)
1784 widget = input->grab;
1785 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001786 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001787 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001788 cursor = widget->motion_handler(input->focus_widget,
1789 input, time, sx, sy,
1790 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001791
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001792 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001793}
1794
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001795void
1796input_grab(struct input *input, struct widget *widget, uint32_t button)
1797{
1798 input->grab = widget;
1799 input->grab_button = button;
1800}
1801
1802void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001803input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001804{
1805 struct widget *widget;
1806
1807 input->grab = NULL;
1808 if (input->pointer_focus) {
1809 widget = widget_find_widget(input->pointer_focus->widget,
1810 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001811 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001812 }
1813}
1814
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001815static void
Daniel Stone37816df2012-05-16 18:45:18 +01001816pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001817 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001818{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001819 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001820 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001821 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001822
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001823 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001824 if (input->focus_widget && input->grab == NULL &&
1825 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001826 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001827
Neil Roberts6b28aad2012-01-23 19:11:18 +00001828 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001829 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001830 (*widget->button_handler)(widget,
1831 input, time,
1832 button, state,
1833 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001834
Daniel Stone4dbadb12012-05-30 16:31:51 +01001835 if (input->grab && input->grab_button == button &&
1836 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001837 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001838}
1839
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001840static void
Daniel Stone37816df2012-05-16 18:45:18 +01001841pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001842 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001843{
1844}
1845
1846static void
Daniel Stone37816df2012-05-16 18:45:18 +01001847keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001848 uint32_t serial, uint32_t time, uint32_t key,
1849 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001850{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001851 struct input *input = data;
1852 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001853 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001854 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001855 const xkb_keysym_t *syms;
1856 xkb_keysym_t sym;
1857 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001858 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001859
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001860 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001861 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001862 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001863 return;
1864
Daniel Stone97f68542012-05-30 16:32:01 +01001865 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001866
Daniel Stone97f68542012-05-30 16:32:01 +01001867 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001868 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001869 XKB_STATE_LATCHED);
1870 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001871 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001872 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001873 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001874 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001875 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001876 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001877
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001878 sym = XKB_KEY_NoSymbol;
1879 if (num_syms == 1)
1880 sym = syms[0];
1881
1882 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001883 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001884 window_set_maximized(window,
1885 window->type != TYPE_MAXIMIZED);
1886 } else if (window->key_handler) {
1887 (*window->key_handler)(window, input, time, key,
1888 sym, state, window->user_data);
1889 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001890
1891 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
1892 key == input->repeat_key) {
1893 its.it_interval.tv_sec = 0;
1894 its.it_interval.tv_nsec = 0;
1895 its.it_value.tv_sec = 0;
1896 its.it_value.tv_nsec = 0;
1897 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1898 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1899 input->repeat_sym = sym;
1900 input->repeat_key = key;
1901 input->repeat_time = time;
1902 its.it_interval.tv_sec = 0;
1903 its.it_interval.tv_nsec = 25 * 1000 * 1000;
1904 its.it_value.tv_sec = 0;
1905 its.it_value.tv_nsec = 400 * 1000 * 1000;
1906 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1907 }
1908}
1909
1910static void
1911keyboard_repeat_func(struct task *task, uint32_t events)
1912{
1913 struct input *input =
1914 container_of(task, struct input, repeat_task);
1915 struct window *window = input->keyboard_focus;
1916 uint64_t exp;
1917
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04001918 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1919 /* If we change the timer between the fd becoming
1920 * readable and getting here, there'll be nothing to
1921 * read and we get EAGAIN. */
1922 return;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001923
Kristian Høgsbergbd0cd762012-06-20 15:17:18 -04001924 if (window && window->key_handler) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001925 (*window->key_handler)(window, input, input->repeat_time,
1926 input->repeat_key, input->repeat_sym,
1927 WL_KEYBOARD_KEY_STATE_PRESSED,
1928 window->user_data);
1929 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001930}
1931
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001932static void
Daniel Stone351eb612012-05-31 15:27:47 -04001933keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1934 uint32_t serial, uint32_t mods_depressed,
1935 uint32_t mods_latched, uint32_t mods_locked,
1936 uint32_t group)
1937{
1938 struct input *input = data;
1939
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001940 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1941 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001942}
1943
1944static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001945input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001946{
1947 struct window *window = input->pointer_focus;
1948
1949 if (!window)
1950 return;
1951
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001952 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001953
Pekka Paalanene1207c72011-12-16 12:02:09 +02001954 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001955 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001956}
1957
1958static void
Daniel Stone37816df2012-05-16 18:45:18 +01001959pointer_handle_enter(void *data, struct wl_pointer *pointer,
1960 uint32_t serial, struct wl_surface *surface,
1961 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001962{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001963 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001964 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001965 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001966 float sx = wl_fixed_to_double(sx_w);
1967 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001968
Martin Minarik1998b152012-05-10 02:04:35 +02001969 if (!surface) {
1970 /* enter event for a window we've just destroyed */
1971 return;
1972 }
1973
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001974 input->display->serial = serial;
1975 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001976 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001977 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001978
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001979 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001980 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001981 window->pool = NULL;
1982 /* Schedule a redraw to free the pool */
1983 window_schedule_redraw(window);
1984 }
1985
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001986 input->sx = sx;
1987 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001988
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001989 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001990 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001991}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001992
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001993static void
Daniel Stone37816df2012-05-16 18:45:18 +01001994pointer_handle_leave(void *data, struct wl_pointer *pointer,
1995 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001996{
1997 struct input *input = data;
1998
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001999 input->display->serial = serial;
2000 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002001}
2002
2003static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002004input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02002005{
2006 struct window *window = input->keyboard_focus;
Kristian Høgsbergd3c69c22012-06-20 22:41:59 -04002007 struct itimerspec its;
2008
2009 its.it_interval.tv_sec = 0;
2010 its.it_interval.tv_nsec = 0;
2011 its.it_value.tv_sec = 0;
2012 its.it_value.tv_nsec = 0;
2013 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002014
2015 if (!window)
2016 return;
2017
2018 window->keyboard_device = NULL;
2019 if (window->keyboard_focus_handler)
2020 (*window->keyboard_focus_handler)(window, NULL,
2021 window->user_data);
2022
2023 input->keyboard_focus = NULL;
2024}
2025
2026static void
Daniel Stone37816df2012-05-16 18:45:18 +01002027keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2028 uint32_t serial, struct wl_surface *surface,
2029 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002030{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002031 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02002032 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002033
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002034 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002035 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002036
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002037 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002038 window->keyboard_device = input;
2039 if (window->keyboard_focus_handler)
2040 (*window->keyboard_focus_handler)(window,
2041 window->keyboard_device,
2042 window->user_data);
2043}
2044
2045static void
Daniel Stone37816df2012-05-16 18:45:18 +01002046keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2047 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002048{
2049 struct input *input = data;
2050
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002051 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002052 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002053}
2054
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002055static void
2056keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2057 uint32_t format, int fd, uint32_t size)
2058{
2059 struct input *input = data;
2060 char *map_str;
2061
2062 if (!data) {
2063 close(fd);
2064 return;
2065 }
2066
2067 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2068 close(fd);
2069 return;
2070 }
2071
2072 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2073 if (map_str == MAP_FAILED) {
2074 close(fd);
2075 return;
2076 }
2077
2078 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2079 map_str,
2080 XKB_KEYMAP_FORMAT_TEXT_V1,
2081 0);
2082 munmap(map_str, size);
2083 close(fd);
2084
2085 if (!input->xkb.keymap) {
2086 fprintf(stderr, "failed to compile keymap\n");
2087 return;
2088 }
2089
2090 input->xkb.state = xkb_state_new(input->xkb.keymap);
2091 if (!input->xkb.state) {
2092 fprintf(stderr, "failed to create XKB state\n");
2093 xkb_map_unref(input->xkb.keymap);
2094 input->xkb.keymap = NULL;
2095 return;
2096 }
2097
2098 input->xkb.control_mask =
2099 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2100 input->xkb.alt_mask =
2101 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2102 input->xkb.shift_mask =
2103 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2104}
2105
Daniel Stone37816df2012-05-16 18:45:18 +01002106static const struct wl_pointer_listener pointer_listener = {
2107 pointer_handle_enter,
2108 pointer_handle_leave,
2109 pointer_handle_motion,
2110 pointer_handle_button,
2111 pointer_handle_axis,
2112};
2113
2114static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002115 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002116 keyboard_handle_enter,
2117 keyboard_handle_leave,
2118 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002119 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002120};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002121
2122static void
Daniel Stone37816df2012-05-16 18:45:18 +01002123seat_handle_capabilities(void *data, struct wl_seat *seat,
2124 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002125{
Daniel Stone37816df2012-05-16 18:45:18 +01002126 struct input *input = data;
2127
2128 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2129 input->pointer = wl_seat_get_pointer(seat);
2130 wl_pointer_set_user_data(input->pointer, input);
2131 wl_pointer_add_listener(input->pointer, &pointer_listener,
2132 input);
2133 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2134 wl_pointer_destroy(input->pointer);
2135 input->pointer = NULL;
2136 }
2137
2138 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2139 input->keyboard = wl_seat_get_keyboard(seat);
2140 wl_keyboard_set_user_data(input->keyboard, input);
2141 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2142 input);
2143 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2144 wl_keyboard_destroy(input->keyboard);
2145 input->keyboard = NULL;
2146 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002147}
2148
Daniel Stone37816df2012-05-16 18:45:18 +01002149static const struct wl_seat_listener seat_listener = {
2150 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002151};
2152
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002153void
2154input_get_position(struct input *input, int32_t *x, int32_t *y)
2155{
2156 *x = input->sx;
2157 *y = input->sy;
2158}
2159
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002160struct display *
2161input_get_display(struct input *input)
2162{
2163 return input->display;
2164}
2165
Daniel Stone37816df2012-05-16 18:45:18 +01002166struct wl_seat *
2167input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002168{
Daniel Stone37816df2012-05-16 18:45:18 +01002169 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002170}
2171
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002172uint32_t
2173input_get_modifiers(struct input *input)
2174{
2175 return input->modifiers;
2176}
2177
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002178struct widget *
2179input_get_focus_widget(struct input *input)
2180{
2181 return input->focus_widget;
2182}
2183
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002184struct data_offer {
2185 struct wl_data_offer *offer;
2186 struct input *input;
2187 struct wl_array types;
2188 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002189
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002190 struct task io_task;
2191 int fd;
2192 data_func_t func;
2193 int32_t x, y;
2194 void *user_data;
2195};
2196
2197static void
2198data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2199{
2200 struct data_offer *offer = data;
2201 char **p;
2202
2203 p = wl_array_add(&offer->types, sizeof *p);
2204 *p = strdup(type);
2205}
2206
2207static const struct wl_data_offer_listener data_offer_listener = {
2208 data_offer_offer,
2209};
2210
2211static void
2212data_offer_destroy(struct data_offer *offer)
2213{
2214 char **p;
2215
2216 offer->refcount--;
2217 if (offer->refcount == 0) {
2218 wl_data_offer_destroy(offer->offer);
2219 for (p = offer->types.data; *p; p++)
2220 free(*p);
2221 wl_array_release(&offer->types);
2222 free(offer);
2223 }
2224}
2225
2226static void
2227data_device_data_offer(void *data,
2228 struct wl_data_device *data_device, uint32_t id)
2229{
2230 struct data_offer *offer;
2231
2232 offer = malloc(sizeof *offer);
2233
2234 wl_array_init(&offer->types);
2235 offer->refcount = 1;
2236 offer->input = data;
2237
2238 /* FIXME: Generate typesafe wrappers for this */
2239 offer->offer = (struct wl_data_offer *)
2240 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2241 id, &wl_data_offer_interface);
2242
2243 wl_data_offer_add_listener(offer->offer,
2244 &data_offer_listener, offer);
2245}
2246
2247static void
2248data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002249 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002250 wl_fixed_t x_w, wl_fixed_t y_w,
2251 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002252{
2253 struct input *input = data;
2254 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002255 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002256 float x = wl_fixed_to_double(x_w);
2257 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002258 char **p;
2259
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002260 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002261 window = wl_surface_get_user_data(surface);
2262 input->pointer_focus = window;
2263
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002264 if (offer) {
2265 input->drag_offer = wl_data_offer_get_user_data(offer);
2266
2267 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2268 *p = NULL;
2269
2270 types_data = input->drag_offer->types.data;
2271 } else {
2272 input->drag_offer = NULL;
2273 types_data = NULL;
2274 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002275
2276 window = input->pointer_focus;
2277 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002278 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002279 window->user_data);
2280}
2281
2282static void
2283data_device_leave(void *data, struct wl_data_device *data_device)
2284{
2285 struct input *input = data;
2286
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002287 if (input->drag_offer) {
2288 data_offer_destroy(input->drag_offer);
2289 input->drag_offer = NULL;
2290 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002291}
2292
2293static void
2294data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002295 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002296{
2297 struct input *input = data;
2298 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002299 float x = wl_fixed_to_double(x_w);
2300 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002301 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002302
2303 input->sx = x;
2304 input->sy = y;
2305
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002306 if (input->drag_offer)
2307 types_data = input->drag_offer->types.data;
2308 else
2309 types_data = NULL;
2310
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002311 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002312 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002313 window->user_data);
2314}
2315
2316static void
2317data_device_drop(void *data, struct wl_data_device *data_device)
2318{
2319 struct input *input = data;
2320 struct window *window = input->pointer_focus;
2321
2322 if (window->drop_handler)
2323 window->drop_handler(window, input,
2324 input->sx, input->sy, window->user_data);
2325}
2326
2327static void
2328data_device_selection(void *data,
2329 struct wl_data_device *wl_data_device,
2330 struct wl_data_offer *offer)
2331{
2332 struct input *input = data;
2333 char **p;
2334
2335 if (input->selection_offer)
2336 data_offer_destroy(input->selection_offer);
2337
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002338 if (offer) {
2339 input->selection_offer = wl_data_offer_get_user_data(offer);
2340 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2341 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002342 } else {
2343 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002344 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002345}
2346
2347static const struct wl_data_device_listener data_device_listener = {
2348 data_device_data_offer,
2349 data_device_enter,
2350 data_device_leave,
2351 data_device_motion,
2352 data_device_drop,
2353 data_device_selection
2354};
2355
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002356static void
2357input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002358{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002359 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002360 struct wl_cursor *cursor;
2361 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002362
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002363 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002364 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002365 return;
2366
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002367 if (index >= (int) cursor->image_count) {
2368 fprintf(stderr, "cursor index out of range\n");
2369 return;
2370 }
2371
2372 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002373 buffer = wl_cursor_image_get_buffer(image);
2374 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002375 return;
2376
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002377 wl_pointer_set_cursor(input->pointer, input->display->serial,
2378 input->pointer_surface,
2379 image->hotspot_x, image->hotspot_y);
2380 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2381 wl_surface_damage(input->pointer_surface, 0, 0,
2382 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002383}
2384
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002385static const struct wl_callback_listener pointer_surface_listener;
2386
2387static void
2388pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2389 uint32_t time)
2390{
2391 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002392 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002393 int i;
2394
2395 if (callback) {
2396 assert(callback == input->cursor_frame_cb);
2397 wl_callback_destroy(callback);
2398 input->cursor_frame_cb = NULL;
2399 }
2400
2401 if (input->current_cursor == CURSOR_UNSET)
2402 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002403 cursor = input->display->cursors[input->current_cursor];
2404 if (!cursor)
2405 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002406
2407 /* FIXME We don't have the current time on the first call so we set
2408 * the animation start to the time of the first frame callback. */
2409 if (time == 0)
2410 input->cursor_anim_start = 0;
2411 else if (input->cursor_anim_start == 0)
2412 input->cursor_anim_start = time;
2413
2414 if (time == 0 || input->cursor_anim_start == 0)
2415 i = 0;
2416 else
2417 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2418
2419 input_set_pointer_image_index(input, i);
2420
2421 if (cursor->image_count == 1)
2422 return;
2423
2424 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2425 wl_callback_add_listener(input->cursor_frame_cb,
2426 &pointer_surface_listener, input);
2427}
2428
2429static const struct wl_callback_listener pointer_surface_listener = {
2430 pointer_surface_frame_callback
2431};
2432
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002433void
2434input_set_pointer_image(struct input *input, int pointer)
2435{
2436 if (pointer == input->current_cursor)
2437 return;
2438
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002439 input->current_cursor = pointer;
2440 if (!input->cursor_frame_cb)
2441 pointer_surface_frame_callback(input, NULL, 0);
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002442}
2443
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002444struct wl_data_device *
2445input_get_data_device(struct input *input)
2446{
2447 return input->data_device;
2448}
2449
2450void
2451input_set_selection(struct input *input,
2452 struct wl_data_source *source, uint32_t time)
2453{
2454 wl_data_device_set_selection(input->data_device, source, time);
2455}
2456
2457void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002458input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002459{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002460 wl_data_offer_accept(input->drag_offer->offer,
2461 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002462}
2463
2464static void
2465offer_io_func(struct task *task, uint32_t events)
2466{
2467 struct data_offer *offer =
2468 container_of(task, struct data_offer, io_task);
2469 unsigned int len;
2470 char buffer[4096];
2471
2472 len = read(offer->fd, buffer, sizeof buffer);
2473 offer->func(buffer, len,
2474 offer->x, offer->y, offer->user_data);
2475
2476 if (len == 0) {
2477 close(offer->fd);
2478 data_offer_destroy(offer);
2479 }
2480}
2481
2482static void
2483data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2484 data_func_t func, void *user_data)
2485{
2486 int p[2];
2487
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002488 if (pipe2(p, O_CLOEXEC) == -1)
2489 return;
2490
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002491 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2492 close(p[1]);
2493
2494 offer->io_task.run = offer_io_func;
2495 offer->fd = p[0];
2496 offer->func = func;
2497 offer->refcount++;
2498 offer->user_data = user_data;
2499
2500 display_watch_fd(offer->input->display,
2501 offer->fd, EPOLLIN, &offer->io_task);
2502}
2503
2504void
2505input_receive_drag_data(struct input *input, const char *mime_type,
2506 data_func_t func, void *data)
2507{
2508 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2509 input->drag_offer->x = input->sx;
2510 input->drag_offer->y = input->sy;
2511}
2512
2513int
2514input_receive_selection_data(struct input *input, const char *mime_type,
2515 data_func_t func, void *data)
2516{
2517 char **p;
2518
2519 if (input->selection_offer == NULL)
2520 return -1;
2521
2522 for (p = input->selection_offer->types.data; *p; p++)
2523 if (strcmp(mime_type, *p) == 0)
2524 break;
2525
2526 if (*p == NULL)
2527 return -1;
2528
2529 data_offer_receive_data(input->selection_offer,
2530 mime_type, func, data);
2531 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002532}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002533
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002534int
2535input_receive_selection_data_to_fd(struct input *input,
2536 const char *mime_type, int fd)
2537{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002538 if (input->selection_offer)
2539 wl_data_offer_receive(input->selection_offer->offer,
2540 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002541
2542 return 0;
2543}
2544
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002545void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002546window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002547{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002548 if (!window->shell_surface)
2549 return;
2550
Daniel Stone37816df2012-05-16 18:45:18 +01002551 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002552}
2553
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002554static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002555idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002556{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002557 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002558
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002559 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002560 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002561 widget_set_allocation(widget,
2562 window->pending_allocation.x,
2563 window->pending_allocation.y,
2564 window->pending_allocation.width,
2565 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002566
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002567 if (window->input_region) {
2568 wl_region_destroy(window->input_region);
2569 window->input_region = NULL;
2570 }
2571
2572 if (window->opaque_region) {
2573 wl_region_destroy(window->opaque_region);
2574 window->opaque_region = NULL;
2575 }
2576
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002577 if (widget->resize_handler)
2578 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002579 widget->allocation.width,
2580 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002581 widget->user_data);
2582
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002583 if (window->allocation.width != widget->allocation.width ||
2584 window->allocation.height != widget->allocation.height) {
2585 window->allocation = widget->allocation;
2586 window_schedule_redraw(window);
2587 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002588}
2589
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002590void
2591window_schedule_resize(struct window *window, int width, int height)
2592{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002593 window->pending_allocation.x = 0;
2594 window->pending_allocation.y = 0;
2595 window->pending_allocation.width = width;
2596 window->pending_allocation.height = height;
2597
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002598 window->resize_needed = 1;
2599 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002600}
2601
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002602void
2603widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2604{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002605 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002606}
2607
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002608static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002609handle_ping(void *data, struct wl_shell_surface *shell_surface,
2610 uint32_t serial)
2611{
2612 wl_shell_surface_pong(shell_surface, serial);
2613}
2614
2615static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002616handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002617 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002618{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002619 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002620
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002621 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002622 return;
2623
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002624 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002625 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002626}
2627
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002628static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002629menu_destroy(struct menu *menu)
2630{
2631 widget_destroy(menu->widget);
2632 window_destroy(menu->window);
2633 free(menu);
2634}
2635
2636static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002637handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2638{
2639 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002640 struct menu *menu = window->widget->user_data;
2641
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002642 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002643 * device. Or just use wl_callback. And this really needs to
2644 * be a window vfunc that the menu can set. And we need the
2645 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002646
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002647 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002648 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002649 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002650}
2651
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002652static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002653 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002654 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002655 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002656};
2657
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002658void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002659window_get_allocation(struct window *window,
2660 struct rectangle *allocation)
2661{
2662 *allocation = window->allocation;
2663}
2664
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002665static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002666widget_redraw(struct widget *widget)
2667{
2668 struct widget *child;
2669
2670 if (widget->redraw_handler)
2671 widget->redraw_handler(widget, widget->user_data);
2672 wl_list_for_each(child, &widget->child_list, link)
2673 widget_redraw(child);
2674}
2675
2676static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002677frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2678{
2679 struct window *window = data;
2680
2681 wl_callback_destroy(callback);
2682 window->redraw_scheduled = 0;
2683 if (window->redraw_needed)
2684 window_schedule_redraw(window);
2685}
2686
2687static const struct wl_callback_listener listener = {
2688 frame_callback
2689};
2690
2691static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002692idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002693{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002694 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002695 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002696
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002697 if (window->resize_needed)
2698 idle_resize(window);
2699
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002700 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002701 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002702 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002703 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002704 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002705
2706 callback = wl_surface_frame(window->surface);
2707 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002708}
2709
2710void
2711window_schedule_redraw(struct window *window)
2712{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002713 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002714 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002715 window->redraw_task.run = idle_redraw;
2716 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002717 window->redraw_scheduled = 1;
2718 }
2719}
2720
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002721void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002722window_set_custom(struct window *window)
2723{
2724 window->type = TYPE_CUSTOM;
2725}
2726
2727void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002728window_set_fullscreen(struct window *window, int fullscreen)
2729{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002730 if (!window->display->shell)
2731 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002732
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002733 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002734 return;
2735
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002736 if (fullscreen) {
2737 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002738 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002739 wl_shell_surface_set_fullscreen(window->shell_surface,
2740 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2741 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002742 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002743 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002744 wl_shell_surface_set_toplevel(window->shell_surface);
2745 window_schedule_resize(window,
2746 window->saved_allocation.width,
2747 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002748 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002749}
2750
2751void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002752window_set_maximized(struct window *window, int maximized)
2753{
2754 if (!window->display->shell)
2755 return;
2756
2757 if ((window->type == TYPE_MAXIMIZED) == maximized)
2758 return;
2759
2760 if (window->type == TYPE_TOPLEVEL) {
2761 window->saved_allocation = window->allocation;
2762 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2763 window->type = TYPE_MAXIMIZED;
2764 } else {
2765 wl_shell_surface_set_toplevel(window->shell_surface);
2766 window->type = TYPE_TOPLEVEL;
2767 window_schedule_resize(window,
2768 window->saved_allocation.width,
2769 window->saved_allocation.height);
2770 }
2771}
2772
2773void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002774window_set_user_data(struct window *window, void *data)
2775{
2776 window->user_data = data;
2777}
2778
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002779void *
2780window_get_user_data(struct window *window)
2781{
2782 return window->user_data;
2783}
2784
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002785void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002786window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002787 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002788{
2789 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002790}
2791
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002792void
2793window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002794 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002795{
2796 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002797}
2798
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002799void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002800window_set_data_handler(struct window *window, window_data_handler_t handler)
2801{
2802 window->data_handler = handler;
2803}
2804
2805void
2806window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2807{
2808 window->drop_handler = handler;
2809}
2810
2811void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002812window_set_close_handler(struct window *window,
2813 window_close_handler_t handler)
2814{
2815 window->close_handler = handler;
2816}
2817
2818void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002819window_set_title(struct window *window, const char *title)
2820{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002821 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002822 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002823 if (window->shell_surface)
2824 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002825}
2826
2827const char *
2828window_get_title(struct window *window)
2829{
2830 return window->title;
2831}
2832
2833void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002834window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2835{
2836 struct text_cursor_position *text_cursor_position =
2837 window->display->text_cursor_position;
2838
Scott Moreau9295ce02012-06-01 12:46:10 -06002839 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002840 return;
2841
2842 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002843 window->surface,
2844 wl_fixed_from_int(x),
2845 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002846}
2847
2848void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002849window_damage(struct window *window, int32_t x, int32_t y,
2850 int32_t width, int32_t height)
2851{
2852 wl_surface_damage(window->surface, x, y, width, height);
2853}
2854
Casey Dahlin9074db52012-04-19 22:50:09 -04002855static void
2856surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002857 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002858{
Rob Bradford7507b572012-05-15 17:55:34 +01002859 struct window *window = data;
2860 struct output *output;
2861 struct output *output_found = NULL;
2862 struct window_output *window_output;
2863
2864 wl_list_for_each(output, &window->display->output_list, link) {
2865 if (output->output == wl_output) {
2866 output_found = output;
2867 break;
2868 }
2869 }
2870
2871 if (!output_found)
2872 return;
2873
2874 window_output = malloc (sizeof *window_output);
2875 window_output->output = output_found;
2876
2877 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002878}
2879
2880static void
2881surface_leave(void *data,
2882 struct wl_surface *wl_surface, struct wl_output *output)
2883{
Rob Bradford7507b572012-05-15 17:55:34 +01002884 struct window *window = data;
2885 struct window_output *window_output;
2886 struct window_output *window_output_found = NULL;
2887
2888 wl_list_for_each(window_output, &window->window_output_list, link) {
2889 if (window_output->output->output == output) {
2890 window_output_found = window_output;
2891 break;
2892 }
2893 }
2894
2895 if (window_output_found) {
2896 wl_list_remove(&window_output_found->link);
2897 free(window_output_found);
2898 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002899}
2900
2901static const struct wl_surface_listener surface_listener = {
2902 surface_enter,
2903 surface_leave
2904};
2905
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002906static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002907window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002908{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002909 struct window *window;
2910
2911 window = malloc(sizeof *window);
2912 if (window == NULL)
2913 return NULL;
2914
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002915 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002916 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002917 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002918 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002919 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002920 if (display->shell) {
2921 window->shell_surface =
2922 wl_shell_get_shell_surface(display->shell,
2923 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002924 if (window->title)
2925 wl_shell_surface_set_title(window->shell_surface,
2926 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002927 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002928 window->allocation.x = 0;
2929 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002930 window->allocation.width = 0;
2931 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002932 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002933 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002934 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002935 window->input_region = NULL;
2936 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002937
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002938 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002939#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002940 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002941#else
2942 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2943#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002944 else
2945 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002946
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002947 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002948 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002949 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002950
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002951 if (window->shell_surface) {
2952 wl_shell_surface_set_user_data(window->shell_surface, window);
2953 wl_shell_surface_add_listener(window->shell_surface,
2954 &shell_surface_listener, window);
2955 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002956
Rob Bradford7507b572012-05-15 17:55:34 +01002957 wl_list_init (&window->window_output_list);
2958
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002959 return window;
2960}
2961
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002962struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002963window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002964{
2965 struct window *window;
2966
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002967 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002968 if (!window)
2969 return NULL;
2970
2971 return window;
2972}
2973
2974struct window *
2975window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002976 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002977{
2978 struct window *window;
2979
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002980 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002981 if (!window)
2982 return NULL;
2983
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002984 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002985 window->x = x;
2986 window->y = y;
2987
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002988 if (display->shell)
2989 wl_shell_surface_set_transient(window->shell_surface,
2990 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002991 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002992
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002993 return window;
2994}
2995
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002996static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002997menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002998{
2999 int next;
3000
3001 next = (sy - 8) / 20;
3002 if (menu->current != next) {
3003 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003004 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003005 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003006}
3007
3008static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003009menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003010 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003011 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003012{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003013 struct menu *menu = data;
3014
3015 if (widget == menu->widget)
3016 menu_set_item(data, y);
3017
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003018 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003019}
3020
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003021static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003022menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003023 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003024{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003025 struct menu *menu = data;
3026
3027 if (widget == menu->widget)
3028 menu_set_item(data, y);
3029
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003030 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003031}
3032
3033static void
3034menu_leave_handler(struct widget *widget, struct input *input, void *data)
3035{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003036 struct menu *menu = data;
3037
3038 if (widget == menu->widget)
3039 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003040}
3041
3042static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003043menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003044 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003045 uint32_t button, enum wl_pointer_button_state state,
3046 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003047
3048{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003049 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003050
Daniel Stone4dbadb12012-05-30 16:31:51 +01003051 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3052 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003053 /* Either relase after press-drag-release or
3054 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003055 menu->func(menu->window->parent,
3056 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003057 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003058 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003059 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003060}
3061
3062static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003063menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003064{
3065 cairo_t *cr;
3066 const int32_t r = 3, margin = 3;
3067 struct menu *menu = data;
3068 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003069 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003070
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003071 cr = cairo_create(window->cairo_surface);
3072 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3073 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3074 cairo_paint(cr);
3075
3076 width = window->allocation.width;
3077 height = window->allocation.height;
3078 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003079
3080 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003081 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3082 cairo_fill(cr);
3083
3084 for (i = 0; i < menu->count; i++) {
3085 if (i == menu->current) {
3086 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3087 cairo_rectangle(cr, margin, i * 20 + margin,
3088 width - 2 * margin, 20);
3089 cairo_fill(cr);
3090 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3091 cairo_move_to(cr, 10, i * 20 + 16);
3092 cairo_show_text(cr, menu->entries[i]);
3093 } else {
3094 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3095 cairo_move_to(cr, 10, i * 20 + 16);
3096 cairo_show_text(cr, menu->entries[i]);
3097 }
3098 }
3099
3100 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003101}
3102
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003103void
3104window_show_menu(struct display *display,
3105 struct input *input, uint32_t time, struct window *parent,
3106 int32_t x, int32_t y,
3107 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003108{
3109 struct window *window;
3110 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003111 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003112
3113 menu = malloc(sizeof *menu);
3114 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003115 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003116
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003117 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003118 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003119 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003120
3121 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003122 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003123 menu->entries = entries;
3124 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003125 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003126 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003127 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003128 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003129 window->type = TYPE_MENU;
3130 window->x = x;
3131 window->y = y;
3132
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003133 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003134 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003135 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003136 window->parent->shell_surface,
3137 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003138
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003139 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003140 widget_set_enter_handler(menu->widget, menu_enter_handler);
3141 widget_set_leave_handler(menu->widget, menu_leave_handler);
3142 widget_set_motion_handler(menu->widget, menu_motion_handler);
3143 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003144
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003145 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003146 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003147}
3148
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003149void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003150window_set_buffer_type(struct window *window, enum window_buffer_type type)
3151{
3152 window->buffer_type = type;
3153}
3154
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003155
3156static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003157display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003158 struct wl_output *wl_output,
3159 int x, int y,
3160 int physical_width,
3161 int physical_height,
3162 int subpixel,
3163 const char *make,
3164 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003165{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003166 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003167
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003168 output->allocation.x = x;
3169 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003170}
3171
3172static void
3173display_handle_mode(void *data,
3174 struct wl_output *wl_output,
3175 uint32_t flags,
3176 int width,
3177 int height,
3178 int refresh)
3179{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003180 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003181 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003182
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003183 if (flags & WL_OUTPUT_MODE_CURRENT) {
3184 output->allocation.width = width;
3185 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003186 if (display->output_configure_handler)
3187 (*display->output_configure_handler)(
3188 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003189 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003190}
3191
3192static const struct wl_output_listener output_listener = {
3193 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003194 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003195};
3196
3197static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003198display_add_output(struct display *d, uint32_t id)
3199{
3200 struct output *output;
3201
3202 output = malloc(sizeof *output);
3203 if (output == NULL)
3204 return;
3205
3206 memset(output, 0, sizeof *output);
3207 output->display = d;
3208 output->output =
3209 wl_display_bind(d->display, id, &wl_output_interface);
3210 wl_list_insert(d->output_list.prev, &output->link);
3211
3212 wl_output_add_listener(output->output, &output_listener, output);
3213}
3214
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003215static void
3216output_destroy(struct output *output)
3217{
3218 if (output->destroy_handler)
3219 (*output->destroy_handler)(output, output->user_data);
3220
3221 wl_output_destroy(output->output);
3222 wl_list_remove(&output->link);
3223 free(output);
3224}
3225
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003226void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003227display_set_output_configure_handler(struct display *display,
3228 display_output_handler_t handler)
3229{
3230 struct output *output;
3231
3232 display->output_configure_handler = handler;
3233 if (!handler)
3234 return;
3235
3236 wl_list_for_each(output, &display->output_list, link)
3237 (*display->output_configure_handler)(output,
3238 display->user_data);
3239}
3240
3241void
3242output_set_user_data(struct output *output, void *data)
3243{
3244 output->user_data = data;
3245}
3246
3247void *
3248output_get_user_data(struct output *output)
3249{
3250 return output->user_data;
3251}
3252
3253void
3254output_set_destroy_handler(struct output *output,
3255 display_output_handler_t handler)
3256{
3257 output->destroy_handler = handler;
3258 /* FIXME: implement this, once we have way to remove outputs */
3259}
3260
3261void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003262output_get_allocation(struct output *output, struct rectangle *allocation)
3263{
3264 *allocation = output->allocation;
3265}
3266
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003267struct wl_output *
3268output_get_wl_output(struct output *output)
3269{
3270 return output->output;
3271}
3272
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003273static void
Daniel Stone97f68542012-05-30 16:32:01 +01003274fini_xkb(struct input *input)
3275{
3276 xkb_state_unref(input->xkb.state);
3277 xkb_map_unref(input->xkb.keymap);
3278}
3279
3280static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003281display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003282{
3283 struct input *input;
3284
3285 input = malloc(sizeof *input);
3286 if (input == NULL)
3287 return;
3288
3289 memset(input, 0, sizeof *input);
3290 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003291 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003292 input->pointer_focus = NULL;
3293 input->keyboard_focus = NULL;
3294 wl_list_insert(d->input_list.prev, &input->link);
3295
Daniel Stone37816df2012-05-16 18:45:18 +01003296 wl_seat_add_listener(input->seat, &seat_listener, input);
3297 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003298
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003299 input->data_device =
3300 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003301 input->seat);
3302 wl_data_device_add_listener(input->data_device, &data_device_listener,
3303 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003304
3305 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003306
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003307 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3308 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003309 input->repeat_task.run = keyboard_repeat_func;
3310 display_watch_fd(d, input->repeat_timer_fd,
3311 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003312}
3313
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003314static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003315input_destroy(struct input *input)
3316{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003317 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003318 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003319
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003320 if (input->xkb.state)
3321 xkb_state_unref(input->xkb.state);
3322 if (input->xkb.keymap)
3323 xkb_map_unref(input->xkb.keymap);
3324
Pekka Paalanene1207c72011-12-16 12:02:09 +02003325 if (input->drag_offer)
3326 data_offer_destroy(input->drag_offer);
3327
3328 if (input->selection_offer)
3329 data_offer_destroy(input->selection_offer);
3330
3331 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003332 fini_xkb(input);
3333
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003334 wl_surface_destroy(input->pointer_surface);
3335
Pekka Paalanene1207c72011-12-16 12:02:09 +02003336 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003337 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003338 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003339 free(input);
3340}
3341
3342static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003343display_handle_global(struct wl_display *display, uint32_t id,
3344 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003345{
3346 struct display *d = data;
3347
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003348 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003349 d->compositor =
3350 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003351 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003352 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003353 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003354 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003355 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003356 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003357 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003358 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003359 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3360 d->data_device_manager =
3361 wl_display_bind(display, id,
3362 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003363 } else if (strcmp(interface, "text_cursor_position") == 0) {
3364 d->text_cursor_position =
3365 wl_display_bind(display, id,
3366 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003367 }
3368}
3369
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003370#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003371static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003372init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003373{
3374 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003375 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003376
Rob Clark6396ed32012-03-11 19:48:41 -05003377#ifdef USE_CAIRO_GLESV2
3378# define GL_BIT EGL_OPENGL_ES2_BIT
3379#else
3380# define GL_BIT EGL_OPENGL_BIT
3381#endif
3382
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003383 static const EGLint argb_cfg_attribs[] = {
3384 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003385 EGL_RED_SIZE, 1,
3386 EGL_GREEN_SIZE, 1,
3387 EGL_BLUE_SIZE, 1,
3388 EGL_ALPHA_SIZE, 1,
3389 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003390 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003391 EGL_NONE
3392 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003393
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003394#ifdef USE_CAIRO_GLESV2
3395 static const EGLint context_attribs[] = {
3396 EGL_CONTEXT_CLIENT_VERSION, 2,
3397 EGL_NONE
3398 };
3399 EGLint api = EGL_OPENGL_ES_API;
3400#else
3401 EGLint *context_attribs = NULL;
3402 EGLint api = EGL_OPENGL_API;
3403#endif
3404
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003405 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003406 if (!eglInitialize(d->dpy, &major, &minor)) {
3407 fprintf(stderr, "failed to initialize display\n");
3408 return -1;
3409 }
3410
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003411 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003412 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3413 return -1;
3414 }
3415
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003416 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3417 &d->argb_config, 1, &n) || n != 1) {
3418 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003419 return -1;
3420 }
3421
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003422 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003423 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003424 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003425 fprintf(stderr, "failed to create context\n");
3426 return -1;
3427 }
3428
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003429 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003430 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003431 return -1;
3432 }
3433
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003434#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003435 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3436 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3437 fprintf(stderr, "failed to get cairo egl argb device\n");
3438 return -1;
3439 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003440#endif
3441
3442 return 0;
3443}
3444
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003445static void
3446fini_egl(struct display *display)
3447{
3448#ifdef HAVE_CAIRO_EGL
3449 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003450#endif
3451
3452 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3453 EGL_NO_CONTEXT);
3454
3455 eglTerminate(display->dpy);
3456 eglReleaseThread();
3457}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003458#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003459
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003460static int
3461event_mask_update(uint32_t mask, void *data)
3462{
3463 struct display *d = data;
3464
3465 d->mask = mask;
3466
3467 return 0;
3468}
3469
3470static void
3471handle_display_data(struct task *task, uint32_t events)
3472{
3473 struct display *display =
3474 container_of(task, struct display, display_task);
3475
3476 wl_display_iterate(display->display, display->mask);
3477}
3478
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003479struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003480display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003481{
3482 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003483
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003484 argc = parse_options(xkb_options,
3485 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003486
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003487 d = malloc(sizeof *d);
3488 if (d == NULL)
3489 return NULL;
3490
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003491 memset(d, 0, sizeof *d);
3492
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003493 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003494 if (d->display == NULL) {
3495 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003496 return NULL;
3497 }
3498
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003499 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003500 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3501 d->display_task.run = handle_display_data;
3502 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3503
3504 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003505 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003506 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003507
Daniel Stone97f68542012-05-30 16:32:01 +01003508 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003509 if (d->xkb_context == NULL) {
3510 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003511 return NULL;
3512 }
3513
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003514 /* Set up listener so we'll catch all events. */
3515 wl_display_add_global_listener(d->display,
3516 display_handle_global, d);
3517
3518 /* Process connection events. */
3519 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003520#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003521 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003522 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003523#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003524
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003525 d->image_target_texture_2d =
3526 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3527 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3528 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3529
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003530 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003531
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003532 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003533
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003534 wl_list_init(&d->window_list);
3535
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003536 return d;
3537}
3538
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003539static void
3540display_destroy_outputs(struct display *display)
3541{
3542 struct output *tmp;
3543 struct output *output;
3544
3545 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3546 output_destroy(output);
3547}
3548
Pekka Paalanene1207c72011-12-16 12:02:09 +02003549static void
3550display_destroy_inputs(struct display *display)
3551{
3552 struct input *tmp;
3553 struct input *input;
3554
3555 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3556 input_destroy(input);
3557}
3558
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003559void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003560display_destroy(struct display *display)
3561{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003562 if (!wl_list_empty(&display->window_list))
3563 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3564
3565 if (!wl_list_empty(&display->deferred_list))
3566 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3567
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003568 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003569 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003570
Daniel Stone97f68542012-05-30 16:32:01 +01003571 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003572
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003573 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003574 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003575
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003576#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003577 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003578#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003579
Pekka Paalanenc2052982011-12-16 11:41:32 +02003580 if (display->shell)
3581 wl_shell_destroy(display->shell);
3582
3583 if (display->shm)
3584 wl_shm_destroy(display->shm);
3585
3586 if (display->data_device_manager)
3587 wl_data_device_manager_destroy(display->data_device_manager);
3588
3589 wl_compositor_destroy(display->compositor);
3590
3591 close(display->epoll_fd);
3592
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003593 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003594 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003595 free(display);
3596}
3597
3598void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003599display_set_user_data(struct display *display, void *data)
3600{
3601 display->user_data = data;
3602}
3603
3604void *
3605display_get_user_data(struct display *display)
3606{
3607 return display->user_data;
3608}
3609
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003610struct wl_display *
3611display_get_display(struct display *display)
3612{
3613 return display->display;
3614}
3615
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003616struct output *
3617display_get_output(struct display *display)
3618{
3619 return container_of(display->output_list.next, struct output, link);
3620}
3621
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003622struct wl_compositor *
3623display_get_compositor(struct display *display)
3624{
3625 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003626}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003627
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003628uint32_t
3629display_get_serial(struct display *display)
3630{
3631 return display->serial;
3632}
3633
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003634EGLDisplay
3635display_get_egl_display(struct display *d)
3636{
3637 return d->dpy;
3638}
3639
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003640struct wl_data_source *
3641display_create_data_source(struct display *display)
3642{
3643 return wl_data_device_manager_create_data_source(display->data_device_manager);
3644}
3645
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003646EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003647display_get_argb_egl_config(struct display *d)
3648{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003649 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003650}
3651
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003652struct wl_shell *
3653display_get_shell(struct display *display)
3654{
3655 return display->shell;
3656}
3657
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003658int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003659display_acquire_window_surface(struct display *display,
3660 struct window *window,
3661 EGLContext ctx)
3662{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003663#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003664 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003665 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003666
3667 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003668 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003669 device = cairo_surface_get_device(window->cairo_surface);
3670 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003671 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003672
Benjamin Franzke0c991632011-09-27 21:57:31 +02003673 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003674 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003675 ctx = display->argb_ctx;
3676 else
3677 assert(0);
3678 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003679
3680 data = cairo_surface_get_user_data(window->cairo_surface,
3681 &surface_data_key);
3682
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003683 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003684 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003685 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3686 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003687
3688 return 0;
3689#else
3690 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003691#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003692}
3693
3694void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003695display_release_window_surface(struct display *display,
3696 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003697{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003698#ifdef HAVE_CAIRO_EGL
3699 cairo_device_t *device;
3700
3701 device = cairo_surface_get_device(window->cairo_surface);
3702 if (!device)
3703 return;
3704
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003705 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003706 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003707 cairo_device_release(device);
3708#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003709}
3710
3711void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003712display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003713{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003714 wl_list_insert(&display->deferred_list, &task->link);
3715}
3716
3717void
3718display_watch_fd(struct display *display,
3719 int fd, uint32_t events, struct task *task)
3720{
3721 struct epoll_event ep;
3722
3723 ep.events = events;
3724 ep.data.ptr = task;
3725 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3726}
3727
3728void
3729display_run(struct display *display)
3730{
3731 struct task *task;
3732 struct epoll_event ep[16];
3733 int i, count;
3734
Pekka Paalanen826d7952011-12-15 10:14:07 +02003735 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003736 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003737 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003738
3739 while (!wl_list_empty(&display->deferred_list)) {
3740 task = container_of(display->deferred_list.next,
3741 struct task, link);
3742 wl_list_remove(&task->link);
3743 task->run(task, 0);
3744 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003745
3746 if (!display->running)
3747 break;
3748
3749 wl_display_flush(display->display);
3750
3751 count = epoll_wait(display->epoll_fd,
3752 ep, ARRAY_LENGTH(ep), -1);
3753 for (i = 0; i < count; i++) {
3754 task = ep[i].data.ptr;
3755 task->run(task, ep[i].events);
3756 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003757 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003758}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003759
3760void
3761display_exit(struct display *display)
3762{
3763 display->running = 0;
3764}