blob: f66d4160e1f97d4c61935b85cd4fff8d3d948f55 [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øgsberg70163132012-05-08 15:55:39 -040059#include <X11/keysym.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050060#include <X11/X.h>
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030061#include <X11/Xcursor/Xcursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040062
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050063#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020064#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040065#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030069struct cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070070struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030071
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050072struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050073 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050074 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040075 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040076 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040077 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050079 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020081 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040082 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040083
84 int display_fd;
85 uint32_t mask;
86 struct task display_task;
87
88 int epoll_fd;
89 struct wl_list deferred_list;
90
Pekka Paalanen826d7952011-12-15 10:14:07 +020091 int running;
92
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040094 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050095 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040096 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050097 int frame_radius;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
99 struct {
100 struct xkb_rule_names names;
101 struct xkb_keymap *keymap;
102 struct xkb_state *state;
103 struct xkb_context *context;
104 xkb_mod_mask_t control_mask;
105 xkb_mod_mask_t alt_mask;
106 xkb_mod_mask_t shift_mask;
107 } xkb;
108
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300109 struct cursor *cursors;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700110 struct shm_pool *cursor_shm_pool;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400111
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500112 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
113 PFNEGLCREATEIMAGEKHRPROC create_image;
114 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200115
116 display_output_handler_t output_configure_handler;
117
118 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500119};
120
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400121enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400122 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400124 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500125 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400126 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500127 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400128 TYPE_CUSTOM
129};
130
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500131struct window {
132 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500133 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500134 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200135 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500136 struct wl_region *input_region;
137 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500138 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500139 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500140 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500141 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400142 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400143 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400144 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400145 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400146 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400147 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400148 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400149 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400150 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500151
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400152 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500153
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700154 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400155
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500156 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500157 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400158 window_data_handler_t data_handler;
159 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500160 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200162 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500163 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500164
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500165 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400166 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500167};
168
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500169struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500170 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500171 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400172 struct wl_list link;
173 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500174 widget_resize_handler_t resize_handler;
175 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500176 widget_enter_handler_t enter_handler;
177 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500178 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500179 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400180 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500181 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400182};
183
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400184struct input {
185 struct display *display;
186 struct wl_input_device *input_device;
187 struct window *pointer_focus;
188 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300189 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400190 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400191 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400192 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400194
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500195 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500196 struct widget *grab;
197 uint32_t grab_button;
198
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400199 struct wl_data_device *data_device;
200 struct data_offer *drag_offer;
201 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400202};
203
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500204struct output {
205 struct display *display;
206 struct wl_output *output;
207 struct rectangle allocation;
208 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200209
210 display_output_handler_t destroy_handler;
211 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500212};
213
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500214struct frame {
215 struct widget *widget;
216 struct widget *child;
217 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400218 int width;
219 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500220};
221
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500222struct menu {
223 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500224 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500225 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500226 const char **entries;
227 uint32_t time;
228 int current;
229 int count;
230 menu_func_t func;
231};
232
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300233struct cursor_image {
234 cairo_surface_t *surface;
235 int width, height;
236 int hotspot_x, hotspot_y;
237 int delay;
238};
239
240struct cursor {
241 int n_images;
242 struct cursor_image *images;
243};
244
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700245struct shm_pool {
246 struct wl_shm_pool *pool;
247 size_t size;
248 size_t used;
249 void *data;
250};
251
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400252enum {
253 POINTER_DEFAULT = 100,
254 POINTER_UNSET
255};
256
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500257enum window_location {
258 WINDOW_INTERIOR = 0,
259 WINDOW_RESIZING_TOP = 1,
260 WINDOW_RESIZING_BOTTOM = 2,
261 WINDOW_RESIZING_LEFT = 4,
262 WINDOW_RESIZING_TOP_LEFT = 5,
263 WINDOW_RESIZING_BOTTOM_LEFT = 6,
264 WINDOW_RESIZING_RIGHT = 8,
265 WINDOW_RESIZING_TOP_RIGHT = 9,
266 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
267 WINDOW_RESIZING_MASK = 15,
268 WINDOW_EXTERIOR = 16,
269 WINDOW_TITLEBAR = 17,
270 WINDOW_CLIENT_AREA = 18,
271};
272
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400273const char *option_xkb_layout = "us";
274const char *option_xkb_variant = "";
275const char *option_xkb_options = "";
276
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400277static const struct weston_option xkb_options[] = {
278 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
279 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
280 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400281};
282
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400283static const cairo_user_data_key_t surface_data_key;
284struct surface_data {
285 struct wl_buffer *buffer;
286};
287
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500288#define MULT(_d,c,a,t) \
289 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
290
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500291#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400292
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100293struct egl_window_surface_data {
294 struct display *display;
295 struct wl_surface *surface;
296 struct wl_egl_window *window;
297 EGLSurface surf;
298};
299
300static void
301egl_window_surface_data_destroy(void *p)
302{
303 struct egl_window_surface_data *data = p;
304 struct display *d = data->display;
305
306 eglDestroySurface(d->dpy, data->surf);
307 wl_egl_window_destroy(data->window);
308 data->surface = NULL;
309
310 free(p);
311}
312
313static cairo_surface_t *
314display_create_egl_window_surface(struct display *display,
315 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400316 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100317 struct rectangle *rectangle)
318{
319 cairo_surface_t *cairo_surface;
320 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400321 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200322 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100323
324 data = malloc(sizeof *data);
325 if (data == NULL)
326 return NULL;
327
328 data->display = display;
329 data->surface = surface;
330
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500331 config = display->argb_config;
332 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400333
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400334 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100335 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400336 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100337
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400338 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500339 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100340
Benjamin Franzke0c991632011-09-27 21:57:31 +0200341 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100342 data->surf,
343 rectangle->width,
344 rectangle->height);
345
346 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
347 data, egl_window_surface_data_destroy);
348
349 return cairo_surface;
350}
351
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352#endif
353
354struct wl_buffer *
355display_get_buffer_for_surface(struct display *display,
356 cairo_surface_t *surface)
357{
358 struct surface_data *data;
359
360 data = cairo_surface_get_user_data (surface, &surface_data_key);
361
362 return data->buffer;
363}
364
365struct shm_surface_data {
366 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700367 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400368};
369
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500370static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700371shm_pool_destroy(struct shm_pool *pool);
372
373static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400374shm_surface_data_destroy(void *p)
375{
376 struct shm_surface_data *data = p;
377
378 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700379 if (data->pool)
380 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400381}
382
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300383static void
384shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
385{
386 void *dest = cairo_image_surface_get_data(surface);
387
388 memcpy(dest, data, count);
389}
390
Kristian Høgsberg16626282012-04-03 11:21:27 -0400391static struct wl_shm_pool *
392make_shm_pool(struct display *display, int size, void **data)
393{
394 char filename[] = "/tmp/wayland-shm-XXXXXX";
395 struct wl_shm_pool *pool;
396 int fd;
397
398 fd = mkstemp(filename);
399 if (fd < 0) {
400 fprintf(stderr, "open %s failed: %m\n", filename);
401 return NULL;
402 }
403 if (ftruncate(fd, size) < 0) {
404 fprintf(stderr, "ftruncate failed: %m\n");
405 close(fd);
406 return NULL;
407 }
408
409 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
410 unlink(filename);
411
412 if (*data == MAP_FAILED) {
413 fprintf(stderr, "mmap failed: %m\n");
414 close(fd);
415 return NULL;
416 }
417
418 pool = wl_shm_create_pool(display->shm, fd, size);
419
420 close(fd);
421
422 return pool;
423}
424
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700425static struct shm_pool *
426shm_pool_create(struct display *display, size_t size)
427{
428 struct shm_pool *pool = malloc(sizeof *pool);
429
430 if (!pool)
431 return NULL;
432
433 pool->pool = make_shm_pool(display, size, &pool->data);
434 if (!pool->pool) {
435 free(pool);
436 return NULL;
437 }
438
439 pool->size = size;
440 pool->used = 0;
441
442 return pool;
443}
444
445static void *
446shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
447{
448 if (pool->used + size > pool->size)
449 return NULL;
450
451 *offset = pool->used;
452 pool->used += size;
453
454 return (char *) pool->data + *offset;
455}
456
457/* destroy the pool. this does not unmap the memory though */
458static void
459shm_pool_destroy(struct shm_pool *pool)
460{
461 munmap(pool->data, pool->size);
462 wl_shm_pool_destroy(pool->pool);
463 free(pool);
464}
465
466/* Start allocating from the beginning of the pool again */
467static void
468shm_pool_reset(struct shm_pool *pool)
469{
470 pool->used = 0;
471}
472
473static int
474data_length_for_shm_surface(struct rectangle *rect)
475{
476 int stride;
477
478 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
479 rect->width);
480 return stride * rect->height;
481}
482
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500483static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700484display_create_shm_surface_from_pool(struct display *display,
485 struct rectangle *rectangle,
486 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400487{
488 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400489 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400490 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700491 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400492 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400493
494 data = malloc(sizeof *data);
495 if (data == NULL)
496 return NULL;
497
498 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
499 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700500 length = stride * rectangle->height;
501 data->pool = NULL;
502 map = shm_pool_allocate(pool, length, &offset);
503
504 if (!map) {
505 free(data);
506 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400507 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400508
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400509 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400510 CAIRO_FORMAT_ARGB32,
511 rectangle->width,
512 rectangle->height,
513 stride);
514
515 cairo_surface_set_user_data (surface, &surface_data_key,
516 data, shm_surface_data_destroy);
517
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400518 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500519 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400520 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500521 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400522
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700523 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400524 rectangle->width,
525 rectangle->height,
526 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400527
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700528 return surface;
529}
530
531static cairo_surface_t *
532display_create_shm_surface(struct display *display,
533 struct rectangle *rectangle, uint32_t flags,
534 struct window *window)
535{
536 struct shm_surface_data *data;
537 struct shm_pool *pool;
538 cairo_surface_t *surface;
539
540 if (window && window->pool) {
541 shm_pool_reset(window->pool);
542 surface = display_create_shm_surface_from_pool(display,
543 rectangle,
544 flags,
545 window->pool);
546 if (surface)
547 return surface;
548 }
549
550 pool = shm_pool_create(display,
551 data_length_for_shm_surface(rectangle));
552 if (!pool)
553 return NULL;
554
555 surface =
556 display_create_shm_surface_from_pool(display, rectangle,
557 flags, pool);
558
559 if (!surface) {
560 shm_pool_destroy(pool);
561 return NULL;
562 }
563
564 /* make sure we destroy the pool when the surface is destroyed */
565 data = cairo_surface_get_user_data(surface, &surface_data_key);
566 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400567
568 return surface;
569}
570
nobled7b87cb02011-02-01 18:51:47 +0000571static int
572check_size(struct rectangle *rect)
573{
574 if (rect->width && rect->height)
575 return 0;
576
577 fprintf(stderr, "tried to create surface of "
578 "width: %d, height: %d\n", rect->width, rect->height);
579 return -1;
580}
581
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400582cairo_surface_t *
583display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100584 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400585 struct rectangle *rectangle,
586 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400587{
nobled7b87cb02011-02-01 18:51:47 +0000588 if (check_size(rectangle) < 0)
589 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500590#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400591 if (display->dpy)
592 return display_create_egl_window_surface(display,
593 surface,
594 flags,
595 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400596#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400597 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400598}
599
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300600static const char *cursors[] = {
601 "bottom_left_corner",
602 "bottom_right_corner",
603 "bottom_side",
604 "grabbing",
605 "left_ptr",
606 "left_side",
607 "right_side",
608 "top_left_corner",
609 "top_right_corner",
610 "top_side",
611 "xterm",
612 "hand1",
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400613};
614
615static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300616create_cursor_from_images(struct display *display, struct cursor *cursor,
617 XcursorImages *images)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400618{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300619 int i;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400620 struct rectangle rect;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300621 XcursorImage *image;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400622
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300623 cursor->images = malloc(images->nimage * sizeof *cursor->images);
624 cursor->n_images = images->nimage;
625
626 for (i = 0; i < images->nimage; i++) {
627 image = images->images[i];
628
629 rect.width = image->width;
630 rect.height = image->height;
631
632 cursor->images[i].surface =
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700633 display_create_shm_surface_from_pool(display, &rect, 0,
634 display->cursor_shm_pool);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300635
636 shm_surface_write(cursor->images[i].surface,
637 (unsigned char *) image->pixels,
638 image->width * image->height * sizeof image->pixels[0]);
639
640 cursor->images[i].width = image->width;
641 cursor->images[i].height = image->height;
642 cursor->images[i].hotspot_x = image->xhot;
643 cursor->images[i].hotspot_y = image->yhot;
644 cursor->images[i].delay = image->delay;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400645 }
646
647}
648
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700649static size_t
650data_length_for_cursor_images(XcursorImages *images)
651{
652 int i;
653 size_t length = 0;
654 struct rectangle rect;
655
656 for (i = 0; i < images->nimage; i++) {
657 rect.width = images->images[i]->width;
658 rect.height = images->images[i]->height;
659 length += data_length_for_shm_surface(&rect);
660 }
661
662 return length;
663}
664
Pekka Paalanen325bb602011-12-19 10:31:45 +0200665static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300666create_cursors(struct display *display)
667{
668 int i, count;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700669 size_t pool_size = 0;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300670 struct cursor *cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700671 XcursorImages **images;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300672
673 count = ARRAY_LENGTH(cursors);
674 display->cursors = malloc(count * sizeof *display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700675 images = malloc(count * sizeof images[0]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300676
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700677 for (i = 0; i < count; i++) {
678 images[i] = XcursorLibraryLoadImages(cursors[i], NULL, 32);
Rafal Mielniczuk87e4c932012-05-08 22:10:44 +0200679 if (!images[i]) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300680 fprintf(stderr, "Error loading cursor: %s\n",
681 cursors[i]);
682 continue;
683 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700684 pool_size += data_length_for_cursor_images(images[i]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300685 }
686
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700687 display->cursor_shm_pool = shm_pool_create(display, pool_size);
688
689 for (i = 0; i < count; i++) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700690 cursor = &display->cursors[i];
Dima Ryazanovff1c2d72012-05-08 21:02:33 -0700691
692 if (!images[i]) {
693 cursor->n_images = 0;
694 cursor->images = NULL;
695 continue;
696 }
697
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700698 create_cursor_from_images(display, cursor, images[i]);
699
700 XcursorImagesDestroy(images[i]);
701 }
702
703 free(images);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300704}
705
706static void
707destroy_cursor_images(struct cursor *cursor)
708{
709 int i;
710
711 for (i = 0; i < cursor->n_images; i++)
712 if (cursor->images[i].surface)
713 cairo_surface_destroy(cursor->images[i].surface);
714
715 free(cursor->images);
716}
717
718static void
719destroy_cursors(struct display *display)
Pekka Paalanen325bb602011-12-19 10:31:45 +0200720{
721 int i, count;
722
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300723 count = ARRAY_LENGTH(cursors);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200724 for (i = 0; i < count; ++i) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300725 destroy_cursor_images(&display->cursors[i]);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200726 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700727
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300728 free(display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700729 shm_pool_destroy(display->cursor_shm_pool);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200730}
731
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400732cairo_surface_t *
733display_get_pointer_surface(struct display *display, int pointer,
734 int *width, int *height,
735 int *hotspot_x, int *hotspot_y)
736{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300737 struct cursor *cursor = &display->cursors[pointer];
738 cairo_surface_t *surface = cursor->images[0].surface;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400739
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300740 /* FIXME returning information for the first image. Something better
741 * is needed for animated cursors */
742
nobledf8475c92011-01-05 17:41:55 +0000743 *width = cairo_image_surface_get_width(surface);
744 *height = cairo_image_surface_get_height(surface);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300745
746 *hotspot_x = cursor->images[0].hotspot_x;
747 *hotspot_y = cursor->images[0].hotspot_y;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400748
749 return cairo_surface_reference(surface);
750}
751
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400752static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200753window_get_resize_dx_dy(struct window *window, int *x, int *y)
754{
755 if (window->resize_edges & WINDOW_RESIZING_LEFT)
756 *x = window->server_allocation.width - window->allocation.width;
757 else
758 *x = 0;
759
760 if (window->resize_edges & WINDOW_RESIZING_TOP)
761 *y = window->server_allocation.height -
762 window->allocation.height;
763 else
764 *y = 0;
765
766 window->resize_edges = 0;
767}
768
769static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500770window_attach_surface(struct window *window)
771{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400772 struct display *display = window->display;
773 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000774#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100775 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000776#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500777 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100778
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400779 if (window->type == TYPE_NONE) {
780 window->type = TYPE_TOPLEVEL;
781 if (display->shell)
782 wl_shell_surface_set_toplevel(window->shell_surface);
783 }
784
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100785 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000786#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
788 data = cairo_surface_get_user_data(window->cairo_surface,
789 &surface_data_key);
790
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100791 cairo_gl_surface_swapbuffers(window->cairo_surface);
792 wl_egl_window_get_attached_size(data->window,
793 &window->server_allocation.width,
794 &window->server_allocation.height);
795 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000796#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100797 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798 buffer =
799 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400800 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100801
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200802 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100803 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400804 wl_surface_damage(window->surface, 0, 0,
805 window->allocation.width,
806 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100807 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400808 cairo_surface_destroy(window->cairo_surface);
809 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100810 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000811 default:
812 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100813 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500814
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500815 if (window->input_region) {
816 wl_surface_set_input_region(window->surface,
817 window->input_region);
818 wl_region_destroy(window->input_region);
819 window->input_region = NULL;
820 }
821
822 if (window->opaque_region) {
823 wl_surface_set_opaque_region(window->surface,
824 window->opaque_region);
825 wl_region_destroy(window->opaque_region);
826 window->opaque_region = NULL;
827 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500828}
829
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500830void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400831window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500832{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400833 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100834 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500835}
836
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400837void
838window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400839{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500840 cairo_surface_reference(surface);
841
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400842 if (window->cairo_surface != NULL)
843 cairo_surface_destroy(window->cairo_surface);
844
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500845 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400846}
847
Benjamin Franzke22d54812011-07-16 19:50:32 +0000848#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849static void
850window_resize_cairo_window_surface(struct window *window)
851{
852 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200853 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854
855 data = cairo_surface_get_user_data(window->cairo_surface,
856 &surface_data_key);
857
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200858 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859 wl_egl_window_resize(data->window,
860 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200861 window->allocation.height,
862 x,y);
863
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100864 cairo_gl_surface_set_size(window->cairo_surface,
865 window->allocation.width,
866 window->allocation.height);
867}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000868#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100869
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400870struct display *
871window_get_display(struct window *window)
872{
873 return window->display;
874}
875
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400876void
877window_create_surface(struct window *window)
878{
879 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400880 uint32_t flags = 0;
881
882 if (!window->transparent)
883 flags = SURFACE_OPAQUE;
884
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400885 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500886#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100887 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
888 if (window->cairo_surface) {
889 window_resize_cairo_window_surface(window);
890 return;
891 }
892 surface = display_create_surface(window->display,
893 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400894 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100895 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400896#endif
897 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400898 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400899 &window->allocation,
900 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400901 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800902 default:
903 surface = NULL;
904 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400905 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400906
907 window_set_surface(window, surface);
908 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909}
910
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200911static void frame_destroy(struct frame *frame);
912
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400913void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500914window_destroy(struct window *window)
915{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200916 struct display *display = window->display;
917 struct input *input;
918
919 if (window->redraw_scheduled)
920 wl_list_remove(&window->redraw_task.link);
921
922 wl_list_for_each(input, &display->input_list, link) {
923 if (input->pointer_focus == window)
924 input->pointer_focus = NULL;
925 if (input->keyboard_focus == window)
926 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500927 if (input->focus_widget &&
928 input->focus_widget->window == window)
929 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200930 }
931
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500932 if (window->input_region)
933 wl_region_destroy(window->input_region);
934 if (window->opaque_region)
935 wl_region_destroy(window->opaque_region);
936
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200937 if (window->frame)
938 frame_destroy(window->frame);
939
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200940 if (window->shell_surface)
941 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500942 wl_surface_destroy(window->surface);
943 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200944
945 if (window->cairo_surface != NULL)
946 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200947
948 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500949 free(window);
950}
951
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500952static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500953widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400954{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500955 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400956
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500957 wl_list_for_each(child, &widget->child_list, link) {
958 target = widget_find_widget(child, x, y);
959 if (target)
960 return target;
961 }
962
963 if (widget->allocation.x <= x &&
964 x < widget->allocation.x + widget->allocation.width &&
965 widget->allocation.y <= y &&
966 y < widget->allocation.y + widget->allocation.height) {
967 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400968 }
969
970 return NULL;
971}
972
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500973static struct widget *
974widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400975{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500976 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400977
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500978 widget = malloc(sizeof *widget);
979 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500980 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500981 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500982 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500983 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500984 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500985
986 return widget;
987}
988
989struct widget *
990window_add_widget(struct window *window, void *data)
991{
992 window->widget = widget_create(window, data);
993 wl_list_init(&window->widget->link);
994
995 return window->widget;
996}
997
998struct widget *
999widget_add_widget(struct widget *parent, void *data)
1000{
1001 struct widget *widget;
1002
1003 widget = widget_create(parent->window, data);
1004 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001005
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001006 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007}
1008
1009void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001010widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001011{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001012 struct display *display = widget->window->display;
1013 struct input *input;
1014
1015 wl_list_for_each(input, &display->input_list, link) {
1016 if (input->focus_widget == widget)
1017 input->focus_widget = NULL;
1018 }
1019
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001020 wl_list_remove(&widget->link);
1021 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001022}
1023
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001024void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001025widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001026{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001027 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028}
1029
1030void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001031widget_set_size(struct widget *widget, int32_t width, int32_t height)
1032{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001033 widget->allocation.width = width;
1034 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001035}
1036
1037void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001038widget_set_allocation(struct widget *widget,
1039 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001040{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001041 widget->allocation.x = x;
1042 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001043 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001044}
1045
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001046void
1047widget_set_transparent(struct widget *widget, int transparent)
1048{
1049 widget->opaque = !transparent;
1050}
1051
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001052void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001053widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001054{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001055 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001056}
1057
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001058void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001059widget_set_resize_handler(struct widget *widget,
1060 widget_resize_handler_t handler)
1061{
1062 widget->resize_handler = handler;
1063}
1064
1065void
1066widget_set_redraw_handler(struct widget *widget,
1067 widget_redraw_handler_t handler)
1068{
1069 widget->redraw_handler = handler;
1070}
1071
1072void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001073widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001074{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001075 widget->enter_handler = handler;
1076}
1077
1078void
1079widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1080{
1081 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001082}
1083
1084void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001085widget_set_motion_handler(struct widget *widget,
1086 widget_motion_handler_t handler)
1087{
1088 widget->motion_handler = handler;
1089}
1090
1091void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001092widget_set_button_handler(struct widget *widget,
1093 widget_button_handler_t handler)
1094{
1095 widget->button_handler = handler;
1096}
1097
1098void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001099widget_schedule_redraw(struct widget *widget)
1100{
1101 window_schedule_redraw(widget->window);
1102}
1103
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001104cairo_surface_t *
1105window_get_surface(struct window *window)
1106{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001107 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001108}
1109
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001110struct wl_surface *
1111window_get_wl_surface(struct window *window)
1112{
1113 return window->surface;
1114}
1115
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001116struct wl_shell_surface *
1117window_get_wl_shell_surface(struct window *window)
1118{
1119 return window->shell_surface;
1120}
1121
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001122static void
1123frame_resize_handler(struct widget *widget,
1124 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001125{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001126 struct frame *frame = data;
1127 struct widget *child = frame->child;
1128 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001129 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001130 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001131 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001132
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001133 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001134 decoration_width = (frame->width + frame->margin) * 2;
1135 decoration_height = frame->width +
1136 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001137
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001138 allocation.x = frame->width + frame->margin;
1139 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001140 allocation.width = width - decoration_width;
1141 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001142
1143 widget->window->input_region =
1144 wl_compositor_create_region(display->compositor);
1145 wl_region_add(widget->window->input_region,
1146 frame->margin, frame->margin,
1147 width - 2 * frame->margin,
1148 height - 2 * frame->margin);
1149
1150 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001151 } else {
1152 decoration_width = 0;
1153 decoration_height = 0;
1154
1155 allocation.x = 0;
1156 allocation.y = 0;
1157 allocation.width = width;
1158 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001159 opaque_margin = 0;
1160 }
1161
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001162 widget_set_allocation(child, allocation.x, allocation.y,
1163 allocation.width, allocation.height);
1164
1165 if (child->resize_handler)
1166 child->resize_handler(child,
1167 allocation.width,
1168 allocation.height,
1169 child->user_data);
1170
1171 widget_set_allocation(widget, 0, 0,
1172 child->allocation.width + decoration_width,
1173 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001174
1175 if (child->opaque) {
1176 widget->window->opaque_region =
1177 wl_compositor_create_region(display->compositor);
1178 wl_region_add(widget->window->opaque_region,
1179 opaque_margin, opaque_margin,
1180 widget->allocation.width - 2 * opaque_margin,
1181 widget->allocation.height - 2 * opaque_margin);
1182 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001183}
1184
1185static void
1186frame_redraw_handler(struct widget *widget, void *data)
1187{
1188 struct frame *frame = data;
1189 cairo_t *cr;
1190 cairo_text_extents_t extents;
1191 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001192 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001193 struct window *window = widget->window;
1194
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001195 if (window->type == TYPE_FULLSCREEN)
1196 return;
1197
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001198 width = widget->allocation.width;
1199 height = widget->allocation.height;
1200
1201 cr = cairo_create(window->cairo_surface);
1202
1203 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1204 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1205 cairo_paint(cr);
1206
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001207 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001208 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001209 2, 2, width + 8, height + 8,
1210 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001211
1212 if (window->keyboard_device)
1213 source = window->display->active_frame;
1214 else
1215 source = window->display->inactive_frame;
1216
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001217 tile_source(cr, source,
1218 frame->margin, frame->margin,
1219 width - frame->margin * 2, height - frame->margin * 2,
1220 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001221
1222 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001223 cairo_select_font_face(cr, "sans",
1224 CAIRO_FONT_SLANT_NORMAL,
1225 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001226 cairo_set_font_size(cr, 14);
1227 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001228 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001229 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001230 if (window->keyboard_device) {
1231 cairo_move_to(cr, x + 1, y + 1);
1232 cairo_set_source_rgb(cr, 1, 1, 1);
1233 cairo_show_text(cr, window->title);
1234 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001235 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001236 cairo_show_text(cr, window->title);
1237 } else {
1238 cairo_move_to(cr, x, y);
1239 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1240 cairo_show_text(cr, window->title);
1241 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242
1243 cairo_destroy(cr);
1244}
1245
1246static int
1247frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1248{
1249 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001250 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001251 const int grip_size = 8;
1252
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001253 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001254 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001255 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001257 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001258 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001259 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001260 hlocation = WINDOW_RESIZING_RIGHT;
1261 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001262 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001264 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001265 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001266 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001267 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001268 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001269 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001270 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271 vlocation = WINDOW_RESIZING_BOTTOM;
1272 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001273 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001275 location = vlocation | hlocation;
1276 if (location & WINDOW_EXTERIOR)
1277 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001278 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001279 location = WINDOW_TITLEBAR;
1280 else if (location == WINDOW_INTERIOR)
1281 location = WINDOW_CLIENT_AREA;
1282
1283 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001284}
1285
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001286static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001287frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001288{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001289 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001290
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001291 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001292 switch (location) {
1293 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001294 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001295 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001296 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001297 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001298 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001299 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001300 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001301 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001302 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001303 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001304 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001305 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001306 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001307 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001308 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001309 case WINDOW_EXTERIOR:
1310 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001311 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001312 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001313 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001314}
1315
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001316static void
1317frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001318{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001319 switch (index) {
1320 case 0: /* close */
1321 if (window->close_handler)
1322 window->close_handler(window->parent,
1323 window->user_data);
1324 else
1325 display_exit(window->display);
1326 break;
1327 case 1: /* fullscreen */
1328 /* we don't have a way to get out of fullscreen for now */
1329 window_set_fullscreen(window, 1);
1330 break;
1331 case 2: /* rotate */
1332 case 3: /* scale */
1333 break;
1334 }
1335}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001336
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001337void
1338window_show_frame_menu(struct window *window,
1339 struct input *input, uint32_t time)
1340{
1341 int32_t x, y;
1342
1343 static const char *entries[] = {
1344 "Close", "Fullscreen", "Rotate", "Scale"
1345 };
1346
1347 input_get_position(input, &x, &y);
1348 window_show_menu(window->display, input, time, window,
1349 x - 10, y - 10, frame_menu_func, entries, 4);
1350}
1351
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001352static int
1353frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001354 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001355{
1356 return frame_get_pointer_image_for_location(data, input);
1357}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001358
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001359static int
1360frame_motion_handler(struct widget *widget,
1361 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001362 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001363{
1364 return frame_get_pointer_image_for_location(data, input);
1365}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001366
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001367static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001368frame_button_handler(struct widget *widget,
1369 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001370 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001371
1372{
1373 struct frame *frame = data;
1374 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001375 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001376 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001377
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001378 location = frame_get_pointer_location(frame, input->sx, input->sy);
1379
1380 if (window->display->shell && button == BTN_LEFT && state == 1) {
1381 switch (location) {
1382 case WINDOW_TITLEBAR:
1383 if (!window->shell_surface)
1384 break;
1385 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001386 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001387 wl_shell_surface_move(window->shell_surface,
1388 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001389 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001390 break;
1391 case WINDOW_RESIZING_TOP:
1392 case WINDOW_RESIZING_BOTTOM:
1393 case WINDOW_RESIZING_LEFT:
1394 case WINDOW_RESIZING_RIGHT:
1395 case WINDOW_RESIZING_TOP_LEFT:
1396 case WINDOW_RESIZING_TOP_RIGHT:
1397 case WINDOW_RESIZING_BOTTOM_LEFT:
1398 case WINDOW_RESIZING_BOTTOM_RIGHT:
1399 if (!window->shell_surface)
1400 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001401 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001402
1403 if (!display->dpy) {
1404 /* If we're using shm, allocate a big
1405 pool to create buffers out of while
1406 we resize. We should probably base
1407 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001408 window->pool =
1409 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001410 }
1411
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001412 wl_shell_surface_resize(window->shell_surface,
1413 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001414 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001415 break;
1416 }
1417 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001418 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001419 }
1420}
1421
1422struct widget *
1423frame_create(struct window *window, void *data)
1424{
1425 struct frame *frame;
1426
1427 frame = malloc(sizeof *frame);
1428 memset(frame, 0, sizeof *frame);
1429
1430 frame->widget = window_add_widget(window, frame);
1431 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001432 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001433 frame->width = 4;
1434 frame->titlebar_height = 30
1435;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001436 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1437 widget_set_resize_handler(frame->widget, frame_resize_handler);
1438 widget_set_enter_handler(frame->widget, frame_enter_handler);
1439 widget_set_motion_handler(frame->widget, frame_motion_handler);
1440 widget_set_button_handler(frame->widget, frame_button_handler);
1441
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001442 window->frame = frame;
1443
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001444 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001445}
1446
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001447static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001448frame_destroy(struct frame *frame)
1449{
1450 /* frame->child must be destroyed by the application */
1451 widget_destroy(frame->widget);
1452 free(frame);
1453}
1454
1455static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001456input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001457 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001458{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001459 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001460 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001461
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001462 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001463 return;
1464
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001465 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001466 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001467 widget = old;
1468 if (input->grab)
1469 widget = input->grab;
1470 if (widget->leave_handler)
1471 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001472 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001473 }
1474
1475 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001476 widget = focus;
1477 if (input->grab)
1478 widget = input->grab;
1479 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001480 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001481 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001482 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001483
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001484 input_set_pointer_image(input, input->pointer_enter_serial,
1485 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001486 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001487}
1488
1489static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001490input_handle_motion(void *data, struct wl_input_device *input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001491 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001492{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001493 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001494 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001495 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001496 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001497 float sx = wl_fixed_to_double(sx_w);
1498 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001499
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001500 input->sx = sx;
1501 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001502
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001503 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001504 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001505 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001506 }
1507
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001508 if (input->grab)
1509 widget = input->grab;
1510 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001511 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001512 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001513 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001514 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001515 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001516
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001517 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001518}
1519
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001520void
1521input_grab(struct input *input, struct widget *widget, uint32_t button)
1522{
1523 input->grab = widget;
1524 input->grab_button = button;
1525}
1526
1527void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001528input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001529{
1530 struct widget *widget;
1531
1532 input->grab = NULL;
1533 if (input->pointer_focus) {
1534 widget = widget_find_widget(input->pointer_focus->widget,
1535 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001536 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001537 }
1538}
1539
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001540static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001541input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001542 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001543 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001544{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001545 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001546 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001547
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001548 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001549 if (input->focus_widget && input->grab == NULL && state)
1550 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001551
Neil Roberts6b28aad2012-01-23 19:11:18 +00001552 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001553 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001554 (*widget->button_handler)(widget,
1555 input, time,
1556 button, state,
1557 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001558
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001559 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001560 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001561}
1562
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001563static void
Scott Moreau210d0792012-03-22 10:47:01 -06001564input_handle_axis(void *data,
1565 struct wl_input_device *input_device,
1566 uint32_t time, uint32_t axis, int32_t value)
1567{
1568}
1569
1570static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001571input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001572 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001573{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001574 struct input *input = data;
1575 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001576 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001577 uint32_t code, num_syms;
1578 const xkb_keysym_t *syms;
1579 xkb_keysym_t sym;
1580 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001581
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001582 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001583 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001584 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001585 return;
1586
Kristian Høgsberg70163132012-05-08 15:55:39 -04001587 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1588 xkb_state_update_key(d->xkb.state, code,
1589 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001590
Kristian Høgsberg70163132012-05-08 15:55:39 -04001591 mask = xkb_state_serialise_mods(d->xkb.state,
1592 XKB_STATE_DEPRESSED |
1593 XKB_STATE_LATCHED);
1594 input->modifiers = 0;
1595 if (mask & input->display->xkb.control_mask)
1596 input->modifiers |= MOD_CONTROL_MASK;
1597 if (mask & input->display->xkb.alt_mask)
1598 input->modifiers |= MOD_ALT_MASK;
1599 if (mask & input->display->xkb.shift_mask)
1600 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001601
Kristian Høgsberg70163132012-05-08 15:55:39 -04001602 if (num_syms == 1 && syms[0] == XK_F5 &&
1603 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001604 if (state)
1605 window_set_maximized(window,
1606 window->type != TYPE_MAXIMIZED);
1607 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001608 if (num_syms == 1)
1609 sym = syms[0];
1610 else
1611 sym = NoSymbol;
1612
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001613 (*window->key_handler)(window, input, time, key,
1614 sym, state, window->user_data);
1615 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001616}
1617
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001618static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001619input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001620{
1621 struct window *window = input->pointer_focus;
1622
1623 if (!window)
1624 return;
1625
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001626 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001627
Pekka Paalanene1207c72011-12-16 12:02:09 +02001628 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001629 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001630}
1631
1632static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001633input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001634 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001635 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001636 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001637{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001638 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001639 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001640 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001641 float sx = wl_fixed_to_double(sx_w);
1642 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001643
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001644 input->display->serial = serial;
1645 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001646 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001647 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001648
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001649 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001650 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001651 window->pool = NULL;
1652 /* Schedule a redraw to free the pool */
1653 window_schedule_redraw(window);
1654 }
1655
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001656 input->sx = sx;
1657 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001658
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001659 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001660 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001661}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001662
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001663static void
1664input_handle_pointer_leave(void *data,
1665 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001666 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001667{
1668 struct input *input = data;
1669
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001670 input->display->serial = serial;
1671 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001672}
1673
1674static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001675input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001676{
1677 struct window *window = input->keyboard_focus;
1678
1679 if (!window)
1680 return;
1681
1682 window->keyboard_device = NULL;
1683 if (window->keyboard_focus_handler)
1684 (*window->keyboard_focus_handler)(window, NULL,
1685 window->user_data);
1686
1687 input->keyboard_focus = NULL;
1688}
1689
1690static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001691input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001692 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001693 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001694 struct wl_surface *surface,
1695 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001696{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001697 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001698 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001699
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001700 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001701 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001702
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001703 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001704 window->keyboard_device = input;
1705 if (window->keyboard_focus_handler)
1706 (*window->keyboard_focus_handler)(window,
1707 window->keyboard_device,
1708 window->user_data);
1709}
1710
1711static void
1712input_handle_keyboard_leave(void *data,
1713 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001714 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001715 struct wl_surface *surface)
1716{
1717 struct input *input = data;
1718
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001719 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001720 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001721}
1722
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001723static void
1724input_handle_touch_down(void *data,
1725 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001726 uint32_t serial, uint32_t time,
1727 struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001728 int32_t id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001729{
1730}
1731
1732static void
1733input_handle_touch_up(void *data,
1734 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001735 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001736{
1737}
1738
1739static void
1740input_handle_touch_motion(void *data,
1741 struct wl_input_device *wl_input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001742 uint32_t time, int32_t id,
1743 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001744{
1745}
1746
1747static void
1748input_handle_touch_frame(void *data,
1749 struct wl_input_device *wl_input_device)
1750{
1751}
1752
1753static void
1754input_handle_touch_cancel(void *data,
1755 struct wl_input_device *wl_input_device)
1756{
1757}
1758
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001759static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001760 input_handle_motion,
1761 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001762 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001763 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001764 input_handle_pointer_enter,
1765 input_handle_pointer_leave,
1766 input_handle_keyboard_enter,
1767 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001768 input_handle_touch_down,
1769 input_handle_touch_up,
1770 input_handle_touch_motion,
1771 input_handle_touch_frame,
1772 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001773};
1774
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001775void
1776input_get_position(struct input *input, int32_t *x, int32_t *y)
1777{
1778 *x = input->sx;
1779 *y = input->sy;
1780}
1781
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001782struct wl_input_device *
1783input_get_input_device(struct input *input)
1784{
1785 return input->input_device;
1786}
1787
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001788uint32_t
1789input_get_modifiers(struct input *input)
1790{
1791 return input->modifiers;
1792}
1793
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001794struct widget *
1795input_get_focus_widget(struct input *input)
1796{
1797 return input->focus_widget;
1798}
1799
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001800struct data_offer {
1801 struct wl_data_offer *offer;
1802 struct input *input;
1803 struct wl_array types;
1804 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001805
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001806 struct task io_task;
1807 int fd;
1808 data_func_t func;
1809 int32_t x, y;
1810 void *user_data;
1811};
1812
1813static void
1814data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1815{
1816 struct data_offer *offer = data;
1817 char **p;
1818
1819 p = wl_array_add(&offer->types, sizeof *p);
1820 *p = strdup(type);
1821}
1822
1823static const struct wl_data_offer_listener data_offer_listener = {
1824 data_offer_offer,
1825};
1826
1827static void
1828data_offer_destroy(struct data_offer *offer)
1829{
1830 char **p;
1831
1832 offer->refcount--;
1833 if (offer->refcount == 0) {
1834 wl_data_offer_destroy(offer->offer);
1835 for (p = offer->types.data; *p; p++)
1836 free(*p);
1837 wl_array_release(&offer->types);
1838 free(offer);
1839 }
1840}
1841
1842static void
1843data_device_data_offer(void *data,
1844 struct wl_data_device *data_device, uint32_t id)
1845{
1846 struct data_offer *offer;
1847
1848 offer = malloc(sizeof *offer);
1849
1850 wl_array_init(&offer->types);
1851 offer->refcount = 1;
1852 offer->input = data;
1853
1854 /* FIXME: Generate typesafe wrappers for this */
1855 offer->offer = (struct wl_data_offer *)
1856 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1857 id, &wl_data_offer_interface);
1858
1859 wl_data_offer_add_listener(offer->offer,
1860 &data_offer_listener, offer);
1861}
1862
1863static void
1864data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001865 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001866 wl_fixed_t x_w, wl_fixed_t y_w,
1867 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001868{
1869 struct input *input = data;
1870 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001871 float x = wl_fixed_to_double(x_w);
1872 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001873 char **p;
1874
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001875 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001876 input->drag_offer = wl_data_offer_get_user_data(offer);
1877 window = wl_surface_get_user_data(surface);
1878 input->pointer_focus = window;
1879
1880 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1881 *p = NULL;
1882
1883 window = input->pointer_focus;
1884 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001885 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001886 input->drag_offer->types.data,
1887 window->user_data);
1888}
1889
1890static void
1891data_device_leave(void *data, struct wl_data_device *data_device)
1892{
1893 struct input *input = data;
1894
1895 data_offer_destroy(input->drag_offer);
1896 input->drag_offer = NULL;
1897}
1898
1899static void
1900data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001901 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001902{
1903 struct input *input = data;
1904 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001905 float x = wl_fixed_to_double(x_w);
1906 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001907
1908 input->sx = x;
1909 input->sy = y;
1910
1911 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001912 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001913 input->drag_offer->types.data,
1914 window->user_data);
1915}
1916
1917static void
1918data_device_drop(void *data, struct wl_data_device *data_device)
1919{
1920 struct input *input = data;
1921 struct window *window = input->pointer_focus;
1922
1923 if (window->drop_handler)
1924 window->drop_handler(window, input,
1925 input->sx, input->sy, window->user_data);
1926}
1927
1928static void
1929data_device_selection(void *data,
1930 struct wl_data_device *wl_data_device,
1931 struct wl_data_offer *offer)
1932{
1933 struct input *input = data;
1934 char **p;
1935
1936 if (input->selection_offer)
1937 data_offer_destroy(input->selection_offer);
1938
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001939 if (offer) {
1940 input->selection_offer = wl_data_offer_get_user_data(offer);
1941 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1942 *p = NULL;
1943 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001944}
1945
1946static const struct wl_data_device_listener data_device_listener = {
1947 data_device_data_offer,
1948 data_device_enter,
1949 data_device_leave,
1950 data_device_motion,
1951 data_device_drop,
1952 data_device_selection
1953};
1954
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001955void
1956input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1957{
1958 struct display *display = input->display;
1959 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001960 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001961
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001962 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001963 return;
1964
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07001965 if (display->cursors[pointer].n_images == 0)
1966 return;
1967
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001968 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001969
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001970 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001971 return;
1972
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001973 input->current_cursor = pointer;
1974 buffer = display_get_buffer_for_surface(display, image->surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001975 wl_input_device_attach(input->input_device, time, buffer,
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001976 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001977}
1978
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001979struct wl_data_device *
1980input_get_data_device(struct input *input)
1981{
1982 return input->data_device;
1983}
1984
1985void
1986input_set_selection(struct input *input,
1987 struct wl_data_source *source, uint32_t time)
1988{
1989 wl_data_device_set_selection(input->data_device, source, time);
1990}
1991
1992void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001993input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001994{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001995 wl_data_offer_accept(input->drag_offer->offer,
1996 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001997}
1998
1999static void
2000offer_io_func(struct task *task, uint32_t events)
2001{
2002 struct data_offer *offer =
2003 container_of(task, struct data_offer, io_task);
2004 unsigned int len;
2005 char buffer[4096];
2006
2007 len = read(offer->fd, buffer, sizeof buffer);
2008 offer->func(buffer, len,
2009 offer->x, offer->y, offer->user_data);
2010
2011 if (len == 0) {
2012 close(offer->fd);
2013 data_offer_destroy(offer);
2014 }
2015}
2016
2017static void
2018data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2019 data_func_t func, void *user_data)
2020{
2021 int p[2];
2022
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002023 if (pipe2(p, O_CLOEXEC) == -1)
2024 return;
2025
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002026 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2027 close(p[1]);
2028
2029 offer->io_task.run = offer_io_func;
2030 offer->fd = p[0];
2031 offer->func = func;
2032 offer->refcount++;
2033 offer->user_data = user_data;
2034
2035 display_watch_fd(offer->input->display,
2036 offer->fd, EPOLLIN, &offer->io_task);
2037}
2038
2039void
2040input_receive_drag_data(struct input *input, const char *mime_type,
2041 data_func_t func, void *data)
2042{
2043 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2044 input->drag_offer->x = input->sx;
2045 input->drag_offer->y = input->sy;
2046}
2047
2048int
2049input_receive_selection_data(struct input *input, const char *mime_type,
2050 data_func_t func, void *data)
2051{
2052 char **p;
2053
2054 if (input->selection_offer == NULL)
2055 return -1;
2056
2057 for (p = input->selection_offer->types.data; *p; p++)
2058 if (strcmp(mime_type, *p) == 0)
2059 break;
2060
2061 if (*p == NULL)
2062 return -1;
2063
2064 data_offer_receive_data(input->selection_offer,
2065 mime_type, func, data);
2066 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002067}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002068
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002069int
2070input_receive_selection_data_to_fd(struct input *input,
2071 const char *mime_type, int fd)
2072{
2073 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2074
2075 return 0;
2076}
2077
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002078void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002079window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002080{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002081 if (!window->shell_surface)
2082 return;
2083
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002084 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002085 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002086}
2087
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002088static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002089idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002090{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002091 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002092
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002093 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002094 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002095 widget_set_allocation(widget,
2096 window->pending_allocation.x,
2097 window->pending_allocation.y,
2098 window->pending_allocation.width,
2099 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002100
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002101 if (window->input_region) {
2102 wl_region_destroy(window->input_region);
2103 window->input_region = NULL;
2104 }
2105
2106 if (window->opaque_region) {
2107 wl_region_destroy(window->opaque_region);
2108 window->opaque_region = NULL;
2109 }
2110
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002111 if (widget->resize_handler)
2112 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002113 widget->allocation.width,
2114 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002115 widget->user_data);
2116
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002117 if (window->allocation.width != widget->allocation.width ||
2118 window->allocation.height != widget->allocation.height) {
2119 window->allocation = widget->allocation;
2120 window_schedule_redraw(window);
2121 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002122}
2123
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002124void
2125window_schedule_resize(struct window *window, int width, int height)
2126{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002127 window->pending_allocation.x = 0;
2128 window->pending_allocation.y = 0;
2129 window->pending_allocation.width = width;
2130 window->pending_allocation.height = height;
2131
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002132 window->resize_needed = 1;
2133 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002134}
2135
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002136void
2137widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2138{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002139 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002140}
2141
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002142static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002143handle_ping(void *data, struct wl_shell_surface *shell_surface,
2144 uint32_t serial)
2145{
2146 wl_shell_surface_pong(shell_surface, serial);
2147}
2148
2149static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002150handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002151 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002152{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002153 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002154
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002155 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002156 return;
2157
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002158 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002159 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002160}
2161
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002162static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002163menu_destroy(struct menu *menu)
2164{
2165 widget_destroy(menu->widget);
2166 window_destroy(menu->window);
2167 free(menu);
2168}
2169
2170static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002171handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2172{
2173 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002174 struct menu *menu = window->widget->user_data;
2175
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002176 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002177 * device. Or just use wl_callback. And this really needs to
2178 * be a window vfunc that the menu can set. And we need the
2179 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002180
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002181 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002182 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002183 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002184}
2185
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002186static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002187 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002188 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002189 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002190};
2191
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002192void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002193window_get_allocation(struct window *window,
2194 struct rectangle *allocation)
2195{
2196 *allocation = window->allocation;
2197}
2198
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002199static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002200widget_redraw(struct widget *widget)
2201{
2202 struct widget *child;
2203
2204 if (widget->redraw_handler)
2205 widget->redraw_handler(widget, widget->user_data);
2206 wl_list_for_each(child, &widget->child_list, link)
2207 widget_redraw(child);
2208}
2209
2210static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002211frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2212{
2213 struct window *window = data;
2214
2215 wl_callback_destroy(callback);
2216 window->redraw_scheduled = 0;
2217 if (window->redraw_needed)
2218 window_schedule_redraw(window);
2219}
2220
2221static const struct wl_callback_listener listener = {
2222 frame_callback
2223};
2224
2225static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002226idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002227{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002228 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002229 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002230
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002231 if (window->resize_needed)
2232 idle_resize(window);
2233
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002234 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002235 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002236 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002237 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002238 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002239
2240 callback = wl_surface_frame(window->surface);
2241 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002242}
2243
2244void
2245window_schedule_redraw(struct window *window)
2246{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002247 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002248 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002249 window->redraw_task.run = idle_redraw;
2250 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002251 window->redraw_scheduled = 1;
2252 }
2253}
2254
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002255void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002256window_set_custom(struct window *window)
2257{
2258 window->type = TYPE_CUSTOM;
2259}
2260
2261void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002262window_set_fullscreen(struct window *window, int fullscreen)
2263{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002264 if (!window->display->shell)
2265 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002266
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002267 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002268 return;
2269
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002270 if (fullscreen) {
2271 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002272 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002273 wl_shell_surface_set_fullscreen(window->shell_surface,
2274 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2275 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002276 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002277 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002278 wl_shell_surface_set_toplevel(window->shell_surface);
2279 window_schedule_resize(window,
2280 window->saved_allocation.width,
2281 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002282 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002283}
2284
2285void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002286window_set_maximized(struct window *window, int maximized)
2287{
2288 if (!window->display->shell)
2289 return;
2290
2291 if ((window->type == TYPE_MAXIMIZED) == maximized)
2292 return;
2293
2294 if (window->type == TYPE_TOPLEVEL) {
2295 window->saved_allocation = window->allocation;
2296 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2297 window->type = TYPE_MAXIMIZED;
2298 } else {
2299 wl_shell_surface_set_toplevel(window->shell_surface);
2300 window->type = TYPE_TOPLEVEL;
2301 window_schedule_resize(window,
2302 window->saved_allocation.width,
2303 window->saved_allocation.height);
2304 }
2305}
2306
2307void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002308window_set_user_data(struct window *window, void *data)
2309{
2310 window->user_data = data;
2311}
2312
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002313void *
2314window_get_user_data(struct window *window)
2315{
2316 return window->user_data;
2317}
2318
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002319void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002320window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002321 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002322{
2323 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002324}
2325
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002326void
2327window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002328 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002329{
2330 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002331}
2332
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002333void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002334window_set_data_handler(struct window *window, window_data_handler_t handler)
2335{
2336 window->data_handler = handler;
2337}
2338
2339void
2340window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2341{
2342 window->drop_handler = handler;
2343}
2344
2345void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002346window_set_close_handler(struct window *window,
2347 window_close_handler_t handler)
2348{
2349 window->close_handler = handler;
2350}
2351
2352void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002353window_set_title(struct window *window, const char *title)
2354{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002355 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002356 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002357 if (window->shell_surface)
2358 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002359}
2360
2361const char *
2362window_get_title(struct window *window)
2363{
2364 return window->title;
2365}
2366
2367void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002368window_damage(struct window *window, int32_t x, int32_t y,
2369 int32_t width, int32_t height)
2370{
2371 wl_surface_damage(window->surface, x, y, width, height);
2372}
2373
Casey Dahlin9074db52012-04-19 22:50:09 -04002374static void
2375surface_enter(void *data,
2376 struct wl_surface *wl_surface, struct wl_output *output)
2377{
2378}
2379
2380static void
2381surface_leave(void *data,
2382 struct wl_surface *wl_surface, struct wl_output *output)
2383{
2384}
2385
2386static const struct wl_surface_listener surface_listener = {
2387 surface_enter,
2388 surface_leave
2389};
2390
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002391static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002392window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002393{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002394 struct window *window;
2395
2396 window = malloc(sizeof *window);
2397 if (window == NULL)
2398 return NULL;
2399
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002400 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002401 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002402 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002403 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002404 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002405 if (display->shell) {
2406 window->shell_surface =
2407 wl_shell_get_shell_surface(display->shell,
2408 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002409 if (window->title)
2410 wl_shell_surface_set_title(window->shell_surface,
2411 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002412 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002413 window->allocation.x = 0;
2414 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002415 window->allocation.width = 0;
2416 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002417 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002418 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002419 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002420 window->input_region = NULL;
2421 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002422
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002423 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002424#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002425 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002426#else
2427 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2428#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002429 else
2430 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002431
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002432 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002433 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002434 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002435
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002436 if (window->shell_surface) {
2437 wl_shell_surface_set_user_data(window->shell_surface, window);
2438 wl_shell_surface_add_listener(window->shell_surface,
2439 &shell_surface_listener, window);
2440 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002441
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002442 return window;
2443}
2444
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002445struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002446window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002447{
2448 struct window *window;
2449
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002450 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002451 if (!window)
2452 return NULL;
2453
2454 return window;
2455}
2456
2457struct window *
2458window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002459 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002460{
2461 struct window *window;
2462
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002463 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002464 if (!window)
2465 return NULL;
2466
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002467 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002468 window->x = x;
2469 window->y = y;
2470
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002471 if (display->shell)
2472 wl_shell_surface_set_transient(window->shell_surface,
2473 window->parent->shell_surface,
2474 window->x, window->y, 0);
2475
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002476 return window;
2477}
2478
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002479static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002480menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002481{
2482 int next;
2483
2484 next = (sy - 8) / 20;
2485 if (menu->current != next) {
2486 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002487 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002488 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002489}
2490
2491static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002492menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002493 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002494 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002495{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002496 struct menu *menu = data;
2497
2498 if (widget == menu->widget)
2499 menu_set_item(data, y);
2500
2501 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002502}
2503
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002504static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002505menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002506 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002507{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002508 struct menu *menu = data;
2509
2510 if (widget == menu->widget)
2511 menu_set_item(data, y);
2512
2513 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002514}
2515
2516static void
2517menu_leave_handler(struct widget *widget, struct input *input, void *data)
2518{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002519 struct menu *menu = data;
2520
2521 if (widget == menu->widget)
2522 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002523}
2524
2525static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002526menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002527 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002528 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002529
2530{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002531 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002532
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002533 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002534 /* Either relase after press-drag-release or
2535 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002536 menu->func(menu->window->parent,
2537 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002538 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002539 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002540 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002541}
2542
2543static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002544menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002545{
2546 cairo_t *cr;
2547 const int32_t r = 3, margin = 3;
2548 struct menu *menu = data;
2549 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002550 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002551
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002552 cr = cairo_create(window->cairo_surface);
2553 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2554 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2555 cairo_paint(cr);
2556
2557 width = window->allocation.width;
2558 height = window->allocation.height;
2559 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002560
2561 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002562 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2563 cairo_fill(cr);
2564
2565 for (i = 0; i < menu->count; i++) {
2566 if (i == menu->current) {
2567 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2568 cairo_rectangle(cr, margin, i * 20 + margin,
2569 width - 2 * margin, 20);
2570 cairo_fill(cr);
2571 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2572 cairo_move_to(cr, 10, i * 20 + 16);
2573 cairo_show_text(cr, menu->entries[i]);
2574 } else {
2575 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2576 cairo_move_to(cr, 10, i * 20 + 16);
2577 cairo_show_text(cr, menu->entries[i]);
2578 }
2579 }
2580
2581 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002582}
2583
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002584void
2585window_show_menu(struct display *display,
2586 struct input *input, uint32_t time, struct window *parent,
2587 int32_t x, int32_t y,
2588 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002589{
2590 struct window *window;
2591 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002592 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002593
2594 menu = malloc(sizeof *menu);
2595 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002596 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002597
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002598 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002599 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002600 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002601
2602 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002603 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002604 menu->entries = entries;
2605 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002606 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002607 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002608 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002609 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002610 window->type = TYPE_MENU;
2611 window->x = x;
2612 window->y = y;
2613
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002614 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002615 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2616 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002617 window->parent->shell_surface,
2618 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002619
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002620 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002621 widget_set_enter_handler(menu->widget, menu_enter_handler);
2622 widget_set_leave_handler(menu->widget, menu_leave_handler);
2623 widget_set_motion_handler(menu->widget, menu_motion_handler);
2624 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002625
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002626 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002627 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002628}
2629
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002630void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002631window_set_buffer_type(struct window *window, enum window_buffer_type type)
2632{
2633 window->buffer_type = type;
2634}
2635
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002636
2637static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002638display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002639 struct wl_output *wl_output,
2640 int x, int y,
2641 int physical_width,
2642 int physical_height,
2643 int subpixel,
2644 const char *make,
2645 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002646{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002647 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002648
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002649 output->allocation.x = x;
2650 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002651}
2652
2653static void
2654display_handle_mode(void *data,
2655 struct wl_output *wl_output,
2656 uint32_t flags,
2657 int width,
2658 int height,
2659 int refresh)
2660{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002661 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002662 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002663
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002664 if (flags & WL_OUTPUT_MODE_CURRENT) {
2665 output->allocation.width = width;
2666 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002667 if (display->output_configure_handler)
2668 (*display->output_configure_handler)(
2669 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002670 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002671}
2672
2673static const struct wl_output_listener output_listener = {
2674 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002675 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002676};
2677
2678static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002679display_add_output(struct display *d, uint32_t id)
2680{
2681 struct output *output;
2682
2683 output = malloc(sizeof *output);
2684 if (output == NULL)
2685 return;
2686
2687 memset(output, 0, sizeof *output);
2688 output->display = d;
2689 output->output =
2690 wl_display_bind(d->display, id, &wl_output_interface);
2691 wl_list_insert(d->output_list.prev, &output->link);
2692
2693 wl_output_add_listener(output->output, &output_listener, output);
2694}
2695
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002696static void
2697output_destroy(struct output *output)
2698{
2699 if (output->destroy_handler)
2700 (*output->destroy_handler)(output, output->user_data);
2701
2702 wl_output_destroy(output->output);
2703 wl_list_remove(&output->link);
2704 free(output);
2705}
2706
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002707void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002708display_set_output_configure_handler(struct display *display,
2709 display_output_handler_t handler)
2710{
2711 struct output *output;
2712
2713 display->output_configure_handler = handler;
2714 if (!handler)
2715 return;
2716
2717 wl_list_for_each(output, &display->output_list, link)
2718 (*display->output_configure_handler)(output,
2719 display->user_data);
2720}
2721
2722void
2723output_set_user_data(struct output *output, void *data)
2724{
2725 output->user_data = data;
2726}
2727
2728void *
2729output_get_user_data(struct output *output)
2730{
2731 return output->user_data;
2732}
2733
2734void
2735output_set_destroy_handler(struct output *output,
2736 display_output_handler_t handler)
2737{
2738 output->destroy_handler = handler;
2739 /* FIXME: implement this, once we have way to remove outputs */
2740}
2741
2742void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002743output_get_allocation(struct output *output, struct rectangle *allocation)
2744{
2745 *allocation = output->allocation;
2746}
2747
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002748struct wl_output *
2749output_get_wl_output(struct output *output)
2750{
2751 return output->output;
2752}
2753
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002754static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002755display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002756{
2757 struct input *input;
2758
2759 input = malloc(sizeof *input);
2760 if (input == NULL)
2761 return;
2762
2763 memset(input, 0, sizeof *input);
2764 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002765 input->input_device =
2766 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002767 input->pointer_focus = NULL;
2768 input->keyboard_focus = NULL;
2769 wl_list_insert(d->input_list.prev, &input->link);
2770
2771 wl_input_device_add_listener(input->input_device,
2772 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002773 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002774
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002775 input->data_device =
2776 wl_data_device_manager_get_data_device(d->data_device_manager,
2777 input->input_device);
2778 wl_data_device_add_listener(input->data_device,
2779 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002780}
2781
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002782static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002783input_destroy(struct input *input)
2784{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002785 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002786 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002787
2788 if (input->drag_offer)
2789 data_offer_destroy(input->drag_offer);
2790
2791 if (input->selection_offer)
2792 data_offer_destroy(input->selection_offer);
2793
2794 wl_data_device_destroy(input->data_device);
2795 wl_list_remove(&input->link);
2796 wl_input_device_destroy(input->input_device);
2797 free(input);
2798}
2799
2800static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002801display_handle_global(struct wl_display *display, uint32_t id,
2802 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002803{
2804 struct display *d = data;
2805
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002806 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002807 d->compositor =
2808 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002809 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002810 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002811 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002812 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002813 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002814 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002815 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002816 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002817 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2818 d->data_device_manager =
2819 wl_display_bind(display, id,
2820 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002821 }
2822}
2823
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002824static void
2825display_render_frame(struct display *d)
2826{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002827 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002828 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002829
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002830 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002831 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2832 cr = cairo_create(d->shadow);
2833 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2834 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002835 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002836 cairo_fill(cr);
2837 cairo_destroy(cr);
2838 blur_surface(d->shadow, 64);
2839
2840 d->active_frame =
2841 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2842 cr = cairo_create(d->active_frame);
2843 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002844
2845 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2846 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2847 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2848 cairo_set_source(cr, pattern);
2849 cairo_pattern_destroy(pattern);
2850
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002851 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002852 cairo_fill(cr);
2853 cairo_destroy(cr);
2854
2855 d->inactive_frame =
2856 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2857 cr = cairo_create(d->inactive_frame);
2858 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002859 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002860 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002861 cairo_fill(cr);
2862 cairo_destroy(cr);
2863}
2864
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002865static void
2866init_xkb(struct display *d)
2867{
Kristian Høgsberg70163132012-05-08 15:55:39 -04002868 d->xkb.names.rules = "evdev";
2869 d->xkb.names.model = "pc105";
2870 d->xkb.names.layout = (char *) option_xkb_layout;
2871 d->xkb.names.variant = (char *) option_xkb_variant;
2872 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002873
Kristian Høgsberg70163132012-05-08 15:55:39 -04002874 d->xkb.context = xkb_context_new();
2875 if (!d->xkb.context) {
2876 fprintf(stderr, "Failed to create XKB context\n");
2877 exit(1);
2878 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002879
Kristian Høgsberg70163132012-05-08 15:55:39 -04002880 d->xkb.keymap =
2881 xkb_map_new_from_names(d->xkb.context, &d->xkb.names);
2882 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002883 fprintf(stderr, "Failed to compile keymap\n");
2884 exit(1);
2885 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04002886
2887 d->xkb.state = xkb_state_new(d->xkb.keymap);
2888 if (!d->xkb.state) {
2889 fprintf(stderr, "Failed to create XKB state\n");
2890 exit(1);
2891 }
2892
2893 d->xkb.control_mask =
2894 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
2895 d->xkb.alt_mask =
2896 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
2897 d->xkb.shift_mask =
2898 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
2899
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002900}
2901
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002902static void
2903fini_xkb(struct display *display)
2904{
Kristian Høgsberg70163132012-05-08 15:55:39 -04002905 xkb_state_unref(display->xkb.state);
2906 xkb_map_unref(display->xkb.keymap);
2907 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002908}
2909
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002910#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002911static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002912init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002913{
2914 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002915 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002916
Rob Clark6396ed32012-03-11 19:48:41 -05002917#ifdef USE_CAIRO_GLESV2
2918# define GL_BIT EGL_OPENGL_ES2_BIT
2919#else
2920# define GL_BIT EGL_OPENGL_BIT
2921#endif
2922
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002923 static const EGLint argb_cfg_attribs[] = {
2924 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002925 EGL_RED_SIZE, 1,
2926 EGL_GREEN_SIZE, 1,
2927 EGL_BLUE_SIZE, 1,
2928 EGL_ALPHA_SIZE, 1,
2929 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002930 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002931 EGL_NONE
2932 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002933
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002934#ifdef USE_CAIRO_GLESV2
2935 static const EGLint context_attribs[] = {
2936 EGL_CONTEXT_CLIENT_VERSION, 2,
2937 EGL_NONE
2938 };
2939 EGLint api = EGL_OPENGL_ES_API;
2940#else
2941 EGLint *context_attribs = NULL;
2942 EGLint api = EGL_OPENGL_API;
2943#endif
2944
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002945 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002946 if (!eglInitialize(d->dpy, &major, &minor)) {
2947 fprintf(stderr, "failed to initialize display\n");
2948 return -1;
2949 }
2950
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002951 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002952 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2953 return -1;
2954 }
2955
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002956 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2957 &d->argb_config, 1, &n) || n != 1) {
2958 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002959 return -1;
2960 }
2961
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002962 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002963 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002964 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002965 fprintf(stderr, "failed to create context\n");
2966 return -1;
2967 }
2968
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002969 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002970 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002971 return -1;
2972 }
2973
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002974#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002975 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2976 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2977 fprintf(stderr, "failed to get cairo egl argb device\n");
2978 return -1;
2979 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002980#endif
2981
2982 return 0;
2983}
2984
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002985static void
2986fini_egl(struct display *display)
2987{
2988#ifdef HAVE_CAIRO_EGL
2989 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002990#endif
2991
2992 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2993 EGL_NO_CONTEXT);
2994
2995 eglTerminate(display->dpy);
2996 eglReleaseThread();
2997}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002998#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002999
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003000static int
3001event_mask_update(uint32_t mask, void *data)
3002{
3003 struct display *d = data;
3004
3005 d->mask = mask;
3006
3007 return 0;
3008}
3009
3010static void
3011handle_display_data(struct task *task, uint32_t events)
3012{
3013 struct display *display =
3014 container_of(task, struct display, display_task);
3015
3016 wl_display_iterate(display->display, display->mask);
3017}
3018
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003019struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003020display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003021{
3022 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003023
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003024 argc = parse_options(xkb_options,
3025 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003026
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003027 d = malloc(sizeof *d);
3028 if (d == NULL)
3029 return NULL;
3030
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003031 memset(d, 0, sizeof *d);
3032
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003033 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003034 if (d->display == NULL) {
3035 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003036 return NULL;
3037 }
3038
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003039 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3040 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3041 d->display_task.run = handle_display_data;
3042 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3043
3044 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003045 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003046 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003047
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003048 /* Set up listener so we'll catch all events. */
3049 wl_display_add_global_listener(d->display,
3050 display_handle_global, d);
3051
3052 /* Process connection events. */
3053 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003055 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003056 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003057#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003058
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003059 d->image_target_texture_2d =
3060 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3061 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3062 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3063
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003064 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003065
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003066 display_render_frame(d);
3067
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003068 wl_list_init(&d->window_list);
3069
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003070 init_xkb(d);
3071
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003072 return d;
3073}
3074
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003075static void
3076display_destroy_outputs(struct display *display)
3077{
3078 struct output *tmp;
3079 struct output *output;
3080
3081 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3082 output_destroy(output);
3083}
3084
Pekka Paalanene1207c72011-12-16 12:02:09 +02003085static void
3086display_destroy_inputs(struct display *display)
3087{
3088 struct input *tmp;
3089 struct input *input;
3090
3091 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3092 input_destroy(input);
3093}
3094
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003095void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003096display_destroy(struct display *display)
3097{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003098 if (!wl_list_empty(&display->window_list))
3099 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3100
3101 if (!wl_list_empty(&display->deferred_list))
3102 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3103
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003104 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003105 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003106
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003107 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003108
3109 cairo_surface_destroy(display->active_frame);
3110 cairo_surface_destroy(display->inactive_frame);
3111 cairo_surface_destroy(display->shadow);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003112 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003113
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003114#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003115 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003116#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003117
Pekka Paalanenc2052982011-12-16 11:41:32 +02003118 if (display->shell)
3119 wl_shell_destroy(display->shell);
3120
3121 if (display->shm)
3122 wl_shm_destroy(display->shm);
3123
3124 if (display->data_device_manager)
3125 wl_data_device_manager_destroy(display->data_device_manager);
3126
3127 wl_compositor_destroy(display->compositor);
3128
3129 close(display->epoll_fd);
3130
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003131 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003132 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003133 free(display);
3134}
3135
3136void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003137display_set_user_data(struct display *display, void *data)
3138{
3139 display->user_data = data;
3140}
3141
3142void *
3143display_get_user_data(struct display *display)
3144{
3145 return display->user_data;
3146}
3147
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003148struct wl_display *
3149display_get_display(struct display *display)
3150{
3151 return display->display;
3152}
3153
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003154struct output *
3155display_get_output(struct display *display)
3156{
3157 return container_of(display->output_list.next, struct output, link);
3158}
3159
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003160struct wl_compositor *
3161display_get_compositor(struct display *display)
3162{
3163 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003164}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003165
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003166uint32_t
3167display_get_serial(struct display *display)
3168{
3169 return display->serial;
3170}
3171
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003172EGLDisplay
3173display_get_egl_display(struct display *d)
3174{
3175 return d->dpy;
3176}
3177
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003178struct wl_data_source *
3179display_create_data_source(struct display *display)
3180{
3181 return wl_data_device_manager_create_data_source(display->data_device_manager);
3182}
3183
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003184EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003185display_get_argb_egl_config(struct display *d)
3186{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003187 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003188}
3189
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003190struct wl_shell *
3191display_get_shell(struct display *display)
3192{
3193 return display->shell;
3194}
3195
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003196int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003197display_acquire_window_surface(struct display *display,
3198 struct window *window,
3199 EGLContext ctx)
3200{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003201#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003202 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003203 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003204
3205 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003206 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003207 device = cairo_surface_get_device(window->cairo_surface);
3208 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003209 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003210
Benjamin Franzke0c991632011-09-27 21:57:31 +02003211 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003212 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003213 ctx = display->argb_ctx;
3214 else
3215 assert(0);
3216 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003217
3218 data = cairo_surface_get_user_data(window->cairo_surface,
3219 &surface_data_key);
3220
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003221 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003222 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003223 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3224 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003225
3226 return 0;
3227#else
3228 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003229#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003230}
3231
3232void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003233display_release_window_surface(struct display *display,
3234 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003235{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003236#ifdef HAVE_CAIRO_EGL
3237 cairo_device_t *device;
3238
3239 device = cairo_surface_get_device(window->cairo_surface);
3240 if (!device)
3241 return;
3242
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003243 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003244 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003245 cairo_device_release(device);
3246#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003247}
3248
3249void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003250display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003251{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003252 wl_list_insert(&display->deferred_list, &task->link);
3253}
3254
3255void
3256display_watch_fd(struct display *display,
3257 int fd, uint32_t events, struct task *task)
3258{
3259 struct epoll_event ep;
3260
3261 ep.events = events;
3262 ep.data.ptr = task;
3263 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3264}
3265
3266void
3267display_run(struct display *display)
3268{
3269 struct task *task;
3270 struct epoll_event ep[16];
3271 int i, count;
3272
Pekka Paalanen826d7952011-12-15 10:14:07 +02003273 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003274 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003275 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003276
3277 while (!wl_list_empty(&display->deferred_list)) {
3278 task = container_of(display->deferred_list.next,
3279 struct task, link);
3280 wl_list_remove(&task->link);
3281 task->run(task, 0);
3282 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003283
3284 if (!display->running)
3285 break;
3286
3287 wl_display_flush(display->display);
3288
3289 count = epoll_wait(display->epoll_fd,
3290 ep, ARRAY_LENGTH(ep), -1);
3291 for (i = 0; i < count; i++) {
3292 task = ep[i].data.ptr;
3293 task->run(task, ep[i].events);
3294 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003295 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003296}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003297
3298void
3299display_exit(struct display *display)
3300{
3301 display->running = 0;
3302}