blob: 81a0d7d2aab14a672bcc89ef2549e82b90862624 [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 Oliveira1493d2a2012-05-03 12:29:46 +030058#include <X11/Xcursor/Xcursor.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øgsbergb91cd102010-08-16 16:17:42 -040062#include "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 Oliveira1493d2a2012-05-03 12:29:46 +030066struct cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070067struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030068
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050069struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050070 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050071 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040072 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040073 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040074 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040075 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050076 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020077 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020078 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040079 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040080
81 int display_fd;
82 uint32_t mask;
83 struct task display_task;
84
85 int epoll_fd;
86 struct wl_list deferred_list;
87
Pekka Paalanen826d7952011-12-15 10:14:07 +020088 int running;
89
Kristian Høgsberg478d9262010-06-08 20:34:11 -040090 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040091 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050092 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040093 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050094 int frame_radius;
Kristian Høgsberg70163132012-05-08 15:55:39 -040095
96 struct {
97 struct xkb_rule_names names;
98 struct xkb_keymap *keymap;
99 struct xkb_state *state;
100 struct xkb_context *context;
101 xkb_mod_mask_t control_mask;
102 xkb_mod_mask_t alt_mask;
103 xkb_mod_mask_t shift_mask;
104 } xkb;
105
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300106 struct cursor *cursors;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700107 struct shm_pool *cursor_shm_pool;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400108
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500109 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
110 PFNEGLCREATEIMAGEKHRPROC create_image;
111 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200112
113 display_output_handler_t output_configure_handler;
114
115 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500116};
117
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400119 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400121 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500122 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500124 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400125 TYPE_CUSTOM
126};
127
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128struct window {
129 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500130 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500131 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200132 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500133 struct wl_region *input_region;
134 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500135 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500136 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500137 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500138 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400139 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400140 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400141 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400142 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400143 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400144 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400145 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400146 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400147 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500148
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400149 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500150
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700151 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400152
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500153 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500154 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400155 window_data_handler_t data_handler;
156 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500157 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400158
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200159 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500160 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500161
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500162 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400163 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500164};
165
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500166struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500167 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500168 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400169 struct wl_list link;
170 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500171 widget_resize_handler_t resize_handler;
172 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500173 widget_enter_handler_t enter_handler;
174 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500175 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500176 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400177 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500178 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179};
180
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400181struct input {
182 struct display *display;
183 struct wl_input_device *input_device;
184 struct window *pointer_focus;
185 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300186 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400187 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400188 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400189 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400190 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400191
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500192 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500193 struct widget *grab;
194 uint32_t grab_button;
195
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400196 struct wl_data_device *data_device;
197 struct data_offer *drag_offer;
198 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400199};
200
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500201struct output {
202 struct display *display;
203 struct wl_output *output;
204 struct rectangle allocation;
205 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200206
207 display_output_handler_t destroy_handler;
208 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500209};
210
Martin Minarik1998b152012-05-10 02:04:35 +0200211enum frame_button_action {
212 FRAME_BUTTON_NULL = 0,
213 FRAME_BUTTON_ICON = 1,
214 FRAME_BUTTON_CLOSE = 2,
215 FRAME_BUTTON_MINIMIZE = 3,
216 FRAME_BUTTON_MAXIMIZE = 4,
217};
218
219enum frame_button_pointer {
220 FRAME_BUTTON_DEFAULT = 0,
221 FRAME_BUTTON_OVER = 1,
222 FRAME_BUTTON_ACTIVE = 2,
223};
224
225enum frame_button_align {
226 FRAME_BUTTON_RIGHT = 0,
227 FRAME_BUTTON_LEFT = 1,
228};
229
230enum frame_button_decoration {
231 FRAME_BUTTON_NONE = 0,
232 FRAME_BUTTON_FANCY = 1,
233};
234
235struct frame_button {
236 struct widget *widget;
237 struct frame *frame;
238 cairo_surface_t *icon;
239 enum frame_button_action type;
240 enum frame_button_pointer state;
241 struct wl_list link; /* buttons_list */
242 enum frame_button_align align;
243 enum frame_button_decoration decoration;
244};
245
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500246struct frame {
247 struct widget *widget;
248 struct widget *child;
249 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400250 int width;
251 int titlebar_height;
Martin Minarik1998b152012-05-10 02:04:35 +0200252 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500253};
254
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500255struct menu {
256 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500257 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500258 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500259 const char **entries;
260 uint32_t time;
261 int current;
262 int count;
263 menu_func_t func;
264};
265
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300266struct cursor_image {
267 cairo_surface_t *surface;
268 int width, height;
269 int hotspot_x, hotspot_y;
270 int delay;
271};
272
273struct cursor {
274 int n_images;
275 struct cursor_image *images;
276};
277
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700278struct shm_pool {
279 struct wl_shm_pool *pool;
280 size_t size;
281 size_t used;
282 void *data;
283};
284
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400285enum {
286 POINTER_DEFAULT = 100,
287 POINTER_UNSET
288};
289
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500290enum window_location {
291 WINDOW_INTERIOR = 0,
292 WINDOW_RESIZING_TOP = 1,
293 WINDOW_RESIZING_BOTTOM = 2,
294 WINDOW_RESIZING_LEFT = 4,
295 WINDOW_RESIZING_TOP_LEFT = 5,
296 WINDOW_RESIZING_BOTTOM_LEFT = 6,
297 WINDOW_RESIZING_RIGHT = 8,
298 WINDOW_RESIZING_TOP_RIGHT = 9,
299 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
300 WINDOW_RESIZING_MASK = 15,
301 WINDOW_EXTERIOR = 16,
302 WINDOW_TITLEBAR = 17,
303 WINDOW_CLIENT_AREA = 18,
304};
305
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400306const char *option_xkb_layout = "us";
307const char *option_xkb_variant = "";
308const char *option_xkb_options = "";
309
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400310static const struct weston_option xkb_options[] = {
311 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
312 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
313 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400314};
315
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316static const cairo_user_data_key_t surface_data_key;
317struct surface_data {
318 struct wl_buffer *buffer;
319};
320
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500321#define MULT(_d,c,a,t) \
322 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
323
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500324#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100326struct egl_window_surface_data {
327 struct display *display;
328 struct wl_surface *surface;
329 struct wl_egl_window *window;
330 EGLSurface surf;
331};
332
333static void
334egl_window_surface_data_destroy(void *p)
335{
336 struct egl_window_surface_data *data = p;
337 struct display *d = data->display;
338
339 eglDestroySurface(d->dpy, data->surf);
340 wl_egl_window_destroy(data->window);
341 data->surface = NULL;
342
343 free(p);
344}
345
346static cairo_surface_t *
347display_create_egl_window_surface(struct display *display,
348 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400349 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100350 struct rectangle *rectangle)
351{
352 cairo_surface_t *cairo_surface;
353 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400354 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200355 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100356
357 data = malloc(sizeof *data);
358 if (data == NULL)
359 return NULL;
360
361 data->display = display;
362 data->surface = surface;
363
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500364 config = display->argb_config;
365 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400366
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400367 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100368 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400369 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100370
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400371 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500372 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100373
Benjamin Franzke0c991632011-09-27 21:57:31 +0200374 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100375 data->surf,
376 rectangle->width,
377 rectangle->height);
378
379 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
380 data, egl_window_surface_data_destroy);
381
382 return cairo_surface;
383}
384
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400385#endif
386
387struct wl_buffer *
388display_get_buffer_for_surface(struct display *display,
389 cairo_surface_t *surface)
390{
391 struct surface_data *data;
392
393 data = cairo_surface_get_user_data (surface, &surface_data_key);
394
395 return data->buffer;
396}
397
398struct shm_surface_data {
399 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700400 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401};
402
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500403static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700404shm_pool_destroy(struct shm_pool *pool);
405
406static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400407shm_surface_data_destroy(void *p)
408{
409 struct shm_surface_data *data = p;
410
411 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700412 if (data->pool)
413 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400414}
415
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300416static void
417shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
418{
419 void *dest = cairo_image_surface_get_data(surface);
420
421 memcpy(dest, data, count);
422}
423
Kristian Høgsberg16626282012-04-03 11:21:27 -0400424static struct wl_shm_pool *
425make_shm_pool(struct display *display, int size, void **data)
426{
427 char filename[] = "/tmp/wayland-shm-XXXXXX";
428 struct wl_shm_pool *pool;
429 int fd;
430
431 fd = mkstemp(filename);
432 if (fd < 0) {
433 fprintf(stderr, "open %s failed: %m\n", filename);
434 return NULL;
435 }
436 if (ftruncate(fd, size) < 0) {
437 fprintf(stderr, "ftruncate failed: %m\n");
438 close(fd);
439 return NULL;
440 }
441
442 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
443 unlink(filename);
444
445 if (*data == MAP_FAILED) {
446 fprintf(stderr, "mmap failed: %m\n");
447 close(fd);
448 return NULL;
449 }
450
451 pool = wl_shm_create_pool(display->shm, fd, size);
452
453 close(fd);
454
455 return pool;
456}
457
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700458static struct shm_pool *
459shm_pool_create(struct display *display, size_t size)
460{
461 struct shm_pool *pool = malloc(sizeof *pool);
462
463 if (!pool)
464 return NULL;
465
466 pool->pool = make_shm_pool(display, size, &pool->data);
467 if (!pool->pool) {
468 free(pool);
469 return NULL;
470 }
471
472 pool->size = size;
473 pool->used = 0;
474
475 return pool;
476}
477
478static void *
479shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
480{
481 if (pool->used + size > pool->size)
482 return NULL;
483
484 *offset = pool->used;
485 pool->used += size;
486
487 return (char *) pool->data + *offset;
488}
489
490/* destroy the pool. this does not unmap the memory though */
491static void
492shm_pool_destroy(struct shm_pool *pool)
493{
494 munmap(pool->data, pool->size);
495 wl_shm_pool_destroy(pool->pool);
496 free(pool);
497}
498
499/* Start allocating from the beginning of the pool again */
500static void
501shm_pool_reset(struct shm_pool *pool)
502{
503 pool->used = 0;
504}
505
506static int
507data_length_for_shm_surface(struct rectangle *rect)
508{
509 int stride;
510
511 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
512 rect->width);
513 return stride * rect->height;
514}
515
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500516static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700517display_create_shm_surface_from_pool(struct display *display,
518 struct rectangle *rectangle,
519 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520{
521 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400522 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400523 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700524 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400525 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526
527 data = malloc(sizeof *data);
528 if (data == NULL)
529 return NULL;
530
531 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
532 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700533 length = stride * rectangle->height;
534 data->pool = NULL;
535 map = shm_pool_allocate(pool, length, &offset);
536
537 if (!map) {
538 free(data);
539 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400540 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400542 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400543 CAIRO_FORMAT_ARGB32,
544 rectangle->width,
545 rectangle->height,
546 stride);
547
548 cairo_surface_set_user_data (surface, &surface_data_key,
549 data, shm_surface_data_destroy);
550
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500552 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500554 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400555
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700556 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400557 rectangle->width,
558 rectangle->height,
559 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700561 return surface;
562}
563
564static cairo_surface_t *
565display_create_shm_surface(struct display *display,
566 struct rectangle *rectangle, uint32_t flags,
567 struct window *window)
568{
569 struct shm_surface_data *data;
570 struct shm_pool *pool;
571 cairo_surface_t *surface;
572
573 if (window && window->pool) {
574 shm_pool_reset(window->pool);
575 surface = display_create_shm_surface_from_pool(display,
576 rectangle,
577 flags,
578 window->pool);
579 if (surface)
580 return surface;
581 }
582
583 pool = shm_pool_create(display,
584 data_length_for_shm_surface(rectangle));
585 if (!pool)
586 return NULL;
587
588 surface =
589 display_create_shm_surface_from_pool(display, rectangle,
590 flags, pool);
591
592 if (!surface) {
593 shm_pool_destroy(pool);
594 return NULL;
595 }
596
597 /* make sure we destroy the pool when the surface is destroyed */
598 data = cairo_surface_get_user_data(surface, &surface_data_key);
599 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400600
601 return surface;
602}
603
nobled7b87cb02011-02-01 18:51:47 +0000604static int
605check_size(struct rectangle *rect)
606{
607 if (rect->width && rect->height)
608 return 0;
609
610 fprintf(stderr, "tried to create surface of "
611 "width: %d, height: %d\n", rect->width, rect->height);
612 return -1;
613}
614
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615cairo_surface_t *
616display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100617 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400618 struct rectangle *rectangle,
619 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400620{
nobled7b87cb02011-02-01 18:51:47 +0000621 if (check_size(rectangle) < 0)
622 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500623#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400624 if (display->dpy)
625 return display_create_egl_window_surface(display,
626 surface,
627 flags,
628 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400630 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631}
632
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300633static const char *cursors[] = {
634 "bottom_left_corner",
635 "bottom_right_corner",
636 "bottom_side",
637 "grabbing",
638 "left_ptr",
639 "left_side",
640 "right_side",
641 "top_left_corner",
642 "top_right_corner",
643 "top_side",
644 "xterm",
645 "hand1",
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400646};
647
648static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300649create_cursor_from_images(struct display *display, struct cursor *cursor,
650 XcursorImages *images)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400651{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300652 int i;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400653 struct rectangle rect;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300654 XcursorImage *image;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400655
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300656 cursor->images = malloc(images->nimage * sizeof *cursor->images);
657 cursor->n_images = images->nimage;
658
659 for (i = 0; i < images->nimage; i++) {
660 image = images->images[i];
661
662 rect.width = image->width;
663 rect.height = image->height;
664
665 cursor->images[i].surface =
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700666 display_create_shm_surface_from_pool(display, &rect, 0,
667 display->cursor_shm_pool);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300668
669 shm_surface_write(cursor->images[i].surface,
670 (unsigned char *) image->pixels,
671 image->width * image->height * sizeof image->pixels[0]);
672
673 cursor->images[i].width = image->width;
674 cursor->images[i].height = image->height;
675 cursor->images[i].hotspot_x = image->xhot;
676 cursor->images[i].hotspot_y = image->yhot;
677 cursor->images[i].delay = image->delay;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400678 }
679
680}
681
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700682static size_t
683data_length_for_cursor_images(XcursorImages *images)
684{
685 int i;
686 size_t length = 0;
687 struct rectangle rect;
688
689 for (i = 0; i < images->nimage; i++) {
690 rect.width = images->images[i]->width;
691 rect.height = images->images[i]->height;
692 length += data_length_for_shm_surface(&rect);
693 }
694
695 return length;
696}
697
Pekka Paalanen325bb602011-12-19 10:31:45 +0200698static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300699create_cursors(struct display *display)
700{
701 int i, count;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700702 size_t pool_size = 0;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300703 struct cursor *cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700704 XcursorImages **images;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300705
706 count = ARRAY_LENGTH(cursors);
707 display->cursors = malloc(count * sizeof *display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700708 images = malloc(count * sizeof images[0]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300709
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700710 for (i = 0; i < count; i++) {
711 images[i] = XcursorLibraryLoadImages(cursors[i], NULL, 32);
Rafal Mielniczuk87e4c932012-05-08 22:10:44 +0200712 if (!images[i]) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300713 fprintf(stderr, "Error loading cursor: %s\n",
714 cursors[i]);
715 continue;
716 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700717 pool_size += data_length_for_cursor_images(images[i]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300718 }
719
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700720 display->cursor_shm_pool = shm_pool_create(display, pool_size);
721
722 for (i = 0; i < count; i++) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723 cursor = &display->cursors[i];
Dima Ryazanovff1c2d72012-05-08 21:02:33 -0700724
725 if (!images[i]) {
726 cursor->n_images = 0;
727 cursor->images = NULL;
728 continue;
729 }
730
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700731 create_cursor_from_images(display, cursor, images[i]);
732
733 XcursorImagesDestroy(images[i]);
734 }
735
736 free(images);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300737}
738
739static void
740destroy_cursor_images(struct cursor *cursor)
741{
742 int i;
743
744 for (i = 0; i < cursor->n_images; i++)
745 if (cursor->images[i].surface)
746 cairo_surface_destroy(cursor->images[i].surface);
747
748 free(cursor->images);
749}
750
751static void
752destroy_cursors(struct display *display)
Pekka Paalanen325bb602011-12-19 10:31:45 +0200753{
754 int i, count;
755
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300756 count = ARRAY_LENGTH(cursors);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200757 for (i = 0; i < count; ++i) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300758 destroy_cursor_images(&display->cursors[i]);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200759 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700760
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300761 free(display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700762 shm_pool_destroy(display->cursor_shm_pool);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200763}
764
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400765cairo_surface_t *
766display_get_pointer_surface(struct display *display, int pointer,
767 int *width, int *height,
768 int *hotspot_x, int *hotspot_y)
769{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300770 struct cursor *cursor = &display->cursors[pointer];
771 cairo_surface_t *surface = cursor->images[0].surface;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400772
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300773 /* FIXME returning information for the first image. Something better
774 * is needed for animated cursors */
775
nobledf8475c92011-01-05 17:41:55 +0000776 *width = cairo_image_surface_get_width(surface);
777 *height = cairo_image_surface_get_height(surface);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300778
779 *hotspot_x = cursor->images[0].hotspot_x;
780 *hotspot_y = cursor->images[0].hotspot_y;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400781
782 return cairo_surface_reference(surface);
783}
784
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400785static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200786window_get_resize_dx_dy(struct window *window, int *x, int *y)
787{
788 if (window->resize_edges & WINDOW_RESIZING_LEFT)
789 *x = window->server_allocation.width - window->allocation.width;
790 else
791 *x = 0;
792
793 if (window->resize_edges & WINDOW_RESIZING_TOP)
794 *y = window->server_allocation.height -
795 window->allocation.height;
796 else
797 *y = 0;
798
799 window->resize_edges = 0;
800}
801
802static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500803window_attach_surface(struct window *window)
804{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400805 struct display *display = window->display;
806 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000807#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100808 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000809#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500810 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400812 if (window->type == TYPE_NONE) {
813 window->type = TYPE_TOPLEVEL;
814 if (display->shell)
815 wl_shell_surface_set_toplevel(window->shell_surface);
816 }
817
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100818 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000819#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
821 data = cairo_surface_get_user_data(window->cairo_surface,
822 &surface_data_key);
823
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 cairo_gl_surface_swapbuffers(window->cairo_surface);
825 wl_egl_window_get_attached_size(data->window,
826 &window->server_allocation.width,
827 &window->server_allocation.height);
828 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000829#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831 buffer =
832 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400833 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200835 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100836 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400837 wl_surface_damage(window->surface, 0, 0,
838 window->allocation.width,
839 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100840 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400841 cairo_surface_destroy(window->cairo_surface);
842 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000844 default:
845 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500847
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500848 if (window->input_region) {
849 wl_surface_set_input_region(window->surface,
850 window->input_region);
851 wl_region_destroy(window->input_region);
852 window->input_region = NULL;
853 }
854
855 if (window->opaque_region) {
856 wl_surface_set_opaque_region(window->surface,
857 window->opaque_region);
858 wl_region_destroy(window->opaque_region);
859 window->opaque_region = NULL;
860 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500861}
862
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500863void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400864window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500865{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400866 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100867 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500868}
869
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400870void
871window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400872{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500873 cairo_surface_reference(surface);
874
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400875 if (window->cairo_surface != NULL)
876 cairo_surface_destroy(window->cairo_surface);
877
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500878 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400879}
880
Benjamin Franzke22d54812011-07-16 19:50:32 +0000881#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100882static void
883window_resize_cairo_window_surface(struct window *window)
884{
885 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200886 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100887
888 data = cairo_surface_get_user_data(window->cairo_surface,
889 &surface_data_key);
890
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200891 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100892 wl_egl_window_resize(data->window,
893 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200894 window->allocation.height,
895 x,y);
896
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100897 cairo_gl_surface_set_size(window->cairo_surface,
898 window->allocation.width,
899 window->allocation.height);
900}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000901#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100902
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400903struct display *
904window_get_display(struct window *window)
905{
906 return window->display;
907}
908
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400909void
910window_create_surface(struct window *window)
911{
912 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400913 uint32_t flags = 0;
914
915 if (!window->transparent)
916 flags = SURFACE_OPAQUE;
917
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400918 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500919#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100920 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
921 if (window->cairo_surface) {
922 window_resize_cairo_window_surface(window);
923 return;
924 }
925 surface = display_create_surface(window->display,
926 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400927 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100928 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400929#endif
930 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400931 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400932 &window->allocation,
933 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400934 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800935 default:
936 surface = NULL;
937 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400938 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400939
940 window_set_surface(window, surface);
941 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400942}
943
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200944static void frame_destroy(struct frame *frame);
945
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400946void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500947window_destroy(struct window *window)
948{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200949 struct display *display = window->display;
950 struct input *input;
951
952 if (window->redraw_scheduled)
953 wl_list_remove(&window->redraw_task.link);
954
955 wl_list_for_each(input, &display->input_list, link) {
956 if (input->pointer_focus == window)
957 input->pointer_focus = NULL;
958 if (input->keyboard_focus == window)
959 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500960 if (input->focus_widget &&
961 input->focus_widget->window == window)
962 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200963 }
964
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500965 if (window->input_region)
966 wl_region_destroy(window->input_region);
967 if (window->opaque_region)
968 wl_region_destroy(window->opaque_region);
969
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200970 if (window->frame)
971 frame_destroy(window->frame);
972
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200973 if (window->shell_surface)
974 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500975 wl_surface_destroy(window->surface);
976 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200977
978 if (window->cairo_surface != NULL)
979 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200980
981 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500982 free(window);
983}
984
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500985static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500986widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400987{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500988 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400989
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500990 wl_list_for_each(child, &widget->child_list, link) {
991 target = widget_find_widget(child, x, y);
992 if (target)
993 return target;
994 }
995
996 if (widget->allocation.x <= x &&
997 x < widget->allocation.x + widget->allocation.width &&
998 widget->allocation.y <= y &&
999 y < widget->allocation.y + widget->allocation.height) {
1000 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001001 }
1002
1003 return NULL;
1004}
1005
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001006static struct widget *
1007widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001008{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001009 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001010
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001011 widget = malloc(sizeof *widget);
1012 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001013 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001014 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001015 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001016 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001017 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001018
1019 return widget;
1020}
1021
1022struct widget *
1023window_add_widget(struct window *window, void *data)
1024{
1025 window->widget = widget_create(window, data);
1026 wl_list_init(&window->widget->link);
1027
1028 return window->widget;
1029}
1030
1031struct widget *
1032widget_add_widget(struct widget *parent, void *data)
1033{
1034 struct widget *widget;
1035
1036 widget = widget_create(parent->window, data);
1037 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001038
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001039 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001040}
1041
1042void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001043widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001044{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001045 struct display *display = widget->window->display;
1046 struct input *input;
1047
1048 wl_list_for_each(input, &display->input_list, link) {
1049 if (input->focus_widget == widget)
1050 input->focus_widget = NULL;
1051 }
1052
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001053 wl_list_remove(&widget->link);
1054 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001055}
1056
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001058widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001059{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001060 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001061}
1062
1063void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001064widget_set_size(struct widget *widget, int32_t width, int32_t height)
1065{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001066 widget->allocation.width = width;
1067 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001068}
1069
1070void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001071widget_set_allocation(struct widget *widget,
1072 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001074 widget->allocation.x = x;
1075 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001076 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001077}
1078
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001079void
1080widget_set_transparent(struct widget *widget, int transparent)
1081{
1082 widget->opaque = !transparent;
1083}
1084
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001085void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001088 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001089}
1090
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001091void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001092widget_set_resize_handler(struct widget *widget,
1093 widget_resize_handler_t handler)
1094{
1095 widget->resize_handler = handler;
1096}
1097
1098void
1099widget_set_redraw_handler(struct widget *widget,
1100 widget_redraw_handler_t handler)
1101{
1102 widget->redraw_handler = handler;
1103}
1104
1105void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001106widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001107{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001108 widget->enter_handler = handler;
1109}
1110
1111void
1112widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1113{
1114 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001115}
1116
1117void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001118widget_set_motion_handler(struct widget *widget,
1119 widget_motion_handler_t handler)
1120{
1121 widget->motion_handler = handler;
1122}
1123
1124void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001125widget_set_button_handler(struct widget *widget,
1126 widget_button_handler_t handler)
1127{
1128 widget->button_handler = handler;
1129}
1130
1131void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001132widget_schedule_redraw(struct widget *widget)
1133{
1134 window_schedule_redraw(widget->window);
1135}
1136
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001137cairo_surface_t *
1138window_get_surface(struct window *window)
1139{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001140 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001141}
1142
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001143struct wl_surface *
1144window_get_wl_surface(struct window *window)
1145{
1146 return window->surface;
1147}
1148
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001149struct wl_shell_surface *
1150window_get_wl_shell_surface(struct window *window)
1151{
1152 return window->shell_surface;
1153}
1154
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001155static void
1156frame_resize_handler(struct widget *widget,
1157 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001158{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001159 struct frame *frame = data;
1160 struct widget *child = frame->child;
1161 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001162 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001163 struct frame_button * button;
1164 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001165 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001166 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001167
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001168 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001169 decoration_width = (frame->width + frame->margin) * 2;
1170 decoration_height = frame->width +
1171 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001172
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001173 allocation.x = frame->width + frame->margin;
1174 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001175 allocation.width = width - decoration_width;
1176 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001177
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001178 opaque_margin = frame->margin + display->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001179
1180 wl_list_for_each(button, &frame->buttons_list, link)
1181 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001182 } else {
1183 decoration_width = 0;
1184 decoration_height = 0;
1185
1186 allocation.x = 0;
1187 allocation.y = 0;
1188 allocation.width = width;
1189 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001190 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001191
1192 wl_list_for_each(button, &frame->buttons_list, link)
1193 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001194 }
1195
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001196 widget_set_allocation(child, allocation.x, allocation.y,
1197 allocation.width, allocation.height);
1198
1199 if (child->resize_handler)
1200 child->resize_handler(child,
1201 allocation.width,
1202 allocation.height,
1203 child->user_data);
1204
Scott Moreauf7e498c2012-05-14 11:39:29 -06001205 width = child->allocation.width + decoration_width;
1206 height = child->allocation.height + decoration_height;
1207
1208 widget->window->input_region =
1209 wl_compositor_create_region(display->compositor);
1210 wl_region_add(widget->window->input_region,
1211 frame->margin, frame->margin,
1212 width - 2 * frame->margin,
1213 height - 2 * frame->margin);
1214
1215 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001216
1217 if (child->opaque) {
1218 widget->window->opaque_region =
1219 wl_compositor_create_region(display->compositor);
1220 wl_region_add(widget->window->opaque_region,
1221 opaque_margin, opaque_margin,
1222 widget->allocation.width - 2 * opaque_margin,
1223 widget->allocation.height - 2 * opaque_margin);
1224 }
Martin Minarik1998b152012-05-10 02:04:35 +02001225
1226 /* frame internal buttons */
1227 x_r = frame->widget->allocation.width - frame->width - frame->margin;
1228 x_l = frame->width + frame->margin;
1229 y = frame->width + frame->margin;
1230 wl_list_for_each(button, &frame->buttons_list, link) {
1231 const int button_padding = 4;
1232 w = cairo_image_surface_get_width(button->icon);
1233 h = cairo_image_surface_get_height(button->icon);
1234
1235 if (button->decoration == FRAME_BUTTON_FANCY)
1236 w += 10;
1237
1238 if (button->align == FRAME_BUTTON_LEFT) {
1239 widget_set_allocation(button->widget,
1240 x_l, y , w + 1, h + 1);
1241 x_l += w;
1242 x_l += button_padding;
1243 } else {
1244 x_r -= w;
1245 widget_set_allocation(button->widget,
1246 x_r, y , w + 1, h + 1);
1247 x_r -= button_padding;
1248 }
1249 }
1250}
1251
1252static int
1253frame_button_enter_handler(struct widget *widget,
1254 struct input *input, float x, float y, void *data)
1255{
1256 struct frame_button *frame_button = data;
1257
1258 widget_schedule_redraw(frame_button->widget);
1259 frame_button->state = FRAME_BUTTON_OVER;
1260
1261 return POINTER_LEFT_PTR;
1262}
1263
1264static void
1265frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1266{
1267 struct frame_button *frame_button = data;
1268
1269 widget_schedule_redraw(frame_button->widget);
1270 frame_button->state = FRAME_BUTTON_DEFAULT;
1271}
1272
1273static void
1274frame_button_button_handler(struct widget *widget,
1275 struct input *input, uint32_t time,
1276 uint32_t button, uint32_t state, void *data)
1277{
1278 struct frame_button *frame_button = data;
1279 struct window *window = widget->window;
1280
1281 if (button != BTN_LEFT)
1282 return;
1283
1284 switch (state) {
1285 case 1:
1286 frame_button->state = FRAME_BUTTON_ACTIVE;
1287 widget_schedule_redraw(frame_button->widget);
1288
1289 if (frame_button->type == FRAME_BUTTON_ICON)
1290 window_show_frame_menu(window, input, time);
1291 return;
1292 case 0:
1293 frame_button->state = FRAME_BUTTON_DEFAULT;
1294 widget_schedule_redraw(frame_button->widget);
1295 break;
1296 }
1297
1298 switch (frame_button->type) {
1299 case FRAME_BUTTON_CLOSE:
1300 if (window->close_handler)
1301 window->close_handler(window->parent,
1302 window->user_data);
1303 else
1304 display_exit(window->display);
1305 break;
1306 case FRAME_BUTTON_MINIMIZE:
1307 fprintf(stderr,"Minimize stub\n");
1308 break;
1309 case FRAME_BUTTON_MAXIMIZE:
1310 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1311 break;
1312 default:
1313 /* Unknown operation */
1314 break;
1315 }
1316}
1317
1318static void
1319frame_button_redraw_handler(struct widget *widget, void *data)
1320{
1321 struct frame_button *frame_button = data;
1322 cairo_t *cr;
1323 int width, height, x, y;
1324 struct window *window = widget->window;
1325
1326 x = widget->allocation.x;
1327 y = widget->allocation.y;
1328 width = widget->allocation.width;
1329 height = widget->allocation.height;
1330
1331 if (!width)
1332 return;
1333 if (!height)
1334 return;
1335 if (widget->opaque)
1336 return;
1337
1338 cr = cairo_create(window->cairo_surface);
1339
1340 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1341 cairo_set_line_width(cr, 1);
1342
1343 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1344 cairo_rectangle (cr, x, y, 25, 16);
1345
1346 cairo_stroke_preserve(cr);
1347
1348 switch (frame_button->state) {
1349 case FRAME_BUTTON_DEFAULT:
1350 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1351 break;
1352 case FRAME_BUTTON_OVER:
1353 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1354 break;
1355 case FRAME_BUTTON_ACTIVE:
1356 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1357 break;
1358 }
1359
1360 cairo_fill (cr);
1361
1362 x += 4;
1363 }
1364
1365 cairo_set_source_surface(cr, frame_button->icon, x, y);
1366 cairo_paint(cr);
1367
1368 cairo_destroy(cr);
1369}
1370
1371static struct widget *
1372frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1373 enum frame_button_align align, enum frame_button_decoration style)
1374{
1375 struct frame_button *frame_button;
1376 const char *icon = data;
1377
1378 frame_button = malloc (sizeof *frame_button);
1379 memset(frame_button, 0, sizeof *frame_button);
1380
1381 frame_button->icon = cairo_image_surface_create_from_png(icon);
1382 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1383 frame_button->frame = frame;
1384 frame_button->type = type;
1385 frame_button->align = align;
1386 frame_button->decoration = style;
1387
1388 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1389
1390 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1391 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1392 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1393 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1394 return frame_button->widget;
1395}
1396
1397static void
1398frame_button_destroy(struct frame_button *frame_button)
1399{
1400 widget_destroy(frame_button->widget);
1401 wl_list_remove(&frame_button->link);
1402 cairo_surface_destroy(frame_button->icon);
1403 free(frame_button);
1404
1405 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001406}
1407
1408static void
1409frame_redraw_handler(struct widget *widget, void *data)
1410{
1411 struct frame *frame = data;
1412 cairo_t *cr;
1413 cairo_text_extents_t extents;
1414 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001415 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001416 struct window *window = widget->window;
1417
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001418 if (window->type == TYPE_FULLSCREEN)
1419 return;
1420
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001421 width = widget->allocation.width;
1422 height = widget->allocation.height;
1423
1424 cr = cairo_create(window->cairo_surface);
1425
1426 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1427 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1428 cairo_paint(cr);
1429
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001430 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001431 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001432 2, 2, width + 8, height + 8,
1433 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001434
1435 if (window->keyboard_device)
1436 source = window->display->active_frame;
1437 else
1438 source = window->display->inactive_frame;
1439
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001440 tile_source(cr, source,
1441 frame->margin, frame->margin,
1442 width - frame->margin * 2, height - frame->margin * 2,
1443 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001444
1445 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001446 cairo_select_font_face(cr, "sans",
1447 CAIRO_FONT_SLANT_NORMAL,
1448 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001449 cairo_set_font_size(cr, 14);
1450 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001451 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001452 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001453 if (window->keyboard_device) {
1454 cairo_move_to(cr, x + 1, y + 1);
1455 cairo_set_source_rgb(cr, 1, 1, 1);
1456 cairo_show_text(cr, window->title);
1457 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001458 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001459 cairo_show_text(cr, window->title);
1460 } else {
1461 cairo_move_to(cr, x, y);
1462 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1463 cairo_show_text(cr, window->title);
1464 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001465
1466 cairo_destroy(cr);
1467}
1468
1469static int
1470frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1471{
1472 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001473 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001474 const int grip_size = 8;
1475
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001476 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001477 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001478 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001479 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001480 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001481 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001482 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001483 hlocation = WINDOW_RESIZING_RIGHT;
1484 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001485 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001486
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001487 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001488 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001489 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001490 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001491 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001492 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001493 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001494 vlocation = WINDOW_RESIZING_BOTTOM;
1495 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001496 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001497
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001498 location = vlocation | hlocation;
1499 if (location & WINDOW_EXTERIOR)
1500 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001501 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001502 location = WINDOW_TITLEBAR;
1503 else if (location == WINDOW_INTERIOR)
1504 location = WINDOW_CLIENT_AREA;
1505
1506 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001507}
1508
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001509static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001510frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001511{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001512 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001513
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001514 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001515 switch (location) {
1516 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001517 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001518 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001519 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001520 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001521 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001522 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001523 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001524 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001525 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001526 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001527 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001528 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001529 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001530 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001531 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001532 case WINDOW_EXTERIOR:
1533 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001534 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001535 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001536 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001537}
1538
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001539static void
1540frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001541{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001542 switch (index) {
1543 case 0: /* close */
1544 if (window->close_handler)
1545 window->close_handler(window->parent,
1546 window->user_data);
1547 else
1548 display_exit(window->display);
1549 break;
1550 case 1: /* fullscreen */
1551 /* we don't have a way to get out of fullscreen for now */
1552 window_set_fullscreen(window, 1);
1553 break;
1554 case 2: /* rotate */
1555 case 3: /* scale */
1556 break;
1557 }
1558}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001559
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001560void
1561window_show_frame_menu(struct window *window,
1562 struct input *input, uint32_t time)
1563{
1564 int32_t x, y;
1565
1566 static const char *entries[] = {
1567 "Close", "Fullscreen", "Rotate", "Scale"
1568 };
1569
1570 input_get_position(input, &x, &y);
1571 window_show_menu(window->display, input, time, window,
1572 x - 10, y - 10, frame_menu_func, entries, 4);
1573}
1574
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001575static int
1576frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001577 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001578{
1579 return frame_get_pointer_image_for_location(data, input);
1580}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001581
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001582static int
1583frame_motion_handler(struct widget *widget,
1584 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001585 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001586{
1587 return frame_get_pointer_image_for_location(data, input);
1588}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001589
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001590static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001591frame_button_handler(struct widget *widget,
1592 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001593 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001594
1595{
1596 struct frame *frame = data;
1597 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001598 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001599 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001600
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001601 location = frame_get_pointer_location(frame, input->sx, input->sy);
1602
1603 if (window->display->shell && button == BTN_LEFT && state == 1) {
1604 switch (location) {
1605 case WINDOW_TITLEBAR:
1606 if (!window->shell_surface)
1607 break;
1608 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001609 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610 wl_shell_surface_move(window->shell_surface,
1611 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001612 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001613 break;
1614 case WINDOW_RESIZING_TOP:
1615 case WINDOW_RESIZING_BOTTOM:
1616 case WINDOW_RESIZING_LEFT:
1617 case WINDOW_RESIZING_RIGHT:
1618 case WINDOW_RESIZING_TOP_LEFT:
1619 case WINDOW_RESIZING_TOP_RIGHT:
1620 case WINDOW_RESIZING_BOTTOM_LEFT:
1621 case WINDOW_RESIZING_BOTTOM_RIGHT:
1622 if (!window->shell_surface)
1623 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001624 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001625
1626 if (!display->dpy) {
1627 /* If we're using shm, allocate a big
1628 pool to create buffers out of while
1629 we resize. We should probably base
1630 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001631 window->pool =
1632 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001633 }
1634
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001635 wl_shell_surface_resize(window->shell_surface,
1636 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001637 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001638 break;
1639 }
1640 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001641 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 }
1643}
1644
1645struct widget *
1646frame_create(struct window *window, void *data)
1647{
1648 struct frame *frame;
1649
1650 frame = malloc(sizeof *frame);
1651 memset(frame, 0, sizeof *frame);
1652
1653 frame->widget = window_add_widget(window, frame);
1654 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001655 frame->margin = 32;
Martin Minarik1998b152012-05-10 02:04:35 +02001656 frame->width = 6;
1657 frame->titlebar_height = 27;
1658
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001659 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1660 widget_set_resize_handler(frame->widget, frame_resize_handler);
1661 widget_set_enter_handler(frame->widget, frame_enter_handler);
1662 widget_set_motion_handler(frame->widget, frame_motion_handler);
1663 widget_set_button_handler(frame->widget, frame_button_handler);
1664
Martin Minarik1998b152012-05-10 02:04:35 +02001665 /* Create empty list for frame buttons */
1666 wl_list_init(&frame->buttons_list);
1667
1668 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1669 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1670
1671 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1672 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1673
1674 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1675 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1676
1677 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1678 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1679
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001680 window->frame = frame;
1681
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001682 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001683}
1684
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001685static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001686frame_destroy(struct frame *frame)
1687{
Martin Minarik1998b152012-05-10 02:04:35 +02001688 struct frame_button *button, *tmp;
1689
1690 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1691 frame_button_destroy(button);
1692
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001693 /* frame->child must be destroyed by the application */
1694 widget_destroy(frame->widget);
1695 free(frame);
1696}
1697
1698static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001699input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001700 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001701{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001702 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001703 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001704
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001705 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706 return;
1707
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001708 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001709 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001710 widget = old;
1711 if (input->grab)
1712 widget = input->grab;
1713 if (widget->leave_handler)
1714 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001715 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001716 }
1717
1718 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001719 widget = focus;
1720 if (input->grab)
1721 widget = input->grab;
1722 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001723 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001724 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001725 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001726
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001727 input_set_pointer_image(input, input->pointer_enter_serial,
1728 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001729 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001730}
1731
1732static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001733input_handle_motion(void *data, struct wl_input_device *input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001734 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001735{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001736 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001737 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001738 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001739 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001740 float sx = wl_fixed_to_double(sx_w);
1741 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001742
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001743 input->sx = sx;
1744 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001745
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001746 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001747 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001748 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001749 }
1750
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001751 if (input->grab)
1752 widget = input->grab;
1753 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001754 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001755 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001756 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001757 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001758 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001759
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001760 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001761}
1762
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001763void
1764input_grab(struct input *input, struct widget *widget, uint32_t button)
1765{
1766 input->grab = widget;
1767 input->grab_button = button;
1768}
1769
1770void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001771input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001772{
1773 struct widget *widget;
1774
1775 input->grab = NULL;
1776 if (input->pointer_focus) {
1777 widget = widget_find_widget(input->pointer_focus->widget,
1778 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001779 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001780 }
1781}
1782
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001783static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001784input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001785 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001786 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001787{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001788 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001789 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001790
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001791 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001792 if (input->focus_widget && input->grab == NULL && state)
1793 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001794
Neil Roberts6b28aad2012-01-23 19:11:18 +00001795 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001796 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001797 (*widget->button_handler)(widget,
1798 input, time,
1799 button, state,
1800 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001801
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001802 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001803 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001804}
1805
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001806static void
Scott Moreau210d0792012-03-22 10:47:01 -06001807input_handle_axis(void *data,
1808 struct wl_input_device *input_device,
1809 uint32_t time, uint32_t axis, int32_t value)
1810{
1811}
1812
1813static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001814input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001815 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001816{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001817 struct input *input = data;
1818 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001819 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001820 uint32_t code, num_syms;
1821 const xkb_keysym_t *syms;
1822 xkb_keysym_t sym;
1823 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001824
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001825 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001826 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001827 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001828 return;
1829
Kristian Høgsberg70163132012-05-08 15:55:39 -04001830 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1831 xkb_state_update_key(d->xkb.state, code,
1832 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001833
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001834 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001835 XKB_STATE_DEPRESSED |
1836 XKB_STATE_LATCHED);
1837 input->modifiers = 0;
1838 if (mask & input->display->xkb.control_mask)
1839 input->modifiers |= MOD_CONTROL_MASK;
1840 if (mask & input->display->xkb.alt_mask)
1841 input->modifiers |= MOD_ALT_MASK;
1842 if (mask & input->display->xkb.shift_mask)
1843 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001844
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001845 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001846 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001847 if (state)
1848 window_set_maximized(window,
1849 window->type != TYPE_MAXIMIZED);
1850 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001851 if (num_syms == 1)
1852 sym = syms[0];
1853 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001854 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001855
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001856 (*window->key_handler)(window, input, time, key,
1857 sym, state, window->user_data);
1858 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001859}
1860
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001861static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001862input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001863{
1864 struct window *window = input->pointer_focus;
1865
1866 if (!window)
1867 return;
1868
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001869 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001870
Pekka Paalanene1207c72011-12-16 12:02:09 +02001871 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001872 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001873}
1874
1875static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001876input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001877 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001878 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001879 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001880{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001881 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001882 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001883 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001884 float sx = wl_fixed_to_double(sx_w);
1885 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001886
Martin Minarik1998b152012-05-10 02:04:35 +02001887 if (!surface) {
1888 /* enter event for a window we've just destroyed */
1889 return;
1890 }
1891
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001892 input->display->serial = serial;
1893 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001894 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001895 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001896
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001897 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001898 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001899 window->pool = NULL;
1900 /* Schedule a redraw to free the pool */
1901 window_schedule_redraw(window);
1902 }
1903
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001904 input->sx = sx;
1905 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001906
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001907 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001908 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001909}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001910
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001911static void
1912input_handle_pointer_leave(void *data,
1913 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001914 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001915{
1916 struct input *input = data;
1917
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001918 input->display->serial = serial;
1919 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001920}
1921
1922static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001923input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001924{
1925 struct window *window = input->keyboard_focus;
1926
1927 if (!window)
1928 return;
1929
1930 window->keyboard_device = NULL;
1931 if (window->keyboard_focus_handler)
1932 (*window->keyboard_focus_handler)(window, NULL,
1933 window->user_data);
1934
1935 input->keyboard_focus = NULL;
1936}
1937
1938static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001939input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001940 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001941 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001942 struct wl_surface *surface,
1943 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001944{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001945 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001946 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001947
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001948 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001949 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001950
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001951 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001952 window->keyboard_device = input;
1953 if (window->keyboard_focus_handler)
1954 (*window->keyboard_focus_handler)(window,
1955 window->keyboard_device,
1956 window->user_data);
1957}
1958
1959static void
1960input_handle_keyboard_leave(void *data,
1961 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001962 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001963 struct wl_surface *surface)
1964{
1965 struct input *input = data;
1966
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001967 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001968 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001969}
1970
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001971static void
1972input_handle_touch_down(void *data,
1973 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001974 uint32_t serial, uint32_t time,
1975 struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001976 int32_t id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001977{
1978}
1979
1980static void
1981input_handle_touch_up(void *data,
1982 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001983 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001984{
1985}
1986
1987static void
1988input_handle_touch_motion(void *data,
1989 struct wl_input_device *wl_input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001990 uint32_t time, int32_t id,
1991 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001992{
1993}
1994
1995static void
1996input_handle_touch_frame(void *data,
1997 struct wl_input_device *wl_input_device)
1998{
1999}
2000
2001static void
2002input_handle_touch_cancel(void *data,
2003 struct wl_input_device *wl_input_device)
2004{
2005}
2006
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002007static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002008 input_handle_motion,
2009 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06002010 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002011 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002012 input_handle_pointer_enter,
2013 input_handle_pointer_leave,
2014 input_handle_keyboard_enter,
2015 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002016 input_handle_touch_down,
2017 input_handle_touch_up,
2018 input_handle_touch_motion,
2019 input_handle_touch_frame,
2020 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002021};
2022
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002023void
2024input_get_position(struct input *input, int32_t *x, int32_t *y)
2025{
2026 *x = input->sx;
2027 *y = input->sy;
2028}
2029
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002030struct wl_input_device *
2031input_get_input_device(struct input *input)
2032{
2033 return input->input_device;
2034}
2035
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002036uint32_t
2037input_get_modifiers(struct input *input)
2038{
2039 return input->modifiers;
2040}
2041
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002042struct widget *
2043input_get_focus_widget(struct input *input)
2044{
2045 return input->focus_widget;
2046}
2047
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002048struct data_offer {
2049 struct wl_data_offer *offer;
2050 struct input *input;
2051 struct wl_array types;
2052 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002053
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002054 struct task io_task;
2055 int fd;
2056 data_func_t func;
2057 int32_t x, y;
2058 void *user_data;
2059};
2060
2061static void
2062data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2063{
2064 struct data_offer *offer = data;
2065 char **p;
2066
2067 p = wl_array_add(&offer->types, sizeof *p);
2068 *p = strdup(type);
2069}
2070
2071static const struct wl_data_offer_listener data_offer_listener = {
2072 data_offer_offer,
2073};
2074
2075static void
2076data_offer_destroy(struct data_offer *offer)
2077{
2078 char **p;
2079
2080 offer->refcount--;
2081 if (offer->refcount == 0) {
2082 wl_data_offer_destroy(offer->offer);
2083 for (p = offer->types.data; *p; p++)
2084 free(*p);
2085 wl_array_release(&offer->types);
2086 free(offer);
2087 }
2088}
2089
2090static void
2091data_device_data_offer(void *data,
2092 struct wl_data_device *data_device, uint32_t id)
2093{
2094 struct data_offer *offer;
2095
2096 offer = malloc(sizeof *offer);
2097
2098 wl_array_init(&offer->types);
2099 offer->refcount = 1;
2100 offer->input = data;
2101
2102 /* FIXME: Generate typesafe wrappers for this */
2103 offer->offer = (struct wl_data_offer *)
2104 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2105 id, &wl_data_offer_interface);
2106
2107 wl_data_offer_add_listener(offer->offer,
2108 &data_offer_listener, offer);
2109}
2110
2111static void
2112data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002113 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002114 wl_fixed_t x_w, wl_fixed_t y_w,
2115 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002116{
2117 struct input *input = data;
2118 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002119 float x = wl_fixed_to_double(x_w);
2120 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002121 char **p;
2122
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002123 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002124 input->drag_offer = wl_data_offer_get_user_data(offer);
2125 window = wl_surface_get_user_data(surface);
2126 input->pointer_focus = window;
2127
2128 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2129 *p = NULL;
2130
2131 window = input->pointer_focus;
2132 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002133 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002134 input->drag_offer->types.data,
2135 window->user_data);
2136}
2137
2138static void
2139data_device_leave(void *data, struct wl_data_device *data_device)
2140{
2141 struct input *input = data;
2142
2143 data_offer_destroy(input->drag_offer);
2144 input->drag_offer = NULL;
2145}
2146
2147static void
2148data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002149 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002150{
2151 struct input *input = data;
2152 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002153 float x = wl_fixed_to_double(x_w);
2154 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002155
2156 input->sx = x;
2157 input->sy = y;
2158
2159 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002160 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002161 input->drag_offer->types.data,
2162 window->user_data);
2163}
2164
2165static void
2166data_device_drop(void *data, struct wl_data_device *data_device)
2167{
2168 struct input *input = data;
2169 struct window *window = input->pointer_focus;
2170
2171 if (window->drop_handler)
2172 window->drop_handler(window, input,
2173 input->sx, input->sy, window->user_data);
2174}
2175
2176static void
2177data_device_selection(void *data,
2178 struct wl_data_device *wl_data_device,
2179 struct wl_data_offer *offer)
2180{
2181 struct input *input = data;
2182 char **p;
2183
2184 if (input->selection_offer)
2185 data_offer_destroy(input->selection_offer);
2186
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002187 if (offer) {
2188 input->selection_offer = wl_data_offer_get_user_data(offer);
2189 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2190 *p = NULL;
2191 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002192}
2193
2194static const struct wl_data_device_listener data_device_listener = {
2195 data_device_data_offer,
2196 data_device_enter,
2197 data_device_leave,
2198 data_device_motion,
2199 data_device_drop,
2200 data_device_selection
2201};
2202
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002203void
2204input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2205{
2206 struct display *display = input->display;
2207 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002208 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002209
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002210 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211 return;
2212
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002213 if (display->cursors[pointer].n_images == 0)
2214 return;
2215
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002216 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002217
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002218 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219 return;
2220
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002221 input->current_cursor = pointer;
2222 buffer = display_get_buffer_for_surface(display, image->surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002223 wl_input_device_attach(input->input_device, time, buffer,
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002224 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002225}
2226
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002227struct wl_data_device *
2228input_get_data_device(struct input *input)
2229{
2230 return input->data_device;
2231}
2232
2233void
2234input_set_selection(struct input *input,
2235 struct wl_data_source *source, uint32_t time)
2236{
2237 wl_data_device_set_selection(input->data_device, source, time);
2238}
2239
2240void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002241input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002242{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002243 wl_data_offer_accept(input->drag_offer->offer,
2244 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002245}
2246
2247static void
2248offer_io_func(struct task *task, uint32_t events)
2249{
2250 struct data_offer *offer =
2251 container_of(task, struct data_offer, io_task);
2252 unsigned int len;
2253 char buffer[4096];
2254
2255 len = read(offer->fd, buffer, sizeof buffer);
2256 offer->func(buffer, len,
2257 offer->x, offer->y, offer->user_data);
2258
2259 if (len == 0) {
2260 close(offer->fd);
2261 data_offer_destroy(offer);
2262 }
2263}
2264
2265static void
2266data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2267 data_func_t func, void *user_data)
2268{
2269 int p[2];
2270
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002271 if (pipe2(p, O_CLOEXEC) == -1)
2272 return;
2273
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002274 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2275 close(p[1]);
2276
2277 offer->io_task.run = offer_io_func;
2278 offer->fd = p[0];
2279 offer->func = func;
2280 offer->refcount++;
2281 offer->user_data = user_data;
2282
2283 display_watch_fd(offer->input->display,
2284 offer->fd, EPOLLIN, &offer->io_task);
2285}
2286
2287void
2288input_receive_drag_data(struct input *input, const char *mime_type,
2289 data_func_t func, void *data)
2290{
2291 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2292 input->drag_offer->x = input->sx;
2293 input->drag_offer->y = input->sy;
2294}
2295
2296int
2297input_receive_selection_data(struct input *input, const char *mime_type,
2298 data_func_t func, void *data)
2299{
2300 char **p;
2301
2302 if (input->selection_offer == NULL)
2303 return -1;
2304
2305 for (p = input->selection_offer->types.data; *p; p++)
2306 if (strcmp(mime_type, *p) == 0)
2307 break;
2308
2309 if (*p == NULL)
2310 return -1;
2311
2312 data_offer_receive_data(input->selection_offer,
2313 mime_type, func, data);
2314 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002315}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002316
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002317int
2318input_receive_selection_data_to_fd(struct input *input,
2319 const char *mime_type, int fd)
2320{
2321 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2322
2323 return 0;
2324}
2325
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002326void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002327window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002328{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002329 if (!window->shell_surface)
2330 return;
2331
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002332 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002333 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002334}
2335
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002336static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002337idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002338{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002339 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002340
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002341 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002342 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002343 widget_set_allocation(widget,
2344 window->pending_allocation.x,
2345 window->pending_allocation.y,
2346 window->pending_allocation.width,
2347 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002348
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002349 if (window->input_region) {
2350 wl_region_destroy(window->input_region);
2351 window->input_region = NULL;
2352 }
2353
2354 if (window->opaque_region) {
2355 wl_region_destroy(window->opaque_region);
2356 window->opaque_region = NULL;
2357 }
2358
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002359 if (widget->resize_handler)
2360 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002361 widget->allocation.width,
2362 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002363 widget->user_data);
2364
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002365 if (window->allocation.width != widget->allocation.width ||
2366 window->allocation.height != widget->allocation.height) {
2367 window->allocation = widget->allocation;
2368 window_schedule_redraw(window);
2369 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002370}
2371
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002372void
2373window_schedule_resize(struct window *window, int width, int height)
2374{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002375 window->pending_allocation.x = 0;
2376 window->pending_allocation.y = 0;
2377 window->pending_allocation.width = width;
2378 window->pending_allocation.height = height;
2379
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002380 window->resize_needed = 1;
2381 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002382}
2383
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002384void
2385widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2386{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002387 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002388}
2389
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002390static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002391handle_ping(void *data, struct wl_shell_surface *shell_surface,
2392 uint32_t serial)
2393{
2394 wl_shell_surface_pong(shell_surface, serial);
2395}
2396
2397static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002398handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002399 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002400{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002401 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002402
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002403 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002404 return;
2405
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002406 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002407 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002408}
2409
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002410static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002411menu_destroy(struct menu *menu)
2412{
2413 widget_destroy(menu->widget);
2414 window_destroy(menu->window);
2415 free(menu);
2416}
2417
2418static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002419handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2420{
2421 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002422 struct menu *menu = window->widget->user_data;
2423
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002424 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002425 * device. Or just use wl_callback. And this really needs to
2426 * be a window vfunc that the menu can set. And we need the
2427 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002428
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002429 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002430 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002431 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002432}
2433
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002434static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002435 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002436 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002437 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002438};
2439
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002440void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002441window_get_allocation(struct window *window,
2442 struct rectangle *allocation)
2443{
2444 *allocation = window->allocation;
2445}
2446
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002447static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002448widget_redraw(struct widget *widget)
2449{
2450 struct widget *child;
2451
2452 if (widget->redraw_handler)
2453 widget->redraw_handler(widget, widget->user_data);
2454 wl_list_for_each(child, &widget->child_list, link)
2455 widget_redraw(child);
2456}
2457
2458static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002459frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2460{
2461 struct window *window = data;
2462
2463 wl_callback_destroy(callback);
2464 window->redraw_scheduled = 0;
2465 if (window->redraw_needed)
2466 window_schedule_redraw(window);
2467}
2468
2469static const struct wl_callback_listener listener = {
2470 frame_callback
2471};
2472
2473static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002474idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002475{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002476 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002477 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002478
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002479 if (window->resize_needed)
2480 idle_resize(window);
2481
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002482 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002483 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002484 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002485 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002486 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002487
2488 callback = wl_surface_frame(window->surface);
2489 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002490}
2491
2492void
2493window_schedule_redraw(struct window *window)
2494{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002495 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002496 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002497 window->redraw_task.run = idle_redraw;
2498 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002499 window->redraw_scheduled = 1;
2500 }
2501}
2502
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002503void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002504window_set_custom(struct window *window)
2505{
2506 window->type = TYPE_CUSTOM;
2507}
2508
2509void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002510window_set_fullscreen(struct window *window, int fullscreen)
2511{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002512 if (!window->display->shell)
2513 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002514
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002515 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002516 return;
2517
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002518 if (fullscreen) {
2519 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002520 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002521 wl_shell_surface_set_fullscreen(window->shell_surface,
2522 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2523 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002524 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002525 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002526 wl_shell_surface_set_toplevel(window->shell_surface);
2527 window_schedule_resize(window,
2528 window->saved_allocation.width,
2529 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002530 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002531}
2532
2533void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002534window_set_maximized(struct window *window, int maximized)
2535{
2536 if (!window->display->shell)
2537 return;
2538
2539 if ((window->type == TYPE_MAXIMIZED) == maximized)
2540 return;
2541
2542 if (window->type == TYPE_TOPLEVEL) {
2543 window->saved_allocation = window->allocation;
2544 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2545 window->type = TYPE_MAXIMIZED;
2546 } else {
2547 wl_shell_surface_set_toplevel(window->shell_surface);
2548 window->type = TYPE_TOPLEVEL;
2549 window_schedule_resize(window,
2550 window->saved_allocation.width,
2551 window->saved_allocation.height);
2552 }
2553}
2554
2555void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002556window_set_user_data(struct window *window, void *data)
2557{
2558 window->user_data = data;
2559}
2560
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002561void *
2562window_get_user_data(struct window *window)
2563{
2564 return window->user_data;
2565}
2566
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002567void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002568window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002569 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002570{
2571 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002572}
2573
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002574void
2575window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002576 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002577{
2578 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002579}
2580
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002581void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002582window_set_data_handler(struct window *window, window_data_handler_t handler)
2583{
2584 window->data_handler = handler;
2585}
2586
2587void
2588window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2589{
2590 window->drop_handler = handler;
2591}
2592
2593void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002594window_set_close_handler(struct window *window,
2595 window_close_handler_t handler)
2596{
2597 window->close_handler = handler;
2598}
2599
2600void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002601window_set_title(struct window *window, const char *title)
2602{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002603 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002604 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002605 if (window->shell_surface)
2606 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002607}
2608
2609const char *
2610window_get_title(struct window *window)
2611{
2612 return window->title;
2613}
2614
2615void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002616window_damage(struct window *window, int32_t x, int32_t y,
2617 int32_t width, int32_t height)
2618{
2619 wl_surface_damage(window->surface, x, y, width, height);
2620}
2621
Casey Dahlin9074db52012-04-19 22:50:09 -04002622static void
2623surface_enter(void *data,
2624 struct wl_surface *wl_surface, struct wl_output *output)
2625{
2626}
2627
2628static void
2629surface_leave(void *data,
2630 struct wl_surface *wl_surface, struct wl_output *output)
2631{
2632}
2633
2634static const struct wl_surface_listener surface_listener = {
2635 surface_enter,
2636 surface_leave
2637};
2638
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002639static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002640window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002641{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002642 struct window *window;
2643
2644 window = malloc(sizeof *window);
2645 if (window == NULL)
2646 return NULL;
2647
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002648 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002649 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002650 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002651 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002652 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002653 if (display->shell) {
2654 window->shell_surface =
2655 wl_shell_get_shell_surface(display->shell,
2656 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002657 if (window->title)
2658 wl_shell_surface_set_title(window->shell_surface,
2659 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002660 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002661 window->allocation.x = 0;
2662 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002663 window->allocation.width = 0;
2664 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002665 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002666 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002667 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002668 window->input_region = NULL;
2669 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002670
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002671 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002672#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002673 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002674#else
2675 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2676#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002677 else
2678 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002679
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002680 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002681 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002682 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002683
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002684 if (window->shell_surface) {
2685 wl_shell_surface_set_user_data(window->shell_surface, window);
2686 wl_shell_surface_add_listener(window->shell_surface,
2687 &shell_surface_listener, window);
2688 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002689
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002690 return window;
2691}
2692
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002693struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002694window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002695{
2696 struct window *window;
2697
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002698 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002699 if (!window)
2700 return NULL;
2701
2702 return window;
2703}
2704
2705struct window *
2706window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002707 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002708{
2709 struct window *window;
2710
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002711 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002712 if (!window)
2713 return NULL;
2714
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002715 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002716 window->x = x;
2717 window->y = y;
2718
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002719 if (display->shell)
2720 wl_shell_surface_set_transient(window->shell_surface,
2721 window->parent->shell_surface,
2722 window->x, window->y, 0);
2723
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002724 return window;
2725}
2726
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002727static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002728menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002729{
2730 int next;
2731
2732 next = (sy - 8) / 20;
2733 if (menu->current != next) {
2734 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002735 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002736 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002737}
2738
2739static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002740menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002741 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002742 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002743{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002744 struct menu *menu = data;
2745
2746 if (widget == menu->widget)
2747 menu_set_item(data, y);
2748
2749 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002750}
2751
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002752static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002753menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002754 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002755{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002756 struct menu *menu = data;
2757
2758 if (widget == menu->widget)
2759 menu_set_item(data, y);
2760
2761 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002762}
2763
2764static void
2765menu_leave_handler(struct widget *widget, struct input *input, void *data)
2766{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002767 struct menu *menu = data;
2768
2769 if (widget == menu->widget)
2770 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002771}
2772
2773static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002774menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002775 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002776 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002777
2778{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002779 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002780
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002781 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002782 /* Either relase after press-drag-release or
2783 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002784 menu->func(menu->window->parent,
2785 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002786 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002787 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002788 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002789}
2790
2791static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002792menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002793{
2794 cairo_t *cr;
2795 const int32_t r = 3, margin = 3;
2796 struct menu *menu = data;
2797 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002798 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002799
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002800 cr = cairo_create(window->cairo_surface);
2801 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2802 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2803 cairo_paint(cr);
2804
2805 width = window->allocation.width;
2806 height = window->allocation.height;
2807 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002808
2809 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002810 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2811 cairo_fill(cr);
2812
2813 for (i = 0; i < menu->count; i++) {
2814 if (i == menu->current) {
2815 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2816 cairo_rectangle(cr, margin, i * 20 + margin,
2817 width - 2 * margin, 20);
2818 cairo_fill(cr);
2819 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2820 cairo_move_to(cr, 10, i * 20 + 16);
2821 cairo_show_text(cr, menu->entries[i]);
2822 } else {
2823 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2824 cairo_move_to(cr, 10, i * 20 + 16);
2825 cairo_show_text(cr, menu->entries[i]);
2826 }
2827 }
2828
2829 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002830}
2831
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002832void
2833window_show_menu(struct display *display,
2834 struct input *input, uint32_t time, struct window *parent,
2835 int32_t x, int32_t y,
2836 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002837{
2838 struct window *window;
2839 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002840 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002841
2842 menu = malloc(sizeof *menu);
2843 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002844 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002845
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002846 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002847 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002848 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002849
2850 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002851 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002852 menu->entries = entries;
2853 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002854 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002855 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002856 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002857 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002858 window->type = TYPE_MENU;
2859 window->x = x;
2860 window->y = y;
2861
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002862 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002863 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2864 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002865 window->parent->shell_surface,
2866 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002867
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002868 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002869 widget_set_enter_handler(menu->widget, menu_enter_handler);
2870 widget_set_leave_handler(menu->widget, menu_leave_handler);
2871 widget_set_motion_handler(menu->widget, menu_motion_handler);
2872 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002873
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002874 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002875 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002876}
2877
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002878void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002879window_set_buffer_type(struct window *window, enum window_buffer_type type)
2880{
2881 window->buffer_type = type;
2882}
2883
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002884
2885static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002886display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002887 struct wl_output *wl_output,
2888 int x, int y,
2889 int physical_width,
2890 int physical_height,
2891 int subpixel,
2892 const char *make,
2893 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002894{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002895 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002896
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002897 output->allocation.x = x;
2898 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002899}
2900
2901static void
2902display_handle_mode(void *data,
2903 struct wl_output *wl_output,
2904 uint32_t flags,
2905 int width,
2906 int height,
2907 int refresh)
2908{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002909 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002910 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002911
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002912 if (flags & WL_OUTPUT_MODE_CURRENT) {
2913 output->allocation.width = width;
2914 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002915 if (display->output_configure_handler)
2916 (*display->output_configure_handler)(
2917 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002918 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002919}
2920
2921static const struct wl_output_listener output_listener = {
2922 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002923 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002924};
2925
2926static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002927display_add_output(struct display *d, uint32_t id)
2928{
2929 struct output *output;
2930
2931 output = malloc(sizeof *output);
2932 if (output == NULL)
2933 return;
2934
2935 memset(output, 0, sizeof *output);
2936 output->display = d;
2937 output->output =
2938 wl_display_bind(d->display, id, &wl_output_interface);
2939 wl_list_insert(d->output_list.prev, &output->link);
2940
2941 wl_output_add_listener(output->output, &output_listener, output);
2942}
2943
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002944static void
2945output_destroy(struct output *output)
2946{
2947 if (output->destroy_handler)
2948 (*output->destroy_handler)(output, output->user_data);
2949
2950 wl_output_destroy(output->output);
2951 wl_list_remove(&output->link);
2952 free(output);
2953}
2954
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002955void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002956display_set_output_configure_handler(struct display *display,
2957 display_output_handler_t handler)
2958{
2959 struct output *output;
2960
2961 display->output_configure_handler = handler;
2962 if (!handler)
2963 return;
2964
2965 wl_list_for_each(output, &display->output_list, link)
2966 (*display->output_configure_handler)(output,
2967 display->user_data);
2968}
2969
2970void
2971output_set_user_data(struct output *output, void *data)
2972{
2973 output->user_data = data;
2974}
2975
2976void *
2977output_get_user_data(struct output *output)
2978{
2979 return output->user_data;
2980}
2981
2982void
2983output_set_destroy_handler(struct output *output,
2984 display_output_handler_t handler)
2985{
2986 output->destroy_handler = handler;
2987 /* FIXME: implement this, once we have way to remove outputs */
2988}
2989
2990void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002991output_get_allocation(struct output *output, struct rectangle *allocation)
2992{
2993 *allocation = output->allocation;
2994}
2995
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002996struct wl_output *
2997output_get_wl_output(struct output *output)
2998{
2999 return output->output;
3000}
3001
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003002static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003003display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003004{
3005 struct input *input;
3006
3007 input = malloc(sizeof *input);
3008 if (input == NULL)
3009 return;
3010
3011 memset(input, 0, sizeof *input);
3012 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003013 input->input_device =
3014 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003015 input->pointer_focus = NULL;
3016 input->keyboard_focus = NULL;
3017 wl_list_insert(d->input_list.prev, &input->link);
3018
3019 wl_input_device_add_listener(input->input_device,
3020 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003021 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003022
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003023 input->data_device =
3024 wl_data_device_manager_get_data_device(d->data_device_manager,
3025 input->input_device);
3026 wl_data_device_add_listener(input->data_device,
3027 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003028}
3029
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003030static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003031input_destroy(struct input *input)
3032{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003033 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003034 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003035
3036 if (input->drag_offer)
3037 data_offer_destroy(input->drag_offer);
3038
3039 if (input->selection_offer)
3040 data_offer_destroy(input->selection_offer);
3041
3042 wl_data_device_destroy(input->data_device);
3043 wl_list_remove(&input->link);
3044 wl_input_device_destroy(input->input_device);
3045 free(input);
3046}
3047
3048static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003049display_handle_global(struct wl_display *display, uint32_t id,
3050 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003051{
3052 struct display *d = data;
3053
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003054 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003055 d->compositor =
3056 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003057 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003058 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003059 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003060 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003061 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003062 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003063 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003064 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003065 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3066 d->data_device_manager =
3067 wl_display_bind(display, id,
3068 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003069 }
3070}
3071
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003072static void
3073display_render_frame(struct display *d)
3074{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003075 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003076 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003077
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003078 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003079 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3080 cr = cairo_create(d->shadow);
3081 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3082 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04003083 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003084 cairo_fill(cr);
3085 cairo_destroy(cr);
3086 blur_surface(d->shadow, 64);
3087
3088 d->active_frame =
3089 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3090 cr = cairo_create(d->active_frame);
3091 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003092
3093 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
3094 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
3095 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
3096 cairo_set_source(cr, pattern);
3097 cairo_pattern_destroy(pattern);
3098
Kristian Høgsbergec323d22012-03-21 01:07:49 -04003099 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003100 cairo_fill(cr);
3101 cairo_destroy(cr);
3102
3103 d->inactive_frame =
3104 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3105 cr = cairo_create(d->inactive_frame);
3106 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003107 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04003108 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003109 cairo_fill(cr);
3110 cairo_destroy(cr);
3111}
3112
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003113static void
3114init_xkb(struct display *d)
3115{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003116 d->xkb.names.rules = "evdev";
3117 d->xkb.names.model = "pc105";
3118 d->xkb.names.layout = (char *) option_xkb_layout;
3119 d->xkb.names.variant = (char *) option_xkb_variant;
3120 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003121
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003122 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003123 if (!d->xkb.context) {
3124 fprintf(stderr, "Failed to create XKB context\n");
3125 exit(1);
3126 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003127
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003128 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003129 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003130 fprintf(stderr, "Failed to compile keymap\n");
3131 exit(1);
3132 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003133
3134 d->xkb.state = xkb_state_new(d->xkb.keymap);
3135 if (!d->xkb.state) {
3136 fprintf(stderr, "Failed to create XKB state\n");
3137 exit(1);
3138 }
3139
3140 d->xkb.control_mask =
3141 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3142 d->xkb.alt_mask =
3143 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3144 d->xkb.shift_mask =
3145 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3146
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003147}
3148
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003149static void
3150fini_xkb(struct display *display)
3151{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003152 xkb_state_unref(display->xkb.state);
3153 xkb_map_unref(display->xkb.keymap);
3154 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003155}
3156
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003157#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003158static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003159init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003160{
3161 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003162 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003163
Rob Clark6396ed32012-03-11 19:48:41 -05003164#ifdef USE_CAIRO_GLESV2
3165# define GL_BIT EGL_OPENGL_ES2_BIT
3166#else
3167# define GL_BIT EGL_OPENGL_BIT
3168#endif
3169
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003170 static const EGLint argb_cfg_attribs[] = {
3171 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003172 EGL_RED_SIZE, 1,
3173 EGL_GREEN_SIZE, 1,
3174 EGL_BLUE_SIZE, 1,
3175 EGL_ALPHA_SIZE, 1,
3176 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003177 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003178 EGL_NONE
3179 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003180
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003181#ifdef USE_CAIRO_GLESV2
3182 static const EGLint context_attribs[] = {
3183 EGL_CONTEXT_CLIENT_VERSION, 2,
3184 EGL_NONE
3185 };
3186 EGLint api = EGL_OPENGL_ES_API;
3187#else
3188 EGLint *context_attribs = NULL;
3189 EGLint api = EGL_OPENGL_API;
3190#endif
3191
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003192 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003193 if (!eglInitialize(d->dpy, &major, &minor)) {
3194 fprintf(stderr, "failed to initialize display\n");
3195 return -1;
3196 }
3197
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003198 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003199 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3200 return -1;
3201 }
3202
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003203 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3204 &d->argb_config, 1, &n) || n != 1) {
3205 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003206 return -1;
3207 }
3208
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003209 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003210 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003211 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003212 fprintf(stderr, "failed to create context\n");
3213 return -1;
3214 }
3215
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003216 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003217 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003218 return -1;
3219 }
3220
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003221#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003222 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3223 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3224 fprintf(stderr, "failed to get cairo egl argb device\n");
3225 return -1;
3226 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003227#endif
3228
3229 return 0;
3230}
3231
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003232static void
3233fini_egl(struct display *display)
3234{
3235#ifdef HAVE_CAIRO_EGL
3236 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003237#endif
3238
3239 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3240 EGL_NO_CONTEXT);
3241
3242 eglTerminate(display->dpy);
3243 eglReleaseThread();
3244}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003245#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003246
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003247static int
3248event_mask_update(uint32_t mask, void *data)
3249{
3250 struct display *d = data;
3251
3252 d->mask = mask;
3253
3254 return 0;
3255}
3256
3257static void
3258handle_display_data(struct task *task, uint32_t events)
3259{
3260 struct display *display =
3261 container_of(task, struct display, display_task);
3262
3263 wl_display_iterate(display->display, display->mask);
3264}
3265
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003266struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003267display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003268{
3269 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003270
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003271 argc = parse_options(xkb_options,
3272 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003273
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003274 d = malloc(sizeof *d);
3275 if (d == NULL)
3276 return NULL;
3277
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003278 memset(d, 0, sizeof *d);
3279
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003280 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003281 if (d->display == NULL) {
3282 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003283 return NULL;
3284 }
3285
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003286 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3287 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3288 d->display_task.run = handle_display_data;
3289 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3290
3291 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003292 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003293 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003294
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003295 /* Set up listener so we'll catch all events. */
3296 wl_display_add_global_listener(d->display,
3297 display_handle_global, d);
3298
3299 /* Process connection events. */
3300 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003301#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003302 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003303 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003304#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003305
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003306 d->image_target_texture_2d =
3307 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3308 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3309 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3310
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003311 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003312
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003313 display_render_frame(d);
3314
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003315 wl_list_init(&d->window_list);
3316
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003317 init_xkb(d);
3318
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003319 return d;
3320}
3321
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003322static void
3323display_destroy_outputs(struct display *display)
3324{
3325 struct output *tmp;
3326 struct output *output;
3327
3328 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3329 output_destroy(output);
3330}
3331
Pekka Paalanene1207c72011-12-16 12:02:09 +02003332static void
3333display_destroy_inputs(struct display *display)
3334{
3335 struct input *tmp;
3336 struct input *input;
3337
3338 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3339 input_destroy(input);
3340}
3341
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003342void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003343display_destroy(struct display *display)
3344{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003345 if (!wl_list_empty(&display->window_list))
3346 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3347
3348 if (!wl_list_empty(&display->deferred_list))
3349 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3350
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003351 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003352 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003353
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003354 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003355
3356 cairo_surface_destroy(display->active_frame);
3357 cairo_surface_destroy(display->inactive_frame);
3358 cairo_surface_destroy(display->shadow);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003359 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003360
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003361#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003362 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003363#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003364
Pekka Paalanenc2052982011-12-16 11:41:32 +02003365 if (display->shell)
3366 wl_shell_destroy(display->shell);
3367
3368 if (display->shm)
3369 wl_shm_destroy(display->shm);
3370
3371 if (display->data_device_manager)
3372 wl_data_device_manager_destroy(display->data_device_manager);
3373
3374 wl_compositor_destroy(display->compositor);
3375
3376 close(display->epoll_fd);
3377
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003378 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003379 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003380 free(display);
3381}
3382
3383void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003384display_set_user_data(struct display *display, void *data)
3385{
3386 display->user_data = data;
3387}
3388
3389void *
3390display_get_user_data(struct display *display)
3391{
3392 return display->user_data;
3393}
3394
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003395struct wl_display *
3396display_get_display(struct display *display)
3397{
3398 return display->display;
3399}
3400
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003401struct output *
3402display_get_output(struct display *display)
3403{
3404 return container_of(display->output_list.next, struct output, link);
3405}
3406
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003407struct wl_compositor *
3408display_get_compositor(struct display *display)
3409{
3410 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003411}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003412
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003413uint32_t
3414display_get_serial(struct display *display)
3415{
3416 return display->serial;
3417}
3418
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003419EGLDisplay
3420display_get_egl_display(struct display *d)
3421{
3422 return d->dpy;
3423}
3424
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003425struct wl_data_source *
3426display_create_data_source(struct display *display)
3427{
3428 return wl_data_device_manager_create_data_source(display->data_device_manager);
3429}
3430
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003431EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003432display_get_argb_egl_config(struct display *d)
3433{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003434 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003435}
3436
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003437struct wl_shell *
3438display_get_shell(struct display *display)
3439{
3440 return display->shell;
3441}
3442
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003443int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003444display_acquire_window_surface(struct display *display,
3445 struct window *window,
3446 EGLContext ctx)
3447{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003448#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003449 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003450 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003451
3452 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003453 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003454 device = cairo_surface_get_device(window->cairo_surface);
3455 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003456 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003457
Benjamin Franzke0c991632011-09-27 21:57:31 +02003458 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003459 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003460 ctx = display->argb_ctx;
3461 else
3462 assert(0);
3463 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003464
3465 data = cairo_surface_get_user_data(window->cairo_surface,
3466 &surface_data_key);
3467
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003468 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003469 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003470 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3471 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003472
3473 return 0;
3474#else
3475 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003476#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003477}
3478
3479void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003480display_release_window_surface(struct display *display,
3481 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003482{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003483#ifdef HAVE_CAIRO_EGL
3484 cairo_device_t *device;
3485
3486 device = cairo_surface_get_device(window->cairo_surface);
3487 if (!device)
3488 return;
3489
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003490 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003491 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003492 cairo_device_release(device);
3493#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003494}
3495
3496void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003497display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003498{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003499 wl_list_insert(&display->deferred_list, &task->link);
3500}
3501
3502void
3503display_watch_fd(struct display *display,
3504 int fd, uint32_t events, struct task *task)
3505{
3506 struct epoll_event ep;
3507
3508 ep.events = events;
3509 ep.data.ptr = task;
3510 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3511}
3512
3513void
3514display_run(struct display *display)
3515{
3516 struct task *task;
3517 struct epoll_event ep[16];
3518 int i, count;
3519
Pekka Paalanen826d7952011-12-15 10:14:07 +02003520 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003521 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003522 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003523
3524 while (!wl_list_empty(&display->deferred_list)) {
3525 task = container_of(display->deferred_list.next,
3526 struct task, link);
3527 wl_list_remove(&task->link);
3528 task->run(task, 0);
3529 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003530
3531 if (!display->running)
3532 break;
3533
3534 wl_display_flush(display->display);
3535
3536 count = epoll_wait(display->epoll_fd,
3537 ep, ARRAY_LENGTH(ep), -1);
3538 for (i = 0; i < count; i++) {
3539 task = ep[i].data.ptr;
3540 task->run(task, ep[i].events);
3541 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003542 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003543}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003544
3545void
3546display_exit(struct display *display)
3547{
3548 display->running = 0;
3549}