blob: 8f3c94239f614a2c7a3f5afdc16375460e1fce59 [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øgsberg11f600d2012-06-22 10:52:58 -0400197 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400198 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400199 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400200
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500201 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500202 struct widget *grab;
203 uint32_t grab_button;
204
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400205 struct wl_data_device *data_device;
206 struct data_offer *drag_offer;
207 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100208
209 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100210 struct xkb_keymap *keymap;
211 struct xkb_state *state;
212 xkb_mod_mask_t control_mask;
213 xkb_mod_mask_t alt_mask;
214 xkb_mod_mask_t shift_mask;
215 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400216
217 struct task repeat_task;
218 int repeat_timer_fd;
219 uint32_t repeat_sym;
220 uint32_t repeat_key;
221 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400222};
223
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500224struct output {
225 struct display *display;
226 struct wl_output *output;
227 struct rectangle allocation;
228 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200229
230 display_output_handler_t destroy_handler;
231 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500232};
233
Martin Minarik1998b152012-05-10 02:04:35 +0200234enum frame_button_action {
235 FRAME_BUTTON_NULL = 0,
236 FRAME_BUTTON_ICON = 1,
237 FRAME_BUTTON_CLOSE = 2,
238 FRAME_BUTTON_MINIMIZE = 3,
239 FRAME_BUTTON_MAXIMIZE = 4,
240};
241
242enum frame_button_pointer {
243 FRAME_BUTTON_DEFAULT = 0,
244 FRAME_BUTTON_OVER = 1,
245 FRAME_BUTTON_ACTIVE = 2,
246};
247
248enum frame_button_align {
249 FRAME_BUTTON_RIGHT = 0,
250 FRAME_BUTTON_LEFT = 1,
251};
252
253enum frame_button_decoration {
254 FRAME_BUTTON_NONE = 0,
255 FRAME_BUTTON_FANCY = 1,
256};
257
258struct frame_button {
259 struct widget *widget;
260 struct frame *frame;
261 cairo_surface_t *icon;
262 enum frame_button_action type;
263 enum frame_button_pointer state;
264 struct wl_list link; /* buttons_list */
265 enum frame_button_align align;
266 enum frame_button_decoration decoration;
267};
268
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500269struct frame {
270 struct widget *widget;
271 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200272 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500273};
274
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500275struct menu {
276 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500277 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500278 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500279 const char **entries;
280 uint32_t time;
281 int current;
282 int count;
283 menu_func_t func;
284};
285
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300286struct tooltip {
287 struct widget *parent;
288 struct window *window;
289 struct widget *widget;
290 char *entry;
291 struct task tooltip_task;
292 int tooltip_fd;
293 float x, y;
294};
295
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700296struct shm_pool {
297 struct wl_shm_pool *pool;
298 size_t size;
299 size_t used;
300 void *data;
301};
302
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400303enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300304 CURSOR_DEFAULT = 100,
305 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400306};
307
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500308enum window_location {
309 WINDOW_INTERIOR = 0,
310 WINDOW_RESIZING_TOP = 1,
311 WINDOW_RESIZING_BOTTOM = 2,
312 WINDOW_RESIZING_LEFT = 4,
313 WINDOW_RESIZING_TOP_LEFT = 5,
314 WINDOW_RESIZING_BOTTOM_LEFT = 6,
315 WINDOW_RESIZING_RIGHT = 8,
316 WINDOW_RESIZING_TOP_RIGHT = 9,
317 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
318 WINDOW_RESIZING_MASK = 15,
319 WINDOW_EXTERIOR = 16,
320 WINDOW_TITLEBAR = 17,
321 WINDOW_CLIENT_AREA = 18,
322};
323
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400324const char *option_xkb_layout = "us";
325const char *option_xkb_variant = "";
326const char *option_xkb_options = "";
327
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400328static const struct weston_option xkb_options[] = {
329 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
330 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
331 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400332};
333
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400334static const cairo_user_data_key_t surface_data_key;
335struct surface_data {
336 struct wl_buffer *buffer;
337};
338
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500339#define MULT(_d,c,a,t) \
340 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
341
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500342#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400343
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100344struct egl_window_surface_data {
345 struct display *display;
346 struct wl_surface *surface;
347 struct wl_egl_window *window;
348 EGLSurface surf;
349};
350
351static void
352egl_window_surface_data_destroy(void *p)
353{
354 struct egl_window_surface_data *data = p;
355 struct display *d = data->display;
356
357 eglDestroySurface(d->dpy, data->surf);
358 wl_egl_window_destroy(data->window);
359 data->surface = NULL;
360
361 free(p);
362}
363
364static cairo_surface_t *
365display_create_egl_window_surface(struct display *display,
366 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400367 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100368 struct rectangle *rectangle)
369{
370 cairo_surface_t *cairo_surface;
371 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400372 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200373 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100374
375 data = malloc(sizeof *data);
376 if (data == NULL)
377 return NULL;
378
379 data->display = display;
380 data->surface = surface;
381
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500382 config = display->argb_config;
383 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400384
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400385 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100386 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400387 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100388
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400389 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500390 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100391
Benjamin Franzke0c991632011-09-27 21:57:31 +0200392 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100393 data->surf,
394 rectangle->width,
395 rectangle->height);
396
397 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
398 data, egl_window_surface_data_destroy);
399
400 return cairo_surface;
401}
402
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400403#endif
404
405struct wl_buffer *
406display_get_buffer_for_surface(struct display *display,
407 cairo_surface_t *surface)
408{
409 struct surface_data *data;
410
411 data = cairo_surface_get_user_data (surface, &surface_data_key);
412
413 return data->buffer;
414}
415
416struct shm_surface_data {
417 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700418 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419};
420
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500421static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700422shm_pool_destroy(struct shm_pool *pool);
423
424static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400425shm_surface_data_destroy(void *p)
426{
427 struct shm_surface_data *data = p;
428
429 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700430 if (data->pool)
431 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300432
433 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400434}
435
Kristian Høgsberg16626282012-04-03 11:21:27 -0400436static struct wl_shm_pool *
437make_shm_pool(struct display *display, int size, void **data)
438{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400439 struct wl_shm_pool *pool;
440 int fd;
441
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300442 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400443 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300444 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
445 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400446 return NULL;
447 }
448
449 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400450 if (*data == MAP_FAILED) {
451 fprintf(stderr, "mmap failed: %m\n");
452 close(fd);
453 return NULL;
454 }
455
456 pool = wl_shm_create_pool(display->shm, fd, size);
457
458 close(fd);
459
460 return pool;
461}
462
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700463static struct shm_pool *
464shm_pool_create(struct display *display, size_t size)
465{
466 struct shm_pool *pool = malloc(sizeof *pool);
467
468 if (!pool)
469 return NULL;
470
471 pool->pool = make_shm_pool(display, size, &pool->data);
472 if (!pool->pool) {
473 free(pool);
474 return NULL;
475 }
476
477 pool->size = size;
478 pool->used = 0;
479
480 return pool;
481}
482
483static void *
484shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
485{
486 if (pool->used + size > pool->size)
487 return NULL;
488
489 *offset = pool->used;
490 pool->used += size;
491
492 return (char *) pool->data + *offset;
493}
494
495/* destroy the pool. this does not unmap the memory though */
496static void
497shm_pool_destroy(struct shm_pool *pool)
498{
499 munmap(pool->data, pool->size);
500 wl_shm_pool_destroy(pool->pool);
501 free(pool);
502}
503
504/* Start allocating from the beginning of the pool again */
505static void
506shm_pool_reset(struct shm_pool *pool)
507{
508 pool->used = 0;
509}
510
511static int
512data_length_for_shm_surface(struct rectangle *rect)
513{
514 int stride;
515
516 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
517 rect->width);
518 return stride * rect->height;
519}
520
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500521static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700522display_create_shm_surface_from_pool(struct display *display,
523 struct rectangle *rectangle,
524 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400525{
526 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400527 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400528 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700529 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400530 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400531
532 data = malloc(sizeof *data);
533 if (data == NULL)
534 return NULL;
535
536 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
537 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700538 length = stride * rectangle->height;
539 data->pool = NULL;
540 map = shm_pool_allocate(pool, length, &offset);
541
542 if (!map) {
543 free(data);
544 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400545 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400547 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548 CAIRO_FORMAT_ARGB32,
549 rectangle->width,
550 rectangle->height,
551 stride);
552
553 cairo_surface_set_user_data (surface, &surface_data_key,
554 data, shm_surface_data_destroy);
555
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400556 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500557 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400558 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500559 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400560
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700561 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400562 rectangle->width,
563 rectangle->height,
564 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400565
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700566 return surface;
567}
568
569static cairo_surface_t *
570display_create_shm_surface(struct display *display,
571 struct rectangle *rectangle, uint32_t flags,
572 struct window *window)
573{
574 struct shm_surface_data *data;
575 struct shm_pool *pool;
576 cairo_surface_t *surface;
577
578 if (window && window->pool) {
579 shm_pool_reset(window->pool);
580 surface = display_create_shm_surface_from_pool(display,
581 rectangle,
582 flags,
583 window->pool);
584 if (surface)
585 return surface;
586 }
587
588 pool = shm_pool_create(display,
589 data_length_for_shm_surface(rectangle));
590 if (!pool)
591 return NULL;
592
593 surface =
594 display_create_shm_surface_from_pool(display, rectangle,
595 flags, pool);
596
597 if (!surface) {
598 shm_pool_destroy(pool);
599 return NULL;
600 }
601
602 /* make sure we destroy the pool when the surface is destroyed */
603 data = cairo_surface_get_user_data(surface, &surface_data_key);
604 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400605
606 return surface;
607}
608
nobled7b87cb02011-02-01 18:51:47 +0000609static int
610check_size(struct rectangle *rect)
611{
612 if (rect->width && rect->height)
613 return 0;
614
615 fprintf(stderr, "tried to create surface of "
616 "width: %d, height: %d\n", rect->width, rect->height);
617 return -1;
618}
619
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400620cairo_surface_t *
621display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100622 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400623 struct rectangle *rectangle,
624 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400625{
nobled7b87cb02011-02-01 18:51:47 +0000626 if (check_size(rectangle) < 0)
627 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500628#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300629 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400630 return display_create_egl_window_surface(display,
631 surface,
632 flags,
633 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400635 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400636}
637
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300638static const char *cursors[] = {
639 "bottom_left_corner",
640 "bottom_right_corner",
641 "bottom_side",
642 "grabbing",
643 "left_ptr",
644 "left_side",
645 "right_side",
646 "top_left_corner",
647 "top_right_corner",
648 "top_side",
649 "xterm",
650 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400651 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300652};
653
654static void
655create_cursors(struct display *display)
656{
657 unsigned int i;
658
659 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
660 display->cursors =
661 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
662
663 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
664 display->cursors[i] =
665 wl_cursor_theme_get_cursor(display->cursor_theme,
666 cursors[i]);
667}
668
669static void
670destroy_cursors(struct display *display)
671{
672 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800673 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300674}
675
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300676struct wl_cursor_image *
677display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400678{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300679 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300680 wl_cursor_theme_get_cursor(display->cursor_theme,
681 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400682
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300683 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400684}
685
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400686static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200687window_get_resize_dx_dy(struct window *window, int *x, int *y)
688{
689 if (window->resize_edges & WINDOW_RESIZING_LEFT)
690 *x = window->server_allocation.width - window->allocation.width;
691 else
692 *x = 0;
693
694 if (window->resize_edges & WINDOW_RESIZING_TOP)
695 *y = window->server_allocation.height -
696 window->allocation.height;
697 else
698 *y = 0;
699
700 window->resize_edges = 0;
701}
702
703static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500704window_attach_surface(struct window *window)
705{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400706 struct display *display = window->display;
707 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000708#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100709 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000710#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500711 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100712
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400713 if (window->type == TYPE_NONE) {
714 window->type = TYPE_TOPLEVEL;
715 if (display->shell)
716 wl_shell_surface_set_toplevel(window->shell_surface);
717 }
718
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100719 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000720#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100721 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
722 data = cairo_surface_get_user_data(window->cairo_surface,
723 &surface_data_key);
724
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100725 cairo_gl_surface_swapbuffers(window->cairo_surface);
726 wl_egl_window_get_attached_size(data->window,
727 &window->server_allocation.width,
728 &window->server_allocation.height);
729 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000730#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 buffer =
733 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400734 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200736 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100737 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400738 wl_surface_damage(window->surface, 0, 0,
739 window->allocation.width,
740 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100741 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400742 cairo_surface_destroy(window->cairo_surface);
743 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100744 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000745 default:
746 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100747 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500748
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500749 if (window->input_region) {
750 wl_surface_set_input_region(window->surface,
751 window->input_region);
752 wl_region_destroy(window->input_region);
753 window->input_region = NULL;
754 }
755
756 if (window->opaque_region) {
757 wl_surface_set_opaque_region(window->surface,
758 window->opaque_region);
759 wl_region_destroy(window->opaque_region);
760 window->opaque_region = NULL;
761 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500762}
763
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500764void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400765window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500766{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400767 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100768 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500769}
770
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400771void
772window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400773{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500774 cairo_surface_reference(surface);
775
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400776 if (window->cairo_surface != NULL)
777 cairo_surface_destroy(window->cairo_surface);
778
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500779 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400780}
781
Benjamin Franzke22d54812011-07-16 19:50:32 +0000782#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100783static void
784window_resize_cairo_window_surface(struct window *window)
785{
786 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200787 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788
789 data = cairo_surface_get_user_data(window->cairo_surface,
790 &surface_data_key);
791
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200792 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100793 wl_egl_window_resize(data->window,
794 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200795 window->allocation.height,
796 x,y);
797
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798 cairo_gl_surface_set_size(window->cairo_surface,
799 window->allocation.width,
800 window->allocation.height);
801}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000802#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100803
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400804struct display *
805window_get_display(struct window *window)
806{
807 return window->display;
808}
809
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400810void
811window_create_surface(struct window *window)
812{
813 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400814 uint32_t flags = 0;
815
816 if (!window->transparent)
817 flags = SURFACE_OPAQUE;
818
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400819 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500820#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
822 if (window->cairo_surface) {
823 window_resize_cairo_window_surface(window);
824 return;
825 }
826 surface = display_create_surface(window->display,
827 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400828 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830#endif
831 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400832 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400833 &window->allocation,
834 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800836 default:
837 surface = NULL;
838 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400839 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400840
841 window_set_surface(window, surface);
842 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400843}
844
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200845static void frame_destroy(struct frame *frame);
846
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400847void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500848window_destroy(struct window *window)
849{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200850 struct display *display = window->display;
851 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100852 struct window_output *window_output;
853 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200854
855 if (window->redraw_scheduled)
856 wl_list_remove(&window->redraw_task.link);
857
858 wl_list_for_each(input, &display->input_list, link) {
859 if (input->pointer_focus == window)
860 input->pointer_focus = NULL;
861 if (input->keyboard_focus == window)
862 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500863 if (input->focus_widget &&
864 input->focus_widget->window == window)
865 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200866 }
867
Rob Bradford7507b572012-05-15 17:55:34 +0100868 wl_list_for_each_safe(window_output, window_output_tmp,
869 &window->window_output_list, link) {
870 free (window_output);
871 }
872
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500873 if (window->input_region)
874 wl_region_destroy(window->input_region);
875 if (window->opaque_region)
876 wl_region_destroy(window->opaque_region);
877
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200878 if (window->frame)
879 frame_destroy(window->frame);
880
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200881 if (window->shell_surface)
882 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500883 wl_surface_destroy(window->surface);
884 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200885
886 if (window->cairo_surface != NULL)
887 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200888
889 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500890 free(window);
891}
892
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500893static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500894widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400895{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500896 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400897
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500898 wl_list_for_each(child, &widget->child_list, link) {
899 target = widget_find_widget(child, x, y);
900 if (target)
901 return target;
902 }
903
904 if (widget->allocation.x <= x &&
905 x < widget->allocation.x + widget->allocation.width &&
906 widget->allocation.y <= y &&
907 y < widget->allocation.y + widget->allocation.height) {
908 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400909 }
910
911 return NULL;
912}
913
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500914static struct widget *
915widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400916{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500917 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400918
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500919 widget = malloc(sizeof *widget);
920 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500921 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500922 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500923 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500924 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500925 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300926 widget->tooltip = NULL;
927 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500928
929 return widget;
930}
931
932struct widget *
933window_add_widget(struct window *window, void *data)
934{
935 window->widget = widget_create(window, data);
936 wl_list_init(&window->widget->link);
937
938 return window->widget;
939}
940
941struct widget *
942widget_add_widget(struct widget *parent, void *data)
943{
944 struct widget *widget;
945
946 widget = widget_create(parent->window, data);
947 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400948
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500949 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400950}
951
952void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500953widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400954{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200955 struct display *display = widget->window->display;
956 struct input *input;
957
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300958 if (widget->tooltip) {
959 free(widget->tooltip);
960 widget->tooltip = NULL;
961 }
962
Pekka Paalanene156fb62012-01-19 13:51:38 +0200963 wl_list_for_each(input, &display->input_list, link) {
964 if (input->focus_widget == widget)
965 input->focus_widget = NULL;
966 }
967
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500968 wl_list_remove(&widget->link);
969 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400970}
971
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400972void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500973widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400974{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500975 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400976}
977
978void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500979widget_set_size(struct widget *widget, int32_t width, int32_t height)
980{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500981 widget->allocation.width = width;
982 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500983}
984
985void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500986widget_set_allocation(struct widget *widget,
987 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400988{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500989 widget->allocation.x = x;
990 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200991 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400992}
993
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500994void
995widget_set_transparent(struct widget *widget, int transparent)
996{
997 widget->opaque = !transparent;
998}
999
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001000void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001001widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001002{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001003 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001004}
1005
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001006void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001007widget_set_resize_handler(struct widget *widget,
1008 widget_resize_handler_t handler)
1009{
1010 widget->resize_handler = handler;
1011}
1012
1013void
1014widget_set_redraw_handler(struct widget *widget,
1015 widget_redraw_handler_t handler)
1016{
1017 widget->redraw_handler = handler;
1018}
1019
1020void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001021widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001022{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001023 widget->enter_handler = handler;
1024}
1025
1026void
1027widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1028{
1029 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001030}
1031
1032void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001033widget_set_motion_handler(struct widget *widget,
1034 widget_motion_handler_t handler)
1035{
1036 widget->motion_handler = handler;
1037}
1038
1039void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001040widget_set_button_handler(struct widget *widget,
1041 widget_button_handler_t handler)
1042{
1043 widget->button_handler = handler;
1044}
1045
1046void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001047widget_schedule_redraw(struct widget *widget)
1048{
1049 window_schedule_redraw(widget->window);
1050}
1051
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001052cairo_surface_t *
1053window_get_surface(struct window *window)
1054{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001055 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001056}
1057
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001058struct wl_surface *
1059window_get_wl_surface(struct window *window)
1060{
1061 return window->surface;
1062}
1063
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001064struct wl_shell_surface *
1065window_get_wl_shell_surface(struct window *window)
1066{
1067 return window->shell_surface;
1068}
1069
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001070static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001071tooltip_redraw_handler(struct widget *widget, void *data)
1072{
1073 cairo_t *cr;
1074 const int32_t r = 3;
1075 struct tooltip *tooltip = data;
1076 int32_t width, height;
1077 struct window *window = widget->window;
1078
1079 cr = cairo_create(window->cairo_surface);
1080 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1081 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1082 cairo_paint(cr);
1083
1084 width = window->allocation.width;
1085 height = window->allocation.height;
1086 rounded_rect(cr, 0, 0, width, height, r);
1087
1088 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1089 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1090 cairo_fill(cr);
1091
1092 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1093 cairo_move_to(cr, 10, 16);
1094 cairo_show_text(cr, tooltip->entry);
1095 cairo_destroy(cr);
1096}
1097
1098static cairo_text_extents_t
1099get_text_extents(struct tooltip *tooltip)
1100{
1101 struct window *window;
1102 cairo_t *cr;
1103 cairo_text_extents_t extents;
1104
1105 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1106 * created yet */
1107 window = tooltip->widget->window->parent;
1108 cr = cairo_create(window->cairo_surface);
1109 cairo_text_extents(cr, tooltip->entry, &extents);
1110 cairo_destroy(cr);
1111
1112 return extents;
1113}
1114
1115static int
1116window_create_tooltip(struct tooltip *tooltip)
1117{
1118 struct widget *parent = tooltip->parent;
1119 struct display *display = parent->window->display;
1120 struct window *window;
1121 const int offset_y = 27;
1122 const int margin = 3;
1123 cairo_text_extents_t extents;
1124
1125 if (tooltip->widget)
1126 return 0;
1127
1128 window = window_create_transient(display, parent->window, tooltip->x,
1129 tooltip->y + offset_y,
1130 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1131 if (!window)
1132 return -1;
1133
1134 tooltip->window = window;
1135 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1136
1137 extents = get_text_extents(tooltip);
1138 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1139 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1140
1141 return 0;
1142}
1143
1144void
1145widget_destroy_tooltip(struct widget *parent)
1146{
1147 struct tooltip *tooltip = parent->tooltip;
1148
1149 parent->tooltip_count = 0;
1150 if (!tooltip)
1151 return;
1152
1153 if (tooltip->widget) {
1154 widget_destroy(tooltip->widget);
1155 window_destroy(tooltip->window);
1156 tooltip->widget = NULL;
1157 tooltip->window = NULL;
1158 }
1159
1160 close(tooltip->tooltip_fd);
1161 free(tooltip->entry);
1162 free(tooltip);
1163 parent->tooltip = NULL;
1164}
1165
1166static void
1167tooltip_func(struct task *task, uint32_t events)
1168{
1169 struct tooltip *tooltip =
1170 container_of(task, struct tooltip, tooltip_task);
1171 uint64_t exp;
1172
1173 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1174 window_create_tooltip(tooltip);
1175}
1176
1177#define TOOLTIP_TIMEOUT 500
1178static int
1179tooltip_timer_reset(struct tooltip *tooltip)
1180{
1181 struct itimerspec its;
1182
1183 its.it_interval.tv_sec = 0;
1184 its.it_interval.tv_nsec = 0;
1185 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1186 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1187 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1188 fprintf(stderr, "could not set timerfd\n: %m");
1189 return -1;
1190 }
1191
1192 return 0;
1193}
1194
1195int
1196widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1197{
1198 struct tooltip *tooltip = parent->tooltip;
1199
1200 parent->tooltip_count++;
1201 if (tooltip) {
1202 tooltip->x = x;
1203 tooltip->y = y;
1204 tooltip_timer_reset(tooltip);
1205 return 0;
1206 }
1207
1208 /* the handler might be triggered too fast via input device motion, so
1209 * we need this check here to make sure tooltip is fully initialized */
1210 if (parent->tooltip_count > 1)
1211 return 0;
1212
1213 tooltip = malloc(sizeof *tooltip);
1214 if (!tooltip)
1215 return -1;
1216
1217 parent->tooltip = tooltip;
1218 tooltip->parent = parent;
1219 tooltip->widget = NULL;
1220 tooltip->window = NULL;
1221 tooltip->x = x;
1222 tooltip->y = y;
1223 tooltip->entry = strdup(entry);
1224 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1225 if (tooltip->tooltip_fd < 0) {
1226 fprintf(stderr, "could not create timerfd\n: %m");
1227 return -1;
1228 }
1229
1230 tooltip->tooltip_task.run = tooltip_func;
1231 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1232 EPOLLIN, &tooltip->tooltip_task);
1233 tooltip_timer_reset(tooltip);
1234
1235 return 0;
1236}
1237
1238static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001239frame_resize_handler(struct widget *widget,
1240 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001241{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242 struct frame *frame = data;
1243 struct widget *child = frame->child;
1244 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001245 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001246 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001247 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001248 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001250 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001252 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001253 decoration_width = (t->width + t->margin) * 2;
1254 decoration_height = t->width +
1255 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001256
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001257 allocation.x = t->width + t->margin;
1258 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001259 allocation.width = width - decoration_width;
1260 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001261
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001262 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001263
1264 wl_list_for_each(button, &frame->buttons_list, link)
1265 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001266 } else {
1267 decoration_width = 0;
1268 decoration_height = 0;
1269
1270 allocation.x = 0;
1271 allocation.y = 0;
1272 allocation.width = width;
1273 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001274 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001275
1276 wl_list_for_each(button, &frame->buttons_list, link)
1277 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001278 }
1279
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001280 widget_set_allocation(child, allocation.x, allocation.y,
1281 allocation.width, allocation.height);
1282
1283 if (child->resize_handler)
1284 child->resize_handler(child,
1285 allocation.width,
1286 allocation.height,
1287 child->user_data);
1288
Scott Moreauf7e498c2012-05-14 11:39:29 -06001289 width = child->allocation.width + decoration_width;
1290 height = child->allocation.height + decoration_height;
1291
1292 widget->window->input_region =
1293 wl_compositor_create_region(display->compositor);
1294 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001295 t->margin, t->margin,
1296 width - 2 * t->margin,
1297 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001298
1299 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001300
1301 if (child->opaque) {
1302 widget->window->opaque_region =
1303 wl_compositor_create_region(display->compositor);
1304 wl_region_add(widget->window->opaque_region,
1305 opaque_margin, opaque_margin,
1306 widget->allocation.width - 2 * opaque_margin,
1307 widget->allocation.height - 2 * opaque_margin);
1308 }
Martin Minarik1998b152012-05-10 02:04:35 +02001309
1310 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001311 x_r = frame->widget->allocation.width - t->width - t->margin;
1312 x_l = t->width + t->margin;
1313 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001314 wl_list_for_each(button, &frame->buttons_list, link) {
1315 const int button_padding = 4;
1316 w = cairo_image_surface_get_width(button->icon);
1317 h = cairo_image_surface_get_height(button->icon);
1318
1319 if (button->decoration == FRAME_BUTTON_FANCY)
1320 w += 10;
1321
1322 if (button->align == FRAME_BUTTON_LEFT) {
1323 widget_set_allocation(button->widget,
1324 x_l, y , w + 1, h + 1);
1325 x_l += w;
1326 x_l += button_padding;
1327 } else {
1328 x_r -= w;
1329 widget_set_allocation(button->widget,
1330 x_r, y , w + 1, h + 1);
1331 x_r -= button_padding;
1332 }
1333 }
1334}
1335
1336static int
1337frame_button_enter_handler(struct widget *widget,
1338 struct input *input, float x, float y, void *data)
1339{
1340 struct frame_button *frame_button = data;
1341
1342 widget_schedule_redraw(frame_button->widget);
1343 frame_button->state = FRAME_BUTTON_OVER;
1344
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001345 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001346}
1347
1348static void
1349frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1350{
1351 struct frame_button *frame_button = data;
1352
1353 widget_schedule_redraw(frame_button->widget);
1354 frame_button->state = FRAME_BUTTON_DEFAULT;
1355}
1356
1357static void
1358frame_button_button_handler(struct widget *widget,
1359 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001360 uint32_t button,
1361 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001362{
1363 struct frame_button *frame_button = data;
1364 struct window *window = widget->window;
1365
1366 if (button != BTN_LEFT)
1367 return;
1368
1369 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001370 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001371 frame_button->state = FRAME_BUTTON_ACTIVE;
1372 widget_schedule_redraw(frame_button->widget);
1373
1374 if (frame_button->type == FRAME_BUTTON_ICON)
1375 window_show_frame_menu(window, input, time);
1376 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001377 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001378 frame_button->state = FRAME_BUTTON_DEFAULT;
1379 widget_schedule_redraw(frame_button->widget);
1380 break;
1381 }
1382
1383 switch (frame_button->type) {
1384 case FRAME_BUTTON_CLOSE:
1385 if (window->close_handler)
1386 window->close_handler(window->parent,
1387 window->user_data);
1388 else
1389 display_exit(window->display);
1390 break;
1391 case FRAME_BUTTON_MINIMIZE:
1392 fprintf(stderr,"Minimize stub\n");
1393 break;
1394 case FRAME_BUTTON_MAXIMIZE:
1395 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1396 break;
1397 default:
1398 /* Unknown operation */
1399 break;
1400 }
1401}
1402
1403static void
1404frame_button_redraw_handler(struct widget *widget, void *data)
1405{
1406 struct frame_button *frame_button = data;
1407 cairo_t *cr;
1408 int width, height, x, y;
1409 struct window *window = widget->window;
1410
1411 x = widget->allocation.x;
1412 y = widget->allocation.y;
1413 width = widget->allocation.width;
1414 height = widget->allocation.height;
1415
1416 if (!width)
1417 return;
1418 if (!height)
1419 return;
1420 if (widget->opaque)
1421 return;
1422
1423 cr = cairo_create(window->cairo_surface);
1424
1425 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1426 cairo_set_line_width(cr, 1);
1427
1428 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1429 cairo_rectangle (cr, x, y, 25, 16);
1430
1431 cairo_stroke_preserve(cr);
1432
1433 switch (frame_button->state) {
1434 case FRAME_BUTTON_DEFAULT:
1435 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1436 break;
1437 case FRAME_BUTTON_OVER:
1438 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1439 break;
1440 case FRAME_BUTTON_ACTIVE:
1441 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1442 break;
1443 }
1444
1445 cairo_fill (cr);
1446
1447 x += 4;
1448 }
1449
1450 cairo_set_source_surface(cr, frame_button->icon, x, y);
1451 cairo_paint(cr);
1452
1453 cairo_destroy(cr);
1454}
1455
1456static struct widget *
1457frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1458 enum frame_button_align align, enum frame_button_decoration style)
1459{
1460 struct frame_button *frame_button;
1461 const char *icon = data;
1462
1463 frame_button = malloc (sizeof *frame_button);
1464 memset(frame_button, 0, sizeof *frame_button);
1465
1466 frame_button->icon = cairo_image_surface_create_from_png(icon);
1467 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1468 frame_button->frame = frame;
1469 frame_button->type = type;
1470 frame_button->align = align;
1471 frame_button->decoration = style;
1472
1473 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1474
1475 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1476 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1477 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1478 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1479 return frame_button->widget;
1480}
1481
1482static void
1483frame_button_destroy(struct frame_button *frame_button)
1484{
1485 widget_destroy(frame_button->widget);
1486 wl_list_remove(&frame_button->link);
1487 cairo_surface_destroy(frame_button->icon);
1488 free(frame_button);
1489
1490 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001491}
1492
1493static void
1494frame_redraw_handler(struct widget *widget, void *data)
1495{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001496 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001497 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001498 struct theme *t = window->display->theme;
1499 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001500
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001501 if (window->type == TYPE_FULLSCREEN)
1502 return;
1503
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001504 cr = cairo_create(window->cairo_surface);
1505
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001506 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001507 flags |= THEME_FRAME_ACTIVE;
1508 theme_render_frame(t, cr, widget->allocation.width,
1509 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001510
1511 cairo_destroy(cr);
1512}
1513
1514static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001515frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001516{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001517 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001518 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001519
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001520 location = theme_get_location(t, input->sx, input->sy,
1521 frame->widget->allocation.width,
1522 frame->widget->allocation.height);
1523
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001524 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001525 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001526 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001527 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001528 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001529 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001530 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001531 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001532 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001533 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001534 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001535 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001536 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001537 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001538 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001539 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001540 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001541 case THEME_LOCATION_EXTERIOR:
1542 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001543 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001544 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001545 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001546}
1547
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001548static void
1549frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001550{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001551 switch (index) {
1552 case 0: /* close */
1553 if (window->close_handler)
1554 window->close_handler(window->parent,
1555 window->user_data);
1556 else
1557 display_exit(window->display);
1558 break;
1559 case 1: /* fullscreen */
1560 /* we don't have a way to get out of fullscreen for now */
1561 window_set_fullscreen(window, 1);
1562 break;
1563 case 2: /* rotate */
1564 case 3: /* scale */
1565 break;
1566 }
1567}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001568
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001569void
1570window_show_frame_menu(struct window *window,
1571 struct input *input, uint32_t time)
1572{
1573 int32_t x, y;
1574
1575 static const char *entries[] = {
1576 "Close", "Fullscreen", "Rotate", "Scale"
1577 };
1578
1579 input_get_position(input, &x, &y);
1580 window_show_menu(window->display, input, time, window,
1581 x - 10, y - 10, frame_menu_func, entries, 4);
1582}
1583
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001584static int
1585frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001586 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001587{
1588 return frame_get_pointer_image_for_location(data, input);
1589}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001590
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001591static int
1592frame_motion_handler(struct widget *widget,
1593 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001594 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001595{
1596 return frame_get_pointer_image_for_location(data, input);
1597}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001598
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001599static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001600frame_button_handler(struct widget *widget,
1601 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001602 uint32_t button, enum wl_pointer_button_state state,
1603 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001604
1605{
1606 struct frame *frame = data;
1607 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001608 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001609 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001610
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001611 location = theme_get_location(display->theme, input->sx, input->sy,
1612 frame->widget->allocation.width,
1613 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614
Daniel Stone4dbadb12012-05-30 16:31:51 +01001615 if (window->display->shell && button == BTN_LEFT &&
1616 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001617 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001618 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001619 if (!window->shell_surface)
1620 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001621 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001622 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001623 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001624 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001625 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001626 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001627 case THEME_LOCATION_RESIZING_TOP:
1628 case THEME_LOCATION_RESIZING_BOTTOM:
1629 case THEME_LOCATION_RESIZING_LEFT:
1630 case THEME_LOCATION_RESIZING_RIGHT:
1631 case THEME_LOCATION_RESIZING_TOP_LEFT:
1632 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1633 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1634 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001635 if (!window->shell_surface)
1636 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001637 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001638
1639 if (!display->dpy) {
1640 /* If we're using shm, allocate a big
1641 pool to create buffers out of while
1642 we resize. We should probably base
1643 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001644 window->pool =
1645 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001646 }
1647
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001648 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001649 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001650 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001651 break;
1652 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001653 } else if (button == BTN_RIGHT &&
1654 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001655 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001656 }
1657}
1658
1659struct widget *
1660frame_create(struct window *window, void *data)
1661{
1662 struct frame *frame;
1663
1664 frame = malloc(sizeof *frame);
1665 memset(frame, 0, sizeof *frame);
1666
1667 frame->widget = window_add_widget(window, frame);
1668 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001669
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001670 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1671 widget_set_resize_handler(frame->widget, frame_resize_handler);
1672 widget_set_enter_handler(frame->widget, frame_enter_handler);
1673 widget_set_motion_handler(frame->widget, frame_motion_handler);
1674 widget_set_button_handler(frame->widget, frame_button_handler);
1675
Martin Minarik1998b152012-05-10 02:04:35 +02001676 /* Create empty list for frame buttons */
1677 wl_list_init(&frame->buttons_list);
1678
1679 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1680 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1681
1682 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1683 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1684
1685 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1686 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1687
1688 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1689 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1690
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001691 window->frame = frame;
1692
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001693 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001694}
1695
Kristian Høgsberga1627922012-06-20 17:30:03 -04001696void
1697frame_set_child_size(struct widget *widget, int child_width, int child_height)
1698{
1699 struct display *display = widget->window->display;
1700 struct theme *t = display->theme;
1701 int decoration_width, decoration_height;
1702 int width, height;
1703
1704 if (widget->window->type != TYPE_FULLSCREEN) {
1705 decoration_width = (t->width + t->margin) * 2;
1706 decoration_height = t->width +
1707 t->titlebar_height + t->margin * 2;
1708
1709 width = child_width + decoration_width;
1710 height = child_height + decoration_height;
1711 } else {
1712 width = child_width;
1713 height = child_height;
1714 }
1715
1716 window_schedule_resize(widget->window, width, height);
1717}
1718
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001719static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001720frame_destroy(struct frame *frame)
1721{
Martin Minarik1998b152012-05-10 02:04:35 +02001722 struct frame_button *button, *tmp;
1723
1724 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1725 frame_button_destroy(button);
1726
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001727 /* frame->child must be destroyed by the application */
1728 widget_destroy(frame->widget);
1729 free(frame);
1730}
1731
1732static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001733input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001734 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001735{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001736 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001737 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001738
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001739 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001740 return;
1741
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001742 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001743 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001744 widget = old;
1745 if (input->grab)
1746 widget = input->grab;
1747 if (widget->leave_handler)
1748 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001749 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001750 }
1751
1752 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001753 widget = focus;
1754 if (input->grab)
1755 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001756 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001757 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001758 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001759 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001760
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001761 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001762 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001763}
1764
1765static void
Daniel Stone37816df2012-05-16 18:45:18 +01001766pointer_handle_motion(void *data, struct wl_pointer *pointer,
1767 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001768{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001769 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001770 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001771 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001772 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001773 float sx = wl_fixed_to_double(sx_w);
1774 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001775
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001776 input->sx = sx;
1777 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001778
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001779 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001780 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001781 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001782 }
1783
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001784 if (input->grab)
1785 widget = input->grab;
1786 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001787 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001788 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001789 cursor = widget->motion_handler(input->focus_widget,
1790 input, time, sx, sy,
1791 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001792
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001793 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001794}
1795
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001796void
1797input_grab(struct input *input, struct widget *widget, uint32_t button)
1798{
1799 input->grab = widget;
1800 input->grab_button = button;
1801}
1802
1803void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001804input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001805{
1806 struct widget *widget;
1807
1808 input->grab = NULL;
1809 if (input->pointer_focus) {
1810 widget = widget_find_widget(input->pointer_focus->widget,
1811 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001812 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001813 }
1814}
1815
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001816static void
Daniel Stone37816df2012-05-16 18:45:18 +01001817pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001818 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001819{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001820 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001821 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001822 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001823
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001824 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001825 if (input->focus_widget && input->grab == NULL &&
1826 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001827 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001828
Neil Roberts6b28aad2012-01-23 19:11:18 +00001829 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001830 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001831 (*widget->button_handler)(widget,
1832 input, time,
1833 button, state,
1834 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001835
Daniel Stone4dbadb12012-05-30 16:31:51 +01001836 if (input->grab && input->grab_button == button &&
1837 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001838 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001839}
1840
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001841static void
Daniel Stone37816df2012-05-16 18:45:18 +01001842pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001843 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001844{
1845}
1846
1847static void
Daniel Stone37816df2012-05-16 18:45:18 +01001848keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001849 uint32_t serial, uint32_t time, uint32_t key,
1850 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001851{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001852 struct input *input = data;
1853 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001854 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001855 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001856 const xkb_keysym_t *syms;
1857 xkb_keysym_t sym;
1858 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001859 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001860
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001861 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001862 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001863 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001864 return;
1865
Daniel Stone97f68542012-05-30 16:32:01 +01001866 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001867
Daniel Stone97f68542012-05-30 16:32:01 +01001868 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001869 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001870 XKB_STATE_LATCHED);
1871 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001872 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001873 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001874 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001875 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001876 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001877 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001878
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001879 sym = XKB_KEY_NoSymbol;
1880 if (num_syms == 1)
1881 sym = syms[0];
1882
1883 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001884 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001885 window_set_maximized(window,
1886 window->type != TYPE_MAXIMIZED);
1887 } else if (window->key_handler) {
1888 (*window->key_handler)(window, input, time, key,
1889 sym, state, window->user_data);
1890 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001891
1892 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
1893 key == input->repeat_key) {
1894 its.it_interval.tv_sec = 0;
1895 its.it_interval.tv_nsec = 0;
1896 its.it_value.tv_sec = 0;
1897 its.it_value.tv_nsec = 0;
1898 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1899 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1900 input->repeat_sym = sym;
1901 input->repeat_key = key;
1902 input->repeat_time = time;
1903 its.it_interval.tv_sec = 0;
1904 its.it_interval.tv_nsec = 25 * 1000 * 1000;
1905 its.it_value.tv_sec = 0;
1906 its.it_value.tv_nsec = 400 * 1000 * 1000;
1907 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1908 }
1909}
1910
1911static void
1912keyboard_repeat_func(struct task *task, uint32_t events)
1913{
1914 struct input *input =
1915 container_of(task, struct input, repeat_task);
1916 struct window *window = input->keyboard_focus;
1917 uint64_t exp;
1918
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04001919 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1920 /* If we change the timer between the fd becoming
1921 * readable and getting here, there'll be nothing to
1922 * read and we get EAGAIN. */
1923 return;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001924
Kristian Høgsbergbd0cd762012-06-20 15:17:18 -04001925 if (window && window->key_handler) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001926 (*window->key_handler)(window, input, input->repeat_time,
1927 input->repeat_key, input->repeat_sym,
1928 WL_KEYBOARD_KEY_STATE_PRESSED,
1929 window->user_data);
1930 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001931}
1932
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001933static void
Daniel Stone351eb612012-05-31 15:27:47 -04001934keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1935 uint32_t serial, uint32_t mods_depressed,
1936 uint32_t mods_latched, uint32_t mods_locked,
1937 uint32_t group)
1938{
1939 struct input *input = data;
1940
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001941 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1942 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001943}
1944
1945static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001946input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001947{
1948 struct window *window = input->pointer_focus;
1949
1950 if (!window)
1951 return;
1952
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001953 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001954
Pekka Paalanene1207c72011-12-16 12:02:09 +02001955 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001956 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001957}
1958
1959static void
Daniel Stone37816df2012-05-16 18:45:18 +01001960pointer_handle_enter(void *data, struct wl_pointer *pointer,
1961 uint32_t serial, struct wl_surface *surface,
1962 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001963{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001964 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001965 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001966 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001967 float sx = wl_fixed_to_double(sx_w);
1968 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001969
Martin Minarik1998b152012-05-10 02:04:35 +02001970 if (!surface) {
1971 /* enter event for a window we've just destroyed */
1972 return;
1973 }
1974
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001975 input->display->serial = serial;
1976 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001977 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001978 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001979
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001980 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001981 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001982 window->pool = NULL;
1983 /* Schedule a redraw to free the pool */
1984 window_schedule_redraw(window);
1985 }
1986
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001987 input->sx = sx;
1988 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001989
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001990 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001991 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001992}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001993
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001994static void
Daniel Stone37816df2012-05-16 18:45:18 +01001995pointer_handle_leave(void *data, struct wl_pointer *pointer,
1996 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001997{
1998 struct input *input = data;
1999
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002000 input->display->serial = serial;
2001 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002002}
2003
2004static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002005input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02002006{
2007 struct window *window = input->keyboard_focus;
Kristian Høgsbergd3c69c22012-06-20 22:41:59 -04002008 struct itimerspec its;
2009
2010 its.it_interval.tv_sec = 0;
2011 its.it_interval.tv_nsec = 0;
2012 its.it_value.tv_sec = 0;
2013 its.it_value.tv_nsec = 0;
2014 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002015
2016 if (!window)
2017 return;
2018
2019 window->keyboard_device = NULL;
2020 if (window->keyboard_focus_handler)
2021 (*window->keyboard_focus_handler)(window, NULL,
2022 window->user_data);
2023
2024 input->keyboard_focus = NULL;
2025}
2026
2027static void
Daniel Stone37816df2012-05-16 18:45:18 +01002028keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2029 uint32_t serial, struct wl_surface *surface,
2030 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002031{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002032 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02002033 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002034
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002035 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002036 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002037
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002038 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002039 window->keyboard_device = input;
2040 if (window->keyboard_focus_handler)
2041 (*window->keyboard_focus_handler)(window,
2042 window->keyboard_device,
2043 window->user_data);
2044}
2045
2046static void
Daniel Stone37816df2012-05-16 18:45:18 +01002047keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2048 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002049{
2050 struct input *input = data;
2051
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002052 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002053 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002054}
2055
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002056static void
2057keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2058 uint32_t format, int fd, uint32_t size)
2059{
2060 struct input *input = data;
2061 char *map_str;
2062
2063 if (!data) {
2064 close(fd);
2065 return;
2066 }
2067
2068 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2069 close(fd);
2070 return;
2071 }
2072
2073 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2074 if (map_str == MAP_FAILED) {
2075 close(fd);
2076 return;
2077 }
2078
2079 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2080 map_str,
2081 XKB_KEYMAP_FORMAT_TEXT_V1,
2082 0);
2083 munmap(map_str, size);
2084 close(fd);
2085
2086 if (!input->xkb.keymap) {
2087 fprintf(stderr, "failed to compile keymap\n");
2088 return;
2089 }
2090
2091 input->xkb.state = xkb_state_new(input->xkb.keymap);
2092 if (!input->xkb.state) {
2093 fprintf(stderr, "failed to create XKB state\n");
2094 xkb_map_unref(input->xkb.keymap);
2095 input->xkb.keymap = NULL;
2096 return;
2097 }
2098
2099 input->xkb.control_mask =
2100 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2101 input->xkb.alt_mask =
2102 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2103 input->xkb.shift_mask =
2104 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2105}
2106
Daniel Stone37816df2012-05-16 18:45:18 +01002107static const struct wl_pointer_listener pointer_listener = {
2108 pointer_handle_enter,
2109 pointer_handle_leave,
2110 pointer_handle_motion,
2111 pointer_handle_button,
2112 pointer_handle_axis,
2113};
2114
2115static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002116 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002117 keyboard_handle_enter,
2118 keyboard_handle_leave,
2119 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002120 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002121};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002122
2123static void
Daniel Stone37816df2012-05-16 18:45:18 +01002124seat_handle_capabilities(void *data, struct wl_seat *seat,
2125 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002126{
Daniel Stone37816df2012-05-16 18:45:18 +01002127 struct input *input = data;
2128
2129 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2130 input->pointer = wl_seat_get_pointer(seat);
2131 wl_pointer_set_user_data(input->pointer, input);
2132 wl_pointer_add_listener(input->pointer, &pointer_listener,
2133 input);
2134 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2135 wl_pointer_destroy(input->pointer);
2136 input->pointer = NULL;
2137 }
2138
2139 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2140 input->keyboard = wl_seat_get_keyboard(seat);
2141 wl_keyboard_set_user_data(input->keyboard, input);
2142 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2143 input);
2144 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2145 wl_keyboard_destroy(input->keyboard);
2146 input->keyboard = NULL;
2147 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002148}
2149
Daniel Stone37816df2012-05-16 18:45:18 +01002150static const struct wl_seat_listener seat_listener = {
2151 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002152};
2153
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002154void
2155input_get_position(struct input *input, int32_t *x, int32_t *y)
2156{
2157 *x = input->sx;
2158 *y = input->sy;
2159}
2160
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002161struct display *
2162input_get_display(struct input *input)
2163{
2164 return input->display;
2165}
2166
Daniel Stone37816df2012-05-16 18:45:18 +01002167struct wl_seat *
2168input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002169{
Daniel Stone37816df2012-05-16 18:45:18 +01002170 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002171}
2172
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002173uint32_t
2174input_get_modifiers(struct input *input)
2175{
2176 return input->modifiers;
2177}
2178
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002179struct widget *
2180input_get_focus_widget(struct input *input)
2181{
2182 return input->focus_widget;
2183}
2184
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002185struct data_offer {
2186 struct wl_data_offer *offer;
2187 struct input *input;
2188 struct wl_array types;
2189 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002190
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002191 struct task io_task;
2192 int fd;
2193 data_func_t func;
2194 int32_t x, y;
2195 void *user_data;
2196};
2197
2198static void
2199data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2200{
2201 struct data_offer *offer = data;
2202 char **p;
2203
2204 p = wl_array_add(&offer->types, sizeof *p);
2205 *p = strdup(type);
2206}
2207
2208static const struct wl_data_offer_listener data_offer_listener = {
2209 data_offer_offer,
2210};
2211
2212static void
2213data_offer_destroy(struct data_offer *offer)
2214{
2215 char **p;
2216
2217 offer->refcount--;
2218 if (offer->refcount == 0) {
2219 wl_data_offer_destroy(offer->offer);
2220 for (p = offer->types.data; *p; p++)
2221 free(*p);
2222 wl_array_release(&offer->types);
2223 free(offer);
2224 }
2225}
2226
2227static void
2228data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002229 struct wl_data_device *data_device,
2230 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002231{
2232 struct data_offer *offer;
2233
2234 offer = malloc(sizeof *offer);
2235
2236 wl_array_init(&offer->types);
2237 offer->refcount = 1;
2238 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002239 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002240 wl_data_offer_add_listener(offer->offer,
2241 &data_offer_listener, offer);
2242}
2243
2244static void
2245data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002246 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002247 wl_fixed_t x_w, wl_fixed_t y_w,
2248 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002249{
2250 struct input *input = data;
2251 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002252 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002253 float x = wl_fixed_to_double(x_w);
2254 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002255 char **p;
2256
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002257 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002258 window = wl_surface_get_user_data(surface);
2259 input->pointer_focus = window;
2260
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002261 if (offer) {
2262 input->drag_offer = wl_data_offer_get_user_data(offer);
2263
2264 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2265 *p = NULL;
2266
2267 types_data = input->drag_offer->types.data;
2268 } else {
2269 input->drag_offer = NULL;
2270 types_data = NULL;
2271 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002272
2273 window = input->pointer_focus;
2274 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002275 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002276 window->user_data);
2277}
2278
2279static void
2280data_device_leave(void *data, struct wl_data_device *data_device)
2281{
2282 struct input *input = data;
2283
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002284 if (input->drag_offer) {
2285 data_offer_destroy(input->drag_offer);
2286 input->drag_offer = NULL;
2287 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002288}
2289
2290static void
2291data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002292 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002293{
2294 struct input *input = data;
2295 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002296 float x = wl_fixed_to_double(x_w);
2297 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002298 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002299
2300 input->sx = x;
2301 input->sy = y;
2302
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002303 if (input->drag_offer)
2304 types_data = input->drag_offer->types.data;
2305 else
2306 types_data = NULL;
2307
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002308 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002309 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002310 window->user_data);
2311}
2312
2313static void
2314data_device_drop(void *data, struct wl_data_device *data_device)
2315{
2316 struct input *input = data;
2317 struct window *window = input->pointer_focus;
2318
2319 if (window->drop_handler)
2320 window->drop_handler(window, input,
2321 input->sx, input->sy, window->user_data);
2322}
2323
2324static void
2325data_device_selection(void *data,
2326 struct wl_data_device *wl_data_device,
2327 struct wl_data_offer *offer)
2328{
2329 struct input *input = data;
2330 char **p;
2331
2332 if (input->selection_offer)
2333 data_offer_destroy(input->selection_offer);
2334
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002335 if (offer) {
2336 input->selection_offer = wl_data_offer_get_user_data(offer);
2337 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2338 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002339 } else {
2340 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002341 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002342}
2343
2344static const struct wl_data_device_listener data_device_listener = {
2345 data_device_data_offer,
2346 data_device_enter,
2347 data_device_leave,
2348 data_device_motion,
2349 data_device_drop,
2350 data_device_selection
2351};
2352
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002353static void
2354input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002355{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002356 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002357 struct wl_cursor *cursor;
2358 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002359
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002360 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002361 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002362 return;
2363
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002364 if (index >= (int) cursor->image_count) {
2365 fprintf(stderr, "cursor index out of range\n");
2366 return;
2367 }
2368
2369 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002370 buffer = wl_cursor_image_get_buffer(image);
2371 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002372 return;
2373
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002374 wl_pointer_set_cursor(input->pointer, input->display->serial,
2375 input->pointer_surface,
2376 image->hotspot_x, image->hotspot_y);
2377 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2378 wl_surface_damage(input->pointer_surface, 0, 0,
2379 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002380}
2381
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002382static const struct wl_callback_listener pointer_surface_listener;
2383
2384static void
2385pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2386 uint32_t time)
2387{
2388 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002389 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002390 int i;
2391
2392 if (callback) {
2393 assert(callback == input->cursor_frame_cb);
2394 wl_callback_destroy(callback);
2395 input->cursor_frame_cb = NULL;
2396 }
2397
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002398 if (input->current_cursor == CURSOR_BLANK) {
2399 wl_pointer_set_cursor(input->pointer, input->display->serial,
2400 NULL, 0, 0);
2401 return;
2402 }
2403
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002404 if (input->current_cursor == CURSOR_UNSET)
2405 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002406 cursor = input->display->cursors[input->current_cursor];
2407 if (!cursor)
2408 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002409
2410 /* FIXME We don't have the current time on the first call so we set
2411 * the animation start to the time of the first frame callback. */
2412 if (time == 0)
2413 input->cursor_anim_start = 0;
2414 else if (input->cursor_anim_start == 0)
2415 input->cursor_anim_start = time;
2416
2417 if (time == 0 || input->cursor_anim_start == 0)
2418 i = 0;
2419 else
2420 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2421
2422 input_set_pointer_image_index(input, i);
2423
2424 if (cursor->image_count == 1)
2425 return;
2426
2427 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2428 wl_callback_add_listener(input->cursor_frame_cb,
2429 &pointer_surface_listener, input);
2430}
2431
2432static const struct wl_callback_listener pointer_surface_listener = {
2433 pointer_surface_frame_callback
2434};
2435
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002436void
2437input_set_pointer_image(struct input *input, int pointer)
2438{
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002439 if (pointer == input->current_cursor &&
2440 input->pointer_enter_serial == input->cursor_serial)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002441 return;
2442
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002443 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002444 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002445 if (!input->cursor_frame_cb)
2446 pointer_surface_frame_callback(input, NULL, 0);
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002447}
2448
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002449struct wl_data_device *
2450input_get_data_device(struct input *input)
2451{
2452 return input->data_device;
2453}
2454
2455void
2456input_set_selection(struct input *input,
2457 struct wl_data_source *source, uint32_t time)
2458{
2459 wl_data_device_set_selection(input->data_device, source, time);
2460}
2461
2462void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002463input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002464{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002465 wl_data_offer_accept(input->drag_offer->offer,
2466 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002467}
2468
2469static void
2470offer_io_func(struct task *task, uint32_t events)
2471{
2472 struct data_offer *offer =
2473 container_of(task, struct data_offer, io_task);
2474 unsigned int len;
2475 char buffer[4096];
2476
2477 len = read(offer->fd, buffer, sizeof buffer);
2478 offer->func(buffer, len,
2479 offer->x, offer->y, offer->user_data);
2480
2481 if (len == 0) {
2482 close(offer->fd);
2483 data_offer_destroy(offer);
2484 }
2485}
2486
2487static void
2488data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2489 data_func_t func, void *user_data)
2490{
2491 int p[2];
2492
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002493 if (pipe2(p, O_CLOEXEC) == -1)
2494 return;
2495
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002496 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2497 close(p[1]);
2498
2499 offer->io_task.run = offer_io_func;
2500 offer->fd = p[0];
2501 offer->func = func;
2502 offer->refcount++;
2503 offer->user_data = user_data;
2504
2505 display_watch_fd(offer->input->display,
2506 offer->fd, EPOLLIN, &offer->io_task);
2507}
2508
2509void
2510input_receive_drag_data(struct input *input, const char *mime_type,
2511 data_func_t func, void *data)
2512{
2513 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2514 input->drag_offer->x = input->sx;
2515 input->drag_offer->y = input->sy;
2516}
2517
2518int
2519input_receive_selection_data(struct input *input, const char *mime_type,
2520 data_func_t func, void *data)
2521{
2522 char **p;
2523
2524 if (input->selection_offer == NULL)
2525 return -1;
2526
2527 for (p = input->selection_offer->types.data; *p; p++)
2528 if (strcmp(mime_type, *p) == 0)
2529 break;
2530
2531 if (*p == NULL)
2532 return -1;
2533
2534 data_offer_receive_data(input->selection_offer,
2535 mime_type, func, data);
2536 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002537}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002538
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002539int
2540input_receive_selection_data_to_fd(struct input *input,
2541 const char *mime_type, int fd)
2542{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002543 if (input->selection_offer)
2544 wl_data_offer_receive(input->selection_offer->offer,
2545 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002546
2547 return 0;
2548}
2549
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002550void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002551window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002552{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002553 if (!window->shell_surface)
2554 return;
2555
Daniel Stone37816df2012-05-16 18:45:18 +01002556 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002557}
2558
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002559static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002560idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002561{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002562 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002563
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002564 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002565 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002566 widget_set_allocation(widget,
2567 window->pending_allocation.x,
2568 window->pending_allocation.y,
2569 window->pending_allocation.width,
2570 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002571
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002572 if (window->input_region) {
2573 wl_region_destroy(window->input_region);
2574 window->input_region = NULL;
2575 }
2576
2577 if (window->opaque_region) {
2578 wl_region_destroy(window->opaque_region);
2579 window->opaque_region = NULL;
2580 }
2581
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002582 if (widget->resize_handler)
2583 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002584 widget->allocation.width,
2585 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002586 widget->user_data);
2587
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002588 if (window->allocation.width != widget->allocation.width ||
2589 window->allocation.height != widget->allocation.height) {
2590 window->allocation = widget->allocation;
2591 window_schedule_redraw(window);
2592 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002593}
2594
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002595void
2596window_schedule_resize(struct window *window, int width, int height)
2597{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002598 window->pending_allocation.x = 0;
2599 window->pending_allocation.y = 0;
2600 window->pending_allocation.width = width;
2601 window->pending_allocation.height = height;
2602
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002603 window->resize_needed = 1;
2604 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002605}
2606
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002607void
2608widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2609{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002610 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002611}
2612
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002613static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002614handle_ping(void *data, struct wl_shell_surface *shell_surface,
2615 uint32_t serial)
2616{
2617 wl_shell_surface_pong(shell_surface, serial);
2618}
2619
2620static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002621handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002622 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002623{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002624 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002625
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002626 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002627 return;
2628
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002629 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002630 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002631}
2632
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002633static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002634menu_destroy(struct menu *menu)
2635{
2636 widget_destroy(menu->widget);
2637 window_destroy(menu->window);
2638 free(menu);
2639}
2640
2641static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002642handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2643{
2644 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002645 struct menu *menu = window->widget->user_data;
2646
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002647 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002648 * device. Or just use wl_callback. And this really needs to
2649 * be a window vfunc that the menu can set. And we need the
2650 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002651
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002652 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002653 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002654 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002655}
2656
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002657static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002658 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002659 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002660 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002661};
2662
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002663void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002664window_get_allocation(struct window *window,
2665 struct rectangle *allocation)
2666{
2667 *allocation = window->allocation;
2668}
2669
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002670static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002671widget_redraw(struct widget *widget)
2672{
2673 struct widget *child;
2674
2675 if (widget->redraw_handler)
2676 widget->redraw_handler(widget, widget->user_data);
2677 wl_list_for_each(child, &widget->child_list, link)
2678 widget_redraw(child);
2679}
2680
2681static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002682frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2683{
2684 struct window *window = data;
2685
2686 wl_callback_destroy(callback);
2687 window->redraw_scheduled = 0;
2688 if (window->redraw_needed)
2689 window_schedule_redraw(window);
2690}
2691
2692static const struct wl_callback_listener listener = {
2693 frame_callback
2694};
2695
2696static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002697idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002698{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002699 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002700 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002701
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002702 if (window->resize_needed)
2703 idle_resize(window);
2704
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002705 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002706 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002707 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002708 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002709 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002710
2711 callback = wl_surface_frame(window->surface);
2712 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002713}
2714
2715void
2716window_schedule_redraw(struct window *window)
2717{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002718 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002719 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002720 window->redraw_task.run = idle_redraw;
2721 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002722 window->redraw_scheduled = 1;
2723 }
2724}
2725
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002726void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002727window_set_custom(struct window *window)
2728{
2729 window->type = TYPE_CUSTOM;
2730}
2731
2732void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002733window_set_fullscreen(struct window *window, int fullscreen)
2734{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002735 if (!window->display->shell)
2736 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002737
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002738 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002739 return;
2740
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002741 if (fullscreen) {
2742 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002743 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002744 wl_shell_surface_set_fullscreen(window->shell_surface,
2745 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2746 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002747 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002748 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002749 wl_shell_surface_set_toplevel(window->shell_surface);
2750 window_schedule_resize(window,
2751 window->saved_allocation.width,
2752 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002753 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002754}
2755
2756void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002757window_set_maximized(struct window *window, int maximized)
2758{
2759 if (!window->display->shell)
2760 return;
2761
2762 if ((window->type == TYPE_MAXIMIZED) == maximized)
2763 return;
2764
2765 if (window->type == TYPE_TOPLEVEL) {
2766 window->saved_allocation = window->allocation;
2767 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2768 window->type = TYPE_MAXIMIZED;
2769 } else {
2770 wl_shell_surface_set_toplevel(window->shell_surface);
2771 window->type = TYPE_TOPLEVEL;
2772 window_schedule_resize(window,
2773 window->saved_allocation.width,
2774 window->saved_allocation.height);
2775 }
2776}
2777
2778void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002779window_set_user_data(struct window *window, void *data)
2780{
2781 window->user_data = data;
2782}
2783
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002784void *
2785window_get_user_data(struct window *window)
2786{
2787 return window->user_data;
2788}
2789
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002790void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002791window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002792 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002793{
2794 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002795}
2796
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002797void
2798window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002799 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002800{
2801 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002802}
2803
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002804void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002805window_set_data_handler(struct window *window, window_data_handler_t handler)
2806{
2807 window->data_handler = handler;
2808}
2809
2810void
2811window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2812{
2813 window->drop_handler = handler;
2814}
2815
2816void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002817window_set_close_handler(struct window *window,
2818 window_close_handler_t handler)
2819{
2820 window->close_handler = handler;
2821}
2822
2823void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002824window_set_title(struct window *window, const char *title)
2825{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002826 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002827 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002828 if (window->shell_surface)
2829 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002830}
2831
2832const char *
2833window_get_title(struct window *window)
2834{
2835 return window->title;
2836}
2837
2838void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002839window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2840{
2841 struct text_cursor_position *text_cursor_position =
2842 window->display->text_cursor_position;
2843
Scott Moreau9295ce02012-06-01 12:46:10 -06002844 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002845 return;
2846
2847 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002848 window->surface,
2849 wl_fixed_from_int(x),
2850 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002851}
2852
2853void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002854window_damage(struct window *window, int32_t x, int32_t y,
2855 int32_t width, int32_t height)
2856{
2857 wl_surface_damage(window->surface, x, y, width, height);
2858}
2859
Casey Dahlin9074db52012-04-19 22:50:09 -04002860static void
2861surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002862 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002863{
Rob Bradford7507b572012-05-15 17:55:34 +01002864 struct window *window = data;
2865 struct output *output;
2866 struct output *output_found = NULL;
2867 struct window_output *window_output;
2868
2869 wl_list_for_each(output, &window->display->output_list, link) {
2870 if (output->output == wl_output) {
2871 output_found = output;
2872 break;
2873 }
2874 }
2875
2876 if (!output_found)
2877 return;
2878
2879 window_output = malloc (sizeof *window_output);
2880 window_output->output = output_found;
2881
2882 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002883}
2884
2885static void
2886surface_leave(void *data,
2887 struct wl_surface *wl_surface, struct wl_output *output)
2888{
Rob Bradford7507b572012-05-15 17:55:34 +01002889 struct window *window = data;
2890 struct window_output *window_output;
2891 struct window_output *window_output_found = NULL;
2892
2893 wl_list_for_each(window_output, &window->window_output_list, link) {
2894 if (window_output->output->output == output) {
2895 window_output_found = window_output;
2896 break;
2897 }
2898 }
2899
2900 if (window_output_found) {
2901 wl_list_remove(&window_output_found->link);
2902 free(window_output_found);
2903 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002904}
2905
2906static const struct wl_surface_listener surface_listener = {
2907 surface_enter,
2908 surface_leave
2909};
2910
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002911static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002912window_create_internal(struct display *display,
2913 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002914{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002915 struct window *window;
2916
2917 window = malloc(sizeof *window);
2918 if (window == NULL)
2919 return NULL;
2920
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002921 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002922 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002923 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002924 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002925 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002926 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002927 window->shell_surface =
2928 wl_shell_get_shell_surface(display->shell,
2929 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002930 if (window->title)
2931 wl_shell_surface_set_title(window->shell_surface,
2932 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002933 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002934 window->allocation.x = 0;
2935 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002936 window->allocation.width = 0;
2937 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002938 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002939 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002940 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002941 window->input_region = NULL;
2942 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002943
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002944 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002945#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002946 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002947#else
2948 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2949#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002950 else
2951 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002952
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002953 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002954 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002955 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002956
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002957 if (window->shell_surface) {
2958 wl_shell_surface_set_user_data(window->shell_surface, window);
2959 wl_shell_surface_add_listener(window->shell_surface,
2960 &shell_surface_listener, window);
2961 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002962
Rob Bradford7507b572012-05-15 17:55:34 +01002963 wl_list_init (&window->window_output_list);
2964
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002965 return window;
2966}
2967
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002968struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002969window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002970{
2971 struct window *window;
2972
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002973 window = window_create_internal(display, NULL, TYPE_NONE);
2974 if (!window)
2975 return NULL;
2976
2977 return window;
2978}
2979
2980struct window *
2981window_create_custom(struct display *display)
2982{
2983 struct window *window;
2984
2985 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002986 if (!window)
2987 return NULL;
2988
2989 return window;
2990}
2991
2992struct window *
2993window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002994 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002995{
2996 struct window *window;
2997
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002998 window = window_create_internal(parent->display,
2999 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003000 if (!window)
3001 return NULL;
3002
3003 window->x = x;
3004 window->y = y;
3005
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003006 if (display->shell)
3007 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003008 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003009 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003010
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003011 return window;
3012}
3013
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003014static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003015menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003016{
3017 int next;
3018
3019 next = (sy - 8) / 20;
3020 if (menu->current != next) {
3021 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003022 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003023 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003024}
3025
3026static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003027menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003028 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003029 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003030{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003031 struct menu *menu = data;
3032
3033 if (widget == menu->widget)
3034 menu_set_item(data, y);
3035
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003036 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003037}
3038
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003039static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003040menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003041 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003042{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003043 struct menu *menu = data;
3044
3045 if (widget == menu->widget)
3046 menu_set_item(data, y);
3047
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003048 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003049}
3050
3051static void
3052menu_leave_handler(struct widget *widget, struct input *input, void *data)
3053{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003054 struct menu *menu = data;
3055
3056 if (widget == menu->widget)
3057 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003058}
3059
3060static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003061menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003062 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003063 uint32_t button, enum wl_pointer_button_state state,
3064 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003065
3066{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003067 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003068
Daniel Stone4dbadb12012-05-30 16:31:51 +01003069 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3070 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003071 /* Either relase after press-drag-release or
3072 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003073 menu->func(menu->window->parent,
3074 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003075 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003076 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003077 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003078}
3079
3080static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003081menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003082{
3083 cairo_t *cr;
3084 const int32_t r = 3, margin = 3;
3085 struct menu *menu = data;
3086 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003087 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003088
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003089 cr = cairo_create(window->cairo_surface);
3090 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3091 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3092 cairo_paint(cr);
3093
3094 width = window->allocation.width;
3095 height = window->allocation.height;
3096 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003097
3098 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003099 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3100 cairo_fill(cr);
3101
3102 for (i = 0; i < menu->count; i++) {
3103 if (i == menu->current) {
3104 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3105 cairo_rectangle(cr, margin, i * 20 + margin,
3106 width - 2 * margin, 20);
3107 cairo_fill(cr);
3108 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3109 cairo_move_to(cr, 10, i * 20 + 16);
3110 cairo_show_text(cr, menu->entries[i]);
3111 } else {
3112 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3113 cairo_move_to(cr, 10, i * 20 + 16);
3114 cairo_show_text(cr, menu->entries[i]);
3115 }
3116 }
3117
3118 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003119}
3120
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003121void
3122window_show_menu(struct display *display,
3123 struct input *input, uint32_t time, struct window *parent,
3124 int32_t x, int32_t y,
3125 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003126{
3127 struct window *window;
3128 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003129 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003130
3131 menu = malloc(sizeof *menu);
3132 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003133 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003134
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003135 window = window_create_internal(parent->display, parent, TYPE_MENU);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003136 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003137 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003138
3139 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003140 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003141 menu->entries = entries;
3142 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003143 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003144 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003145 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003146 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003147 window->type = TYPE_MENU;
3148 window->x = x;
3149 window->y = y;
3150
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003151 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003152 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003153 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003154 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003155 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003156
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003157 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003158 widget_set_enter_handler(menu->widget, menu_enter_handler);
3159 widget_set_leave_handler(menu->widget, menu_leave_handler);
3160 widget_set_motion_handler(menu->widget, menu_motion_handler);
3161 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003162
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003163 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003164 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003165}
3166
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003167void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003168window_set_buffer_type(struct window *window, enum window_buffer_type type)
3169{
3170 window->buffer_type = type;
3171}
3172
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003173
3174static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003175display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003176 struct wl_output *wl_output,
3177 int x, int y,
3178 int physical_width,
3179 int physical_height,
3180 int subpixel,
3181 const char *make,
3182 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003183{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003184 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003185
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003186 output->allocation.x = x;
3187 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003188}
3189
3190static void
3191display_handle_mode(void *data,
3192 struct wl_output *wl_output,
3193 uint32_t flags,
3194 int width,
3195 int height,
3196 int refresh)
3197{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003198 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003199 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003200
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003201 if (flags & WL_OUTPUT_MODE_CURRENT) {
3202 output->allocation.width = width;
3203 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003204 if (display->output_configure_handler)
3205 (*display->output_configure_handler)(
3206 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003207 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003208}
3209
3210static const struct wl_output_listener output_listener = {
3211 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003212 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003213};
3214
3215static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003216display_add_output(struct display *d, uint32_t id)
3217{
3218 struct output *output;
3219
3220 output = malloc(sizeof *output);
3221 if (output == NULL)
3222 return;
3223
3224 memset(output, 0, sizeof *output);
3225 output->display = d;
3226 output->output =
3227 wl_display_bind(d->display, id, &wl_output_interface);
3228 wl_list_insert(d->output_list.prev, &output->link);
3229
3230 wl_output_add_listener(output->output, &output_listener, output);
3231}
3232
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003233static void
3234output_destroy(struct output *output)
3235{
3236 if (output->destroy_handler)
3237 (*output->destroy_handler)(output, output->user_data);
3238
3239 wl_output_destroy(output->output);
3240 wl_list_remove(&output->link);
3241 free(output);
3242}
3243
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003244void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003245display_set_output_configure_handler(struct display *display,
3246 display_output_handler_t handler)
3247{
3248 struct output *output;
3249
3250 display->output_configure_handler = handler;
3251 if (!handler)
3252 return;
3253
3254 wl_list_for_each(output, &display->output_list, link)
3255 (*display->output_configure_handler)(output,
3256 display->user_data);
3257}
3258
3259void
3260output_set_user_data(struct output *output, void *data)
3261{
3262 output->user_data = data;
3263}
3264
3265void *
3266output_get_user_data(struct output *output)
3267{
3268 return output->user_data;
3269}
3270
3271void
3272output_set_destroy_handler(struct output *output,
3273 display_output_handler_t handler)
3274{
3275 output->destroy_handler = handler;
3276 /* FIXME: implement this, once we have way to remove outputs */
3277}
3278
3279void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003280output_get_allocation(struct output *output, struct rectangle *allocation)
3281{
3282 *allocation = output->allocation;
3283}
3284
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003285struct wl_output *
3286output_get_wl_output(struct output *output)
3287{
3288 return output->output;
3289}
3290
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003291static void
Daniel Stone97f68542012-05-30 16:32:01 +01003292fini_xkb(struct input *input)
3293{
3294 xkb_state_unref(input->xkb.state);
3295 xkb_map_unref(input->xkb.keymap);
3296}
3297
3298static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003299display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003300{
3301 struct input *input;
3302
3303 input = malloc(sizeof *input);
3304 if (input == NULL)
3305 return;
3306
3307 memset(input, 0, sizeof *input);
3308 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003309 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003310 input->pointer_focus = NULL;
3311 input->keyboard_focus = NULL;
3312 wl_list_insert(d->input_list.prev, &input->link);
3313
Daniel Stone37816df2012-05-16 18:45:18 +01003314 wl_seat_add_listener(input->seat, &seat_listener, input);
3315 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003316
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003317 input->data_device =
3318 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003319 input->seat);
3320 wl_data_device_add_listener(input->data_device, &data_device_listener,
3321 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003322
3323 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003324
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003325 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3326 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003327 input->repeat_task.run = keyboard_repeat_func;
3328 display_watch_fd(d, input->repeat_timer_fd,
3329 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003330}
3331
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003332static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003333input_destroy(struct input *input)
3334{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003335 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003336 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003337
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003338 if (input->xkb.state)
3339 xkb_state_unref(input->xkb.state);
3340 if (input->xkb.keymap)
3341 xkb_map_unref(input->xkb.keymap);
3342
Pekka Paalanene1207c72011-12-16 12:02:09 +02003343 if (input->drag_offer)
3344 data_offer_destroy(input->drag_offer);
3345
3346 if (input->selection_offer)
3347 data_offer_destroy(input->selection_offer);
3348
3349 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003350 fini_xkb(input);
3351
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003352 wl_surface_destroy(input->pointer_surface);
3353
Pekka Paalanene1207c72011-12-16 12:02:09 +02003354 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003355 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003356 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003357 free(input);
3358}
3359
3360static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003361display_handle_global(struct wl_display *display, uint32_t id,
3362 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003363{
3364 struct display *d = data;
3365
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003366 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003367 d->compositor =
3368 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003369 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003370 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003371 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003372 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003373 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003374 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003375 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003376 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003377 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3378 d->data_device_manager =
3379 wl_display_bind(display, id,
3380 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003381 } else if (strcmp(interface, "text_cursor_position") == 0) {
3382 d->text_cursor_position =
3383 wl_display_bind(display, id,
3384 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003385 }
3386}
3387
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003388#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003389static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003390init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003391{
3392 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003393 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003394
Rob Clark6396ed32012-03-11 19:48:41 -05003395#ifdef USE_CAIRO_GLESV2
3396# define GL_BIT EGL_OPENGL_ES2_BIT
3397#else
3398# define GL_BIT EGL_OPENGL_BIT
3399#endif
3400
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003401 static const EGLint argb_cfg_attribs[] = {
3402 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003403 EGL_RED_SIZE, 1,
3404 EGL_GREEN_SIZE, 1,
3405 EGL_BLUE_SIZE, 1,
3406 EGL_ALPHA_SIZE, 1,
3407 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003408 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003409 EGL_NONE
3410 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003411
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003412#ifdef USE_CAIRO_GLESV2
3413 static const EGLint context_attribs[] = {
3414 EGL_CONTEXT_CLIENT_VERSION, 2,
3415 EGL_NONE
3416 };
3417 EGLint api = EGL_OPENGL_ES_API;
3418#else
3419 EGLint *context_attribs = NULL;
3420 EGLint api = EGL_OPENGL_API;
3421#endif
3422
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003423 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003424 if (!eglInitialize(d->dpy, &major, &minor)) {
3425 fprintf(stderr, "failed to initialize display\n");
3426 return -1;
3427 }
3428
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003429 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003430 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3431 return -1;
3432 }
3433
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003434 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3435 &d->argb_config, 1, &n) || n != 1) {
3436 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003437 return -1;
3438 }
3439
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003440 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003441 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003442 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003443 fprintf(stderr, "failed to create context\n");
3444 return -1;
3445 }
3446
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003447 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003448 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003449 return -1;
3450 }
3451
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003452#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003453 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3454 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3455 fprintf(stderr, "failed to get cairo egl argb device\n");
3456 return -1;
3457 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003458#endif
3459
3460 return 0;
3461}
3462
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003463static void
3464fini_egl(struct display *display)
3465{
3466#ifdef HAVE_CAIRO_EGL
3467 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003468#endif
3469
3470 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3471 EGL_NO_CONTEXT);
3472
3473 eglTerminate(display->dpy);
3474 eglReleaseThread();
3475}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003476#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003477
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003478static int
3479event_mask_update(uint32_t mask, void *data)
3480{
3481 struct display *d = data;
3482
3483 d->mask = mask;
3484
3485 return 0;
3486}
3487
3488static void
3489handle_display_data(struct task *task, uint32_t events)
3490{
3491 struct display *display =
3492 container_of(task, struct display, display_task);
3493
3494 wl_display_iterate(display->display, display->mask);
3495}
3496
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003497struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003498display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003499{
3500 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003501
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003502 argc = parse_options(xkb_options,
3503 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003504
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003505 d = malloc(sizeof *d);
3506 if (d == NULL)
3507 return NULL;
3508
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003509 memset(d, 0, sizeof *d);
3510
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003511 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003512 if (d->display == NULL) {
3513 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003514 return NULL;
3515 }
3516
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003517 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003518 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3519 d->display_task.run = handle_display_data;
3520 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3521
3522 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003523 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003524 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003525
Daniel Stone97f68542012-05-30 16:32:01 +01003526 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003527 if (d->xkb_context == NULL) {
3528 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003529 return NULL;
3530 }
3531
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003532 /* Set up listener so we'll catch all events. */
3533 wl_display_add_global_listener(d->display,
3534 display_handle_global, d);
3535
3536 /* Process connection events. */
3537 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003538#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003539 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003540 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003541#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003542
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003543 d->image_target_texture_2d =
3544 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3545 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3546 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3547
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003548 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003549
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003550 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003551
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003552 wl_list_init(&d->window_list);
3553
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003554 return d;
3555}
3556
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003557static void
3558display_destroy_outputs(struct display *display)
3559{
3560 struct output *tmp;
3561 struct output *output;
3562
3563 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3564 output_destroy(output);
3565}
3566
Pekka Paalanene1207c72011-12-16 12:02:09 +02003567static void
3568display_destroy_inputs(struct display *display)
3569{
3570 struct input *tmp;
3571 struct input *input;
3572
3573 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3574 input_destroy(input);
3575}
3576
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003577void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003578display_destroy(struct display *display)
3579{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003580 if (!wl_list_empty(&display->window_list))
3581 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3582
3583 if (!wl_list_empty(&display->deferred_list))
3584 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3585
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003586 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003587 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003588
Daniel Stone97f68542012-05-30 16:32:01 +01003589 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003590
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003591 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003592 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003593
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003594#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003595 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003596#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003597
Pekka Paalanenc2052982011-12-16 11:41:32 +02003598 if (display->shell)
3599 wl_shell_destroy(display->shell);
3600
3601 if (display->shm)
3602 wl_shm_destroy(display->shm);
3603
3604 if (display->data_device_manager)
3605 wl_data_device_manager_destroy(display->data_device_manager);
3606
3607 wl_compositor_destroy(display->compositor);
3608
3609 close(display->epoll_fd);
3610
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003611 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003612 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003613 free(display);
3614}
3615
3616void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003617display_set_user_data(struct display *display, void *data)
3618{
3619 display->user_data = data;
3620}
3621
3622void *
3623display_get_user_data(struct display *display)
3624{
3625 return display->user_data;
3626}
3627
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003628struct wl_display *
3629display_get_display(struct display *display)
3630{
3631 return display->display;
3632}
3633
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003634struct output *
3635display_get_output(struct display *display)
3636{
3637 return container_of(display->output_list.next, struct output, link);
3638}
3639
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003640struct wl_compositor *
3641display_get_compositor(struct display *display)
3642{
3643 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003644}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003645
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003646uint32_t
3647display_get_serial(struct display *display)
3648{
3649 return display->serial;
3650}
3651
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003652EGLDisplay
3653display_get_egl_display(struct display *d)
3654{
3655 return d->dpy;
3656}
3657
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003658struct wl_data_source *
3659display_create_data_source(struct display *display)
3660{
3661 return wl_data_device_manager_create_data_source(display->data_device_manager);
3662}
3663
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003664EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003665display_get_argb_egl_config(struct display *d)
3666{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003667 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003668}
3669
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003670struct wl_shell *
3671display_get_shell(struct display *display)
3672{
3673 return display->shell;
3674}
3675
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003676int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003677display_acquire_window_surface(struct display *display,
3678 struct window *window,
3679 EGLContext ctx)
3680{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003681#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003682 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003683 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003684
3685 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003686 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003687 device = cairo_surface_get_device(window->cairo_surface);
3688 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003689 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003690
Benjamin Franzke0c991632011-09-27 21:57:31 +02003691 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003692 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003693 ctx = display->argb_ctx;
3694 else
3695 assert(0);
3696 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003697
3698 data = cairo_surface_get_user_data(window->cairo_surface,
3699 &surface_data_key);
3700
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003701 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003702 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003703 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3704 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003705
3706 return 0;
3707#else
3708 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003709#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003710}
3711
3712void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003713display_release_window_surface(struct display *display,
3714 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003715{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003716#ifdef HAVE_CAIRO_EGL
3717 cairo_device_t *device;
3718
3719 device = cairo_surface_get_device(window->cairo_surface);
3720 if (!device)
3721 return;
3722
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003723 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003724 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003725 cairo_device_release(device);
3726#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003727}
3728
3729void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003730display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003731{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003732 wl_list_insert(&display->deferred_list, &task->link);
3733}
3734
3735void
3736display_watch_fd(struct display *display,
3737 int fd, uint32_t events, struct task *task)
3738{
3739 struct epoll_event ep;
3740
3741 ep.events = events;
3742 ep.data.ptr = task;
3743 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3744}
3745
3746void
3747display_run(struct display *display)
3748{
3749 struct task *task;
3750 struct epoll_event ep[16];
3751 int i, count;
3752
Pekka Paalanen826d7952011-12-15 10:14:07 +02003753 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003754 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003755 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003756
3757 while (!wl_list_empty(&display->deferred_list)) {
3758 task = container_of(display->deferred_list.next,
3759 struct task, link);
3760 wl_list_remove(&task->link);
3761 task->run(task, 0);
3762 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003763
3764 if (!display->running)
3765 break;
3766
3767 wl_display_flush(display->display);
3768
3769 count = epoll_wait(display->epoll_fd,
3770 ep, ARRAY_LENGTH(ep), -1);
3771 for (i = 0; i < count; i++) {
3772 task = ep[i].data.ptr;
3773 task->run(task, ep[i].events);
3774 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003775 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003776}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003777
3778void
3779display_exit(struct display *display)
3780{
3781 display->running = 0;
3782}