blob: e4cfff98445321bc1474f529b9f645b39f343d57 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040039
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040040#include <pixman.h>
41
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Rob Clark6396ed32012-03-11 19:48:41 -050044#ifdef USE_CAIRO_GLESV2
45#include <GLES2/gl2.h>
46#include <GLES2/gl2ext.h>
47#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040048#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050049#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <EGL/egl.h>
51#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040052
Kristian Høgsberg8def2642011-01-14 17:41:33 -050053#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050056
Daniel Stone9d4f0302012-02-15 16:33:21 +000057#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030058#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040059
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020061#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040062#include "../shared/cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050063
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050065
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070066struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030067
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050068struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050069 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050070 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040071 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040072 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040073 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040074 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050075 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020076 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020077 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040078 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040079
80 int display_fd;
81 uint32_t mask;
82 struct task display_task;
83
84 int epoll_fd;
85 struct wl_list deferred_list;
86
Pekka Paalanen826d7952011-12-15 10:14:07 +020087 int running;
88
Kristian Høgsberg478d9262010-06-08 20:34:11 -040089 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040090 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050091 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040092
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040093 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040094
95 struct {
96 struct xkb_rule_names names;
97 struct xkb_keymap *keymap;
98 struct xkb_state *state;
99 struct xkb_context *context;
100 xkb_mod_mask_t control_mask;
101 xkb_mod_mask_t alt_mask;
102 xkb_mod_mask_t shift_mask;
103 } xkb;
104
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300105 struct wl_cursor_theme *cursor_theme;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400106
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500107 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
108 PFNEGLCREATEIMAGEKHRPROC create_image;
109 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200110
111 display_output_handler_t output_configure_handler;
112
113 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500114};
115
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400116enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400117 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400119 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500120 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400121 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500122 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 TYPE_CUSTOM
124};
Rob Bradford7507b572012-05-15 17:55:34 +0100125
126struct window_output {
127 struct output *output;
128 struct wl_list link;
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;
Rob Bradford7507b572012-05-15 17:55:34 +0100134 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500135 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200136 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500137 struct wl_region *input_region;
138 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500139 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500140 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500141 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500142 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400143 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400144 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400145 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400146 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400147 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400148 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400149 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400150 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400151 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500152
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400153 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500154
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700155 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400156
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500157 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500158 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400159 window_data_handler_t data_handler;
160 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500161 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400162
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200163 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500164 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500165
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500166 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400167 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500168};
169
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500170struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500171 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500172 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400173 struct wl_list link;
174 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500175 widget_resize_handler_t resize_handler;
176 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500177 widget_enter_handler_t enter_handler;
178 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500179 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500180 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400181 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500182 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400183};
184
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400185struct input {
186 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100187 struct wl_seat *seat;
188 struct wl_pointer *pointer;
189 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400190 struct window *pointer_focus;
191 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300192 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400194 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400195 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400196 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400197
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500198 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500199 struct widget *grab;
200 uint32_t grab_button;
201
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400202 struct wl_data_device *data_device;
203 struct data_offer *drag_offer;
204 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400205};
206
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500207struct output {
208 struct display *display;
209 struct wl_output *output;
210 struct rectangle allocation;
211 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200212
213 display_output_handler_t destroy_handler;
214 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500215};
216
Martin Minarik1998b152012-05-10 02:04:35 +0200217enum frame_button_action {
218 FRAME_BUTTON_NULL = 0,
219 FRAME_BUTTON_ICON = 1,
220 FRAME_BUTTON_CLOSE = 2,
221 FRAME_BUTTON_MINIMIZE = 3,
222 FRAME_BUTTON_MAXIMIZE = 4,
223};
224
225enum frame_button_pointer {
226 FRAME_BUTTON_DEFAULT = 0,
227 FRAME_BUTTON_OVER = 1,
228 FRAME_BUTTON_ACTIVE = 2,
229};
230
231enum frame_button_align {
232 FRAME_BUTTON_RIGHT = 0,
233 FRAME_BUTTON_LEFT = 1,
234};
235
236enum frame_button_decoration {
237 FRAME_BUTTON_NONE = 0,
238 FRAME_BUTTON_FANCY = 1,
239};
240
241struct frame_button {
242 struct widget *widget;
243 struct frame *frame;
244 cairo_surface_t *icon;
245 enum frame_button_action type;
246 enum frame_button_pointer state;
247 struct wl_list link; /* buttons_list */
248 enum frame_button_align align;
249 enum frame_button_decoration decoration;
250};
251
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500252struct frame {
253 struct widget *widget;
254 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200255 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500256};
257
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500258struct menu {
259 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500260 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500261 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500262 const char **entries;
263 uint32_t time;
264 int current;
265 int count;
266 menu_func_t func;
267};
268
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700269struct shm_pool {
270 struct wl_shm_pool *pool;
271 size_t size;
272 size_t used;
273 void *data;
274};
275
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400276enum {
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300277 WL_CURSOR_DEFAULT = 100,
278 WL_CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400279};
280
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500281enum window_location {
282 WINDOW_INTERIOR = 0,
283 WINDOW_RESIZING_TOP = 1,
284 WINDOW_RESIZING_BOTTOM = 2,
285 WINDOW_RESIZING_LEFT = 4,
286 WINDOW_RESIZING_TOP_LEFT = 5,
287 WINDOW_RESIZING_BOTTOM_LEFT = 6,
288 WINDOW_RESIZING_RIGHT = 8,
289 WINDOW_RESIZING_TOP_RIGHT = 9,
290 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
291 WINDOW_RESIZING_MASK = 15,
292 WINDOW_EXTERIOR = 16,
293 WINDOW_TITLEBAR = 17,
294 WINDOW_CLIENT_AREA = 18,
295};
296
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400297const char *option_xkb_layout = "us";
298const char *option_xkb_variant = "";
299const char *option_xkb_options = "";
300
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400301static const struct weston_option xkb_options[] = {
302 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
303 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
304 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400305};
306
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400307static const cairo_user_data_key_t surface_data_key;
308struct surface_data {
309 struct wl_buffer *buffer;
310};
311
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500312#define MULT(_d,c,a,t) \
313 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
314
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500315#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100317struct egl_window_surface_data {
318 struct display *display;
319 struct wl_surface *surface;
320 struct wl_egl_window *window;
321 EGLSurface surf;
322};
323
324static void
325egl_window_surface_data_destroy(void *p)
326{
327 struct egl_window_surface_data *data = p;
328 struct display *d = data->display;
329
330 eglDestroySurface(d->dpy, data->surf);
331 wl_egl_window_destroy(data->window);
332 data->surface = NULL;
333
334 free(p);
335}
336
337static cairo_surface_t *
338display_create_egl_window_surface(struct display *display,
339 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400340 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100341 struct rectangle *rectangle)
342{
343 cairo_surface_t *cairo_surface;
344 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400345 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200346 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100347
348 data = malloc(sizeof *data);
349 if (data == NULL)
350 return NULL;
351
352 data->display = display;
353 data->surface = surface;
354
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500355 config = display->argb_config;
356 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400357
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400358 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100359 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400360 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100361
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400362 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500363 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100364
Benjamin Franzke0c991632011-09-27 21:57:31 +0200365 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100366 data->surf,
367 rectangle->width,
368 rectangle->height);
369
370 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
371 data, egl_window_surface_data_destroy);
372
373 return cairo_surface;
374}
375
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400376#endif
377
378struct wl_buffer *
379display_get_buffer_for_surface(struct display *display,
380 cairo_surface_t *surface)
381{
382 struct surface_data *data;
383
384 data = cairo_surface_get_user_data (surface, &surface_data_key);
385
386 return data->buffer;
387}
388
389struct shm_surface_data {
390 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700391 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392};
393
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500394static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700395shm_pool_destroy(struct shm_pool *pool);
396
397static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398shm_surface_data_destroy(void *p)
399{
400 struct shm_surface_data *data = p;
401
402 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700403 if (data->pool)
404 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405}
406
Kristian Høgsberg16626282012-04-03 11:21:27 -0400407static struct wl_shm_pool *
408make_shm_pool(struct display *display, int size, void **data)
409{
410 char filename[] = "/tmp/wayland-shm-XXXXXX";
411 struct wl_shm_pool *pool;
412 int fd;
413
414 fd = mkstemp(filename);
415 if (fd < 0) {
416 fprintf(stderr, "open %s failed: %m\n", filename);
417 return NULL;
418 }
419 if (ftruncate(fd, size) < 0) {
420 fprintf(stderr, "ftruncate failed: %m\n");
421 close(fd);
422 return NULL;
423 }
424
425 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
426 unlink(filename);
427
428 if (*data == MAP_FAILED) {
429 fprintf(stderr, "mmap failed: %m\n");
430 close(fd);
431 return NULL;
432 }
433
434 pool = wl_shm_create_pool(display->shm, fd, size);
435
436 close(fd);
437
438 return pool;
439}
440
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700441static struct shm_pool *
442shm_pool_create(struct display *display, size_t size)
443{
444 struct shm_pool *pool = malloc(sizeof *pool);
445
446 if (!pool)
447 return NULL;
448
449 pool->pool = make_shm_pool(display, size, &pool->data);
450 if (!pool->pool) {
451 free(pool);
452 return NULL;
453 }
454
455 pool->size = size;
456 pool->used = 0;
457
458 return pool;
459}
460
461static void *
462shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
463{
464 if (pool->used + size > pool->size)
465 return NULL;
466
467 *offset = pool->used;
468 pool->used += size;
469
470 return (char *) pool->data + *offset;
471}
472
473/* destroy the pool. this does not unmap the memory though */
474static void
475shm_pool_destroy(struct shm_pool *pool)
476{
477 munmap(pool->data, pool->size);
478 wl_shm_pool_destroy(pool->pool);
479 free(pool);
480}
481
482/* Start allocating from the beginning of the pool again */
483static void
484shm_pool_reset(struct shm_pool *pool)
485{
486 pool->used = 0;
487}
488
489static int
490data_length_for_shm_surface(struct rectangle *rect)
491{
492 int stride;
493
494 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
495 rect->width);
496 return stride * rect->height;
497}
498
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500499static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700500display_create_shm_surface_from_pool(struct display *display,
501 struct rectangle *rectangle,
502 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400503{
504 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400505 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400506 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700507 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400508 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400509
510 data = malloc(sizeof *data);
511 if (data == NULL)
512 return NULL;
513
514 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
515 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700516 length = stride * rectangle->height;
517 data->pool = NULL;
518 map = shm_pool_allocate(pool, length, &offset);
519
520 if (!map) {
521 free(data);
522 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400523 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400525 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526 CAIRO_FORMAT_ARGB32,
527 rectangle->width,
528 rectangle->height,
529 stride);
530
531 cairo_surface_set_user_data (surface, &surface_data_key,
532 data, shm_surface_data_destroy);
533
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400534 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500535 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400536 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500537 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400538
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700539 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400540 rectangle->width,
541 rectangle->height,
542 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400543
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700544 return surface;
545}
546
547static cairo_surface_t *
548display_create_shm_surface(struct display *display,
549 struct rectangle *rectangle, uint32_t flags,
550 struct window *window)
551{
552 struct shm_surface_data *data;
553 struct shm_pool *pool;
554 cairo_surface_t *surface;
555
556 if (window && window->pool) {
557 shm_pool_reset(window->pool);
558 surface = display_create_shm_surface_from_pool(display,
559 rectangle,
560 flags,
561 window->pool);
562 if (surface)
563 return surface;
564 }
565
566 pool = shm_pool_create(display,
567 data_length_for_shm_surface(rectangle));
568 if (!pool)
569 return NULL;
570
571 surface =
572 display_create_shm_surface_from_pool(display, rectangle,
573 flags, pool);
574
575 if (!surface) {
576 shm_pool_destroy(pool);
577 return NULL;
578 }
579
580 /* make sure we destroy the pool when the surface is destroyed */
581 data = cairo_surface_get_user_data(surface, &surface_data_key);
582 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400583
584 return surface;
585}
586
nobled7b87cb02011-02-01 18:51:47 +0000587static int
588check_size(struct rectangle *rect)
589{
590 if (rect->width && rect->height)
591 return 0;
592
593 fprintf(stderr, "tried to create surface of "
594 "width: %d, height: %d\n", rect->width, rect->height);
595 return -1;
596}
597
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400598cairo_surface_t *
599display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100600 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400601 struct rectangle *rectangle,
602 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400603{
nobled7b87cb02011-02-01 18:51:47 +0000604 if (check_size(rectangle) < 0)
605 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500606#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400607 if (display->dpy)
608 return display_create_egl_window_surface(display,
609 surface,
610 flags,
611 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400612#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400613 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614}
615
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300616struct wl_cursor_image *
617display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400618{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300619 struct wl_cursor *cursor =
620 wl_cursor_theme_get_cursor(display->cursor_theme, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400621
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300622 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400623}
624
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400625static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200626window_get_resize_dx_dy(struct window *window, int *x, int *y)
627{
628 if (window->resize_edges & WINDOW_RESIZING_LEFT)
629 *x = window->server_allocation.width - window->allocation.width;
630 else
631 *x = 0;
632
633 if (window->resize_edges & WINDOW_RESIZING_TOP)
634 *y = window->server_allocation.height -
635 window->allocation.height;
636 else
637 *y = 0;
638
639 window->resize_edges = 0;
640}
641
642static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500643window_attach_surface(struct window *window)
644{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400645 struct display *display = window->display;
646 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000647#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100648 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000649#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500650 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100651
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400652 if (window->type == TYPE_NONE) {
653 window->type = TYPE_TOPLEVEL;
654 if (display->shell)
655 wl_shell_surface_set_toplevel(window->shell_surface);
656 }
657
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100658 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000659#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100660 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
661 data = cairo_surface_get_user_data(window->cairo_surface,
662 &surface_data_key);
663
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100664 cairo_gl_surface_swapbuffers(window->cairo_surface);
665 wl_egl_window_get_attached_size(data->window,
666 &window->server_allocation.width,
667 &window->server_allocation.height);
668 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000669#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100670 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100671 buffer =
672 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400673 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100674
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200675 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100676 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400677 wl_surface_damage(window->surface, 0, 0,
678 window->allocation.width,
679 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100680 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400681 cairo_surface_destroy(window->cairo_surface);
682 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100683 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000684 default:
685 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100686 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500687
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500688 if (window->input_region) {
689 wl_surface_set_input_region(window->surface,
690 window->input_region);
691 wl_region_destroy(window->input_region);
692 window->input_region = NULL;
693 }
694
695 if (window->opaque_region) {
696 wl_surface_set_opaque_region(window->surface,
697 window->opaque_region);
698 wl_region_destroy(window->opaque_region);
699 window->opaque_region = NULL;
700 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500701}
702
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500703void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400704window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500705{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400706 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100707 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500708}
709
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400710void
711window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400712{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500713 cairo_surface_reference(surface);
714
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400715 if (window->cairo_surface != NULL)
716 cairo_surface_destroy(window->cairo_surface);
717
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500718 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400719}
720
Benjamin Franzke22d54812011-07-16 19:50:32 +0000721#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722static void
723window_resize_cairo_window_surface(struct window *window)
724{
725 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200726 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727
728 data = cairo_surface_get_user_data(window->cairo_surface,
729 &surface_data_key);
730
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200731 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 wl_egl_window_resize(data->window,
733 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200734 window->allocation.height,
735 x,y);
736
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100737 cairo_gl_surface_set_size(window->cairo_surface,
738 window->allocation.width,
739 window->allocation.height);
740}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000741#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100742
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400743struct display *
744window_get_display(struct window *window)
745{
746 return window->display;
747}
748
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400749void
750window_create_surface(struct window *window)
751{
752 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400753 uint32_t flags = 0;
754
755 if (!window->transparent)
756 flags = SURFACE_OPAQUE;
757
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400758 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500759#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100760 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
761 if (window->cairo_surface) {
762 window_resize_cairo_window_surface(window);
763 return;
764 }
765 surface = display_create_surface(window->display,
766 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400767 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100768 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400769#endif
770 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400771 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400772 &window->allocation,
773 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400774 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800775 default:
776 surface = NULL;
777 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400778 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400779
780 window_set_surface(window, surface);
781 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400782}
783
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200784static void frame_destroy(struct frame *frame);
785
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400786void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500787window_destroy(struct window *window)
788{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200789 struct display *display = window->display;
790 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100791 struct window_output *window_output;
792 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200793
794 if (window->redraw_scheduled)
795 wl_list_remove(&window->redraw_task.link);
796
797 wl_list_for_each(input, &display->input_list, link) {
798 if (input->pointer_focus == window)
799 input->pointer_focus = NULL;
800 if (input->keyboard_focus == window)
801 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500802 if (input->focus_widget &&
803 input->focus_widget->window == window)
804 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200805 }
806
Rob Bradford7507b572012-05-15 17:55:34 +0100807 wl_list_for_each_safe(window_output, window_output_tmp,
808 &window->window_output_list, link) {
809 free (window_output);
810 }
811
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500812 if (window->input_region)
813 wl_region_destroy(window->input_region);
814 if (window->opaque_region)
815 wl_region_destroy(window->opaque_region);
816
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200817 if (window->frame)
818 frame_destroy(window->frame);
819
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200820 if (window->shell_surface)
821 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500822 wl_surface_destroy(window->surface);
823 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200824
825 if (window->cairo_surface != NULL)
826 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200827
828 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500829 free(window);
830}
831
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500832static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500833widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400834{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500835 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400836
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500837 wl_list_for_each(child, &widget->child_list, link) {
838 target = widget_find_widget(child, x, y);
839 if (target)
840 return target;
841 }
842
843 if (widget->allocation.x <= x &&
844 x < widget->allocation.x + widget->allocation.width &&
845 widget->allocation.y <= y &&
846 y < widget->allocation.y + widget->allocation.height) {
847 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400848 }
849
850 return NULL;
851}
852
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500853static struct widget *
854widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400855{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500856 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400857
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500858 widget = malloc(sizeof *widget);
859 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500860 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500861 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500862 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500863 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500864 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500865
866 return widget;
867}
868
869struct widget *
870window_add_widget(struct window *window, void *data)
871{
872 window->widget = widget_create(window, data);
873 wl_list_init(&window->widget->link);
874
875 return window->widget;
876}
877
878struct widget *
879widget_add_widget(struct widget *parent, void *data)
880{
881 struct widget *widget;
882
883 widget = widget_create(parent->window, data);
884 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400885
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500886 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400887}
888
889void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500890widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400891{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200892 struct display *display = widget->window->display;
893 struct input *input;
894
895 wl_list_for_each(input, &display->input_list, link) {
896 if (input->focus_widget == widget)
897 input->focus_widget = NULL;
898 }
899
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500900 wl_list_remove(&widget->link);
901 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400902}
903
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400904void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500905widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400906{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500907 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400908}
909
910void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500911widget_set_size(struct widget *widget, int32_t width, int32_t height)
912{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500913 widget->allocation.width = width;
914 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500915}
916
917void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500918widget_set_allocation(struct widget *widget,
919 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400920{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500921 widget->allocation.x = x;
922 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200923 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400924}
925
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500926void
927widget_set_transparent(struct widget *widget, int transparent)
928{
929 widget->opaque = !transparent;
930}
931
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400932void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500933widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400934{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500935 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400936}
937
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500938void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500939widget_set_resize_handler(struct widget *widget,
940 widget_resize_handler_t handler)
941{
942 widget->resize_handler = handler;
943}
944
945void
946widget_set_redraw_handler(struct widget *widget,
947 widget_redraw_handler_t handler)
948{
949 widget->redraw_handler = handler;
950}
951
952void
Kristian Høgsbergee143232012-01-09 08:42:24 -0500953widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500954{
Kristian Høgsbergee143232012-01-09 08:42:24 -0500955 widget->enter_handler = handler;
956}
957
958void
959widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
960{
961 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500962}
963
964void
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500965widget_set_motion_handler(struct widget *widget,
966 widget_motion_handler_t handler)
967{
968 widget->motion_handler = handler;
969}
970
971void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500972widget_set_button_handler(struct widget *widget,
973 widget_button_handler_t handler)
974{
975 widget->button_handler = handler;
976}
977
978void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500979widget_schedule_redraw(struct widget *widget)
980{
981 window_schedule_redraw(widget->window);
982}
983
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400984cairo_surface_t *
985window_get_surface(struct window *window)
986{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400987 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400988}
989
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100990struct wl_surface *
991window_get_wl_surface(struct window *window)
992{
993 return window->surface;
994}
995
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200996struct wl_shell_surface *
997window_get_wl_shell_surface(struct window *window)
998{
999 return window->shell_surface;
1000}
1001
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001002static void
1003frame_resize_handler(struct widget *widget,
1004 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001005{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001006 struct frame *frame = data;
1007 struct widget *child = frame->child;
1008 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001009 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001010 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001011 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001012 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001013 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001014 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001015
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001016 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001017 decoration_width = (t->width + t->margin) * 2;
1018 decoration_height = t->width +
1019 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001020
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001021 allocation.x = t->width + t->margin;
1022 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001023 allocation.width = width - decoration_width;
1024 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001025
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001026 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001027
1028 wl_list_for_each(button, &frame->buttons_list, link)
1029 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001030 } else {
1031 decoration_width = 0;
1032 decoration_height = 0;
1033
1034 allocation.x = 0;
1035 allocation.y = 0;
1036 allocation.width = width;
1037 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001038 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001039
1040 wl_list_for_each(button, &frame->buttons_list, link)
1041 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001042 }
1043
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001044 widget_set_allocation(child, allocation.x, allocation.y,
1045 allocation.width, allocation.height);
1046
1047 if (child->resize_handler)
1048 child->resize_handler(child,
1049 allocation.width,
1050 allocation.height,
1051 child->user_data);
1052
Scott Moreauf7e498c2012-05-14 11:39:29 -06001053 width = child->allocation.width + decoration_width;
1054 height = child->allocation.height + decoration_height;
1055
1056 widget->window->input_region =
1057 wl_compositor_create_region(display->compositor);
1058 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001059 t->margin, t->margin,
1060 width - 2 * t->margin,
1061 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001062
1063 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001064
1065 if (child->opaque) {
1066 widget->window->opaque_region =
1067 wl_compositor_create_region(display->compositor);
1068 wl_region_add(widget->window->opaque_region,
1069 opaque_margin, opaque_margin,
1070 widget->allocation.width - 2 * opaque_margin,
1071 widget->allocation.height - 2 * opaque_margin);
1072 }
Martin Minarik1998b152012-05-10 02:04:35 +02001073
1074 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001075 x_r = frame->widget->allocation.width - t->width - t->margin;
1076 x_l = t->width + t->margin;
1077 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001078 wl_list_for_each(button, &frame->buttons_list, link) {
1079 const int button_padding = 4;
1080 w = cairo_image_surface_get_width(button->icon);
1081 h = cairo_image_surface_get_height(button->icon);
1082
1083 if (button->decoration == FRAME_BUTTON_FANCY)
1084 w += 10;
1085
1086 if (button->align == FRAME_BUTTON_LEFT) {
1087 widget_set_allocation(button->widget,
1088 x_l, y , w + 1, h + 1);
1089 x_l += w;
1090 x_l += button_padding;
1091 } else {
1092 x_r -= w;
1093 widget_set_allocation(button->widget,
1094 x_r, y , w + 1, h + 1);
1095 x_r -= button_padding;
1096 }
1097 }
1098}
1099
1100static int
1101frame_button_enter_handler(struct widget *widget,
1102 struct input *input, float x, float y, void *data)
1103{
1104 struct frame_button *frame_button = data;
1105
1106 widget_schedule_redraw(frame_button->widget);
1107 frame_button->state = FRAME_BUTTON_OVER;
1108
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001109 return WL_CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001110}
1111
1112static void
1113frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1114{
1115 struct frame_button *frame_button = data;
1116
1117 widget_schedule_redraw(frame_button->widget);
1118 frame_button->state = FRAME_BUTTON_DEFAULT;
1119}
1120
1121static void
1122frame_button_button_handler(struct widget *widget,
1123 struct input *input, uint32_t time,
1124 uint32_t button, uint32_t state, void *data)
1125{
1126 struct frame_button *frame_button = data;
1127 struct window *window = widget->window;
1128
1129 if (button != BTN_LEFT)
1130 return;
1131
1132 switch (state) {
1133 case 1:
1134 frame_button->state = FRAME_BUTTON_ACTIVE;
1135 widget_schedule_redraw(frame_button->widget);
1136
1137 if (frame_button->type == FRAME_BUTTON_ICON)
1138 window_show_frame_menu(window, input, time);
1139 return;
1140 case 0:
1141 frame_button->state = FRAME_BUTTON_DEFAULT;
1142 widget_schedule_redraw(frame_button->widget);
1143 break;
1144 }
1145
1146 switch (frame_button->type) {
1147 case FRAME_BUTTON_CLOSE:
1148 if (window->close_handler)
1149 window->close_handler(window->parent,
1150 window->user_data);
1151 else
1152 display_exit(window->display);
1153 break;
1154 case FRAME_BUTTON_MINIMIZE:
1155 fprintf(stderr,"Minimize stub\n");
1156 break;
1157 case FRAME_BUTTON_MAXIMIZE:
1158 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1159 break;
1160 default:
1161 /* Unknown operation */
1162 break;
1163 }
1164}
1165
1166static void
1167frame_button_redraw_handler(struct widget *widget, void *data)
1168{
1169 struct frame_button *frame_button = data;
1170 cairo_t *cr;
1171 int width, height, x, y;
1172 struct window *window = widget->window;
1173
1174 x = widget->allocation.x;
1175 y = widget->allocation.y;
1176 width = widget->allocation.width;
1177 height = widget->allocation.height;
1178
1179 if (!width)
1180 return;
1181 if (!height)
1182 return;
1183 if (widget->opaque)
1184 return;
1185
1186 cr = cairo_create(window->cairo_surface);
1187
1188 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1189 cairo_set_line_width(cr, 1);
1190
1191 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1192 cairo_rectangle (cr, x, y, 25, 16);
1193
1194 cairo_stroke_preserve(cr);
1195
1196 switch (frame_button->state) {
1197 case FRAME_BUTTON_DEFAULT:
1198 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1199 break;
1200 case FRAME_BUTTON_OVER:
1201 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1202 break;
1203 case FRAME_BUTTON_ACTIVE:
1204 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1205 break;
1206 }
1207
1208 cairo_fill (cr);
1209
1210 x += 4;
1211 }
1212
1213 cairo_set_source_surface(cr, frame_button->icon, x, y);
1214 cairo_paint(cr);
1215
1216 cairo_destroy(cr);
1217}
1218
1219static struct widget *
1220frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1221 enum frame_button_align align, enum frame_button_decoration style)
1222{
1223 struct frame_button *frame_button;
1224 const char *icon = data;
1225
1226 frame_button = malloc (sizeof *frame_button);
1227 memset(frame_button, 0, sizeof *frame_button);
1228
1229 frame_button->icon = cairo_image_surface_create_from_png(icon);
1230 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1231 frame_button->frame = frame;
1232 frame_button->type = type;
1233 frame_button->align = align;
1234 frame_button->decoration = style;
1235
1236 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1237
1238 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1239 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1240 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1241 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1242 return frame_button->widget;
1243}
1244
1245static void
1246frame_button_destroy(struct frame_button *frame_button)
1247{
1248 widget_destroy(frame_button->widget);
1249 wl_list_remove(&frame_button->link);
1250 cairo_surface_destroy(frame_button->icon);
1251 free(frame_button);
1252
1253 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001254}
1255
1256static void
1257frame_redraw_handler(struct widget *widget, void *data)
1258{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001259 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001260 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001261 struct theme *t = window->display->theme;
1262 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001263
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001264 if (window->type == TYPE_FULLSCREEN)
1265 return;
1266
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001267 cr = cairo_create(window->cairo_surface);
1268
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001269 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001270 flags |= THEME_FRAME_ACTIVE;
1271 theme_render_frame(t, cr, widget->allocation.width,
1272 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001273
1274 cairo_destroy(cr);
1275}
1276
1277static int
1278frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1279{
1280 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001281 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001282 const int grip_size = 8;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001283 struct theme *t = widget->window->display->theme;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001284
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001285 if (x < t->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001286 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001287 else if (t->margin <= x && x < t->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001288 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001289 else if (x < widget->allocation.width - t->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001290 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001291 else if (x < widget->allocation.width - t->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001292 hlocation = WINDOW_RESIZING_RIGHT;
1293 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001294 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001295
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001296 if (y < t->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001297 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001298 else if (t->margin <= y && y < t->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001299 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001300 else if (y < widget->allocation.height - t->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001301 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001302 else if (y < widget->allocation.height - t->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001303 vlocation = WINDOW_RESIZING_BOTTOM;
1304 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001305 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001306
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001307 location = vlocation | hlocation;
1308 if (location & WINDOW_EXTERIOR)
1309 location = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001310 if (location == WINDOW_INTERIOR && y < t->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001311 location = WINDOW_TITLEBAR;
1312 else if (location == WINDOW_INTERIOR)
1313 location = WINDOW_CLIENT_AREA;
1314
1315 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001316}
1317
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001318static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001319frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001320{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001321 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001322
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001323 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001324 switch (location) {
1325 case WINDOW_RESIZING_TOP:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001326 return WL_CURSOR_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001327 case WINDOW_RESIZING_BOTTOM:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001328 return WL_CURSOR_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001329 case WINDOW_RESIZING_LEFT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001330 return WL_CURSOR_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001331 case WINDOW_RESIZING_RIGHT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001332 return WL_CURSOR_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001333 case WINDOW_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001334 return WL_CURSOR_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001335 case WINDOW_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001336 return WL_CURSOR_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001337 case WINDOW_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001338 return WL_CURSOR_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001339 case WINDOW_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001340 return WL_CURSOR_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001341 case WINDOW_EXTERIOR:
1342 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001343 default:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001344 return WL_CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001345 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001346}
1347
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001348static void
1349frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001350{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001351 switch (index) {
1352 case 0: /* close */
1353 if (window->close_handler)
1354 window->close_handler(window->parent,
1355 window->user_data);
1356 else
1357 display_exit(window->display);
1358 break;
1359 case 1: /* fullscreen */
1360 /* we don't have a way to get out of fullscreen for now */
1361 window_set_fullscreen(window, 1);
1362 break;
1363 case 2: /* rotate */
1364 case 3: /* scale */
1365 break;
1366 }
1367}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001368
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001369void
1370window_show_frame_menu(struct window *window,
1371 struct input *input, uint32_t time)
1372{
1373 int32_t x, y;
1374
1375 static const char *entries[] = {
1376 "Close", "Fullscreen", "Rotate", "Scale"
1377 };
1378
1379 input_get_position(input, &x, &y);
1380 window_show_menu(window->display, input, time, window,
1381 x - 10, y - 10, frame_menu_func, entries, 4);
1382}
1383
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001384static int
1385frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001386 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001387{
1388 return frame_get_pointer_image_for_location(data, input);
1389}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001390
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001391static int
1392frame_motion_handler(struct widget *widget,
1393 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001394 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001395{
1396 return frame_get_pointer_image_for_location(data, input);
1397}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001398
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001399static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001400frame_button_handler(struct widget *widget,
1401 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001402 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001403
1404{
1405 struct frame *frame = data;
1406 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001407 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001408 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001409
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001410 location = frame_get_pointer_location(frame, input->sx, input->sy);
1411
1412 if (window->display->shell && button == BTN_LEFT && state == 1) {
1413 switch (location) {
1414 case WINDOW_TITLEBAR:
1415 if (!window->shell_surface)
1416 break;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001417 input_set_pointer_image(input, time, WL_CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001418 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001419 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001420 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001421 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001422 break;
1423 case WINDOW_RESIZING_TOP:
1424 case WINDOW_RESIZING_BOTTOM:
1425 case WINDOW_RESIZING_LEFT:
1426 case WINDOW_RESIZING_RIGHT:
1427 case WINDOW_RESIZING_TOP_LEFT:
1428 case WINDOW_RESIZING_TOP_RIGHT:
1429 case WINDOW_RESIZING_BOTTOM_LEFT:
1430 case WINDOW_RESIZING_BOTTOM_RIGHT:
1431 if (!window->shell_surface)
1432 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001433 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001434
1435 if (!display->dpy) {
1436 /* If we're using shm, allocate a big
1437 pool to create buffers out of while
1438 we resize. We should probably base
1439 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001440 window->pool =
1441 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001442 }
1443
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001444 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001445 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001446 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001447 break;
1448 }
1449 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001450 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001451 }
1452}
1453
1454struct widget *
1455frame_create(struct window *window, void *data)
1456{
1457 struct frame *frame;
1458
1459 frame = malloc(sizeof *frame);
1460 memset(frame, 0, sizeof *frame);
1461
1462 frame->widget = window_add_widget(window, frame);
1463 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001464
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001465 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1466 widget_set_resize_handler(frame->widget, frame_resize_handler);
1467 widget_set_enter_handler(frame->widget, frame_enter_handler);
1468 widget_set_motion_handler(frame->widget, frame_motion_handler);
1469 widget_set_button_handler(frame->widget, frame_button_handler);
1470
Martin Minarik1998b152012-05-10 02:04:35 +02001471 /* Create empty list for frame buttons */
1472 wl_list_init(&frame->buttons_list);
1473
1474 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1475 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1476
1477 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1478 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1479
1480 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1481 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1482
1483 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1484 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1485
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001486 window->frame = frame;
1487
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001488 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001489}
1490
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001491static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001492frame_destroy(struct frame *frame)
1493{
Martin Minarik1998b152012-05-10 02:04:35 +02001494 struct frame_button *button, *tmp;
1495
1496 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1497 frame_button_destroy(button);
1498
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001499 /* frame->child must be destroyed by the application */
1500 widget_destroy(frame->widget);
1501 free(frame);
1502}
1503
1504static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001505input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001506 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001507{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001508 struct widget *old, *widget;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001509 int pointer = WL_CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001510
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001511 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001512 return;
1513
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001514 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001515 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001516 widget = old;
1517 if (input->grab)
1518 widget = input->grab;
1519 if (widget->leave_handler)
1520 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001521 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001522 }
1523
1524 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001525 widget = focus;
1526 if (input->grab)
1527 widget = input->grab;
1528 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001529 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001530 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001531 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001532
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001533 input_set_pointer_image(input, input->pointer_enter_serial,
1534 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001535 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001536}
1537
1538static void
Daniel Stone37816df2012-05-16 18:45:18 +01001539pointer_handle_motion(void *data, struct wl_pointer *pointer,
1540 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001541{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001542 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001543 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001544 struct widget *widget;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001545 int cursor = WL_CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001546 float sx = wl_fixed_to_double(sx_w);
1547 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001548
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001549 input->sx = sx;
1550 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001551
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001552 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001553 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001554 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001555 }
1556
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001557 if (input->grab)
1558 widget = input->grab;
1559 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001560 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001561 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001562 cursor = widget->motion_handler(input->focus_widget,
1563 input, time, sx, sy,
1564 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001565
Daniel Stone37816df2012-05-16 18:45:18 +01001566 input_set_pointer_image(input, time, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001567}
1568
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001569void
1570input_grab(struct input *input, struct widget *widget, uint32_t button)
1571{
1572 input->grab = widget;
1573 input->grab_button = button;
1574}
1575
1576void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001577input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001578{
1579 struct widget *widget;
1580
1581 input->grab = NULL;
1582 if (input->pointer_focus) {
1583 widget = widget_find_widget(input->pointer_focus->widget,
1584 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001585 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001586 }
1587}
1588
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001589static void
Daniel Stone37816df2012-05-16 18:45:18 +01001590pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1591 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001592{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001593 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001594 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001595
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001596 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001597 if (input->focus_widget && input->grab == NULL && state)
1598 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001599
Neil Roberts6b28aad2012-01-23 19:11:18 +00001600 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001601 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001602 (*widget->button_handler)(widget,
1603 input, time,
1604 button, state,
1605 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001606
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001607 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001608 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001609}
1610
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001611static void
Daniel Stone37816df2012-05-16 18:45:18 +01001612pointer_handle_axis(void *data, struct wl_pointer *pointer,
Scott Moreau210d0792012-03-22 10:47:01 -06001613 uint32_t time, uint32_t axis, int32_t value)
1614{
1615}
1616
1617static void
Daniel Stone37816df2012-05-16 18:45:18 +01001618keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1619 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001620{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001621 struct input *input = data;
1622 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001623 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001624 uint32_t code, num_syms;
1625 const xkb_keysym_t *syms;
1626 xkb_keysym_t sym;
1627 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001628
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001629 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001630 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001631 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001632 return;
1633
Kristian Høgsberg70163132012-05-08 15:55:39 -04001634 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1635 xkb_state_update_key(d->xkb.state, code,
1636 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001637
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001638 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001639 XKB_STATE_DEPRESSED |
1640 XKB_STATE_LATCHED);
1641 input->modifiers = 0;
1642 if (mask & input->display->xkb.control_mask)
1643 input->modifiers |= MOD_CONTROL_MASK;
1644 if (mask & input->display->xkb.alt_mask)
1645 input->modifiers |= MOD_ALT_MASK;
1646 if (mask & input->display->xkb.shift_mask)
1647 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001648
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001649 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001650 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001651 if (state)
1652 window_set_maximized(window,
1653 window->type != TYPE_MAXIMIZED);
1654 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001655 if (num_syms == 1)
1656 sym = syms[0];
1657 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001658 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001659
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001660 (*window->key_handler)(window, input, time, key,
1661 sym, state, window->user_data);
1662 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001663}
1664
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001665static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001666input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001667{
1668 struct window *window = input->pointer_focus;
1669
1670 if (!window)
1671 return;
1672
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001673 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001674
Pekka Paalanene1207c72011-12-16 12:02:09 +02001675 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001676 input->current_cursor = WL_CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001677}
1678
1679static void
Daniel Stone37816df2012-05-16 18:45:18 +01001680pointer_handle_enter(void *data, struct wl_pointer *pointer,
1681 uint32_t serial, struct wl_surface *surface,
1682 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001683{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001684 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001685 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001686 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001687 float sx = wl_fixed_to_double(sx_w);
1688 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001689
Martin Minarik1998b152012-05-10 02:04:35 +02001690 if (!surface) {
1691 /* enter event for a window we've just destroyed */
1692 return;
1693 }
1694
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001695 input->display->serial = serial;
1696 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001697 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001698 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001699
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001700 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001701 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001702 window->pool = NULL;
1703 /* Schedule a redraw to free the pool */
1704 window_schedule_redraw(window);
1705 }
1706
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001707 input->sx = sx;
1708 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001709
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001710 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001711 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001712}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001713
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001714static void
Daniel Stone37816df2012-05-16 18:45:18 +01001715pointer_handle_leave(void *data, struct wl_pointer *pointer,
1716 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001717{
1718 struct input *input = data;
1719
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001720 input->display->serial = serial;
1721 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001722}
1723
1724static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001725input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001726{
1727 struct window *window = input->keyboard_focus;
1728
1729 if (!window)
1730 return;
1731
1732 window->keyboard_device = NULL;
1733 if (window->keyboard_focus_handler)
1734 (*window->keyboard_focus_handler)(window, NULL,
1735 window->user_data);
1736
1737 input->keyboard_focus = NULL;
1738}
1739
1740static void
Daniel Stone37816df2012-05-16 18:45:18 +01001741keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1742 uint32_t serial, struct wl_surface *surface,
1743 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001744{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001745 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001746 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001747
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001748 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001749 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001750
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001751 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001752 window->keyboard_device = input;
1753 if (window->keyboard_focus_handler)
1754 (*window->keyboard_focus_handler)(window,
1755 window->keyboard_device,
1756 window->user_data);
1757}
1758
1759static void
Daniel Stone37816df2012-05-16 18:45:18 +01001760keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1761 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001762{
1763 struct input *input = data;
1764
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001765 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001766 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001767}
1768
Daniel Stone37816df2012-05-16 18:45:18 +01001769static const struct wl_pointer_listener pointer_listener = {
1770 pointer_handle_enter,
1771 pointer_handle_leave,
1772 pointer_handle_motion,
1773 pointer_handle_button,
1774 pointer_handle_axis,
1775};
1776
1777static const struct wl_keyboard_listener keyboard_listener = {
1778 keyboard_handle_enter,
1779 keyboard_handle_leave,
1780 keyboard_handle_key,
1781};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001782
1783static void
Daniel Stone37816df2012-05-16 18:45:18 +01001784seat_handle_capabilities(void *data, struct wl_seat *seat,
1785 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001786{
Daniel Stone37816df2012-05-16 18:45:18 +01001787 struct input *input = data;
1788
1789 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
1790 input->pointer = wl_seat_get_pointer(seat);
1791 wl_pointer_set_user_data(input->pointer, input);
1792 wl_pointer_add_listener(input->pointer, &pointer_listener,
1793 input);
1794 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
1795 wl_pointer_destroy(input->pointer);
1796 input->pointer = NULL;
1797 }
1798
1799 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
1800 input->keyboard = wl_seat_get_keyboard(seat);
1801 wl_keyboard_set_user_data(input->keyboard, input);
1802 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
1803 input);
1804 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
1805 wl_keyboard_destroy(input->keyboard);
1806 input->keyboard = NULL;
1807 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001808}
1809
Daniel Stone37816df2012-05-16 18:45:18 +01001810static const struct wl_seat_listener seat_listener = {
1811 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001812};
1813
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001814void
1815input_get_position(struct input *input, int32_t *x, int32_t *y)
1816{
1817 *x = input->sx;
1818 *y = input->sy;
1819}
1820
Daniel Stone37816df2012-05-16 18:45:18 +01001821struct wl_seat *
1822input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001823{
Daniel Stone37816df2012-05-16 18:45:18 +01001824 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001825}
1826
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001827uint32_t
1828input_get_modifiers(struct input *input)
1829{
1830 return input->modifiers;
1831}
1832
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001833struct widget *
1834input_get_focus_widget(struct input *input)
1835{
1836 return input->focus_widget;
1837}
1838
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001839struct data_offer {
1840 struct wl_data_offer *offer;
1841 struct input *input;
1842 struct wl_array types;
1843 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001844
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001845 struct task io_task;
1846 int fd;
1847 data_func_t func;
1848 int32_t x, y;
1849 void *user_data;
1850};
1851
1852static void
1853data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1854{
1855 struct data_offer *offer = data;
1856 char **p;
1857
1858 p = wl_array_add(&offer->types, sizeof *p);
1859 *p = strdup(type);
1860}
1861
1862static const struct wl_data_offer_listener data_offer_listener = {
1863 data_offer_offer,
1864};
1865
1866static void
1867data_offer_destroy(struct data_offer *offer)
1868{
1869 char **p;
1870
1871 offer->refcount--;
1872 if (offer->refcount == 0) {
1873 wl_data_offer_destroy(offer->offer);
1874 for (p = offer->types.data; *p; p++)
1875 free(*p);
1876 wl_array_release(&offer->types);
1877 free(offer);
1878 }
1879}
1880
1881static void
1882data_device_data_offer(void *data,
1883 struct wl_data_device *data_device, uint32_t id)
1884{
1885 struct data_offer *offer;
1886
1887 offer = malloc(sizeof *offer);
1888
1889 wl_array_init(&offer->types);
1890 offer->refcount = 1;
1891 offer->input = data;
1892
1893 /* FIXME: Generate typesafe wrappers for this */
1894 offer->offer = (struct wl_data_offer *)
1895 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1896 id, &wl_data_offer_interface);
1897
1898 wl_data_offer_add_listener(offer->offer,
1899 &data_offer_listener, offer);
1900}
1901
1902static void
1903data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001904 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001905 wl_fixed_t x_w, wl_fixed_t y_w,
1906 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001907{
1908 struct input *input = data;
1909 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001910 float x = wl_fixed_to_double(x_w);
1911 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001912 char **p;
1913
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001914 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001915 input->drag_offer = wl_data_offer_get_user_data(offer);
1916 window = wl_surface_get_user_data(surface);
1917 input->pointer_focus = window;
1918
1919 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1920 *p = NULL;
1921
1922 window = input->pointer_focus;
1923 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001924 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001925 input->drag_offer->types.data,
1926 window->user_data);
1927}
1928
1929static void
1930data_device_leave(void *data, struct wl_data_device *data_device)
1931{
1932 struct input *input = data;
1933
1934 data_offer_destroy(input->drag_offer);
1935 input->drag_offer = NULL;
1936}
1937
1938static void
1939data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001940 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001941{
1942 struct input *input = data;
1943 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001944 float x = wl_fixed_to_double(x_w);
1945 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001946
1947 input->sx = x;
1948 input->sy = y;
1949
1950 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001951 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001952 input->drag_offer->types.data,
1953 window->user_data);
1954}
1955
1956static void
1957data_device_drop(void *data, struct wl_data_device *data_device)
1958{
1959 struct input *input = data;
1960 struct window *window = input->pointer_focus;
1961
1962 if (window->drop_handler)
1963 window->drop_handler(window, input,
1964 input->sx, input->sy, window->user_data);
1965}
1966
1967static void
1968data_device_selection(void *data,
1969 struct wl_data_device *wl_data_device,
1970 struct wl_data_offer *offer)
1971{
1972 struct input *input = data;
1973 char **p;
1974
1975 if (input->selection_offer)
1976 data_offer_destroy(input->selection_offer);
1977
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001978 if (offer) {
1979 input->selection_offer = wl_data_offer_get_user_data(offer);
1980 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1981 *p = NULL;
1982 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001983}
1984
1985static const struct wl_data_device_listener data_device_listener = {
1986 data_device_data_offer,
1987 data_device_enter,
1988 data_device_leave,
1989 data_device_motion,
1990 data_device_drop,
1991 data_device_selection
1992};
1993
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001994void
1995input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1996{
1997 struct display *display = input->display;
1998 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001999 struct wl_cursor *cursor;
2000 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002001
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002002 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002003 return;
2004
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002005 cursor = wl_cursor_theme_get_cursor(display->cursor_theme, pointer);
2006 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002007 return;
2008
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002009 image = cursor->images[0];
2010 buffer = wl_cursor_image_get_buffer(image);
2011 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002012 return;
2013
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002014 input->current_cursor = pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002015 wl_pointer_attach(input->pointer, time, buffer,
2016 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002017}
2018
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002019struct wl_data_device *
2020input_get_data_device(struct input *input)
2021{
2022 return input->data_device;
2023}
2024
2025void
2026input_set_selection(struct input *input,
2027 struct wl_data_source *source, uint32_t time)
2028{
2029 wl_data_device_set_selection(input->data_device, source, time);
2030}
2031
2032void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002033input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002034{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002035 wl_data_offer_accept(input->drag_offer->offer,
2036 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002037}
2038
2039static void
2040offer_io_func(struct task *task, uint32_t events)
2041{
2042 struct data_offer *offer =
2043 container_of(task, struct data_offer, io_task);
2044 unsigned int len;
2045 char buffer[4096];
2046
2047 len = read(offer->fd, buffer, sizeof buffer);
2048 offer->func(buffer, len,
2049 offer->x, offer->y, offer->user_data);
2050
2051 if (len == 0) {
2052 close(offer->fd);
2053 data_offer_destroy(offer);
2054 }
2055}
2056
2057static void
2058data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2059 data_func_t func, void *user_data)
2060{
2061 int p[2];
2062
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002063 if (pipe2(p, O_CLOEXEC) == -1)
2064 return;
2065
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002066 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2067 close(p[1]);
2068
2069 offer->io_task.run = offer_io_func;
2070 offer->fd = p[0];
2071 offer->func = func;
2072 offer->refcount++;
2073 offer->user_data = user_data;
2074
2075 display_watch_fd(offer->input->display,
2076 offer->fd, EPOLLIN, &offer->io_task);
2077}
2078
2079void
2080input_receive_drag_data(struct input *input, const char *mime_type,
2081 data_func_t func, void *data)
2082{
2083 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2084 input->drag_offer->x = input->sx;
2085 input->drag_offer->y = input->sy;
2086}
2087
2088int
2089input_receive_selection_data(struct input *input, const char *mime_type,
2090 data_func_t func, void *data)
2091{
2092 char **p;
2093
2094 if (input->selection_offer == NULL)
2095 return -1;
2096
2097 for (p = input->selection_offer->types.data; *p; p++)
2098 if (strcmp(mime_type, *p) == 0)
2099 break;
2100
2101 if (*p == NULL)
2102 return -1;
2103
2104 data_offer_receive_data(input->selection_offer,
2105 mime_type, func, data);
2106 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002107}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002108
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002109int
2110input_receive_selection_data_to_fd(struct input *input,
2111 const char *mime_type, int fd)
2112{
2113 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2114
2115 return 0;
2116}
2117
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002118void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002119window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002120{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002121 if (!window->shell_surface)
2122 return;
2123
Daniel Stone37816df2012-05-16 18:45:18 +01002124 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002125}
2126
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002127static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002128idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002129{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002130 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002131
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002132 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002133 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002134 widget_set_allocation(widget,
2135 window->pending_allocation.x,
2136 window->pending_allocation.y,
2137 window->pending_allocation.width,
2138 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002139
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002140 if (window->input_region) {
2141 wl_region_destroy(window->input_region);
2142 window->input_region = NULL;
2143 }
2144
2145 if (window->opaque_region) {
2146 wl_region_destroy(window->opaque_region);
2147 window->opaque_region = NULL;
2148 }
2149
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002150 if (widget->resize_handler)
2151 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002152 widget->allocation.width,
2153 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002154 widget->user_data);
2155
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002156 if (window->allocation.width != widget->allocation.width ||
2157 window->allocation.height != widget->allocation.height) {
2158 window->allocation = widget->allocation;
2159 window_schedule_redraw(window);
2160 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002161}
2162
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002163void
2164window_schedule_resize(struct window *window, int width, int height)
2165{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002166 window->pending_allocation.x = 0;
2167 window->pending_allocation.y = 0;
2168 window->pending_allocation.width = width;
2169 window->pending_allocation.height = height;
2170
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002171 window->resize_needed = 1;
2172 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002173}
2174
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002175void
2176widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2177{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002178 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002179}
2180
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002181static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002182handle_ping(void *data, struct wl_shell_surface *shell_surface,
2183 uint32_t serial)
2184{
2185 wl_shell_surface_pong(shell_surface, serial);
2186}
2187
2188static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002189handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002190 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002191{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002192 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002193
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002194 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002195 return;
2196
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002197 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002198 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002199}
2200
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002201static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002202menu_destroy(struct menu *menu)
2203{
2204 widget_destroy(menu->widget);
2205 window_destroy(menu->window);
2206 free(menu);
2207}
2208
2209static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002210handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2211{
2212 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002213 struct menu *menu = window->widget->user_data;
2214
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002215 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002216 * device. Or just use wl_callback. And this really needs to
2217 * be a window vfunc that the menu can set. And we need the
2218 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002219
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002220 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002221 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002222 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002223}
2224
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002225static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002226 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002227 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002228 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002229};
2230
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002231void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002232window_get_allocation(struct window *window,
2233 struct rectangle *allocation)
2234{
2235 *allocation = window->allocation;
2236}
2237
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002238static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002239widget_redraw(struct widget *widget)
2240{
2241 struct widget *child;
2242
2243 if (widget->redraw_handler)
2244 widget->redraw_handler(widget, widget->user_data);
2245 wl_list_for_each(child, &widget->child_list, link)
2246 widget_redraw(child);
2247}
2248
2249static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002250frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2251{
2252 struct window *window = data;
2253
2254 wl_callback_destroy(callback);
2255 window->redraw_scheduled = 0;
2256 if (window->redraw_needed)
2257 window_schedule_redraw(window);
2258}
2259
2260static const struct wl_callback_listener listener = {
2261 frame_callback
2262};
2263
2264static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002265idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002266{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002267 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002268 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002269
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002270 if (window->resize_needed)
2271 idle_resize(window);
2272
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002273 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002274 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002275 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002276 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002277 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002278
2279 callback = wl_surface_frame(window->surface);
2280 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002281}
2282
2283void
2284window_schedule_redraw(struct window *window)
2285{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002286 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002287 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002288 window->redraw_task.run = idle_redraw;
2289 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002290 window->redraw_scheduled = 1;
2291 }
2292}
2293
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002294void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002295window_set_custom(struct window *window)
2296{
2297 window->type = TYPE_CUSTOM;
2298}
2299
2300void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002301window_set_fullscreen(struct window *window, int fullscreen)
2302{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002303 if (!window->display->shell)
2304 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002305
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002306 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002307 return;
2308
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002309 if (fullscreen) {
2310 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002311 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002312 wl_shell_surface_set_fullscreen(window->shell_surface,
2313 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2314 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002315 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002316 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002317 wl_shell_surface_set_toplevel(window->shell_surface);
2318 window_schedule_resize(window,
2319 window->saved_allocation.width,
2320 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002321 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002322}
2323
2324void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002325window_set_maximized(struct window *window, int maximized)
2326{
2327 if (!window->display->shell)
2328 return;
2329
2330 if ((window->type == TYPE_MAXIMIZED) == maximized)
2331 return;
2332
2333 if (window->type == TYPE_TOPLEVEL) {
2334 window->saved_allocation = window->allocation;
2335 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2336 window->type = TYPE_MAXIMIZED;
2337 } else {
2338 wl_shell_surface_set_toplevel(window->shell_surface);
2339 window->type = TYPE_TOPLEVEL;
2340 window_schedule_resize(window,
2341 window->saved_allocation.width,
2342 window->saved_allocation.height);
2343 }
2344}
2345
2346void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002347window_set_user_data(struct window *window, void *data)
2348{
2349 window->user_data = data;
2350}
2351
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002352void *
2353window_get_user_data(struct window *window)
2354{
2355 return window->user_data;
2356}
2357
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002358void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002359window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002360 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002361{
2362 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002363}
2364
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002365void
2366window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002367 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002368{
2369 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002370}
2371
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002372void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002373window_set_data_handler(struct window *window, window_data_handler_t handler)
2374{
2375 window->data_handler = handler;
2376}
2377
2378void
2379window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2380{
2381 window->drop_handler = handler;
2382}
2383
2384void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002385window_set_close_handler(struct window *window,
2386 window_close_handler_t handler)
2387{
2388 window->close_handler = handler;
2389}
2390
2391void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002392window_set_title(struct window *window, const char *title)
2393{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002394 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002395 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002396 if (window->shell_surface)
2397 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002398}
2399
2400const char *
2401window_get_title(struct window *window)
2402{
2403 return window->title;
2404}
2405
2406void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002407window_damage(struct window *window, int32_t x, int32_t y,
2408 int32_t width, int32_t height)
2409{
2410 wl_surface_damage(window->surface, x, y, width, height);
2411}
2412
Casey Dahlin9074db52012-04-19 22:50:09 -04002413static void
2414surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002415 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002416{
Rob Bradford7507b572012-05-15 17:55:34 +01002417 struct window *window = data;
2418 struct output *output;
2419 struct output *output_found = NULL;
2420 struct window_output *window_output;
2421
2422 wl_list_for_each(output, &window->display->output_list, link) {
2423 if (output->output == wl_output) {
2424 output_found = output;
2425 break;
2426 }
2427 }
2428
2429 if (!output_found)
2430 return;
2431
2432 window_output = malloc (sizeof *window_output);
2433 window_output->output = output_found;
2434
2435 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002436}
2437
2438static void
2439surface_leave(void *data,
2440 struct wl_surface *wl_surface, struct wl_output *output)
2441{
Rob Bradford7507b572012-05-15 17:55:34 +01002442 struct window *window = data;
2443 struct window_output *window_output;
2444 struct window_output *window_output_found = NULL;
2445
2446 wl_list_for_each(window_output, &window->window_output_list, link) {
2447 if (window_output->output->output == output) {
2448 window_output_found = window_output;
2449 break;
2450 }
2451 }
2452
2453 if (window_output_found) {
2454 wl_list_remove(&window_output_found->link);
2455 free(window_output_found);
2456 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002457}
2458
2459static const struct wl_surface_listener surface_listener = {
2460 surface_enter,
2461 surface_leave
2462};
2463
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002464static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002465window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002466{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002467 struct window *window;
2468
2469 window = malloc(sizeof *window);
2470 if (window == NULL)
2471 return NULL;
2472
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002473 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002474 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002475 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002476 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002477 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002478 if (display->shell) {
2479 window->shell_surface =
2480 wl_shell_get_shell_surface(display->shell,
2481 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002482 if (window->title)
2483 wl_shell_surface_set_title(window->shell_surface,
2484 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002485 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002486 window->allocation.x = 0;
2487 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002488 window->allocation.width = 0;
2489 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002490 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002491 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002492 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002493 window->input_region = NULL;
2494 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002495
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002496 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002497#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002498 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002499#else
2500 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2501#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002502 else
2503 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002504
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002505 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002506 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002507 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002508
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002509 if (window->shell_surface) {
2510 wl_shell_surface_set_user_data(window->shell_surface, window);
2511 wl_shell_surface_add_listener(window->shell_surface,
2512 &shell_surface_listener, window);
2513 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002514
Rob Bradford7507b572012-05-15 17:55:34 +01002515 wl_list_init (&window->window_output_list);
2516
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002517 return window;
2518}
2519
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002520struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002521window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002522{
2523 struct window *window;
2524
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002525 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002526 if (!window)
2527 return NULL;
2528
2529 return window;
2530}
2531
2532struct window *
2533window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002534 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002535{
2536 struct window *window;
2537
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002538 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002539 if (!window)
2540 return NULL;
2541
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002542 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002543 window->x = x;
2544 window->y = y;
2545
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002546 if (display->shell)
2547 wl_shell_surface_set_transient(window->shell_surface,
2548 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002549 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002550
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002551 return window;
2552}
2553
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002554static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002555menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002556{
2557 int next;
2558
2559 next = (sy - 8) / 20;
2560 if (menu->current != next) {
2561 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002562 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002563 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002564}
2565
2566static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002567menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002568 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002569 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002570{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002571 struct menu *menu = data;
2572
2573 if (widget == menu->widget)
2574 menu_set_item(data, y);
2575
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002576 return WL_CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002577}
2578
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002579static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002580menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002581 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002582{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002583 struct menu *menu = data;
2584
2585 if (widget == menu->widget)
2586 menu_set_item(data, y);
2587
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002588 return WL_CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002589}
2590
2591static void
2592menu_leave_handler(struct widget *widget, struct input *input, void *data)
2593{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002594 struct menu *menu = data;
2595
2596 if (widget == menu->widget)
2597 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002598}
2599
2600static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002601menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002602 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002603 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002604
2605{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002606 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002607
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002608 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002609 /* Either relase after press-drag-release or
2610 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002611 menu->func(menu->window->parent,
2612 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002613 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002614 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002615 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002616}
2617
2618static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002619menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002620{
2621 cairo_t *cr;
2622 const int32_t r = 3, margin = 3;
2623 struct menu *menu = data;
2624 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002625 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002626
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002627 cr = cairo_create(window->cairo_surface);
2628 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2629 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2630 cairo_paint(cr);
2631
2632 width = window->allocation.width;
2633 height = window->allocation.height;
2634 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002635
2636 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002637 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2638 cairo_fill(cr);
2639
2640 for (i = 0; i < menu->count; i++) {
2641 if (i == menu->current) {
2642 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2643 cairo_rectangle(cr, margin, i * 20 + margin,
2644 width - 2 * margin, 20);
2645 cairo_fill(cr);
2646 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2647 cairo_move_to(cr, 10, i * 20 + 16);
2648 cairo_show_text(cr, menu->entries[i]);
2649 } else {
2650 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2651 cairo_move_to(cr, 10, i * 20 + 16);
2652 cairo_show_text(cr, menu->entries[i]);
2653 }
2654 }
2655
2656 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002657}
2658
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002659void
2660window_show_menu(struct display *display,
2661 struct input *input, uint32_t time, struct window *parent,
2662 int32_t x, int32_t y,
2663 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002664{
2665 struct window *window;
2666 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002667 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002668
2669 menu = malloc(sizeof *menu);
2670 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002671 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002672
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002673 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002674 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002675 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002676
2677 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002678 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002679 menu->entries = entries;
2680 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002681 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002682 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002683 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002684 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002685 window->type = TYPE_MENU;
2686 window->x = x;
2687 window->y = y;
2688
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002689 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002690 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002691 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002692 window->parent->shell_surface,
2693 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002694
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002695 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002696 widget_set_enter_handler(menu->widget, menu_enter_handler);
2697 widget_set_leave_handler(menu->widget, menu_leave_handler);
2698 widget_set_motion_handler(menu->widget, menu_motion_handler);
2699 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002700
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002701 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002702 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002703}
2704
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002705void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002706window_set_buffer_type(struct window *window, enum window_buffer_type type)
2707{
2708 window->buffer_type = type;
2709}
2710
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002711
2712static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002713display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002714 struct wl_output *wl_output,
2715 int x, int y,
2716 int physical_width,
2717 int physical_height,
2718 int subpixel,
2719 const char *make,
2720 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002721{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002722 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002723
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002724 output->allocation.x = x;
2725 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002726}
2727
2728static void
2729display_handle_mode(void *data,
2730 struct wl_output *wl_output,
2731 uint32_t flags,
2732 int width,
2733 int height,
2734 int refresh)
2735{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002736 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002737 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002738
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002739 if (flags & WL_OUTPUT_MODE_CURRENT) {
2740 output->allocation.width = width;
2741 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002742 if (display->output_configure_handler)
2743 (*display->output_configure_handler)(
2744 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002745 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002746}
2747
2748static const struct wl_output_listener output_listener = {
2749 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002750 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002751};
2752
2753static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002754display_add_output(struct display *d, uint32_t id)
2755{
2756 struct output *output;
2757
2758 output = malloc(sizeof *output);
2759 if (output == NULL)
2760 return;
2761
2762 memset(output, 0, sizeof *output);
2763 output->display = d;
2764 output->output =
2765 wl_display_bind(d->display, id, &wl_output_interface);
2766 wl_list_insert(d->output_list.prev, &output->link);
2767
2768 wl_output_add_listener(output->output, &output_listener, output);
2769}
2770
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002771static void
2772output_destroy(struct output *output)
2773{
2774 if (output->destroy_handler)
2775 (*output->destroy_handler)(output, output->user_data);
2776
2777 wl_output_destroy(output->output);
2778 wl_list_remove(&output->link);
2779 free(output);
2780}
2781
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002782void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002783display_set_output_configure_handler(struct display *display,
2784 display_output_handler_t handler)
2785{
2786 struct output *output;
2787
2788 display->output_configure_handler = handler;
2789 if (!handler)
2790 return;
2791
2792 wl_list_for_each(output, &display->output_list, link)
2793 (*display->output_configure_handler)(output,
2794 display->user_data);
2795}
2796
2797void
2798output_set_user_data(struct output *output, void *data)
2799{
2800 output->user_data = data;
2801}
2802
2803void *
2804output_get_user_data(struct output *output)
2805{
2806 return output->user_data;
2807}
2808
2809void
2810output_set_destroy_handler(struct output *output,
2811 display_output_handler_t handler)
2812{
2813 output->destroy_handler = handler;
2814 /* FIXME: implement this, once we have way to remove outputs */
2815}
2816
2817void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002818output_get_allocation(struct output *output, struct rectangle *allocation)
2819{
2820 *allocation = output->allocation;
2821}
2822
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002823struct wl_output *
2824output_get_wl_output(struct output *output)
2825{
2826 return output->output;
2827}
2828
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002829static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002830display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002831{
2832 struct input *input;
2833
2834 input = malloc(sizeof *input);
2835 if (input == NULL)
2836 return;
2837
2838 memset(input, 0, sizeof *input);
2839 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01002840 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002841 input->pointer_focus = NULL;
2842 input->keyboard_focus = NULL;
2843 wl_list_insert(d->input_list.prev, &input->link);
2844
Daniel Stone37816df2012-05-16 18:45:18 +01002845 wl_seat_add_listener(input->seat, &seat_listener, input);
2846 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002847
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002848 input->data_device =
2849 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01002850 input->seat);
2851 wl_data_device_add_listener(input->data_device, &data_device_listener,
2852 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002853}
2854
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002855static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002856input_destroy(struct input *input)
2857{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002858 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002859 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002860
2861 if (input->drag_offer)
2862 data_offer_destroy(input->drag_offer);
2863
2864 if (input->selection_offer)
2865 data_offer_destroy(input->selection_offer);
2866
2867 wl_data_device_destroy(input->data_device);
2868 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01002869 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002870 free(input);
2871}
2872
2873static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002874display_handle_global(struct wl_display *display, uint32_t id,
2875 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002876{
2877 struct display *d = data;
2878
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002879 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002880 d->compositor =
2881 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002882 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002883 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01002884 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002885 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002886 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002887 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002888 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002889 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002890 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2891 d->data_device_manager =
2892 wl_display_bind(display, id,
2893 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002894 }
2895}
2896
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002897static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002898init_xkb(struct display *d)
2899{
Kristian Høgsberg70163132012-05-08 15:55:39 -04002900 d->xkb.names.rules = "evdev";
2901 d->xkb.names.model = "pc105";
2902 d->xkb.names.layout = (char *) option_xkb_layout;
2903 d->xkb.names.variant = (char *) option_xkb_variant;
2904 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002905
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002906 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002907 if (!d->xkb.context) {
2908 fprintf(stderr, "Failed to create XKB context\n");
2909 exit(1);
2910 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002911
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002912 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002913 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002914 fprintf(stderr, "Failed to compile keymap\n");
2915 exit(1);
2916 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04002917
2918 d->xkb.state = xkb_state_new(d->xkb.keymap);
2919 if (!d->xkb.state) {
2920 fprintf(stderr, "Failed to create XKB state\n");
2921 exit(1);
2922 }
2923
2924 d->xkb.control_mask =
2925 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
2926 d->xkb.alt_mask =
2927 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
2928 d->xkb.shift_mask =
2929 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
2930
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002931}
2932
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002933static void
2934fini_xkb(struct display *display)
2935{
Kristian Høgsberg70163132012-05-08 15:55:39 -04002936 xkb_state_unref(display->xkb.state);
2937 xkb_map_unref(display->xkb.keymap);
2938 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002939}
2940
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002941#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002942static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002943init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002944{
2945 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002946 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002947
Rob Clark6396ed32012-03-11 19:48:41 -05002948#ifdef USE_CAIRO_GLESV2
2949# define GL_BIT EGL_OPENGL_ES2_BIT
2950#else
2951# define GL_BIT EGL_OPENGL_BIT
2952#endif
2953
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002954 static const EGLint argb_cfg_attribs[] = {
2955 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002956 EGL_RED_SIZE, 1,
2957 EGL_GREEN_SIZE, 1,
2958 EGL_BLUE_SIZE, 1,
2959 EGL_ALPHA_SIZE, 1,
2960 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002961 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002962 EGL_NONE
2963 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002964
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002965#ifdef USE_CAIRO_GLESV2
2966 static const EGLint context_attribs[] = {
2967 EGL_CONTEXT_CLIENT_VERSION, 2,
2968 EGL_NONE
2969 };
2970 EGLint api = EGL_OPENGL_ES_API;
2971#else
2972 EGLint *context_attribs = NULL;
2973 EGLint api = EGL_OPENGL_API;
2974#endif
2975
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002976 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002977 if (!eglInitialize(d->dpy, &major, &minor)) {
2978 fprintf(stderr, "failed to initialize display\n");
2979 return -1;
2980 }
2981
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002982 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002983 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2984 return -1;
2985 }
2986
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002987 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2988 &d->argb_config, 1, &n) || n != 1) {
2989 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002990 return -1;
2991 }
2992
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002993 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002994 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002995 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002996 fprintf(stderr, "failed to create context\n");
2997 return -1;
2998 }
2999
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003000 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003001 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003002 return -1;
3003 }
3004
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003005#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003006 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3007 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3008 fprintf(stderr, "failed to get cairo egl argb device\n");
3009 return -1;
3010 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003011#endif
3012
3013 return 0;
3014}
3015
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003016static void
3017fini_egl(struct display *display)
3018{
3019#ifdef HAVE_CAIRO_EGL
3020 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003021#endif
3022
3023 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3024 EGL_NO_CONTEXT);
3025
3026 eglTerminate(display->dpy);
3027 eglReleaseThread();
3028}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003029#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003030
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003031static int
3032event_mask_update(uint32_t mask, void *data)
3033{
3034 struct display *d = data;
3035
3036 d->mask = mask;
3037
3038 return 0;
3039}
3040
3041static void
3042handle_display_data(struct task *task, uint32_t events)
3043{
3044 struct display *display =
3045 container_of(task, struct display, display_task);
3046
3047 wl_display_iterate(display->display, display->mask);
3048}
3049
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003050struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003051display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003052{
3053 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003054
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003055 argc = parse_options(xkb_options,
3056 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003057
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003058 d = malloc(sizeof *d);
3059 if (d == NULL)
3060 return NULL;
3061
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003062 memset(d, 0, sizeof *d);
3063
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003064 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003065 if (d->display == NULL) {
3066 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003067 return NULL;
3068 }
3069
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003070 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3071 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3072 d->display_task.run = handle_display_data;
3073 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3074
3075 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003076 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003077 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003078
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003079 /* Set up listener so we'll catch all events. */
3080 wl_display_add_global_listener(d->display,
3081 display_handle_global, d);
3082
3083 /* Process connection events. */
3084 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003085#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003086 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003087 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003088#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003089
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003090 d->image_target_texture_2d =
3091 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3092 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3093 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3094
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003095 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003096
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003097 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003098
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003099 wl_list_init(&d->window_list);
3100
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003101 init_xkb(d);
3102
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003103 return d;
3104}
3105
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003106static void
3107display_destroy_outputs(struct display *display)
3108{
3109 struct output *tmp;
3110 struct output *output;
3111
3112 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3113 output_destroy(output);
3114}
3115
Pekka Paalanene1207c72011-12-16 12:02:09 +02003116static void
3117display_destroy_inputs(struct display *display)
3118{
3119 struct input *tmp;
3120 struct input *input;
3121
3122 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3123 input_destroy(input);
3124}
3125
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003126void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003127display_destroy(struct display *display)
3128{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003129 if (!wl_list_empty(&display->window_list))
3130 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3131
3132 if (!wl_list_empty(&display->deferred_list))
3133 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3134
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003135 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003136 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003137
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003138 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003139
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003140 theme_destroy(display->theme);
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003141 wl_cursor_theme_destroy(display->cursor_theme);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003142
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003143#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003144 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003145#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003146
Pekka Paalanenc2052982011-12-16 11:41:32 +02003147 if (display->shell)
3148 wl_shell_destroy(display->shell);
3149
3150 if (display->shm)
3151 wl_shm_destroy(display->shm);
3152
3153 if (display->data_device_manager)
3154 wl_data_device_manager_destroy(display->data_device_manager);
3155
3156 wl_compositor_destroy(display->compositor);
3157
3158 close(display->epoll_fd);
3159
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003160 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003161 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003162 free(display);
3163}
3164
3165void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003166display_set_user_data(struct display *display, void *data)
3167{
3168 display->user_data = data;
3169}
3170
3171void *
3172display_get_user_data(struct display *display)
3173{
3174 return display->user_data;
3175}
3176
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003177struct wl_display *
3178display_get_display(struct display *display)
3179{
3180 return display->display;
3181}
3182
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003183struct output *
3184display_get_output(struct display *display)
3185{
3186 return container_of(display->output_list.next, struct output, link);
3187}
3188
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003189struct wl_compositor *
3190display_get_compositor(struct display *display)
3191{
3192 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003193}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003194
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003195uint32_t
3196display_get_serial(struct display *display)
3197{
3198 return display->serial;
3199}
3200
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003201EGLDisplay
3202display_get_egl_display(struct display *d)
3203{
3204 return d->dpy;
3205}
3206
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003207struct wl_data_source *
3208display_create_data_source(struct display *display)
3209{
3210 return wl_data_device_manager_create_data_source(display->data_device_manager);
3211}
3212
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003213EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003214display_get_argb_egl_config(struct display *d)
3215{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003216 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003217}
3218
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003219struct wl_shell *
3220display_get_shell(struct display *display)
3221{
3222 return display->shell;
3223}
3224
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003225int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003226display_acquire_window_surface(struct display *display,
3227 struct window *window,
3228 EGLContext ctx)
3229{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003230#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003231 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003232 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003233
3234 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003235 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003236 device = cairo_surface_get_device(window->cairo_surface);
3237 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003238 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003239
Benjamin Franzke0c991632011-09-27 21:57:31 +02003240 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003241 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003242 ctx = display->argb_ctx;
3243 else
3244 assert(0);
3245 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003246
3247 data = cairo_surface_get_user_data(window->cairo_surface,
3248 &surface_data_key);
3249
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003250 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003251 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003252 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3253 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003254
3255 return 0;
3256#else
3257 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003258#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003259}
3260
3261void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003262display_release_window_surface(struct display *display,
3263 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003264{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003265#ifdef HAVE_CAIRO_EGL
3266 cairo_device_t *device;
3267
3268 device = cairo_surface_get_device(window->cairo_surface);
3269 if (!device)
3270 return;
3271
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003272 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003273 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003274 cairo_device_release(device);
3275#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003276}
3277
3278void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003279display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003280{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003281 wl_list_insert(&display->deferred_list, &task->link);
3282}
3283
3284void
3285display_watch_fd(struct display *display,
3286 int fd, uint32_t events, struct task *task)
3287{
3288 struct epoll_event ep;
3289
3290 ep.events = events;
3291 ep.data.ptr = task;
3292 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3293}
3294
3295void
3296display_run(struct display *display)
3297{
3298 struct task *task;
3299 struct epoll_event ep[16];
3300 int i, count;
3301
Pekka Paalanen826d7952011-12-15 10:14:07 +02003302 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003303 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003304 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003305
3306 while (!wl_list_empty(&display->deferred_list)) {
3307 task = container_of(display->deferred_list.next,
3308 struct task, link);
3309 wl_list_remove(&task->link);
3310 task->run(task, 0);
3311 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003312
3313 if (!display->running)
3314 break;
3315
3316 wl_display_flush(display->display);
3317
3318 count = epoll_wait(display->epoll_fd,
3319 ep, ARRAY_LENGTH(ep), -1);
3320 for (i = 0; i < count; i++) {
3321 task = ep[i].data.ptr;
3322 task->run(task, ep[i].events);
3323 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003324 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003325}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003326
3327void
3328display_exit(struct display *display)
3329{
3330 display->running = 0;
3331}