blob: c7da54ac264dc67c38e48bce963f2c425154d597 [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øgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.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>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050059#include <X11/X.h>
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030060#include <X11/Xcursor/Xcursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040061
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050062#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020063#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040064#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050065
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050066#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050067
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030068struct cursor;
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;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040077 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050078 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020079 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040081 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040082
83 int display_fd;
84 uint32_t mask;
85 struct task display_task;
86
87 int epoll_fd;
88 struct wl_list deferred_list;
89
Pekka Paalanen826d7952011-12-15 10:14:07 +020090 int running;
91
Kristian Høgsberg478d9262010-06-08 20:34:11 -040092 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040093 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050094 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040095 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050096 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040097 struct xkb_desc *xkb;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030098 struct cursor *cursors;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070099 struct shm_pool *cursor_shm_pool;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400100
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500101 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
102 PFNEGLCREATEIMAGEKHRPROC create_image;
103 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200104
105 display_output_handler_t output_configure_handler;
106
107 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500108};
109
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400110enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400111 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400112 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400113 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500114 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500116 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400117 TYPE_CUSTOM
118};
119
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500120struct window {
121 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500122 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500123 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200124 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500125 struct wl_region *input_region;
126 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500127 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500128 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500129 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500130 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400131 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400132 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400133 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400134 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400135 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400136 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400137 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400138 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400139 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500140
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400141 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500142
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700143 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400144
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500145 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500146 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400147 window_data_handler_t data_handler;
148 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500149 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400150
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200151 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500152 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500153
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500154 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400155 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500156};
157
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500158struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500159 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500160 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161 struct wl_list link;
162 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500163 widget_resize_handler_t resize_handler;
164 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500165 widget_enter_handler_t enter_handler;
166 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500167 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500168 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400169 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500170 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400171};
172
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400173struct input {
174 struct display *display;
175 struct wl_input_device *input_device;
176 struct window *pointer_focus;
177 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300178 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400179 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400180 uint32_t pointer_enter_serial;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200181 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400182 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400183
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500184 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500185 struct widget *grab;
186 uint32_t grab_button;
187
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400188 struct wl_data_device *data_device;
189 struct data_offer *drag_offer;
190 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400191};
192
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500193struct output {
194 struct display *display;
195 struct wl_output *output;
196 struct rectangle allocation;
197 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200198
199 display_output_handler_t destroy_handler;
200 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500201};
202
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500203struct frame {
204 struct widget *widget;
205 struct widget *child;
206 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400207 int width;
208 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500209};
210
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500211struct menu {
212 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500213 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500214 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500215 const char **entries;
216 uint32_t time;
217 int current;
218 int count;
219 menu_func_t func;
220};
221
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300222struct cursor_image {
223 cairo_surface_t *surface;
224 int width, height;
225 int hotspot_x, hotspot_y;
226 int delay;
227};
228
229struct cursor {
230 int n_images;
231 struct cursor_image *images;
232};
233
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700234struct shm_pool {
235 struct wl_shm_pool *pool;
236 size_t size;
237 size_t used;
238 void *data;
239};
240
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400241enum {
242 POINTER_DEFAULT = 100,
243 POINTER_UNSET
244};
245
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500246enum window_location {
247 WINDOW_INTERIOR = 0,
248 WINDOW_RESIZING_TOP = 1,
249 WINDOW_RESIZING_BOTTOM = 2,
250 WINDOW_RESIZING_LEFT = 4,
251 WINDOW_RESIZING_TOP_LEFT = 5,
252 WINDOW_RESIZING_BOTTOM_LEFT = 6,
253 WINDOW_RESIZING_RIGHT = 8,
254 WINDOW_RESIZING_TOP_RIGHT = 9,
255 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
256 WINDOW_RESIZING_MASK = 15,
257 WINDOW_EXTERIOR = 16,
258 WINDOW_TITLEBAR = 17,
259 WINDOW_CLIENT_AREA = 18,
260};
261
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400262const char *option_xkb_layout = "us";
263const char *option_xkb_variant = "";
264const char *option_xkb_options = "";
265
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400266static const struct weston_option xkb_options[] = {
267 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
268 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
269 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400270};
271
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400272static const cairo_user_data_key_t surface_data_key;
273struct surface_data {
274 struct wl_buffer *buffer;
275};
276
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500277#define MULT(_d,c,a,t) \
278 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
279
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500280#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400281
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100282struct egl_window_surface_data {
283 struct display *display;
284 struct wl_surface *surface;
285 struct wl_egl_window *window;
286 EGLSurface surf;
287};
288
289static void
290egl_window_surface_data_destroy(void *p)
291{
292 struct egl_window_surface_data *data = p;
293 struct display *d = data->display;
294
295 eglDestroySurface(d->dpy, data->surf);
296 wl_egl_window_destroy(data->window);
297 data->surface = NULL;
298
299 free(p);
300}
301
302static cairo_surface_t *
303display_create_egl_window_surface(struct display *display,
304 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400305 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100306 struct rectangle *rectangle)
307{
308 cairo_surface_t *cairo_surface;
309 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400310 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200311 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100312
313 data = malloc(sizeof *data);
314 if (data == NULL)
315 return NULL;
316
317 data->display = display;
318 data->surface = surface;
319
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500320 config = display->argb_config;
321 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400322
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400323 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100324 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400325 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100326
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400327 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500328 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100329
Benjamin Franzke0c991632011-09-27 21:57:31 +0200330 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100331 data->surf,
332 rectangle->width,
333 rectangle->height);
334
335 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
336 data, egl_window_surface_data_destroy);
337
338 return cairo_surface;
339}
340
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400341#endif
342
343struct wl_buffer *
344display_get_buffer_for_surface(struct display *display,
345 cairo_surface_t *surface)
346{
347 struct surface_data *data;
348
349 data = cairo_surface_get_user_data (surface, &surface_data_key);
350
351 return data->buffer;
352}
353
354struct shm_surface_data {
355 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700356 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357};
358
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500359static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700360shm_pool_destroy(struct shm_pool *pool);
361
362static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400363shm_surface_data_destroy(void *p)
364{
365 struct shm_surface_data *data = p;
366
367 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700368 if (data->pool)
369 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400370}
371
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300372static void
373shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
374{
375 void *dest = cairo_image_surface_get_data(surface);
376
377 memcpy(dest, data, count);
378}
379
Kristian Høgsberg16626282012-04-03 11:21:27 -0400380static struct wl_shm_pool *
381make_shm_pool(struct display *display, int size, void **data)
382{
383 char filename[] = "/tmp/wayland-shm-XXXXXX";
384 struct wl_shm_pool *pool;
385 int fd;
386
387 fd = mkstemp(filename);
388 if (fd < 0) {
389 fprintf(stderr, "open %s failed: %m\n", filename);
390 return NULL;
391 }
392 if (ftruncate(fd, size) < 0) {
393 fprintf(stderr, "ftruncate failed: %m\n");
394 close(fd);
395 return NULL;
396 }
397
398 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
399 unlink(filename);
400
401 if (*data == MAP_FAILED) {
402 fprintf(stderr, "mmap failed: %m\n");
403 close(fd);
404 return NULL;
405 }
406
407 pool = wl_shm_create_pool(display->shm, fd, size);
408
409 close(fd);
410
411 return pool;
412}
413
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700414static struct shm_pool *
415shm_pool_create(struct display *display, size_t size)
416{
417 struct shm_pool *pool = malloc(sizeof *pool);
418
419 if (!pool)
420 return NULL;
421
422 pool->pool = make_shm_pool(display, size, &pool->data);
423 if (!pool->pool) {
424 free(pool);
425 return NULL;
426 }
427
428 pool->size = size;
429 pool->used = 0;
430
431 return pool;
432}
433
434static void *
435shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
436{
437 if (pool->used + size > pool->size)
438 return NULL;
439
440 *offset = pool->used;
441 pool->used += size;
442
443 return (char *) pool->data + *offset;
444}
445
446/* destroy the pool. this does not unmap the memory though */
447static void
448shm_pool_destroy(struct shm_pool *pool)
449{
450 munmap(pool->data, pool->size);
451 wl_shm_pool_destroy(pool->pool);
452 free(pool);
453}
454
455/* Start allocating from the beginning of the pool again */
456static void
457shm_pool_reset(struct shm_pool *pool)
458{
459 pool->used = 0;
460}
461
462static int
463data_length_for_shm_surface(struct rectangle *rect)
464{
465 int stride;
466
467 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
468 rect->width);
469 return stride * rect->height;
470}
471
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500472static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700473display_create_shm_surface_from_pool(struct display *display,
474 struct rectangle *rectangle,
475 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400476{
477 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400478 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400479 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700480 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400481 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400482
483 data = malloc(sizeof *data);
484 if (data == NULL)
485 return NULL;
486
487 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
488 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700489 length = stride * rectangle->height;
490 data->pool = NULL;
491 map = shm_pool_allocate(pool, length, &offset);
492
493 if (!map) {
494 free(data);
495 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400496 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400497
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400498 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499 CAIRO_FORMAT_ARGB32,
500 rectangle->width,
501 rectangle->height,
502 stride);
503
504 cairo_surface_set_user_data (surface, &surface_data_key,
505 data, shm_surface_data_destroy);
506
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400507 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500508 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400509 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500510 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400511
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700512 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400513 rectangle->width,
514 rectangle->height,
515 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700517 return surface;
518}
519
520static cairo_surface_t *
521display_create_shm_surface(struct display *display,
522 struct rectangle *rectangle, uint32_t flags,
523 struct window *window)
524{
525 struct shm_surface_data *data;
526 struct shm_pool *pool;
527 cairo_surface_t *surface;
528
529 if (window && window->pool) {
530 shm_pool_reset(window->pool);
531 surface = display_create_shm_surface_from_pool(display,
532 rectangle,
533 flags,
534 window->pool);
535 if (surface)
536 return surface;
537 }
538
539 pool = shm_pool_create(display,
540 data_length_for_shm_surface(rectangle));
541 if (!pool)
542 return NULL;
543
544 surface =
545 display_create_shm_surface_from_pool(display, rectangle,
546 flags, pool);
547
548 if (!surface) {
549 shm_pool_destroy(pool);
550 return NULL;
551 }
552
553 /* make sure we destroy the pool when the surface is destroyed */
554 data = cairo_surface_get_user_data(surface, &surface_data_key);
555 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556
557 return surface;
558}
559
nobled7b87cb02011-02-01 18:51:47 +0000560static int
561check_size(struct rectangle *rect)
562{
563 if (rect->width && rect->height)
564 return 0;
565
566 fprintf(stderr, "tried to create surface of "
567 "width: %d, height: %d\n", rect->width, rect->height);
568 return -1;
569}
570
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400571cairo_surface_t *
572display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100573 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400574 struct rectangle *rectangle,
575 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400576{
nobled7b87cb02011-02-01 18:51:47 +0000577 if (check_size(rectangle) < 0)
578 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500579#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400580 if (display->dpy)
581 return display_create_egl_window_surface(display,
582 surface,
583 flags,
584 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400585#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400586 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400587}
588
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300589static const char *cursors[] = {
590 "bottom_left_corner",
591 "bottom_right_corner",
592 "bottom_side",
593 "grabbing",
594 "left_ptr",
595 "left_side",
596 "right_side",
597 "top_left_corner",
598 "top_right_corner",
599 "top_side",
600 "xterm",
601 "hand1",
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400602};
603
604static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300605create_cursor_from_images(struct display *display, struct cursor *cursor,
606 XcursorImages *images)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400607{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300608 int i;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400609 struct rectangle rect;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300610 XcursorImage *image;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400611
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300612 cursor->images = malloc(images->nimage * sizeof *cursor->images);
613 cursor->n_images = images->nimage;
614
615 for (i = 0; i < images->nimage; i++) {
616 image = images->images[i];
617
618 rect.width = image->width;
619 rect.height = image->height;
620
621 cursor->images[i].surface =
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700622 display_create_shm_surface_from_pool(display, &rect, 0,
623 display->cursor_shm_pool);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300624
625 shm_surface_write(cursor->images[i].surface,
626 (unsigned char *) image->pixels,
627 image->width * image->height * sizeof image->pixels[0]);
628
629 cursor->images[i].width = image->width;
630 cursor->images[i].height = image->height;
631 cursor->images[i].hotspot_x = image->xhot;
632 cursor->images[i].hotspot_y = image->yhot;
633 cursor->images[i].delay = image->delay;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400634 }
635
636}
637
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700638static size_t
639data_length_for_cursor_images(XcursorImages *images)
640{
641 int i;
642 size_t length = 0;
643 struct rectangle rect;
644
645 for (i = 0; i < images->nimage; i++) {
646 rect.width = images->images[i]->width;
647 rect.height = images->images[i]->height;
648 length += data_length_for_shm_surface(&rect);
649 }
650
651 return length;
652}
653
Pekka Paalanen325bb602011-12-19 10:31:45 +0200654static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300655create_cursors(struct display *display)
656{
657 int i, count;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700658 size_t pool_size = 0;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300659 struct cursor *cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700660 XcursorImages **images;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300661
662 count = ARRAY_LENGTH(cursors);
663 display->cursors = malloc(count * sizeof *display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700664 images = malloc(count * sizeof images[0]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300665
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700666 for (i = 0; i < count; i++) {
667 images[i] = XcursorLibraryLoadImages(cursors[i], NULL, 32);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300668 if (!images) {
669 fprintf(stderr, "Error loading cursor: %s\n",
670 cursors[i]);
671 continue;
672 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700673 pool_size += data_length_for_cursor_images(images[i]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300674 }
675
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700676 display->cursor_shm_pool = shm_pool_create(display, pool_size);
677
678 for (i = 0; i < count; i++) {
679 if (!images)
680 continue;
681
682 cursor = &display->cursors[i];
683 create_cursor_from_images(display, cursor, images[i]);
684
685 XcursorImagesDestroy(images[i]);
686 }
687
688 free(images);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300689}
690
691static void
692destroy_cursor_images(struct cursor *cursor)
693{
694 int i;
695
696 for (i = 0; i < cursor->n_images; i++)
697 if (cursor->images[i].surface)
698 cairo_surface_destroy(cursor->images[i].surface);
699
700 free(cursor->images);
701}
702
703static void
704destroy_cursors(struct display *display)
Pekka Paalanen325bb602011-12-19 10:31:45 +0200705{
706 int i, count;
707
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300708 count = ARRAY_LENGTH(cursors);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200709 for (i = 0; i < count; ++i) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300710 destroy_cursor_images(&display->cursors[i]);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200711 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700712
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300713 free(display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700714 shm_pool_destroy(display->cursor_shm_pool);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200715}
716
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400717cairo_surface_t *
718display_get_pointer_surface(struct display *display, int pointer,
719 int *width, int *height,
720 int *hotspot_x, int *hotspot_y)
721{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300722 struct cursor *cursor = &display->cursors[pointer];
723 cairo_surface_t *surface = cursor->images[0].surface;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400724
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300725 /* FIXME returning information for the first image. Something better
726 * is needed for animated cursors */
727
nobledf8475c92011-01-05 17:41:55 +0000728 *width = cairo_image_surface_get_width(surface);
729 *height = cairo_image_surface_get_height(surface);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300730
731 *hotspot_x = cursor->images[0].hotspot_x;
732 *hotspot_y = cursor->images[0].hotspot_y;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400733
734 return cairo_surface_reference(surface);
735}
736
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400737static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200738window_get_resize_dx_dy(struct window *window, int *x, int *y)
739{
740 if (window->resize_edges & WINDOW_RESIZING_LEFT)
741 *x = window->server_allocation.width - window->allocation.width;
742 else
743 *x = 0;
744
745 if (window->resize_edges & WINDOW_RESIZING_TOP)
746 *y = window->server_allocation.height -
747 window->allocation.height;
748 else
749 *y = 0;
750
751 window->resize_edges = 0;
752}
753
754static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500755window_attach_surface(struct window *window)
756{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400757 struct display *display = window->display;
758 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000759#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100760 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000761#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500762 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100763
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400764 if (window->type == TYPE_NONE) {
765 window->type = TYPE_TOPLEVEL;
766 if (display->shell)
767 wl_shell_surface_set_toplevel(window->shell_surface);
768 }
769
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100770 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000771#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100772 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
773 data = cairo_surface_get_user_data(window->cairo_surface,
774 &surface_data_key);
775
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100776 cairo_gl_surface_swapbuffers(window->cairo_surface);
777 wl_egl_window_get_attached_size(data->window,
778 &window->server_allocation.width,
779 &window->server_allocation.height);
780 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000781#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100782 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100783 buffer =
784 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400785 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100786
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200787 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400789 wl_surface_damage(window->surface, 0, 0,
790 window->allocation.width,
791 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400793 cairo_surface_destroy(window->cairo_surface);
794 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100795 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000796 default:
797 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500799
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500800 if (window->input_region) {
801 wl_surface_set_input_region(window->surface,
802 window->input_region);
803 wl_region_destroy(window->input_region);
804 window->input_region = NULL;
805 }
806
807 if (window->opaque_region) {
808 wl_surface_set_opaque_region(window->surface,
809 window->opaque_region);
810 wl_region_destroy(window->opaque_region);
811 window->opaque_region = NULL;
812 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500813}
814
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500815void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400816window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500817{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400818 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100819 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500820}
821
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400822void
823window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400824{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500825 cairo_surface_reference(surface);
826
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400827 if (window->cairo_surface != NULL)
828 cairo_surface_destroy(window->cairo_surface);
829
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500830 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400831}
832
Benjamin Franzke22d54812011-07-16 19:50:32 +0000833#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834static void
835window_resize_cairo_window_surface(struct window *window)
836{
837 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200838 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839
840 data = cairo_surface_get_user_data(window->cairo_surface,
841 &surface_data_key);
842
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200843 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100844 wl_egl_window_resize(data->window,
845 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200846 window->allocation.height,
847 x,y);
848
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 cairo_gl_surface_set_size(window->cairo_surface,
850 window->allocation.width,
851 window->allocation.height);
852}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000853#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400855struct display *
856window_get_display(struct window *window)
857{
858 return window->display;
859}
860
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400861void
862window_create_surface(struct window *window)
863{
864 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400865 uint32_t flags = 0;
866
867 if (!window->transparent)
868 flags = SURFACE_OPAQUE;
869
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400870 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500871#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100872 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
873 if (window->cairo_surface) {
874 window_resize_cairo_window_surface(window);
875 return;
876 }
877 surface = display_create_surface(window->display,
878 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400879 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100880 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400881#endif
882 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400883 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400884 &window->allocation,
885 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400886 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800887 default:
888 surface = NULL;
889 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400890 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400891
892 window_set_surface(window, surface);
893 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400894}
895
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200896static void frame_destroy(struct frame *frame);
897
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400898void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500899window_destroy(struct window *window)
900{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200901 struct display *display = window->display;
902 struct input *input;
903
904 if (window->redraw_scheduled)
905 wl_list_remove(&window->redraw_task.link);
906
907 wl_list_for_each(input, &display->input_list, link) {
908 if (input->pointer_focus == window)
909 input->pointer_focus = NULL;
910 if (input->keyboard_focus == window)
911 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500912 if (input->focus_widget &&
913 input->focus_widget->window == window)
914 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200915 }
916
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500917 if (window->input_region)
918 wl_region_destroy(window->input_region);
919 if (window->opaque_region)
920 wl_region_destroy(window->opaque_region);
921
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200922 if (window->frame)
923 frame_destroy(window->frame);
924
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200925 if (window->shell_surface)
926 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500927 wl_surface_destroy(window->surface);
928 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200929
930 if (window->cairo_surface != NULL)
931 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200932
933 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500934 free(window);
935}
936
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500937static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500938widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400939{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500940 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400941
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500942 wl_list_for_each(child, &widget->child_list, link) {
943 target = widget_find_widget(child, x, y);
944 if (target)
945 return target;
946 }
947
948 if (widget->allocation.x <= x &&
949 x < widget->allocation.x + widget->allocation.width &&
950 widget->allocation.y <= y &&
951 y < widget->allocation.y + widget->allocation.height) {
952 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400953 }
954
955 return NULL;
956}
957
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500958static struct widget *
959widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400960{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500961 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400962
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500963 widget = malloc(sizeof *widget);
964 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500965 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500966 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500967 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500968 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500969 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500970
971 return widget;
972}
973
974struct widget *
975window_add_widget(struct window *window, void *data)
976{
977 window->widget = widget_create(window, data);
978 wl_list_init(&window->widget->link);
979
980 return window->widget;
981}
982
983struct widget *
984widget_add_widget(struct widget *parent, void *data)
985{
986 struct widget *widget;
987
988 widget = widget_create(parent->window, data);
989 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400990
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500991 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400992}
993
994void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500995widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200997 struct display *display = widget->window->display;
998 struct input *input;
999
1000 wl_list_for_each(input, &display->input_list, link) {
1001 if (input->focus_widget == widget)
1002 input->focus_widget = NULL;
1003 }
1004
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001005 wl_list_remove(&widget->link);
1006 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007}
1008
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001009void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001010widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001011{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001012 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001013}
1014
1015void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001016widget_set_size(struct widget *widget, int32_t width, int32_t height)
1017{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001018 widget->allocation.width = width;
1019 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001020}
1021
1022void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001023widget_set_allocation(struct widget *widget,
1024 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001025{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001026 widget->allocation.x = x;
1027 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001028 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001029}
1030
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001031void
1032widget_set_transparent(struct widget *widget, int transparent)
1033{
1034 widget->opaque = !transparent;
1035}
1036
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001037void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001038widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001039{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001040 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041}
1042
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001043void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001044widget_set_resize_handler(struct widget *widget,
1045 widget_resize_handler_t handler)
1046{
1047 widget->resize_handler = handler;
1048}
1049
1050void
1051widget_set_redraw_handler(struct widget *widget,
1052 widget_redraw_handler_t handler)
1053{
1054 widget->redraw_handler = handler;
1055}
1056
1057void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001058widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001059{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001060 widget->enter_handler = handler;
1061}
1062
1063void
1064widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1065{
1066 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001067}
1068
1069void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001070widget_set_motion_handler(struct widget *widget,
1071 widget_motion_handler_t handler)
1072{
1073 widget->motion_handler = handler;
1074}
1075
1076void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001077widget_set_button_handler(struct widget *widget,
1078 widget_button_handler_t handler)
1079{
1080 widget->button_handler = handler;
1081}
1082
1083void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001084widget_schedule_redraw(struct widget *widget)
1085{
1086 window_schedule_redraw(widget->window);
1087}
1088
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001089cairo_surface_t *
1090window_get_surface(struct window *window)
1091{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001092 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001093}
1094
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001095struct wl_surface *
1096window_get_wl_surface(struct window *window)
1097{
1098 return window->surface;
1099}
1100
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001101struct wl_shell_surface *
1102window_get_wl_shell_surface(struct window *window)
1103{
1104 return window->shell_surface;
1105}
1106
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001107static void
1108frame_resize_handler(struct widget *widget,
1109 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001110{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001111 struct frame *frame = data;
1112 struct widget *child = frame->child;
1113 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001114 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001115 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001116 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001117
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001118 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001119 decoration_width = (frame->width + frame->margin) * 2;
1120 decoration_height = frame->width +
1121 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001122
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001123 allocation.x = frame->width + frame->margin;
1124 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001125 allocation.width = width - decoration_width;
1126 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001127
1128 widget->window->input_region =
1129 wl_compositor_create_region(display->compositor);
1130 wl_region_add(widget->window->input_region,
1131 frame->margin, frame->margin,
1132 width - 2 * frame->margin,
1133 height - 2 * frame->margin);
1134
1135 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001136 } else {
1137 decoration_width = 0;
1138 decoration_height = 0;
1139
1140 allocation.x = 0;
1141 allocation.y = 0;
1142 allocation.width = width;
1143 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001144 opaque_margin = 0;
1145 }
1146
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001147 widget_set_allocation(child, allocation.x, allocation.y,
1148 allocation.width, allocation.height);
1149
1150 if (child->resize_handler)
1151 child->resize_handler(child,
1152 allocation.width,
1153 allocation.height,
1154 child->user_data);
1155
1156 widget_set_allocation(widget, 0, 0,
1157 child->allocation.width + decoration_width,
1158 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001159
1160 if (child->opaque) {
1161 widget->window->opaque_region =
1162 wl_compositor_create_region(display->compositor);
1163 wl_region_add(widget->window->opaque_region,
1164 opaque_margin, opaque_margin,
1165 widget->allocation.width - 2 * opaque_margin,
1166 widget->allocation.height - 2 * opaque_margin);
1167 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001168}
1169
1170static void
1171frame_redraw_handler(struct widget *widget, void *data)
1172{
1173 struct frame *frame = data;
1174 cairo_t *cr;
1175 cairo_text_extents_t extents;
1176 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001177 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001178 struct window *window = widget->window;
1179
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001180 if (window->type == TYPE_FULLSCREEN)
1181 return;
1182
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001183 width = widget->allocation.width;
1184 height = widget->allocation.height;
1185
1186 cr = cairo_create(window->cairo_surface);
1187
1188 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1189 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1190 cairo_paint(cr);
1191
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001192 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001193 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001194 2, 2, width + 8, height + 8,
1195 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001196
1197 if (window->keyboard_device)
1198 source = window->display->active_frame;
1199 else
1200 source = window->display->inactive_frame;
1201
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001202 tile_source(cr, source,
1203 frame->margin, frame->margin,
1204 width - frame->margin * 2, height - frame->margin * 2,
1205 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001206
1207 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001208 cairo_select_font_face(cr, "sans",
1209 CAIRO_FONT_SLANT_NORMAL,
1210 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001211 cairo_set_font_size(cr, 14);
1212 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001213 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001214 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001215 if (window->keyboard_device) {
1216 cairo_move_to(cr, x + 1, y + 1);
1217 cairo_set_source_rgb(cr, 1, 1, 1);
1218 cairo_show_text(cr, window->title);
1219 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001220 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001221 cairo_show_text(cr, window->title);
1222 } else {
1223 cairo_move_to(cr, x, y);
1224 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1225 cairo_show_text(cr, window->title);
1226 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001227
1228 cairo_destroy(cr);
1229}
1230
1231static int
1232frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1233{
1234 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001235 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001236 const int grip_size = 8;
1237
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001238 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001239 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001240 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001241 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001243 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001244 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001245 hlocation = WINDOW_RESIZING_RIGHT;
1246 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001247 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001248
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001250 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001252 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001253 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001254 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001255 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 vlocation = WINDOW_RESIZING_BOTTOM;
1257 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001258 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001260 location = vlocation | hlocation;
1261 if (location & WINDOW_EXTERIOR)
1262 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001263 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001264 location = WINDOW_TITLEBAR;
1265 else if (location == WINDOW_INTERIOR)
1266 location = WINDOW_CLIENT_AREA;
1267
1268 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001269}
1270
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001271static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001272frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001273{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001274 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001275
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001276 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001277 switch (location) {
1278 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001279 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001280 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001281 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001282 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001283 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001284 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001285 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001286 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001287 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001288 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001289 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001290 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001291 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001292 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001293 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001294 case WINDOW_EXTERIOR:
1295 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001296 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001297 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001298 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001299}
1300
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001301static void
1302frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001303{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001304 switch (index) {
1305 case 0: /* close */
1306 if (window->close_handler)
1307 window->close_handler(window->parent,
1308 window->user_data);
1309 else
1310 display_exit(window->display);
1311 break;
1312 case 1: /* fullscreen */
1313 /* we don't have a way to get out of fullscreen for now */
1314 window_set_fullscreen(window, 1);
1315 break;
1316 case 2: /* rotate */
1317 case 3: /* scale */
1318 break;
1319 }
1320}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001321
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001322void
1323window_show_frame_menu(struct window *window,
1324 struct input *input, uint32_t time)
1325{
1326 int32_t x, y;
1327
1328 static const char *entries[] = {
1329 "Close", "Fullscreen", "Rotate", "Scale"
1330 };
1331
1332 input_get_position(input, &x, &y);
1333 window_show_menu(window->display, input, time, window,
1334 x - 10, y - 10, frame_menu_func, entries, 4);
1335}
1336
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001337static int
1338frame_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001339 struct input *input, int32_t x, int32_t y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001340{
1341 return frame_get_pointer_image_for_location(data, input);
1342}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001343
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001344static int
1345frame_motion_handler(struct widget *widget,
1346 struct input *input, uint32_t time,
1347 int32_t x, int32_t y, void *data)
1348{
1349 return frame_get_pointer_image_for_location(data, input);
1350}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001351
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001352static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001353frame_button_handler(struct widget *widget,
1354 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001355 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001356
1357{
1358 struct frame *frame = data;
1359 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001360 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001361 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001362
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001363 location = frame_get_pointer_location(frame, input->sx, input->sy);
1364
1365 if (window->display->shell && button == BTN_LEFT && state == 1) {
1366 switch (location) {
1367 case WINDOW_TITLEBAR:
1368 if (!window->shell_surface)
1369 break;
1370 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001371 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001372 wl_shell_surface_move(window->shell_surface,
1373 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001374 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001375 break;
1376 case WINDOW_RESIZING_TOP:
1377 case WINDOW_RESIZING_BOTTOM:
1378 case WINDOW_RESIZING_LEFT:
1379 case WINDOW_RESIZING_RIGHT:
1380 case WINDOW_RESIZING_TOP_LEFT:
1381 case WINDOW_RESIZING_TOP_RIGHT:
1382 case WINDOW_RESIZING_BOTTOM_LEFT:
1383 case WINDOW_RESIZING_BOTTOM_RIGHT:
1384 if (!window->shell_surface)
1385 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001386 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001387
1388 if (!display->dpy) {
1389 /* If we're using shm, allocate a big
1390 pool to create buffers out of while
1391 we resize. We should probably base
1392 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001393 window->pool =
1394 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001395 }
1396
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001397 wl_shell_surface_resize(window->shell_surface,
1398 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001399 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001400 break;
1401 }
1402 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001403 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001404 }
1405}
1406
1407struct widget *
1408frame_create(struct window *window, void *data)
1409{
1410 struct frame *frame;
1411
1412 frame = malloc(sizeof *frame);
1413 memset(frame, 0, sizeof *frame);
1414
1415 frame->widget = window_add_widget(window, frame);
1416 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001417 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001418 frame->width = 4;
1419 frame->titlebar_height = 30
1420;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001421 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1422 widget_set_resize_handler(frame->widget, frame_resize_handler);
1423 widget_set_enter_handler(frame->widget, frame_enter_handler);
1424 widget_set_motion_handler(frame->widget, frame_motion_handler);
1425 widget_set_button_handler(frame->widget, frame_button_handler);
1426
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001427 window->frame = frame;
1428
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001429 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001430}
1431
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001432static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001433frame_destroy(struct frame *frame)
1434{
1435 /* frame->child must be destroyed by the application */
1436 widget_destroy(frame->widget);
1437 free(frame);
1438}
1439
1440static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001441input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001442 int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001443{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001444 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001445 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001446
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001447 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001448 return;
1449
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001450 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001451 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001452 widget = old;
1453 if (input->grab)
1454 widget = input->grab;
1455 if (widget->leave_handler)
1456 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001457 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001458 }
1459
1460 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001461 widget = focus;
1462 if (input->grab)
1463 widget = input->grab;
1464 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001465 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001466 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001467 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001468
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001469 input_set_pointer_image(input, input->pointer_enter_serial,
1470 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001471 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001472}
1473
1474static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001475input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001476 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001477{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001478 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001479 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001480 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001481 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001482
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001483 input->sx = sx;
1484 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001485
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001486 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001487 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001488 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001489 }
1490
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001491 if (input->grab)
1492 widget = input->grab;
1493 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001494 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001495 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001496 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001497 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001498 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001499
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001500 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001501}
1502
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001503void
1504input_grab(struct input *input, struct widget *widget, uint32_t button)
1505{
1506 input->grab = widget;
1507 input->grab_button = button;
1508}
1509
1510void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001511input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001512{
1513 struct widget *widget;
1514
1515 input->grab = NULL;
1516 if (input->pointer_focus) {
1517 widget = widget_find_widget(input->pointer_focus->widget,
1518 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001519 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001520 }
1521}
1522
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001523static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001524input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001525 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001526 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001527{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001528 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001529 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001530
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001531 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001532 if (input->focus_widget && input->grab == NULL && state)
1533 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001534
Neil Roberts6b28aad2012-01-23 19:11:18 +00001535 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001536 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001537 (*widget->button_handler)(widget,
1538 input, time,
1539 button, state,
1540 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001541
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001542 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001543 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001544}
1545
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001546static void
Scott Moreau210d0792012-03-22 10:47:01 -06001547input_handle_axis(void *data,
1548 struct wl_input_device *input_device,
1549 uint32_t time, uint32_t axis, int32_t value)
1550{
1551}
1552
1553static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001554input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001555 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001556{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001557 struct input *input = data;
1558 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001559 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001560 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001561
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001562 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001563 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001564 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001565 return;
1566
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001567 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001568 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001569 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1570 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001571
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001572 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1573
1574 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001575 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001576 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001577 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001578
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001579 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1580 if (state)
1581 window_set_maximized(window,
1582 window->type != TYPE_MAXIMIZED);
1583 } else if (window->key_handler) {
1584 (*window->key_handler)(window, input, time, key,
1585 sym, state, window->user_data);
1586 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001587}
1588
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001589static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001590input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001591{
1592 struct window *window = input->pointer_focus;
1593
1594 if (!window)
1595 return;
1596
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001597 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001598
Pekka Paalanene1207c72011-12-16 12:02:09 +02001599 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001600 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001601}
1602
1603static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001604input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001605 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001606 uint32_t serial, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001607 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001608{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001609 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001610 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001611 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001612
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001613 input->display->serial = serial;
1614 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001615 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001616 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001617
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001618 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001619 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001620 window->pool = NULL;
1621 /* Schedule a redraw to free the pool */
1622 window_schedule_redraw(window);
1623 }
1624
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001625 input->sx = sx;
1626 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001627
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001628 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001629 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001630}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001631
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001632static void
1633input_handle_pointer_leave(void *data,
1634 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001635 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001636{
1637 struct input *input = data;
1638
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001639 input->display->serial = serial;
1640 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001641}
1642
1643static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001644input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001645{
1646 struct window *window = input->keyboard_focus;
1647
1648 if (!window)
1649 return;
1650
1651 window->keyboard_device = NULL;
1652 if (window->keyboard_focus_handler)
1653 (*window->keyboard_focus_handler)(window, NULL,
1654 window->user_data);
1655
1656 input->keyboard_focus = NULL;
1657}
1658
1659static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001660input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001661 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001662 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001663 struct wl_surface *surface,
1664 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001665{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001666 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001667 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001668 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001669 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001670
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001671 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001672 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001673
1674 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001675 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001676 for (k = keys->data; k < end; k++)
1677 input->modifiers |= d->xkb->map->modmap[*k];
1678
1679 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001680 window->keyboard_device = input;
1681 if (window->keyboard_focus_handler)
1682 (*window->keyboard_focus_handler)(window,
1683 window->keyboard_device,
1684 window->user_data);
1685}
1686
1687static void
1688input_handle_keyboard_leave(void *data,
1689 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001690 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001691 struct wl_surface *surface)
1692{
1693 struct input *input = data;
1694
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001695 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001696 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001697}
1698
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001699static void
1700input_handle_touch_down(void *data,
1701 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001702 uint32_t serial, uint32_t time,
1703 struct wl_surface *surface,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001704 int32_t id, int32_t x, int32_t y)
1705{
1706}
1707
1708static void
1709input_handle_touch_up(void *data,
1710 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001711 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001712{
1713}
1714
1715static void
1716input_handle_touch_motion(void *data,
1717 struct wl_input_device *wl_input_device,
1718 uint32_t time, int32_t id, int32_t x, int32_t y)
1719{
1720}
1721
1722static void
1723input_handle_touch_frame(void *data,
1724 struct wl_input_device *wl_input_device)
1725{
1726}
1727
1728static void
1729input_handle_touch_cancel(void *data,
1730 struct wl_input_device *wl_input_device)
1731{
1732}
1733
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001734static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001735 input_handle_motion,
1736 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001737 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001738 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001739 input_handle_pointer_enter,
1740 input_handle_pointer_leave,
1741 input_handle_keyboard_enter,
1742 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001743 input_handle_touch_down,
1744 input_handle_touch_up,
1745 input_handle_touch_motion,
1746 input_handle_touch_frame,
1747 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001748};
1749
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001750void
1751input_get_position(struct input *input, int32_t *x, int32_t *y)
1752{
1753 *x = input->sx;
1754 *y = input->sy;
1755}
1756
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001757struct wl_input_device *
1758input_get_input_device(struct input *input)
1759{
1760 return input->input_device;
1761}
1762
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001763uint32_t
1764input_get_modifiers(struct input *input)
1765{
1766 return input->modifiers;
1767}
1768
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001769struct widget *
1770input_get_focus_widget(struct input *input)
1771{
1772 return input->focus_widget;
1773}
1774
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001775struct data_offer {
1776 struct wl_data_offer *offer;
1777 struct input *input;
1778 struct wl_array types;
1779 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001780
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001781 struct task io_task;
1782 int fd;
1783 data_func_t func;
1784 int32_t x, y;
1785 void *user_data;
1786};
1787
1788static void
1789data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1790{
1791 struct data_offer *offer = data;
1792 char **p;
1793
1794 p = wl_array_add(&offer->types, sizeof *p);
1795 *p = strdup(type);
1796}
1797
1798static const struct wl_data_offer_listener data_offer_listener = {
1799 data_offer_offer,
1800};
1801
1802static void
1803data_offer_destroy(struct data_offer *offer)
1804{
1805 char **p;
1806
1807 offer->refcount--;
1808 if (offer->refcount == 0) {
1809 wl_data_offer_destroy(offer->offer);
1810 for (p = offer->types.data; *p; p++)
1811 free(*p);
1812 wl_array_release(&offer->types);
1813 free(offer);
1814 }
1815}
1816
1817static void
1818data_device_data_offer(void *data,
1819 struct wl_data_device *data_device, uint32_t id)
1820{
1821 struct data_offer *offer;
1822
1823 offer = malloc(sizeof *offer);
1824
1825 wl_array_init(&offer->types);
1826 offer->refcount = 1;
1827 offer->input = data;
1828
1829 /* FIXME: Generate typesafe wrappers for this */
1830 offer->offer = (struct wl_data_offer *)
1831 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1832 id, &wl_data_offer_interface);
1833
1834 wl_data_offer_add_listener(offer->offer,
1835 &data_offer_listener, offer);
1836}
1837
1838static void
1839data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001840 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001841 int32_t x, int32_t y, struct wl_data_offer *offer)
1842{
1843 struct input *input = data;
1844 struct window *window;
1845 char **p;
1846
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001847 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001848 input->drag_offer = wl_data_offer_get_user_data(offer);
1849 window = wl_surface_get_user_data(surface);
1850 input->pointer_focus = window;
1851
1852 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1853 *p = NULL;
1854
1855 window = input->pointer_focus;
1856 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001857 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001858 input->drag_offer->types.data,
1859 window->user_data);
1860}
1861
1862static void
1863data_device_leave(void *data, struct wl_data_device *data_device)
1864{
1865 struct input *input = data;
1866
1867 data_offer_destroy(input->drag_offer);
1868 input->drag_offer = NULL;
1869}
1870
1871static void
1872data_device_motion(void *data, struct wl_data_device *data_device,
1873 uint32_t time, int32_t x, int32_t y)
1874{
1875 struct input *input = data;
1876 struct window *window = input->pointer_focus;
1877
1878 input->sx = x;
1879 input->sy = y;
1880
1881 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001882 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001883 input->drag_offer->types.data,
1884 window->user_data);
1885}
1886
1887static void
1888data_device_drop(void *data, struct wl_data_device *data_device)
1889{
1890 struct input *input = data;
1891 struct window *window = input->pointer_focus;
1892
1893 if (window->drop_handler)
1894 window->drop_handler(window, input,
1895 input->sx, input->sy, window->user_data);
1896}
1897
1898static void
1899data_device_selection(void *data,
1900 struct wl_data_device *wl_data_device,
1901 struct wl_data_offer *offer)
1902{
1903 struct input *input = data;
1904 char **p;
1905
1906 if (input->selection_offer)
1907 data_offer_destroy(input->selection_offer);
1908
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001909 if (offer) {
1910 input->selection_offer = wl_data_offer_get_user_data(offer);
1911 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1912 *p = NULL;
1913 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001914}
1915
1916static const struct wl_data_device_listener data_device_listener = {
1917 data_device_data_offer,
1918 data_device_enter,
1919 data_device_leave,
1920 data_device_motion,
1921 data_device_drop,
1922 data_device_selection
1923};
1924
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001925void
1926input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1927{
1928 struct display *display = input->display;
1929 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001930 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001931
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001932 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001933 return;
1934
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001935 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001936
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001937 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001938 return;
1939
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001940 input->current_cursor = pointer;
1941 buffer = display_get_buffer_for_surface(display, image->surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001942 wl_input_device_attach(input->input_device, time, buffer,
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001943 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001944}
1945
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001946struct wl_data_device *
1947input_get_data_device(struct input *input)
1948{
1949 return input->data_device;
1950}
1951
1952void
1953input_set_selection(struct input *input,
1954 struct wl_data_source *source, uint32_t time)
1955{
1956 wl_data_device_set_selection(input->data_device, source, time);
1957}
1958
1959void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001960input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001961{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001962 wl_data_offer_accept(input->drag_offer->offer,
1963 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001964}
1965
1966static void
1967offer_io_func(struct task *task, uint32_t events)
1968{
1969 struct data_offer *offer =
1970 container_of(task, struct data_offer, io_task);
1971 unsigned int len;
1972 char buffer[4096];
1973
1974 len = read(offer->fd, buffer, sizeof buffer);
1975 offer->func(buffer, len,
1976 offer->x, offer->y, offer->user_data);
1977
1978 if (len == 0) {
1979 close(offer->fd);
1980 data_offer_destroy(offer);
1981 }
1982}
1983
1984static void
1985data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1986 data_func_t func, void *user_data)
1987{
1988 int p[2];
1989
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02001990 if (pipe2(p, O_CLOEXEC) == -1)
1991 return;
1992
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001993 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1994 close(p[1]);
1995
1996 offer->io_task.run = offer_io_func;
1997 offer->fd = p[0];
1998 offer->func = func;
1999 offer->refcount++;
2000 offer->user_data = user_data;
2001
2002 display_watch_fd(offer->input->display,
2003 offer->fd, EPOLLIN, &offer->io_task);
2004}
2005
2006void
2007input_receive_drag_data(struct input *input, const char *mime_type,
2008 data_func_t func, void *data)
2009{
2010 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2011 input->drag_offer->x = input->sx;
2012 input->drag_offer->y = input->sy;
2013}
2014
2015int
2016input_receive_selection_data(struct input *input, const char *mime_type,
2017 data_func_t func, void *data)
2018{
2019 char **p;
2020
2021 if (input->selection_offer == NULL)
2022 return -1;
2023
2024 for (p = input->selection_offer->types.data; *p; p++)
2025 if (strcmp(mime_type, *p) == 0)
2026 break;
2027
2028 if (*p == NULL)
2029 return -1;
2030
2031 data_offer_receive_data(input->selection_offer,
2032 mime_type, func, data);
2033 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002034}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002035
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002036int
2037input_receive_selection_data_to_fd(struct input *input,
2038 const char *mime_type, int fd)
2039{
2040 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2041
2042 return 0;
2043}
2044
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002045void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002046window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002047{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002048 if (!window->shell_surface)
2049 return;
2050
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002051 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002052 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002053}
2054
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002055static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002056idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002057{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002058 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002059
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002060 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002061 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002062 widget_set_allocation(widget,
2063 window->pending_allocation.x,
2064 window->pending_allocation.y,
2065 window->pending_allocation.width,
2066 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002067
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002068 if (window->input_region) {
2069 wl_region_destroy(window->input_region);
2070 window->input_region = NULL;
2071 }
2072
2073 if (window->opaque_region) {
2074 wl_region_destroy(window->opaque_region);
2075 window->opaque_region = NULL;
2076 }
2077
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002078 if (widget->resize_handler)
2079 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002080 widget->allocation.width,
2081 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002082 widget->user_data);
2083
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002084 if (window->allocation.width != widget->allocation.width ||
2085 window->allocation.height != widget->allocation.height) {
2086 window->allocation = widget->allocation;
2087 window_schedule_redraw(window);
2088 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002089}
2090
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002091void
2092window_schedule_resize(struct window *window, int width, int height)
2093{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002094 window->pending_allocation.x = 0;
2095 window->pending_allocation.y = 0;
2096 window->pending_allocation.width = width;
2097 window->pending_allocation.height = height;
2098
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002099 window->resize_needed = 1;
2100 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002101}
2102
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002103void
2104widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2105{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002106 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002107}
2108
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002109static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002110handle_ping(void *data, struct wl_shell_surface *shell_surface,
2111 uint32_t serial)
2112{
2113 wl_shell_surface_pong(shell_surface, serial);
2114}
2115
2116static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002117handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002118 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002119{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002120 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002121
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002122 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002123 return;
2124
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002125 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002126 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002127}
2128
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002129static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002130menu_destroy(struct menu *menu)
2131{
2132 widget_destroy(menu->widget);
2133 window_destroy(menu->window);
2134 free(menu);
2135}
2136
2137static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002138handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2139{
2140 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002141 struct menu *menu = window->widget->user_data;
2142
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002143 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002144 * device. Or just use wl_callback. And this really needs to
2145 * be a window vfunc that the menu can set. And we need the
2146 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002147
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002148 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002149 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002150 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002151}
2152
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002153static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002154 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002155 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002156 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002157};
2158
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002159void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002160window_get_allocation(struct window *window,
2161 struct rectangle *allocation)
2162{
2163 *allocation = window->allocation;
2164}
2165
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002166static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002167widget_redraw(struct widget *widget)
2168{
2169 struct widget *child;
2170
2171 if (widget->redraw_handler)
2172 widget->redraw_handler(widget, widget->user_data);
2173 wl_list_for_each(child, &widget->child_list, link)
2174 widget_redraw(child);
2175}
2176
2177static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002178frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2179{
2180 struct window *window = data;
2181
2182 wl_callback_destroy(callback);
2183 window->redraw_scheduled = 0;
2184 if (window->redraw_needed)
2185 window_schedule_redraw(window);
2186}
2187
2188static const struct wl_callback_listener listener = {
2189 frame_callback
2190};
2191
2192static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002193idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002194{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002195 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002196 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002197
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002198 if (window->resize_needed)
2199 idle_resize(window);
2200
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002201 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002202 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002203 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002204 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002205 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002206
2207 callback = wl_surface_frame(window->surface);
2208 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002209}
2210
2211void
2212window_schedule_redraw(struct window *window)
2213{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002214 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002215 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002216 window->redraw_task.run = idle_redraw;
2217 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002218 window->redraw_scheduled = 1;
2219 }
2220}
2221
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002222void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002223window_set_custom(struct window *window)
2224{
2225 window->type = TYPE_CUSTOM;
2226}
2227
2228void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002229window_set_fullscreen(struct window *window, int fullscreen)
2230{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002231 if (!window->display->shell)
2232 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002233
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002234 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002235 return;
2236
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002237 if (fullscreen) {
2238 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002239 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002240 wl_shell_surface_set_fullscreen(window->shell_surface,
2241 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2242 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002243 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002244 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002245 wl_shell_surface_set_toplevel(window->shell_surface);
2246 window_schedule_resize(window,
2247 window->saved_allocation.width,
2248 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002249 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002250}
2251
2252void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002253window_set_maximized(struct window *window, int maximized)
2254{
2255 if (!window->display->shell)
2256 return;
2257
2258 if ((window->type == TYPE_MAXIMIZED) == maximized)
2259 return;
2260
2261 if (window->type == TYPE_TOPLEVEL) {
2262 window->saved_allocation = window->allocation;
2263 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2264 window->type = TYPE_MAXIMIZED;
2265 } else {
2266 wl_shell_surface_set_toplevel(window->shell_surface);
2267 window->type = TYPE_TOPLEVEL;
2268 window_schedule_resize(window,
2269 window->saved_allocation.width,
2270 window->saved_allocation.height);
2271 }
2272}
2273
2274void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002275window_set_user_data(struct window *window, void *data)
2276{
2277 window->user_data = data;
2278}
2279
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002280void *
2281window_get_user_data(struct window *window)
2282{
2283 return window->user_data;
2284}
2285
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002286void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002287window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002288 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002289{
2290 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002291}
2292
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002293void
2294window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002295 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002296{
2297 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002298}
2299
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002300void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002301window_set_data_handler(struct window *window, window_data_handler_t handler)
2302{
2303 window->data_handler = handler;
2304}
2305
2306void
2307window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2308{
2309 window->drop_handler = handler;
2310}
2311
2312void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002313window_set_close_handler(struct window *window,
2314 window_close_handler_t handler)
2315{
2316 window->close_handler = handler;
2317}
2318
2319void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002320window_set_title(struct window *window, const char *title)
2321{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002322 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002323 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002324 if (window->shell_surface)
2325 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002326}
2327
2328const char *
2329window_get_title(struct window *window)
2330{
2331 return window->title;
2332}
2333
2334void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002335window_damage(struct window *window, int32_t x, int32_t y,
2336 int32_t width, int32_t height)
2337{
2338 wl_surface_damage(window->surface, x, y, width, height);
2339}
2340
Casey Dahlin9074db52012-04-19 22:50:09 -04002341static void
2342surface_enter(void *data,
2343 struct wl_surface *wl_surface, struct wl_output *output)
2344{
2345}
2346
2347static void
2348surface_leave(void *data,
2349 struct wl_surface *wl_surface, struct wl_output *output)
2350{
2351}
2352
2353static const struct wl_surface_listener surface_listener = {
2354 surface_enter,
2355 surface_leave
2356};
2357
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002358static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002359window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002360{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002361 struct window *window;
2362
2363 window = malloc(sizeof *window);
2364 if (window == NULL)
2365 return NULL;
2366
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002367 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002368 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002369 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002370 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002371 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002372 if (display->shell) {
2373 window->shell_surface =
2374 wl_shell_get_shell_surface(display->shell,
2375 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002376 if (window->title)
2377 wl_shell_surface_set_title(window->shell_surface,
2378 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002379 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002380 window->allocation.x = 0;
2381 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002382 window->allocation.width = 0;
2383 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002384 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002385 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002386 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002387 window->input_region = NULL;
2388 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002389
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002390 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002391#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002392 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002393#else
2394 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2395#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002396 else
2397 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002398
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002399 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002400 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002401 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002402
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002403 if (window->shell_surface) {
2404 wl_shell_surface_set_user_data(window->shell_surface, window);
2405 wl_shell_surface_add_listener(window->shell_surface,
2406 &shell_surface_listener, window);
2407 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002408
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002409 return window;
2410}
2411
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002412struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002413window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002414{
2415 struct window *window;
2416
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002417 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002418 if (!window)
2419 return NULL;
2420
2421 return window;
2422}
2423
2424struct window *
2425window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002426 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002427{
2428 struct window *window;
2429
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002430 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002431 if (!window)
2432 return NULL;
2433
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002434 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002435 window->x = x;
2436 window->y = y;
2437
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002438 if (display->shell)
2439 wl_shell_surface_set_transient(window->shell_surface,
2440 window->parent->shell_surface,
2441 window->x, window->y, 0);
2442
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002443 return window;
2444}
2445
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002446static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002447menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002448{
2449 int next;
2450
2451 next = (sy - 8) / 20;
2452 if (menu->current != next) {
2453 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002454 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002455 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002456}
2457
2458static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002459menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002460 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002461 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002462{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002463 struct menu *menu = data;
2464
2465 if (widget == menu->widget)
2466 menu_set_item(data, y);
2467
2468 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002469}
2470
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002471static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002472menu_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002473 struct input *input, int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002474{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002475 struct menu *menu = data;
2476
2477 if (widget == menu->widget)
2478 menu_set_item(data, y);
2479
2480 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002481}
2482
2483static void
2484menu_leave_handler(struct widget *widget, struct input *input, void *data)
2485{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002486 struct menu *menu = data;
2487
2488 if (widget == menu->widget)
2489 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002490}
2491
2492static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002493menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002494 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002495 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002496
2497{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002498 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002499
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002500 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002501 /* Either relase after press-drag-release or
2502 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002503 menu->func(menu->window->parent,
2504 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002505 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002506 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002507 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002508}
2509
2510static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002511menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002512{
2513 cairo_t *cr;
2514 const int32_t r = 3, margin = 3;
2515 struct menu *menu = data;
2516 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002517 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002518
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002519 cr = cairo_create(window->cairo_surface);
2520 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2521 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2522 cairo_paint(cr);
2523
2524 width = window->allocation.width;
2525 height = window->allocation.height;
2526 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002527
2528 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002529 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2530 cairo_fill(cr);
2531
2532 for (i = 0; i < menu->count; i++) {
2533 if (i == menu->current) {
2534 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2535 cairo_rectangle(cr, margin, i * 20 + margin,
2536 width - 2 * margin, 20);
2537 cairo_fill(cr);
2538 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2539 cairo_move_to(cr, 10, i * 20 + 16);
2540 cairo_show_text(cr, menu->entries[i]);
2541 } else {
2542 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2543 cairo_move_to(cr, 10, i * 20 + 16);
2544 cairo_show_text(cr, menu->entries[i]);
2545 }
2546 }
2547
2548 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002549}
2550
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002551void
2552window_show_menu(struct display *display,
2553 struct input *input, uint32_t time, struct window *parent,
2554 int32_t x, int32_t y,
2555 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002556{
2557 struct window *window;
2558 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002559 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002560
2561 menu = malloc(sizeof *menu);
2562 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002563 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002564
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002565 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002566 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002567 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002568
2569 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002570 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002571 menu->entries = entries;
2572 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002573 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002574 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002575 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002576 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002577 window->type = TYPE_MENU;
2578 window->x = x;
2579 window->y = y;
2580
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002581 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002582 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2583 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002584 window->parent->shell_surface,
2585 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002586
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002587 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002588 widget_set_enter_handler(menu->widget, menu_enter_handler);
2589 widget_set_leave_handler(menu->widget, menu_leave_handler);
2590 widget_set_motion_handler(menu->widget, menu_motion_handler);
2591 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002592
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002593 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002594 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002595}
2596
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002597void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002598window_set_buffer_type(struct window *window, enum window_buffer_type type)
2599{
2600 window->buffer_type = type;
2601}
2602
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002603
2604static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002605display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002606 struct wl_output *wl_output,
2607 int x, int y,
2608 int physical_width,
2609 int physical_height,
2610 int subpixel,
2611 const char *make,
2612 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002613{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002614 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002615
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002616 output->allocation.x = x;
2617 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002618}
2619
2620static void
2621display_handle_mode(void *data,
2622 struct wl_output *wl_output,
2623 uint32_t flags,
2624 int width,
2625 int height,
2626 int refresh)
2627{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002628 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002629 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002630
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002631 if (flags & WL_OUTPUT_MODE_CURRENT) {
2632 output->allocation.width = width;
2633 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002634 if (display->output_configure_handler)
2635 (*display->output_configure_handler)(
2636 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002637 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002638}
2639
2640static const struct wl_output_listener output_listener = {
2641 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002642 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002643};
2644
2645static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002646display_add_output(struct display *d, uint32_t id)
2647{
2648 struct output *output;
2649
2650 output = malloc(sizeof *output);
2651 if (output == NULL)
2652 return;
2653
2654 memset(output, 0, sizeof *output);
2655 output->display = d;
2656 output->output =
2657 wl_display_bind(d->display, id, &wl_output_interface);
2658 wl_list_insert(d->output_list.prev, &output->link);
2659
2660 wl_output_add_listener(output->output, &output_listener, output);
2661}
2662
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002663static void
2664output_destroy(struct output *output)
2665{
2666 if (output->destroy_handler)
2667 (*output->destroy_handler)(output, output->user_data);
2668
2669 wl_output_destroy(output->output);
2670 wl_list_remove(&output->link);
2671 free(output);
2672}
2673
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002674void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002675display_set_output_configure_handler(struct display *display,
2676 display_output_handler_t handler)
2677{
2678 struct output *output;
2679
2680 display->output_configure_handler = handler;
2681 if (!handler)
2682 return;
2683
2684 wl_list_for_each(output, &display->output_list, link)
2685 (*display->output_configure_handler)(output,
2686 display->user_data);
2687}
2688
2689void
2690output_set_user_data(struct output *output, void *data)
2691{
2692 output->user_data = data;
2693}
2694
2695void *
2696output_get_user_data(struct output *output)
2697{
2698 return output->user_data;
2699}
2700
2701void
2702output_set_destroy_handler(struct output *output,
2703 display_output_handler_t handler)
2704{
2705 output->destroy_handler = handler;
2706 /* FIXME: implement this, once we have way to remove outputs */
2707}
2708
2709void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002710output_get_allocation(struct output *output, struct rectangle *allocation)
2711{
2712 *allocation = output->allocation;
2713}
2714
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002715struct wl_output *
2716output_get_wl_output(struct output *output)
2717{
2718 return output->output;
2719}
2720
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002721static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002722display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002723{
2724 struct input *input;
2725
2726 input = malloc(sizeof *input);
2727 if (input == NULL)
2728 return;
2729
2730 memset(input, 0, sizeof *input);
2731 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002732 input->input_device =
2733 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002734 input->pointer_focus = NULL;
2735 input->keyboard_focus = NULL;
2736 wl_list_insert(d->input_list.prev, &input->link);
2737
2738 wl_input_device_add_listener(input->input_device,
2739 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002740 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002741
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002742 input->data_device =
2743 wl_data_device_manager_get_data_device(d->data_device_manager,
2744 input->input_device);
2745 wl_data_device_add_listener(input->data_device,
2746 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002747}
2748
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002749static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002750input_destroy(struct input *input)
2751{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002752 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002753 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002754
2755 if (input->drag_offer)
2756 data_offer_destroy(input->drag_offer);
2757
2758 if (input->selection_offer)
2759 data_offer_destroy(input->selection_offer);
2760
2761 wl_data_device_destroy(input->data_device);
2762 wl_list_remove(&input->link);
2763 wl_input_device_destroy(input->input_device);
2764 free(input);
2765}
2766
2767static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002768display_handle_global(struct wl_display *display, uint32_t id,
2769 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002770{
2771 struct display *d = data;
2772
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002773 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002774 d->compositor =
2775 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002776 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002777 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002778 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002779 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002780 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002781 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002782 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002783 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002784 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2785 d->data_device_manager =
2786 wl_display_bind(display, id,
2787 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002788 }
2789}
2790
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002791static void
2792display_render_frame(struct display *d)
2793{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002794 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002795 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002796
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002797 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002798 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2799 cr = cairo_create(d->shadow);
2800 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2801 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002802 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002803 cairo_fill(cr);
2804 cairo_destroy(cr);
2805 blur_surface(d->shadow, 64);
2806
2807 d->active_frame =
2808 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2809 cr = cairo_create(d->active_frame);
2810 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002811
2812 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2813 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2814 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2815 cairo_set_source(cr, pattern);
2816 cairo_pattern_destroy(pattern);
2817
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002818 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002819 cairo_fill(cr);
2820 cairo_destroy(cr);
2821
2822 d->inactive_frame =
2823 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2824 cr = cairo_create(d->inactive_frame);
2825 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002826 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002827 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002828 cairo_fill(cr);
2829 cairo_destroy(cr);
2830}
2831
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002832static void
2833init_xkb(struct display *d)
2834{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002835 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002836
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002837 names.rules = "evdev";
2838 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002839 names.layout = option_xkb_layout;
2840 names.variant = option_xkb_variant;
2841 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002842
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002843 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002844 if (!d->xkb) {
2845 fprintf(stderr, "Failed to compile keymap\n");
2846 exit(1);
2847 }
2848}
2849
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002850static void
2851fini_xkb(struct display *display)
2852{
2853 xkb_free_keymap(display->xkb);
2854}
2855
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002856#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002857static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002858init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002859{
2860 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002861 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002862
Rob Clark6396ed32012-03-11 19:48:41 -05002863#ifdef USE_CAIRO_GLESV2
2864# define GL_BIT EGL_OPENGL_ES2_BIT
2865#else
2866# define GL_BIT EGL_OPENGL_BIT
2867#endif
2868
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002869 static const EGLint argb_cfg_attribs[] = {
2870 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002871 EGL_RED_SIZE, 1,
2872 EGL_GREEN_SIZE, 1,
2873 EGL_BLUE_SIZE, 1,
2874 EGL_ALPHA_SIZE, 1,
2875 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002876 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002877 EGL_NONE
2878 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002879
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002880#ifdef USE_CAIRO_GLESV2
2881 static const EGLint context_attribs[] = {
2882 EGL_CONTEXT_CLIENT_VERSION, 2,
2883 EGL_NONE
2884 };
2885 EGLint api = EGL_OPENGL_ES_API;
2886#else
2887 EGLint *context_attribs = NULL;
2888 EGLint api = EGL_OPENGL_API;
2889#endif
2890
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002891 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002892 if (!eglInitialize(d->dpy, &major, &minor)) {
2893 fprintf(stderr, "failed to initialize display\n");
2894 return -1;
2895 }
2896
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002897 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002898 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2899 return -1;
2900 }
2901
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002902 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2903 &d->argb_config, 1, &n) || n != 1) {
2904 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002905 return -1;
2906 }
2907
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002908 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002909 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002910 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002911 fprintf(stderr, "failed to create context\n");
2912 return -1;
2913 }
2914
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002915 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002916 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002917 return -1;
2918 }
2919
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002920#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002921 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2922 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2923 fprintf(stderr, "failed to get cairo egl argb device\n");
2924 return -1;
2925 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002926#endif
2927
2928 return 0;
2929}
2930
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002931static void
2932fini_egl(struct display *display)
2933{
2934#ifdef HAVE_CAIRO_EGL
2935 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002936#endif
2937
2938 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2939 EGL_NO_CONTEXT);
2940
2941 eglTerminate(display->dpy);
2942 eglReleaseThread();
2943}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002944#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002945
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002946static int
2947event_mask_update(uint32_t mask, void *data)
2948{
2949 struct display *d = data;
2950
2951 d->mask = mask;
2952
2953 return 0;
2954}
2955
2956static void
2957handle_display_data(struct task *task, uint32_t events)
2958{
2959 struct display *display =
2960 container_of(task, struct display, display_task);
2961
2962 wl_display_iterate(display->display, display->mask);
2963}
2964
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002965struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002966display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002967{
2968 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002969
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002970 argc = parse_options(xkb_options,
2971 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002972
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002973 d = malloc(sizeof *d);
2974 if (d == NULL)
2975 return NULL;
2976
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002977 memset(d, 0, sizeof *d);
2978
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002979 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002980 if (d->display == NULL) {
2981 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002982 return NULL;
2983 }
2984
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002985 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2986 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2987 d->display_task.run = handle_display_data;
2988 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2989
2990 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002991 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002992 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002993
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002994 /* Set up listener so we'll catch all events. */
2995 wl_display_add_global_listener(d->display,
2996 display_handle_global, d);
2997
2998 /* Process connection events. */
2999 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003000#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003001 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003002 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003003#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003004
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003005 d->image_target_texture_2d =
3006 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3007 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3008 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3009
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003010 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003011
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003012 display_render_frame(d);
3013
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003014 wl_list_init(&d->window_list);
3015
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003016 init_xkb(d);
3017
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003018 return d;
3019}
3020
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003021static void
3022display_destroy_outputs(struct display *display)
3023{
3024 struct output *tmp;
3025 struct output *output;
3026
3027 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3028 output_destroy(output);
3029}
3030
Pekka Paalanene1207c72011-12-16 12:02:09 +02003031static void
3032display_destroy_inputs(struct display *display)
3033{
3034 struct input *tmp;
3035 struct input *input;
3036
3037 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3038 input_destroy(input);
3039}
3040
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003041void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003042display_destroy(struct display *display)
3043{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003044 if (!wl_list_empty(&display->window_list))
3045 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3046
3047 if (!wl_list_empty(&display->deferred_list))
3048 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3049
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003050 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003051 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003052
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003053 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003054
3055 cairo_surface_destroy(display->active_frame);
3056 cairo_surface_destroy(display->inactive_frame);
3057 cairo_surface_destroy(display->shadow);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003058 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003059
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003060#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003061 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003062#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003063
Pekka Paalanenc2052982011-12-16 11:41:32 +02003064 if (display->shell)
3065 wl_shell_destroy(display->shell);
3066
3067 if (display->shm)
3068 wl_shm_destroy(display->shm);
3069
3070 if (display->data_device_manager)
3071 wl_data_device_manager_destroy(display->data_device_manager);
3072
3073 wl_compositor_destroy(display->compositor);
3074
3075 close(display->epoll_fd);
3076
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003077 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003078 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003079 free(display);
3080}
3081
3082void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003083display_set_user_data(struct display *display, void *data)
3084{
3085 display->user_data = data;
3086}
3087
3088void *
3089display_get_user_data(struct display *display)
3090{
3091 return display->user_data;
3092}
3093
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003094struct wl_display *
3095display_get_display(struct display *display)
3096{
3097 return display->display;
3098}
3099
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003100struct output *
3101display_get_output(struct display *display)
3102{
3103 return container_of(display->output_list.next, struct output, link);
3104}
3105
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003106struct wl_compositor *
3107display_get_compositor(struct display *display)
3108{
3109 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003110}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003111
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003112uint32_t
3113display_get_serial(struct display *display)
3114{
3115 return display->serial;
3116}
3117
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003118EGLDisplay
3119display_get_egl_display(struct display *d)
3120{
3121 return d->dpy;
3122}
3123
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003124struct wl_data_source *
3125display_create_data_source(struct display *display)
3126{
3127 return wl_data_device_manager_create_data_source(display->data_device_manager);
3128}
3129
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003130EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003131display_get_argb_egl_config(struct display *d)
3132{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003133 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003134}
3135
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003136struct wl_shell *
3137display_get_shell(struct display *display)
3138{
3139 return display->shell;
3140}
3141
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003142int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003143display_acquire_window_surface(struct display *display,
3144 struct window *window,
3145 EGLContext ctx)
3146{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003147#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003148 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003149 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003150
3151 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003152 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003153 device = cairo_surface_get_device(window->cairo_surface);
3154 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003155 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003156
Benjamin Franzke0c991632011-09-27 21:57:31 +02003157 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003158 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003159 ctx = display->argb_ctx;
3160 else
3161 assert(0);
3162 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003163
3164 data = cairo_surface_get_user_data(window->cairo_surface,
3165 &surface_data_key);
3166
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003167 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003168 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003169 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3170 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003171
3172 return 0;
3173#else
3174 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003175#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003176}
3177
3178void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003179display_release_window_surface(struct display *display,
3180 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003181{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003182#ifdef HAVE_CAIRO_EGL
3183 cairo_device_t *device;
3184
3185 device = cairo_surface_get_device(window->cairo_surface);
3186 if (!device)
3187 return;
3188
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003189 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003190 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003191 cairo_device_release(device);
3192#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003193}
3194
3195void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003196display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003197{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003198 wl_list_insert(&display->deferred_list, &task->link);
3199}
3200
3201void
3202display_watch_fd(struct display *display,
3203 int fd, uint32_t events, struct task *task)
3204{
3205 struct epoll_event ep;
3206
3207 ep.events = events;
3208 ep.data.ptr = task;
3209 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3210}
3211
3212void
3213display_run(struct display *display)
3214{
3215 struct task *task;
3216 struct epoll_event ep[16];
3217 int i, count;
3218
Pekka Paalanen826d7952011-12-15 10:14:07 +02003219 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003220 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003221 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003222
3223 while (!wl_list_empty(&display->deferred_list)) {
3224 task = container_of(display->deferred_list.next,
3225 struct task, link);
3226 wl_list_remove(&task->link);
3227 task->run(task, 0);
3228 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003229
3230 if (!display->running)
3231 break;
3232
3233 wl_display_flush(display->display);
3234
3235 count = epoll_wait(display->epoll_fd,
3236 ep, ARRAY_LENGTH(ep), -1);
3237 for (i = 0; i < count; i++) {
3238 task = ep[i].data.ptr;
3239 task->run(task, ep[i].events);
3240 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003241 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003242}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003243
3244void
3245display_exit(struct display *display)
3246{
3247 display->running = 0;
3248}