blob: b47e96902bccfcdbf18dfee22a9ce37677f7b7fa [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;
Daniel Stoneb230a7e2012-05-08 17:17:54 +0100181 GLfloat 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,
Daniel Stoneb230a7e2012-05-08 17:17:54 +01001339 struct input *input, GLfloat x, GLfloat 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,
Daniel Stoneb230a7e2012-05-08 17:17:54 +01001347 GLfloat x, GLfloat y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001348{
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,
Daniel Stoneb230a7e2012-05-08 17:17:54 +01001442 GLfloat x, GLfloat 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,
Daniel Stone103db7f2012-05-08 17:17:55 +01001476 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
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;
Daniel Stone103db7f2012-05-08 17:17:55 +01001482 GLfloat sx = wl_fixed_to_double(sx_w);
1483 GLfloat sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001484
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001485 input->sx = sx;
1486 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001487
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001488 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001489 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001490 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001491 }
1492
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001493 if (input->grab)
1494 widget = input->grab;
1495 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001496 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001497 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001498 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001499 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001500 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001501
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001502 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001503}
1504
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001505void
1506input_grab(struct input *input, struct widget *widget, uint32_t button)
1507{
1508 input->grab = widget;
1509 input->grab_button = button;
1510}
1511
1512void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001513input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001514{
1515 struct widget *widget;
1516
1517 input->grab = NULL;
1518 if (input->pointer_focus) {
1519 widget = widget_find_widget(input->pointer_focus->widget,
1520 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001521 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001522 }
1523}
1524
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001525static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001526input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001527 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001528 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001529{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001530 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001531 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001532
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001533 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001534 if (input->focus_widget && input->grab == NULL && state)
1535 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001536
Neil Roberts6b28aad2012-01-23 19:11:18 +00001537 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001538 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001539 (*widget->button_handler)(widget,
1540 input, time,
1541 button, state,
1542 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001543
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001544 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001545 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001546}
1547
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001548static void
Scott Moreau210d0792012-03-22 10:47:01 -06001549input_handle_axis(void *data,
1550 struct wl_input_device *input_device,
1551 uint32_t time, uint32_t axis, int32_t value)
1552{
1553}
1554
1555static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001556input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001557 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001558{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001559 struct input *input = data;
1560 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001561 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001562 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001563
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001564 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001565 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001566 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001567 return;
1568
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001569 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001570 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001571 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1572 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001573
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001574 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1575
1576 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001577 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001578 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001579 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001580
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001581 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1582 if (state)
1583 window_set_maximized(window,
1584 window->type != TYPE_MAXIMIZED);
1585 } else if (window->key_handler) {
1586 (*window->key_handler)(window, input, time, key,
1587 sym, state, window->user_data);
1588 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001589}
1590
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001591static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001592input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001593{
1594 struct window *window = input->pointer_focus;
1595
1596 if (!window)
1597 return;
1598
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001599 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001600
Pekka Paalanene1207c72011-12-16 12:02:09 +02001601 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001602 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001603}
1604
1605static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001606input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001607 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001608 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001609 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001610{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001611 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001612 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001613 struct widget *widget;
Daniel Stone103db7f2012-05-08 17:17:55 +01001614 GLfloat sx = wl_fixed_to_double(sx_w);
1615 GLfloat sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001616
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001617 input->display->serial = serial;
1618 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001619 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001620 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001621
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001622 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001623 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001624 window->pool = NULL;
1625 /* Schedule a redraw to free the pool */
1626 window_schedule_redraw(window);
1627 }
1628
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001629 input->sx = sx;
1630 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001631
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001632 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001633 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001634}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001635
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001636static void
1637input_handle_pointer_leave(void *data,
1638 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001639 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001640{
1641 struct input *input = data;
1642
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001643 input->display->serial = serial;
1644 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001645}
1646
1647static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001648input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001649{
1650 struct window *window = input->keyboard_focus;
1651
1652 if (!window)
1653 return;
1654
1655 window->keyboard_device = NULL;
1656 if (window->keyboard_focus_handler)
1657 (*window->keyboard_focus_handler)(window, NULL,
1658 window->user_data);
1659
1660 input->keyboard_focus = NULL;
1661}
1662
1663static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001664input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001665 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001666 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001667 struct wl_surface *surface,
1668 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001669{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001670 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001671 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001672 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001673 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001674
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001675 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001676 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001677
1678 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001679 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001680 for (k = keys->data; k < end; k++)
1681 input->modifiers |= d->xkb->map->modmap[*k];
1682
1683 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001684 window->keyboard_device = input;
1685 if (window->keyboard_focus_handler)
1686 (*window->keyboard_focus_handler)(window,
1687 window->keyboard_device,
1688 window->user_data);
1689}
1690
1691static void
1692input_handle_keyboard_leave(void *data,
1693 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001694 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001695 struct wl_surface *surface)
1696{
1697 struct input *input = data;
1698
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001699 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001700 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001701}
1702
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001703static void
1704input_handle_touch_down(void *data,
1705 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001706 uint32_t serial, uint32_t time,
1707 struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001708 int32_t id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001709{
1710}
1711
1712static void
1713input_handle_touch_up(void *data,
1714 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001715 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001716{
1717}
1718
1719static void
1720input_handle_touch_motion(void *data,
1721 struct wl_input_device *wl_input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001722 uint32_t time, int32_t id,
1723 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001724{
1725}
1726
1727static void
1728input_handle_touch_frame(void *data,
1729 struct wl_input_device *wl_input_device)
1730{
1731}
1732
1733static void
1734input_handle_touch_cancel(void *data,
1735 struct wl_input_device *wl_input_device)
1736{
1737}
1738
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001739static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001740 input_handle_motion,
1741 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001742 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001743 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001744 input_handle_pointer_enter,
1745 input_handle_pointer_leave,
1746 input_handle_keyboard_enter,
1747 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001748 input_handle_touch_down,
1749 input_handle_touch_up,
1750 input_handle_touch_motion,
1751 input_handle_touch_frame,
1752 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001753};
1754
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001755void
1756input_get_position(struct input *input, int32_t *x, int32_t *y)
1757{
1758 *x = input->sx;
1759 *y = input->sy;
1760}
1761
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001762struct wl_input_device *
1763input_get_input_device(struct input *input)
1764{
1765 return input->input_device;
1766}
1767
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001768uint32_t
1769input_get_modifiers(struct input *input)
1770{
1771 return input->modifiers;
1772}
1773
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001774struct widget *
1775input_get_focus_widget(struct input *input)
1776{
1777 return input->focus_widget;
1778}
1779
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001780struct data_offer {
1781 struct wl_data_offer *offer;
1782 struct input *input;
1783 struct wl_array types;
1784 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001785
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001786 struct task io_task;
1787 int fd;
1788 data_func_t func;
1789 int32_t x, y;
1790 void *user_data;
1791};
1792
1793static void
1794data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1795{
1796 struct data_offer *offer = data;
1797 char **p;
1798
1799 p = wl_array_add(&offer->types, sizeof *p);
1800 *p = strdup(type);
1801}
1802
1803static const struct wl_data_offer_listener data_offer_listener = {
1804 data_offer_offer,
1805};
1806
1807static void
1808data_offer_destroy(struct data_offer *offer)
1809{
1810 char **p;
1811
1812 offer->refcount--;
1813 if (offer->refcount == 0) {
1814 wl_data_offer_destroy(offer->offer);
1815 for (p = offer->types.data; *p; p++)
1816 free(*p);
1817 wl_array_release(&offer->types);
1818 free(offer);
1819 }
1820}
1821
1822static void
1823data_device_data_offer(void *data,
1824 struct wl_data_device *data_device, uint32_t id)
1825{
1826 struct data_offer *offer;
1827
1828 offer = malloc(sizeof *offer);
1829
1830 wl_array_init(&offer->types);
1831 offer->refcount = 1;
1832 offer->input = data;
1833
1834 /* FIXME: Generate typesafe wrappers for this */
1835 offer->offer = (struct wl_data_offer *)
1836 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1837 id, &wl_data_offer_interface);
1838
1839 wl_data_offer_add_listener(offer->offer,
1840 &data_offer_listener, offer);
1841}
1842
1843static void
1844data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001845 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001846 wl_fixed_t x_w, wl_fixed_t y_w,
1847 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001848{
1849 struct input *input = data;
1850 struct window *window;
Daniel Stone103db7f2012-05-08 17:17:55 +01001851 GLfloat x = wl_fixed_to_double(x_w);
1852 GLfloat y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001853 char **p;
1854
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001855 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001856 input->drag_offer = wl_data_offer_get_user_data(offer);
1857 window = wl_surface_get_user_data(surface);
1858 input->pointer_focus = window;
1859
1860 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1861 *p = NULL;
1862
1863 window = input->pointer_focus;
1864 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001865 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001866 input->drag_offer->types.data,
1867 window->user_data);
1868}
1869
1870static void
1871data_device_leave(void *data, struct wl_data_device *data_device)
1872{
1873 struct input *input = data;
1874
1875 data_offer_destroy(input->drag_offer);
1876 input->drag_offer = NULL;
1877}
1878
1879static void
1880data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001881 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001882{
1883 struct input *input = data;
1884 struct window *window = input->pointer_focus;
Daniel Stone103db7f2012-05-08 17:17:55 +01001885 GLfloat x = wl_fixed_to_double(x_w);
1886 GLfloat y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001887
1888 input->sx = x;
1889 input->sy = y;
1890
1891 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001892 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001893 input->drag_offer->types.data,
1894 window->user_data);
1895}
1896
1897static void
1898data_device_drop(void *data, struct wl_data_device *data_device)
1899{
1900 struct input *input = data;
1901 struct window *window = input->pointer_focus;
1902
1903 if (window->drop_handler)
1904 window->drop_handler(window, input,
1905 input->sx, input->sy, window->user_data);
1906}
1907
1908static void
1909data_device_selection(void *data,
1910 struct wl_data_device *wl_data_device,
1911 struct wl_data_offer *offer)
1912{
1913 struct input *input = data;
1914 char **p;
1915
1916 if (input->selection_offer)
1917 data_offer_destroy(input->selection_offer);
1918
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001919 if (offer) {
1920 input->selection_offer = wl_data_offer_get_user_data(offer);
1921 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1922 *p = NULL;
1923 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001924}
1925
1926static const struct wl_data_device_listener data_device_listener = {
1927 data_device_data_offer,
1928 data_device_enter,
1929 data_device_leave,
1930 data_device_motion,
1931 data_device_drop,
1932 data_device_selection
1933};
1934
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001935void
1936input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1937{
1938 struct display *display = input->display;
1939 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001940 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001941
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001942 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001943 return;
1944
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001945 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001946
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001947 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001948 return;
1949
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001950 input->current_cursor = pointer;
1951 buffer = display_get_buffer_for_surface(display, image->surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001952 wl_input_device_attach(input->input_device, time, buffer,
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001953 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001954}
1955
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001956struct wl_data_device *
1957input_get_data_device(struct input *input)
1958{
1959 return input->data_device;
1960}
1961
1962void
1963input_set_selection(struct input *input,
1964 struct wl_data_source *source, uint32_t time)
1965{
1966 wl_data_device_set_selection(input->data_device, source, time);
1967}
1968
1969void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001970input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001971{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001972 wl_data_offer_accept(input->drag_offer->offer,
1973 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001974}
1975
1976static void
1977offer_io_func(struct task *task, uint32_t events)
1978{
1979 struct data_offer *offer =
1980 container_of(task, struct data_offer, io_task);
1981 unsigned int len;
1982 char buffer[4096];
1983
1984 len = read(offer->fd, buffer, sizeof buffer);
1985 offer->func(buffer, len,
1986 offer->x, offer->y, offer->user_data);
1987
1988 if (len == 0) {
1989 close(offer->fd);
1990 data_offer_destroy(offer);
1991 }
1992}
1993
1994static void
1995data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1996 data_func_t func, void *user_data)
1997{
1998 int p[2];
1999
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002000 if (pipe2(p, O_CLOEXEC) == -1)
2001 return;
2002
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002003 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2004 close(p[1]);
2005
2006 offer->io_task.run = offer_io_func;
2007 offer->fd = p[0];
2008 offer->func = func;
2009 offer->refcount++;
2010 offer->user_data = user_data;
2011
2012 display_watch_fd(offer->input->display,
2013 offer->fd, EPOLLIN, &offer->io_task);
2014}
2015
2016void
2017input_receive_drag_data(struct input *input, const char *mime_type,
2018 data_func_t func, void *data)
2019{
2020 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2021 input->drag_offer->x = input->sx;
2022 input->drag_offer->y = input->sy;
2023}
2024
2025int
2026input_receive_selection_data(struct input *input, const char *mime_type,
2027 data_func_t func, void *data)
2028{
2029 char **p;
2030
2031 if (input->selection_offer == NULL)
2032 return -1;
2033
2034 for (p = input->selection_offer->types.data; *p; p++)
2035 if (strcmp(mime_type, *p) == 0)
2036 break;
2037
2038 if (*p == NULL)
2039 return -1;
2040
2041 data_offer_receive_data(input->selection_offer,
2042 mime_type, func, data);
2043 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002044}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002045
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002046int
2047input_receive_selection_data_to_fd(struct input *input,
2048 const char *mime_type, int fd)
2049{
2050 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2051
2052 return 0;
2053}
2054
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002055void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002056window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002057{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002058 if (!window->shell_surface)
2059 return;
2060
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002061 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002062 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002063}
2064
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002065static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002066idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002067{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002068 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002069
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002070 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002071 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002072 widget_set_allocation(widget,
2073 window->pending_allocation.x,
2074 window->pending_allocation.y,
2075 window->pending_allocation.width,
2076 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002077
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002078 if (window->input_region) {
2079 wl_region_destroy(window->input_region);
2080 window->input_region = NULL;
2081 }
2082
2083 if (window->opaque_region) {
2084 wl_region_destroy(window->opaque_region);
2085 window->opaque_region = NULL;
2086 }
2087
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002088 if (widget->resize_handler)
2089 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002090 widget->allocation.width,
2091 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002092 widget->user_data);
2093
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002094 if (window->allocation.width != widget->allocation.width ||
2095 window->allocation.height != widget->allocation.height) {
2096 window->allocation = widget->allocation;
2097 window_schedule_redraw(window);
2098 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002099}
2100
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002101void
2102window_schedule_resize(struct window *window, int width, int height)
2103{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002104 window->pending_allocation.x = 0;
2105 window->pending_allocation.y = 0;
2106 window->pending_allocation.width = width;
2107 window->pending_allocation.height = height;
2108
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002109 window->resize_needed = 1;
2110 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002111}
2112
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002113void
2114widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2115{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002116 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002117}
2118
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002119static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002120handle_ping(void *data, struct wl_shell_surface *shell_surface,
2121 uint32_t serial)
2122{
2123 wl_shell_surface_pong(shell_surface, serial);
2124}
2125
2126static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002127handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002128 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002129{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002130 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002131
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002132 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002133 return;
2134
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002135 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002136 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002137}
2138
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002139static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002140menu_destroy(struct menu *menu)
2141{
2142 widget_destroy(menu->widget);
2143 window_destroy(menu->window);
2144 free(menu);
2145}
2146
2147static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002148handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2149{
2150 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002151 struct menu *menu = window->widget->user_data;
2152
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002153 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002154 * device. Or just use wl_callback. And this really needs to
2155 * be a window vfunc that the menu can set. And we need the
2156 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002157
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002158 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002159 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002160 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002161}
2162
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002163static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002164 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002165 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002166 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002167};
2168
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002169void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002170window_get_allocation(struct window *window,
2171 struct rectangle *allocation)
2172{
2173 *allocation = window->allocation;
2174}
2175
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002176static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002177widget_redraw(struct widget *widget)
2178{
2179 struct widget *child;
2180
2181 if (widget->redraw_handler)
2182 widget->redraw_handler(widget, widget->user_data);
2183 wl_list_for_each(child, &widget->child_list, link)
2184 widget_redraw(child);
2185}
2186
2187static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002188frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2189{
2190 struct window *window = data;
2191
2192 wl_callback_destroy(callback);
2193 window->redraw_scheduled = 0;
2194 if (window->redraw_needed)
2195 window_schedule_redraw(window);
2196}
2197
2198static const struct wl_callback_listener listener = {
2199 frame_callback
2200};
2201
2202static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002203idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002204{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002205 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002206 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002207
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002208 if (window->resize_needed)
2209 idle_resize(window);
2210
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002211 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002212 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002213 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002214 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002215 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002216
2217 callback = wl_surface_frame(window->surface);
2218 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002219}
2220
2221void
2222window_schedule_redraw(struct window *window)
2223{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002224 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002225 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002226 window->redraw_task.run = idle_redraw;
2227 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002228 window->redraw_scheduled = 1;
2229 }
2230}
2231
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002232void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002233window_set_custom(struct window *window)
2234{
2235 window->type = TYPE_CUSTOM;
2236}
2237
2238void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002239window_set_fullscreen(struct window *window, int fullscreen)
2240{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002241 if (!window->display->shell)
2242 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002243
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002244 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002245 return;
2246
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002247 if (fullscreen) {
2248 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002249 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002250 wl_shell_surface_set_fullscreen(window->shell_surface,
2251 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2252 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002253 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002254 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002255 wl_shell_surface_set_toplevel(window->shell_surface);
2256 window_schedule_resize(window,
2257 window->saved_allocation.width,
2258 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002259 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002260}
2261
2262void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002263window_set_maximized(struct window *window, int maximized)
2264{
2265 if (!window->display->shell)
2266 return;
2267
2268 if ((window->type == TYPE_MAXIMIZED) == maximized)
2269 return;
2270
2271 if (window->type == TYPE_TOPLEVEL) {
2272 window->saved_allocation = window->allocation;
2273 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2274 window->type = TYPE_MAXIMIZED;
2275 } else {
2276 wl_shell_surface_set_toplevel(window->shell_surface);
2277 window->type = TYPE_TOPLEVEL;
2278 window_schedule_resize(window,
2279 window->saved_allocation.width,
2280 window->saved_allocation.height);
2281 }
2282}
2283
2284void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002285window_set_user_data(struct window *window, void *data)
2286{
2287 window->user_data = data;
2288}
2289
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002290void *
2291window_get_user_data(struct window *window)
2292{
2293 return window->user_data;
2294}
2295
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002296void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002297window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002298 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002299{
2300 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002301}
2302
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002303void
2304window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002305 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002306{
2307 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002308}
2309
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002310void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002311window_set_data_handler(struct window *window, window_data_handler_t handler)
2312{
2313 window->data_handler = handler;
2314}
2315
2316void
2317window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2318{
2319 window->drop_handler = handler;
2320}
2321
2322void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002323window_set_close_handler(struct window *window,
2324 window_close_handler_t handler)
2325{
2326 window->close_handler = handler;
2327}
2328
2329void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002330window_set_title(struct window *window, const char *title)
2331{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002332 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002333 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002334 if (window->shell_surface)
2335 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002336}
2337
2338const char *
2339window_get_title(struct window *window)
2340{
2341 return window->title;
2342}
2343
2344void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002345window_damage(struct window *window, int32_t x, int32_t y,
2346 int32_t width, int32_t height)
2347{
2348 wl_surface_damage(window->surface, x, y, width, height);
2349}
2350
Casey Dahlin9074db52012-04-19 22:50:09 -04002351static void
2352surface_enter(void *data,
2353 struct wl_surface *wl_surface, struct wl_output *output)
2354{
2355}
2356
2357static void
2358surface_leave(void *data,
2359 struct wl_surface *wl_surface, struct wl_output *output)
2360{
2361}
2362
2363static const struct wl_surface_listener surface_listener = {
2364 surface_enter,
2365 surface_leave
2366};
2367
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002368static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002369window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002370{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002371 struct window *window;
2372
2373 window = malloc(sizeof *window);
2374 if (window == NULL)
2375 return NULL;
2376
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002377 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002378 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002379 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002380 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002381 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002382 if (display->shell) {
2383 window->shell_surface =
2384 wl_shell_get_shell_surface(display->shell,
2385 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002386 if (window->title)
2387 wl_shell_surface_set_title(window->shell_surface,
2388 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002389 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002390 window->allocation.x = 0;
2391 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002392 window->allocation.width = 0;
2393 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002394 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002395 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002396 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002397 window->input_region = NULL;
2398 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002399
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002400 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002401#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002402 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002403#else
2404 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2405#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002406 else
2407 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002408
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002409 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002410 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002411 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002412
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002413 if (window->shell_surface) {
2414 wl_shell_surface_set_user_data(window->shell_surface, window);
2415 wl_shell_surface_add_listener(window->shell_surface,
2416 &shell_surface_listener, window);
2417 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002418
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002419 return window;
2420}
2421
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002422struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002423window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002424{
2425 struct window *window;
2426
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002427 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002428 if (!window)
2429 return NULL;
2430
2431 return window;
2432}
2433
2434struct window *
2435window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002436 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002437{
2438 struct window *window;
2439
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002440 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002441 if (!window)
2442 return NULL;
2443
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002444 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002445 window->x = x;
2446 window->y = y;
2447
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002448 if (display->shell)
2449 wl_shell_surface_set_transient(window->shell_surface,
2450 window->parent->shell_surface,
2451 window->x, window->y, 0);
2452
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002453 return window;
2454}
2455
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002456static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002457menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002458{
2459 int next;
2460
2461 next = (sy - 8) / 20;
2462 if (menu->current != next) {
2463 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002464 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002465 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002466}
2467
2468static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002469menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002470 struct input *input, uint32_t time,
Daniel Stoneb230a7e2012-05-08 17:17:54 +01002471 GLfloat x, GLfloat y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002472{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002473 struct menu *menu = data;
2474
2475 if (widget == menu->widget)
2476 menu_set_item(data, y);
2477
2478 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002479}
2480
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002481static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002482menu_enter_handler(struct widget *widget,
Daniel Stoneb230a7e2012-05-08 17:17:54 +01002483 struct input *input, GLfloat x, GLfloat y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002484{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002485 struct menu *menu = data;
2486
2487 if (widget == menu->widget)
2488 menu_set_item(data, y);
2489
2490 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002491}
2492
2493static void
2494menu_leave_handler(struct widget *widget, struct input *input, void *data)
2495{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002496 struct menu *menu = data;
2497
2498 if (widget == menu->widget)
2499 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002500}
2501
2502static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002503menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002504 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002505 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002506
2507{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002508 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002509
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002510 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002511 /* Either relase after press-drag-release or
2512 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002513 menu->func(menu->window->parent,
2514 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002515 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002516 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002517 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002518}
2519
2520static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002521menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002522{
2523 cairo_t *cr;
2524 const int32_t r = 3, margin = 3;
2525 struct menu *menu = data;
2526 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002527 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002528
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002529 cr = cairo_create(window->cairo_surface);
2530 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2531 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2532 cairo_paint(cr);
2533
2534 width = window->allocation.width;
2535 height = window->allocation.height;
2536 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002537
2538 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002539 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2540 cairo_fill(cr);
2541
2542 for (i = 0; i < menu->count; i++) {
2543 if (i == menu->current) {
2544 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2545 cairo_rectangle(cr, margin, i * 20 + margin,
2546 width - 2 * margin, 20);
2547 cairo_fill(cr);
2548 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2549 cairo_move_to(cr, 10, i * 20 + 16);
2550 cairo_show_text(cr, menu->entries[i]);
2551 } else {
2552 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2553 cairo_move_to(cr, 10, i * 20 + 16);
2554 cairo_show_text(cr, menu->entries[i]);
2555 }
2556 }
2557
2558 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002559}
2560
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002561void
2562window_show_menu(struct display *display,
2563 struct input *input, uint32_t time, struct window *parent,
2564 int32_t x, int32_t y,
2565 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002566{
2567 struct window *window;
2568 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002569 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002570
2571 menu = malloc(sizeof *menu);
2572 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002573 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002574
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002575 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002576 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002577 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002578
2579 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002580 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002581 menu->entries = entries;
2582 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002583 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002584 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002585 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002586 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002587 window->type = TYPE_MENU;
2588 window->x = x;
2589 window->y = y;
2590
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002591 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002592 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2593 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002594 window->parent->shell_surface,
2595 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002596
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002597 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002598 widget_set_enter_handler(menu->widget, menu_enter_handler);
2599 widget_set_leave_handler(menu->widget, menu_leave_handler);
2600 widget_set_motion_handler(menu->widget, menu_motion_handler);
2601 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002602
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002603 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002604 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002605}
2606
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002607void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002608window_set_buffer_type(struct window *window, enum window_buffer_type type)
2609{
2610 window->buffer_type = type;
2611}
2612
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002613
2614static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002615display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002616 struct wl_output *wl_output,
2617 int x, int y,
2618 int physical_width,
2619 int physical_height,
2620 int subpixel,
2621 const char *make,
2622 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002623{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002624 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002625
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002626 output->allocation.x = x;
2627 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002628}
2629
2630static void
2631display_handle_mode(void *data,
2632 struct wl_output *wl_output,
2633 uint32_t flags,
2634 int width,
2635 int height,
2636 int refresh)
2637{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002638 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002639 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002640
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002641 if (flags & WL_OUTPUT_MODE_CURRENT) {
2642 output->allocation.width = width;
2643 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002644 if (display->output_configure_handler)
2645 (*display->output_configure_handler)(
2646 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002647 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002648}
2649
2650static const struct wl_output_listener output_listener = {
2651 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002652 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002653};
2654
2655static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002656display_add_output(struct display *d, uint32_t id)
2657{
2658 struct output *output;
2659
2660 output = malloc(sizeof *output);
2661 if (output == NULL)
2662 return;
2663
2664 memset(output, 0, sizeof *output);
2665 output->display = d;
2666 output->output =
2667 wl_display_bind(d->display, id, &wl_output_interface);
2668 wl_list_insert(d->output_list.prev, &output->link);
2669
2670 wl_output_add_listener(output->output, &output_listener, output);
2671}
2672
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002673static void
2674output_destroy(struct output *output)
2675{
2676 if (output->destroy_handler)
2677 (*output->destroy_handler)(output, output->user_data);
2678
2679 wl_output_destroy(output->output);
2680 wl_list_remove(&output->link);
2681 free(output);
2682}
2683
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002684void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002685display_set_output_configure_handler(struct display *display,
2686 display_output_handler_t handler)
2687{
2688 struct output *output;
2689
2690 display->output_configure_handler = handler;
2691 if (!handler)
2692 return;
2693
2694 wl_list_for_each(output, &display->output_list, link)
2695 (*display->output_configure_handler)(output,
2696 display->user_data);
2697}
2698
2699void
2700output_set_user_data(struct output *output, void *data)
2701{
2702 output->user_data = data;
2703}
2704
2705void *
2706output_get_user_data(struct output *output)
2707{
2708 return output->user_data;
2709}
2710
2711void
2712output_set_destroy_handler(struct output *output,
2713 display_output_handler_t handler)
2714{
2715 output->destroy_handler = handler;
2716 /* FIXME: implement this, once we have way to remove outputs */
2717}
2718
2719void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002720output_get_allocation(struct output *output, struct rectangle *allocation)
2721{
2722 *allocation = output->allocation;
2723}
2724
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002725struct wl_output *
2726output_get_wl_output(struct output *output)
2727{
2728 return output->output;
2729}
2730
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002731static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002732display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002733{
2734 struct input *input;
2735
2736 input = malloc(sizeof *input);
2737 if (input == NULL)
2738 return;
2739
2740 memset(input, 0, sizeof *input);
2741 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002742 input->input_device =
2743 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002744 input->pointer_focus = NULL;
2745 input->keyboard_focus = NULL;
2746 wl_list_insert(d->input_list.prev, &input->link);
2747
2748 wl_input_device_add_listener(input->input_device,
2749 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002750 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002751
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002752 input->data_device =
2753 wl_data_device_manager_get_data_device(d->data_device_manager,
2754 input->input_device);
2755 wl_data_device_add_listener(input->data_device,
2756 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002757}
2758
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002759static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002760input_destroy(struct input *input)
2761{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002762 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002763 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002764
2765 if (input->drag_offer)
2766 data_offer_destroy(input->drag_offer);
2767
2768 if (input->selection_offer)
2769 data_offer_destroy(input->selection_offer);
2770
2771 wl_data_device_destroy(input->data_device);
2772 wl_list_remove(&input->link);
2773 wl_input_device_destroy(input->input_device);
2774 free(input);
2775}
2776
2777static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002778display_handle_global(struct wl_display *display, uint32_t id,
2779 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002780{
2781 struct display *d = data;
2782
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002783 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002784 d->compositor =
2785 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002786 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002787 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002788 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002789 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002790 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002791 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002792 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002793 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002794 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2795 d->data_device_manager =
2796 wl_display_bind(display, id,
2797 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002798 }
2799}
2800
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002801static void
2802display_render_frame(struct display *d)
2803{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002804 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002805 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002806
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002807 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002808 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2809 cr = cairo_create(d->shadow);
2810 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2811 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002812 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002813 cairo_fill(cr);
2814 cairo_destroy(cr);
2815 blur_surface(d->shadow, 64);
2816
2817 d->active_frame =
2818 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2819 cr = cairo_create(d->active_frame);
2820 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002821
2822 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2823 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2824 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2825 cairo_set_source(cr, pattern);
2826 cairo_pattern_destroy(pattern);
2827
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002828 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002829 cairo_fill(cr);
2830 cairo_destroy(cr);
2831
2832 d->inactive_frame =
2833 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2834 cr = cairo_create(d->inactive_frame);
2835 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002836 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002837 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002838 cairo_fill(cr);
2839 cairo_destroy(cr);
2840}
2841
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002842static void
2843init_xkb(struct display *d)
2844{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002845 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002846
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002847 names.rules = "evdev";
2848 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002849 names.layout = option_xkb_layout;
2850 names.variant = option_xkb_variant;
2851 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002852
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002853 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002854 if (!d->xkb) {
2855 fprintf(stderr, "Failed to compile keymap\n");
2856 exit(1);
2857 }
2858}
2859
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002860static void
2861fini_xkb(struct display *display)
2862{
2863 xkb_free_keymap(display->xkb);
2864}
2865
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002866#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002867static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002868init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002869{
2870 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002871 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002872
Rob Clark6396ed32012-03-11 19:48:41 -05002873#ifdef USE_CAIRO_GLESV2
2874# define GL_BIT EGL_OPENGL_ES2_BIT
2875#else
2876# define GL_BIT EGL_OPENGL_BIT
2877#endif
2878
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002879 static const EGLint argb_cfg_attribs[] = {
2880 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002881 EGL_RED_SIZE, 1,
2882 EGL_GREEN_SIZE, 1,
2883 EGL_BLUE_SIZE, 1,
2884 EGL_ALPHA_SIZE, 1,
2885 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002886 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002887 EGL_NONE
2888 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002889
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002890#ifdef USE_CAIRO_GLESV2
2891 static const EGLint context_attribs[] = {
2892 EGL_CONTEXT_CLIENT_VERSION, 2,
2893 EGL_NONE
2894 };
2895 EGLint api = EGL_OPENGL_ES_API;
2896#else
2897 EGLint *context_attribs = NULL;
2898 EGLint api = EGL_OPENGL_API;
2899#endif
2900
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002901 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002902 if (!eglInitialize(d->dpy, &major, &minor)) {
2903 fprintf(stderr, "failed to initialize display\n");
2904 return -1;
2905 }
2906
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002907 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002908 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2909 return -1;
2910 }
2911
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002912 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2913 &d->argb_config, 1, &n) || n != 1) {
2914 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002915 return -1;
2916 }
2917
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002918 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002919 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002920 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002921 fprintf(stderr, "failed to create context\n");
2922 return -1;
2923 }
2924
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002925 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002926 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002927 return -1;
2928 }
2929
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002930#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002931 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2932 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2933 fprintf(stderr, "failed to get cairo egl argb device\n");
2934 return -1;
2935 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002936#endif
2937
2938 return 0;
2939}
2940
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002941static void
2942fini_egl(struct display *display)
2943{
2944#ifdef HAVE_CAIRO_EGL
2945 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002946#endif
2947
2948 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2949 EGL_NO_CONTEXT);
2950
2951 eglTerminate(display->dpy);
2952 eglReleaseThread();
2953}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002954#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002955
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002956static int
2957event_mask_update(uint32_t mask, void *data)
2958{
2959 struct display *d = data;
2960
2961 d->mask = mask;
2962
2963 return 0;
2964}
2965
2966static void
2967handle_display_data(struct task *task, uint32_t events)
2968{
2969 struct display *display =
2970 container_of(task, struct display, display_task);
2971
2972 wl_display_iterate(display->display, display->mask);
2973}
2974
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002975struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002976display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002977{
2978 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002979
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002980 argc = parse_options(xkb_options,
2981 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002982
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002983 d = malloc(sizeof *d);
2984 if (d == NULL)
2985 return NULL;
2986
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002987 memset(d, 0, sizeof *d);
2988
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002989 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002990 if (d->display == NULL) {
2991 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002992 return NULL;
2993 }
2994
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002995 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2996 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2997 d->display_task.run = handle_display_data;
2998 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2999
3000 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003001 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003002 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003003
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003004 /* Set up listener so we'll catch all events. */
3005 wl_display_add_global_listener(d->display,
3006 display_handle_global, d);
3007
3008 /* Process connection events. */
3009 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003010#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003011 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003012 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003013#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003014
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003015 d->image_target_texture_2d =
3016 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3017 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3018 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3019
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003020 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003021
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003022 display_render_frame(d);
3023
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003024 wl_list_init(&d->window_list);
3025
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003026 init_xkb(d);
3027
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003028 return d;
3029}
3030
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003031static void
3032display_destroy_outputs(struct display *display)
3033{
3034 struct output *tmp;
3035 struct output *output;
3036
3037 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3038 output_destroy(output);
3039}
3040
Pekka Paalanene1207c72011-12-16 12:02:09 +02003041static void
3042display_destroy_inputs(struct display *display)
3043{
3044 struct input *tmp;
3045 struct input *input;
3046
3047 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3048 input_destroy(input);
3049}
3050
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003051void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003052display_destroy(struct display *display)
3053{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003054 if (!wl_list_empty(&display->window_list))
3055 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3056
3057 if (!wl_list_empty(&display->deferred_list))
3058 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3059
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003060 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003061 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003062
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003063 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003064
3065 cairo_surface_destroy(display->active_frame);
3066 cairo_surface_destroy(display->inactive_frame);
3067 cairo_surface_destroy(display->shadow);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003068 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003069
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003070#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003071 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003072#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003073
Pekka Paalanenc2052982011-12-16 11:41:32 +02003074 if (display->shell)
3075 wl_shell_destroy(display->shell);
3076
3077 if (display->shm)
3078 wl_shm_destroy(display->shm);
3079
3080 if (display->data_device_manager)
3081 wl_data_device_manager_destroy(display->data_device_manager);
3082
3083 wl_compositor_destroy(display->compositor);
3084
3085 close(display->epoll_fd);
3086
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003087 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003088 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003089 free(display);
3090}
3091
3092void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003093display_set_user_data(struct display *display, void *data)
3094{
3095 display->user_data = data;
3096}
3097
3098void *
3099display_get_user_data(struct display *display)
3100{
3101 return display->user_data;
3102}
3103
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003104struct wl_display *
3105display_get_display(struct display *display)
3106{
3107 return display->display;
3108}
3109
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003110struct output *
3111display_get_output(struct display *display)
3112{
3113 return container_of(display->output_list.next, struct output, link);
3114}
3115
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003116struct wl_compositor *
3117display_get_compositor(struct display *display)
3118{
3119 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003120}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003121
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003122uint32_t
3123display_get_serial(struct display *display)
3124{
3125 return display->serial;
3126}
3127
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003128EGLDisplay
3129display_get_egl_display(struct display *d)
3130{
3131 return d->dpy;
3132}
3133
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003134struct wl_data_source *
3135display_create_data_source(struct display *display)
3136{
3137 return wl_data_device_manager_create_data_source(display->data_device_manager);
3138}
3139
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003140EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003141display_get_argb_egl_config(struct display *d)
3142{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003143 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003144}
3145
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003146struct wl_shell *
3147display_get_shell(struct display *display)
3148{
3149 return display->shell;
3150}
3151
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003152int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003153display_acquire_window_surface(struct display *display,
3154 struct window *window,
3155 EGLContext ctx)
3156{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003157#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003158 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003159 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003160
3161 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003162 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003163 device = cairo_surface_get_device(window->cairo_surface);
3164 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003165 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003166
Benjamin Franzke0c991632011-09-27 21:57:31 +02003167 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003168 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003169 ctx = display->argb_ctx;
3170 else
3171 assert(0);
3172 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003173
3174 data = cairo_surface_get_user_data(window->cairo_surface,
3175 &surface_data_key);
3176
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003177 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003178 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003179 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3180 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003181
3182 return 0;
3183#else
3184 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003185#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003186}
3187
3188void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003189display_release_window_surface(struct display *display,
3190 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003191{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003192#ifdef HAVE_CAIRO_EGL
3193 cairo_device_t *device;
3194
3195 device = cairo_surface_get_device(window->cairo_surface);
3196 if (!device)
3197 return;
3198
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003199 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003200 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003201 cairo_device_release(device);
3202#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003203}
3204
3205void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003206display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003207{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003208 wl_list_insert(&display->deferred_list, &task->link);
3209}
3210
3211void
3212display_watch_fd(struct display *display,
3213 int fd, uint32_t events, struct task *task)
3214{
3215 struct epoll_event ep;
3216
3217 ep.events = events;
3218 ep.data.ptr = task;
3219 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3220}
3221
3222void
3223display_run(struct display *display)
3224{
3225 struct task *task;
3226 struct epoll_event ep[16];
3227 int i, count;
3228
Pekka Paalanen826d7952011-12-15 10:14:07 +02003229 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003230 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003231 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003232
3233 while (!wl_list_empty(&display->deferred_list)) {
3234 task = container_of(display->deferred_list.next,
3235 struct task, link);
3236 wl_list_remove(&task->link);
3237 task->run(task, 0);
3238 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003239
3240 if (!display->running)
3241 break;
3242
3243 wl_display_flush(display->display);
3244
3245 count = epoll_wait(display->epoll_fd,
3246 ep, ARRAY_LENGTH(ep), -1);
3247 for (i = 0; i < count; i++) {
3248 task = ep[i].data.ptr;
3249 task->run(task, ep[i].events);
3250 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003251 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003252}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003253
3254void
3255display_exit(struct display *display)
3256{
3257 display->running = 0;
3258}