blob: 5eca1f4c981dbf87de5fc6d77f00c11604baeebd [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};
Rob Bradford7507b572012-05-15 17:55:34 +0100127
128struct window_output {
129 struct output *output;
130 struct wl_list link;
131};
132
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133struct window {
134 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500135 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100136 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500137 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200138 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500139 struct wl_region *input_region;
140 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500141 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500142 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500143 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500144 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400145 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400146 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400147 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400148 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400149 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400150 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400151 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400152 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400153 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500154
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400155 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500156
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700157 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400158
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500159 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500160 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400161 window_data_handler_t data_handler;
162 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500163 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400164
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200165 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500166 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500167
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500168 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400169 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500170};
171
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500172struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500173 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500174 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400175 struct wl_list link;
176 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500177 widget_resize_handler_t resize_handler;
178 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500179 widget_enter_handler_t enter_handler;
180 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500181 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500182 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400183 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500184 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400185};
186
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400187struct input {
188 struct display *display;
189 struct wl_input_device *input_device;
190 struct window *pointer_focus;
191 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300192 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400194 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400195 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400196 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400197
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500198 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500199 struct widget *grab;
200 uint32_t grab_button;
201
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400202 struct wl_data_device *data_device;
203 struct data_offer *drag_offer;
204 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400205};
206
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500207struct output {
208 struct display *display;
209 struct wl_output *output;
210 struct rectangle allocation;
211 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200212
213 display_output_handler_t destroy_handler;
214 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500215};
216
Martin Minarik1998b152012-05-10 02:04:35 +0200217enum frame_button_action {
218 FRAME_BUTTON_NULL = 0,
219 FRAME_BUTTON_ICON = 1,
220 FRAME_BUTTON_CLOSE = 2,
221 FRAME_BUTTON_MINIMIZE = 3,
222 FRAME_BUTTON_MAXIMIZE = 4,
223};
224
225enum frame_button_pointer {
226 FRAME_BUTTON_DEFAULT = 0,
227 FRAME_BUTTON_OVER = 1,
228 FRAME_BUTTON_ACTIVE = 2,
229};
230
231enum frame_button_align {
232 FRAME_BUTTON_RIGHT = 0,
233 FRAME_BUTTON_LEFT = 1,
234};
235
236enum frame_button_decoration {
237 FRAME_BUTTON_NONE = 0,
238 FRAME_BUTTON_FANCY = 1,
239};
240
241struct frame_button {
242 struct widget *widget;
243 struct frame *frame;
244 cairo_surface_t *icon;
245 enum frame_button_action type;
246 enum frame_button_pointer state;
247 struct wl_list link; /* buttons_list */
248 enum frame_button_align align;
249 enum frame_button_decoration decoration;
250};
251
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500252struct frame {
253 struct widget *widget;
254 struct widget *child;
255 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400256 int width;
257 int titlebar_height;
Martin Minarik1998b152012-05-10 02:04:35 +0200258 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500259};
260
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500261struct menu {
262 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500263 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500264 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500265 const char **entries;
266 uint32_t time;
267 int current;
268 int count;
269 menu_func_t func;
270};
271
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300272struct cursor_image {
273 cairo_surface_t *surface;
274 int width, height;
275 int hotspot_x, hotspot_y;
276 int delay;
277};
278
279struct cursor {
280 int n_images;
281 struct cursor_image *images;
282};
283
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700284struct shm_pool {
285 struct wl_shm_pool *pool;
286 size_t size;
287 size_t used;
288 void *data;
289};
290
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400291enum {
292 POINTER_DEFAULT = 100,
293 POINTER_UNSET
294};
295
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500296enum window_location {
297 WINDOW_INTERIOR = 0,
298 WINDOW_RESIZING_TOP = 1,
299 WINDOW_RESIZING_BOTTOM = 2,
300 WINDOW_RESIZING_LEFT = 4,
301 WINDOW_RESIZING_TOP_LEFT = 5,
302 WINDOW_RESIZING_BOTTOM_LEFT = 6,
303 WINDOW_RESIZING_RIGHT = 8,
304 WINDOW_RESIZING_TOP_RIGHT = 9,
305 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
306 WINDOW_RESIZING_MASK = 15,
307 WINDOW_EXTERIOR = 16,
308 WINDOW_TITLEBAR = 17,
309 WINDOW_CLIENT_AREA = 18,
310};
311
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400312const char *option_xkb_layout = "us";
313const char *option_xkb_variant = "";
314const char *option_xkb_options = "";
315
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400316static const struct weston_option xkb_options[] = {
317 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
318 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
319 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400320};
321
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400322static const cairo_user_data_key_t surface_data_key;
323struct surface_data {
324 struct wl_buffer *buffer;
325};
326
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500327#define MULT(_d,c,a,t) \
328 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
329
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500330#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400331
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100332struct egl_window_surface_data {
333 struct display *display;
334 struct wl_surface *surface;
335 struct wl_egl_window *window;
336 EGLSurface surf;
337};
338
339static void
340egl_window_surface_data_destroy(void *p)
341{
342 struct egl_window_surface_data *data = p;
343 struct display *d = data->display;
344
345 eglDestroySurface(d->dpy, data->surf);
346 wl_egl_window_destroy(data->window);
347 data->surface = NULL;
348
349 free(p);
350}
351
352static cairo_surface_t *
353display_create_egl_window_surface(struct display *display,
354 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400355 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100356 struct rectangle *rectangle)
357{
358 cairo_surface_t *cairo_surface;
359 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400360 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200361 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100362
363 data = malloc(sizeof *data);
364 if (data == NULL)
365 return NULL;
366
367 data->display = display;
368 data->surface = surface;
369
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500370 config = display->argb_config;
371 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400372
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400373 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100374 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400375 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100376
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500378 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100379
Benjamin Franzke0c991632011-09-27 21:57:31 +0200380 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100381 data->surf,
382 rectangle->width,
383 rectangle->height);
384
385 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
386 data, egl_window_surface_data_destroy);
387
388 return cairo_surface;
389}
390
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391#endif
392
393struct wl_buffer *
394display_get_buffer_for_surface(struct display *display,
395 cairo_surface_t *surface)
396{
397 struct surface_data *data;
398
399 data = cairo_surface_get_user_data (surface, &surface_data_key);
400
401 return data->buffer;
402}
403
404struct shm_surface_data {
405 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700406 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400407};
408
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500409static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700410shm_pool_destroy(struct shm_pool *pool);
411
412static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400413shm_surface_data_destroy(void *p)
414{
415 struct shm_surface_data *data = p;
416
417 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700418 if (data->pool)
419 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420}
421
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300422static void
423shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
424{
425 void *dest = cairo_image_surface_get_data(surface);
426
427 memcpy(dest, data, count);
428}
429
Kristian Høgsberg16626282012-04-03 11:21:27 -0400430static struct wl_shm_pool *
431make_shm_pool(struct display *display, int size, void **data)
432{
433 char filename[] = "/tmp/wayland-shm-XXXXXX";
434 struct wl_shm_pool *pool;
435 int fd;
436
437 fd = mkstemp(filename);
438 if (fd < 0) {
439 fprintf(stderr, "open %s failed: %m\n", filename);
440 return NULL;
441 }
442 if (ftruncate(fd, size) < 0) {
443 fprintf(stderr, "ftruncate failed: %m\n");
444 close(fd);
445 return NULL;
446 }
447
448 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
449 unlink(filename);
450
451 if (*data == MAP_FAILED) {
452 fprintf(stderr, "mmap failed: %m\n");
453 close(fd);
454 return NULL;
455 }
456
457 pool = wl_shm_create_pool(display->shm, fd, size);
458
459 close(fd);
460
461 return pool;
462}
463
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700464static struct shm_pool *
465shm_pool_create(struct display *display, size_t size)
466{
467 struct shm_pool *pool = malloc(sizeof *pool);
468
469 if (!pool)
470 return NULL;
471
472 pool->pool = make_shm_pool(display, size, &pool->data);
473 if (!pool->pool) {
474 free(pool);
475 return NULL;
476 }
477
478 pool->size = size;
479 pool->used = 0;
480
481 return pool;
482}
483
484static void *
485shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
486{
487 if (pool->used + size > pool->size)
488 return NULL;
489
490 *offset = pool->used;
491 pool->used += size;
492
493 return (char *) pool->data + *offset;
494}
495
496/* destroy the pool. this does not unmap the memory though */
497static void
498shm_pool_destroy(struct shm_pool *pool)
499{
500 munmap(pool->data, pool->size);
501 wl_shm_pool_destroy(pool->pool);
502 free(pool);
503}
504
505/* Start allocating from the beginning of the pool again */
506static void
507shm_pool_reset(struct shm_pool *pool)
508{
509 pool->used = 0;
510}
511
512static int
513data_length_for_shm_surface(struct rectangle *rect)
514{
515 int stride;
516
517 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
518 rect->width);
519 return stride * rect->height;
520}
521
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500522static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700523display_create_shm_surface_from_pool(struct display *display,
524 struct rectangle *rectangle,
525 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526{
527 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400528 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400529 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700530 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400531 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400532
533 data = malloc(sizeof *data);
534 if (data == NULL)
535 return NULL;
536
537 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
538 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700539 length = stride * rectangle->height;
540 data->pool = NULL;
541 map = shm_pool_allocate(pool, length, &offset);
542
543 if (!map) {
544 free(data);
545 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400546 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400547
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400548 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549 CAIRO_FORMAT_ARGB32,
550 rectangle->width,
551 rectangle->height,
552 stride);
553
554 cairo_surface_set_user_data (surface, &surface_data_key,
555 data, shm_surface_data_destroy);
556
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400557 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500558 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400559 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500560 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400561
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700562 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400563 rectangle->width,
564 rectangle->height,
565 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700567 return surface;
568}
569
570static cairo_surface_t *
571display_create_shm_surface(struct display *display,
572 struct rectangle *rectangle, uint32_t flags,
573 struct window *window)
574{
575 struct shm_surface_data *data;
576 struct shm_pool *pool;
577 cairo_surface_t *surface;
578
579 if (window && window->pool) {
580 shm_pool_reset(window->pool);
581 surface = display_create_shm_surface_from_pool(display,
582 rectangle,
583 flags,
584 window->pool);
585 if (surface)
586 return surface;
587 }
588
589 pool = shm_pool_create(display,
590 data_length_for_shm_surface(rectangle));
591 if (!pool)
592 return NULL;
593
594 surface =
595 display_create_shm_surface_from_pool(display, rectangle,
596 flags, pool);
597
598 if (!surface) {
599 shm_pool_destroy(pool);
600 return NULL;
601 }
602
603 /* make sure we destroy the pool when the surface is destroyed */
604 data = cairo_surface_get_user_data(surface, &surface_data_key);
605 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606
607 return surface;
608}
609
nobled7b87cb02011-02-01 18:51:47 +0000610static int
611check_size(struct rectangle *rect)
612{
613 if (rect->width && rect->height)
614 return 0;
615
616 fprintf(stderr, "tried to create surface of "
617 "width: %d, height: %d\n", rect->width, rect->height);
618 return -1;
619}
620
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400621cairo_surface_t *
622display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100623 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400624 struct rectangle *rectangle,
625 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626{
nobled7b87cb02011-02-01 18:51:47 +0000627 if (check_size(rectangle) < 0)
628 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500629#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400630 if (display->dpy)
631 return display_create_egl_window_surface(display,
632 surface,
633 flags,
634 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400636 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637}
638
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300639static const char *cursors[] = {
640 "bottom_left_corner",
641 "bottom_right_corner",
642 "bottom_side",
643 "grabbing",
644 "left_ptr",
645 "left_side",
646 "right_side",
647 "top_left_corner",
648 "top_right_corner",
649 "top_side",
650 "xterm",
651 "hand1",
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400652};
653
654static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300655create_cursor_from_images(struct display *display, struct cursor *cursor,
656 XcursorImages *images)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400657{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300658 int i;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400659 struct rectangle rect;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300660 XcursorImage *image;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400661
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300662 cursor->images = malloc(images->nimage * sizeof *cursor->images);
663 cursor->n_images = images->nimage;
664
665 for (i = 0; i < images->nimage; i++) {
666 image = images->images[i];
667
668 rect.width = image->width;
669 rect.height = image->height;
670
671 cursor->images[i].surface =
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700672 display_create_shm_surface_from_pool(display, &rect, 0,
673 display->cursor_shm_pool);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300674
675 shm_surface_write(cursor->images[i].surface,
676 (unsigned char *) image->pixels,
677 image->width * image->height * sizeof image->pixels[0]);
678
679 cursor->images[i].width = image->width;
680 cursor->images[i].height = image->height;
681 cursor->images[i].hotspot_x = image->xhot;
682 cursor->images[i].hotspot_y = image->yhot;
683 cursor->images[i].delay = image->delay;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400684 }
685
686}
687
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700688static size_t
689data_length_for_cursor_images(XcursorImages *images)
690{
691 int i;
692 size_t length = 0;
693 struct rectangle rect;
694
695 for (i = 0; i < images->nimage; i++) {
696 rect.width = images->images[i]->width;
697 rect.height = images->images[i]->height;
698 length += data_length_for_shm_surface(&rect);
699 }
700
701 return length;
702}
703
Pekka Paalanen325bb602011-12-19 10:31:45 +0200704static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300705create_cursors(struct display *display)
706{
707 int i, count;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700708 size_t pool_size = 0;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300709 struct cursor *cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700710 XcursorImages **images;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300711
712 count = ARRAY_LENGTH(cursors);
713 display->cursors = malloc(count * sizeof *display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700714 images = malloc(count * sizeof images[0]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300715
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700716 for (i = 0; i < count; i++) {
717 images[i] = XcursorLibraryLoadImages(cursors[i], NULL, 32);
Rafal Mielniczuk87e4c932012-05-08 22:10:44 +0200718 if (!images[i]) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300719 fprintf(stderr, "Error loading cursor: %s\n",
720 cursors[i]);
721 continue;
722 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723 pool_size += data_length_for_cursor_images(images[i]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300724 }
725
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700726 display->cursor_shm_pool = shm_pool_create(display, pool_size);
727
728 for (i = 0; i < count; i++) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700729 cursor = &display->cursors[i];
Dima Ryazanovff1c2d72012-05-08 21:02:33 -0700730
731 if (!images[i]) {
732 cursor->n_images = 0;
733 cursor->images = NULL;
734 continue;
735 }
736
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700737 create_cursor_from_images(display, cursor, images[i]);
738
739 XcursorImagesDestroy(images[i]);
740 }
741
742 free(images);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300743}
744
745static void
746destroy_cursor_images(struct cursor *cursor)
747{
748 int i;
749
750 for (i = 0; i < cursor->n_images; i++)
751 if (cursor->images[i].surface)
752 cairo_surface_destroy(cursor->images[i].surface);
753
754 free(cursor->images);
755}
756
757static void
758destroy_cursors(struct display *display)
Pekka Paalanen325bb602011-12-19 10:31:45 +0200759{
760 int i, count;
761
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300762 count = ARRAY_LENGTH(cursors);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200763 for (i = 0; i < count; ++i) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300764 destroy_cursor_images(&display->cursors[i]);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200765 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700766
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300767 free(display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700768 shm_pool_destroy(display->cursor_shm_pool);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200769}
770
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400771cairo_surface_t *
772display_get_pointer_surface(struct display *display, int pointer,
773 int *width, int *height,
774 int *hotspot_x, int *hotspot_y)
775{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300776 struct cursor *cursor = &display->cursors[pointer];
777 cairo_surface_t *surface = cursor->images[0].surface;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400778
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300779 /* FIXME returning information for the first image. Something better
780 * is needed for animated cursors */
781
nobledf8475c92011-01-05 17:41:55 +0000782 *width = cairo_image_surface_get_width(surface);
783 *height = cairo_image_surface_get_height(surface);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300784
785 *hotspot_x = cursor->images[0].hotspot_x;
786 *hotspot_y = cursor->images[0].hotspot_y;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400787
788 return cairo_surface_reference(surface);
789}
790
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400791static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200792window_get_resize_dx_dy(struct window *window, int *x, int *y)
793{
794 if (window->resize_edges & WINDOW_RESIZING_LEFT)
795 *x = window->server_allocation.width - window->allocation.width;
796 else
797 *x = 0;
798
799 if (window->resize_edges & WINDOW_RESIZING_TOP)
800 *y = window->server_allocation.height -
801 window->allocation.height;
802 else
803 *y = 0;
804
805 window->resize_edges = 0;
806}
807
808static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500809window_attach_surface(struct window *window)
810{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400811 struct display *display = window->display;
812 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000813#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000815#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500816 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400818 if (window->type == TYPE_NONE) {
819 window->type = TYPE_TOPLEVEL;
820 if (display->shell)
821 wl_shell_surface_set_toplevel(window->shell_surface);
822 }
823
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000825#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
827 data = cairo_surface_get_user_data(window->cairo_surface,
828 &surface_data_key);
829
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830 cairo_gl_surface_swapbuffers(window->cairo_surface);
831 wl_egl_window_get_attached_size(data->window,
832 &window->server_allocation.width,
833 &window->server_allocation.height);
834 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000835#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100836 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 buffer =
838 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400839 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100840
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200841 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100842 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400843 wl_surface_damage(window->surface, 0, 0,
844 window->allocation.width,
845 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400847 cairo_surface_destroy(window->cairo_surface);
848 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000850 default:
851 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100852 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500853
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500854 if (window->input_region) {
855 wl_surface_set_input_region(window->surface,
856 window->input_region);
857 wl_region_destroy(window->input_region);
858 window->input_region = NULL;
859 }
860
861 if (window->opaque_region) {
862 wl_surface_set_opaque_region(window->surface,
863 window->opaque_region);
864 wl_region_destroy(window->opaque_region);
865 window->opaque_region = NULL;
866 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500867}
868
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500869void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400870window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500871{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400872 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100873 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500874}
875
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400876void
877window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400878{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500879 cairo_surface_reference(surface);
880
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400881 if (window->cairo_surface != NULL)
882 cairo_surface_destroy(window->cairo_surface);
883
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500884 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400885}
886
Benjamin Franzke22d54812011-07-16 19:50:32 +0000887#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100888static void
889window_resize_cairo_window_surface(struct window *window)
890{
891 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200892 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100893
894 data = cairo_surface_get_user_data(window->cairo_surface,
895 &surface_data_key);
896
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200897 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100898 wl_egl_window_resize(data->window,
899 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200900 window->allocation.height,
901 x,y);
902
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100903 cairo_gl_surface_set_size(window->cairo_surface,
904 window->allocation.width,
905 window->allocation.height);
906}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000907#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100908
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400909struct display *
910window_get_display(struct window *window)
911{
912 return window->display;
913}
914
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400915void
916window_create_surface(struct window *window)
917{
918 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400919 uint32_t flags = 0;
920
921 if (!window->transparent)
922 flags = SURFACE_OPAQUE;
923
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400924 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500925#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100926 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
927 if (window->cairo_surface) {
928 window_resize_cairo_window_surface(window);
929 return;
930 }
931 surface = display_create_surface(window->display,
932 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400933 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100934 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400935#endif
936 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400937 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400938 &window->allocation,
939 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400940 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800941 default:
942 surface = NULL;
943 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400944 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400945
946 window_set_surface(window, surface);
947 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400948}
949
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200950static void frame_destroy(struct frame *frame);
951
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400952void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500953window_destroy(struct window *window)
954{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200955 struct display *display = window->display;
956 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100957 struct window_output *window_output;
958 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200959
960 if (window->redraw_scheduled)
961 wl_list_remove(&window->redraw_task.link);
962
963 wl_list_for_each(input, &display->input_list, link) {
964 if (input->pointer_focus == window)
965 input->pointer_focus = NULL;
966 if (input->keyboard_focus == window)
967 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500968 if (input->focus_widget &&
969 input->focus_widget->window == window)
970 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200971 }
972
Rob Bradford7507b572012-05-15 17:55:34 +0100973 wl_list_for_each_safe(window_output, window_output_tmp,
974 &window->window_output_list, link) {
975 free (window_output);
976 }
977
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500978 if (window->input_region)
979 wl_region_destroy(window->input_region);
980 if (window->opaque_region)
981 wl_region_destroy(window->opaque_region);
982
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200983 if (window->frame)
984 frame_destroy(window->frame);
985
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200986 if (window->shell_surface)
987 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500988 wl_surface_destroy(window->surface);
989 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200990
991 if (window->cairo_surface != NULL)
992 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200993
994 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500995 free(window);
996}
997
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500998static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500999widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001000{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001001 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001002
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001003 wl_list_for_each(child, &widget->child_list, link) {
1004 target = widget_find_widget(child, x, y);
1005 if (target)
1006 return target;
1007 }
1008
1009 if (widget->allocation.x <= x &&
1010 x < widget->allocation.x + widget->allocation.width &&
1011 widget->allocation.y <= y &&
1012 y < widget->allocation.y + widget->allocation.height) {
1013 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001014 }
1015
1016 return NULL;
1017}
1018
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001019static struct widget *
1020widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001021{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001022 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001023
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001024 widget = malloc(sizeof *widget);
1025 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001026 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001027 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001028 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001029 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001030 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001031
1032 return widget;
1033}
1034
1035struct widget *
1036window_add_widget(struct window *window, void *data)
1037{
1038 window->widget = widget_create(window, data);
1039 wl_list_init(&window->widget->link);
1040
1041 return window->widget;
1042}
1043
1044struct widget *
1045widget_add_widget(struct widget *parent, void *data)
1046{
1047 struct widget *widget;
1048
1049 widget = widget_create(parent->window, data);
1050 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001051
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001052 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001053}
1054
1055void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001056widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001058 struct display *display = widget->window->display;
1059 struct input *input;
1060
1061 wl_list_for_each(input, &display->input_list, link) {
1062 if (input->focus_widget == widget)
1063 input->focus_widget = NULL;
1064 }
1065
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001066 wl_list_remove(&widget->link);
1067 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001068}
1069
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001070void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001071widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001072{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001073 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001074}
1075
1076void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001077widget_set_size(struct widget *widget, int32_t width, int32_t height)
1078{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001079 widget->allocation.width = width;
1080 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001081}
1082
1083void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001084widget_set_allocation(struct widget *widget,
1085 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001086{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001087 widget->allocation.x = x;
1088 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001089 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001090}
1091
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001092void
1093widget_set_transparent(struct widget *widget, int transparent)
1094{
1095 widget->opaque = !transparent;
1096}
1097
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001098void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001099widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001100{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001101 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001102}
1103
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001104void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001105widget_set_resize_handler(struct widget *widget,
1106 widget_resize_handler_t handler)
1107{
1108 widget->resize_handler = handler;
1109}
1110
1111void
1112widget_set_redraw_handler(struct widget *widget,
1113 widget_redraw_handler_t handler)
1114{
1115 widget->redraw_handler = handler;
1116}
1117
1118void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001119widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001120{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001121 widget->enter_handler = handler;
1122}
1123
1124void
1125widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1126{
1127 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001128}
1129
1130void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001131widget_set_motion_handler(struct widget *widget,
1132 widget_motion_handler_t handler)
1133{
1134 widget->motion_handler = handler;
1135}
1136
1137void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001138widget_set_button_handler(struct widget *widget,
1139 widget_button_handler_t handler)
1140{
1141 widget->button_handler = handler;
1142}
1143
1144void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001145widget_schedule_redraw(struct widget *widget)
1146{
1147 window_schedule_redraw(widget->window);
1148}
1149
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001150cairo_surface_t *
1151window_get_surface(struct window *window)
1152{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001153 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001154}
1155
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001156struct wl_surface *
1157window_get_wl_surface(struct window *window)
1158{
1159 return window->surface;
1160}
1161
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001162struct wl_shell_surface *
1163window_get_wl_shell_surface(struct window *window)
1164{
1165 return window->shell_surface;
1166}
1167
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001168static void
1169frame_resize_handler(struct widget *widget,
1170 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001171{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001172 struct frame *frame = data;
1173 struct widget *child = frame->child;
1174 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001175 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001176 struct frame_button * button;
1177 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001178 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001179 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001180
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001181 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001182 decoration_width = (frame->width + frame->margin) * 2;
1183 decoration_height = frame->width +
1184 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001185
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001186 allocation.x = frame->width + frame->margin;
1187 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001188 allocation.width = width - decoration_width;
1189 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001190
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001191 opaque_margin = frame->margin + display->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001192
1193 wl_list_for_each(button, &frame->buttons_list, link)
1194 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001195 } else {
1196 decoration_width = 0;
1197 decoration_height = 0;
1198
1199 allocation.x = 0;
1200 allocation.y = 0;
1201 allocation.width = width;
1202 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001203 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001204
1205 wl_list_for_each(button, &frame->buttons_list, link)
1206 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001207 }
1208
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001209 widget_set_allocation(child, allocation.x, allocation.y,
1210 allocation.width, allocation.height);
1211
1212 if (child->resize_handler)
1213 child->resize_handler(child,
1214 allocation.width,
1215 allocation.height,
1216 child->user_data);
1217
Scott Moreauf7e498c2012-05-14 11:39:29 -06001218 width = child->allocation.width + decoration_width;
1219 height = child->allocation.height + decoration_height;
1220
1221 widget->window->input_region =
1222 wl_compositor_create_region(display->compositor);
1223 wl_region_add(widget->window->input_region,
1224 frame->margin, frame->margin,
1225 width - 2 * frame->margin,
1226 height - 2 * frame->margin);
1227
1228 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001229
1230 if (child->opaque) {
1231 widget->window->opaque_region =
1232 wl_compositor_create_region(display->compositor);
1233 wl_region_add(widget->window->opaque_region,
1234 opaque_margin, opaque_margin,
1235 widget->allocation.width - 2 * opaque_margin,
1236 widget->allocation.height - 2 * opaque_margin);
1237 }
Martin Minarik1998b152012-05-10 02:04:35 +02001238
1239 /* frame internal buttons */
1240 x_r = frame->widget->allocation.width - frame->width - frame->margin;
1241 x_l = frame->width + frame->margin;
1242 y = frame->width + frame->margin;
1243 wl_list_for_each(button, &frame->buttons_list, link) {
1244 const int button_padding = 4;
1245 w = cairo_image_surface_get_width(button->icon);
1246 h = cairo_image_surface_get_height(button->icon);
1247
1248 if (button->decoration == FRAME_BUTTON_FANCY)
1249 w += 10;
1250
1251 if (button->align == FRAME_BUTTON_LEFT) {
1252 widget_set_allocation(button->widget,
1253 x_l, y , w + 1, h + 1);
1254 x_l += w;
1255 x_l += button_padding;
1256 } else {
1257 x_r -= w;
1258 widget_set_allocation(button->widget,
1259 x_r, y , w + 1, h + 1);
1260 x_r -= button_padding;
1261 }
1262 }
1263}
1264
1265static int
1266frame_button_enter_handler(struct widget *widget,
1267 struct input *input, float x, float y, void *data)
1268{
1269 struct frame_button *frame_button = data;
1270
1271 widget_schedule_redraw(frame_button->widget);
1272 frame_button->state = FRAME_BUTTON_OVER;
1273
1274 return POINTER_LEFT_PTR;
1275}
1276
1277static void
1278frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1279{
1280 struct frame_button *frame_button = data;
1281
1282 widget_schedule_redraw(frame_button->widget);
1283 frame_button->state = FRAME_BUTTON_DEFAULT;
1284}
1285
1286static void
1287frame_button_button_handler(struct widget *widget,
1288 struct input *input, uint32_t time,
1289 uint32_t button, uint32_t state, void *data)
1290{
1291 struct frame_button *frame_button = data;
1292 struct window *window = widget->window;
1293
1294 if (button != BTN_LEFT)
1295 return;
1296
1297 switch (state) {
1298 case 1:
1299 frame_button->state = FRAME_BUTTON_ACTIVE;
1300 widget_schedule_redraw(frame_button->widget);
1301
1302 if (frame_button->type == FRAME_BUTTON_ICON)
1303 window_show_frame_menu(window, input, time);
1304 return;
1305 case 0:
1306 frame_button->state = FRAME_BUTTON_DEFAULT;
1307 widget_schedule_redraw(frame_button->widget);
1308 break;
1309 }
1310
1311 switch (frame_button->type) {
1312 case FRAME_BUTTON_CLOSE:
1313 if (window->close_handler)
1314 window->close_handler(window->parent,
1315 window->user_data);
1316 else
1317 display_exit(window->display);
1318 break;
1319 case FRAME_BUTTON_MINIMIZE:
1320 fprintf(stderr,"Minimize stub\n");
1321 break;
1322 case FRAME_BUTTON_MAXIMIZE:
1323 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1324 break;
1325 default:
1326 /* Unknown operation */
1327 break;
1328 }
1329}
1330
1331static void
1332frame_button_redraw_handler(struct widget *widget, void *data)
1333{
1334 struct frame_button *frame_button = data;
1335 cairo_t *cr;
1336 int width, height, x, y;
1337 struct window *window = widget->window;
1338
1339 x = widget->allocation.x;
1340 y = widget->allocation.y;
1341 width = widget->allocation.width;
1342 height = widget->allocation.height;
1343
1344 if (!width)
1345 return;
1346 if (!height)
1347 return;
1348 if (widget->opaque)
1349 return;
1350
1351 cr = cairo_create(window->cairo_surface);
1352
1353 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1354 cairo_set_line_width(cr, 1);
1355
1356 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1357 cairo_rectangle (cr, x, y, 25, 16);
1358
1359 cairo_stroke_preserve(cr);
1360
1361 switch (frame_button->state) {
1362 case FRAME_BUTTON_DEFAULT:
1363 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1364 break;
1365 case FRAME_BUTTON_OVER:
1366 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1367 break;
1368 case FRAME_BUTTON_ACTIVE:
1369 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1370 break;
1371 }
1372
1373 cairo_fill (cr);
1374
1375 x += 4;
1376 }
1377
1378 cairo_set_source_surface(cr, frame_button->icon, x, y);
1379 cairo_paint(cr);
1380
1381 cairo_destroy(cr);
1382}
1383
1384static struct widget *
1385frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1386 enum frame_button_align align, enum frame_button_decoration style)
1387{
1388 struct frame_button *frame_button;
1389 const char *icon = data;
1390
1391 frame_button = malloc (sizeof *frame_button);
1392 memset(frame_button, 0, sizeof *frame_button);
1393
1394 frame_button->icon = cairo_image_surface_create_from_png(icon);
1395 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1396 frame_button->frame = frame;
1397 frame_button->type = type;
1398 frame_button->align = align;
1399 frame_button->decoration = style;
1400
1401 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1402
1403 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1404 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1405 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1406 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1407 return frame_button->widget;
1408}
1409
1410static void
1411frame_button_destroy(struct frame_button *frame_button)
1412{
1413 widget_destroy(frame_button->widget);
1414 wl_list_remove(&frame_button->link);
1415 cairo_surface_destroy(frame_button->icon);
1416 free(frame_button);
1417
1418 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001419}
1420
1421static void
1422frame_redraw_handler(struct widget *widget, void *data)
1423{
1424 struct frame *frame = data;
1425 cairo_t *cr;
1426 cairo_text_extents_t extents;
1427 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001428 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001429 struct window *window = widget->window;
1430
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001431 if (window->type == TYPE_FULLSCREEN)
1432 return;
1433
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001434 width = widget->allocation.width;
1435 height = widget->allocation.height;
1436
1437 cr = cairo_create(window->cairo_surface);
1438
1439 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1440 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1441 cairo_paint(cr);
1442
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001443 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001444 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001445 2, 2, width + 8, height + 8,
1446 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001447
1448 if (window->keyboard_device)
1449 source = window->display->active_frame;
1450 else
1451 source = window->display->inactive_frame;
1452
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001453 tile_source(cr, source,
1454 frame->margin, frame->margin,
1455 width - frame->margin * 2, height - frame->margin * 2,
1456 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001457
1458 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001459 cairo_select_font_face(cr, "sans",
1460 CAIRO_FONT_SLANT_NORMAL,
1461 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001462 cairo_set_font_size(cr, 14);
1463 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001464 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001465 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001466 if (window->keyboard_device) {
1467 cairo_move_to(cr, x + 1, y + 1);
1468 cairo_set_source_rgb(cr, 1, 1, 1);
1469 cairo_show_text(cr, window->title);
1470 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001471 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001472 cairo_show_text(cr, window->title);
1473 } else {
1474 cairo_move_to(cr, x, y);
1475 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1476 cairo_show_text(cr, window->title);
1477 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001478
1479 cairo_destroy(cr);
1480}
1481
1482static int
1483frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1484{
1485 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001486 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001487 const int grip_size = 8;
1488
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001489 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001490 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001491 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001492 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001493 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001494 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001496 hlocation = WINDOW_RESIZING_RIGHT;
1497 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001498 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001499
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001500 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001501 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001502 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001503 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001504 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001505 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001506 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001507 vlocation = WINDOW_RESIZING_BOTTOM;
1508 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001509 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001510
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001511 location = vlocation | hlocation;
1512 if (location & WINDOW_EXTERIOR)
1513 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001514 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001515 location = WINDOW_TITLEBAR;
1516 else if (location == WINDOW_INTERIOR)
1517 location = WINDOW_CLIENT_AREA;
1518
1519 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001520}
1521
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001522static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001523frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001524{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001525 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001526
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001527 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001528 switch (location) {
1529 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001530 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001531 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001532 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001533 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001534 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001535 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001536 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001537 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001538 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001539 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001540 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001541 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001542 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001543 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001544 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001545 case WINDOW_EXTERIOR:
1546 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001547 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001548 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001549 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001550}
1551
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001552static void
1553frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001554{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001555 switch (index) {
1556 case 0: /* close */
1557 if (window->close_handler)
1558 window->close_handler(window->parent,
1559 window->user_data);
1560 else
1561 display_exit(window->display);
1562 break;
1563 case 1: /* fullscreen */
1564 /* we don't have a way to get out of fullscreen for now */
1565 window_set_fullscreen(window, 1);
1566 break;
1567 case 2: /* rotate */
1568 case 3: /* scale */
1569 break;
1570 }
1571}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001572
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001573void
1574window_show_frame_menu(struct window *window,
1575 struct input *input, uint32_t time)
1576{
1577 int32_t x, y;
1578
1579 static const char *entries[] = {
1580 "Close", "Fullscreen", "Rotate", "Scale"
1581 };
1582
1583 input_get_position(input, &x, &y);
1584 window_show_menu(window->display, input, time, window,
1585 x - 10, y - 10, frame_menu_func, entries, 4);
1586}
1587
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001588static int
1589frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001590 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001591{
1592 return frame_get_pointer_image_for_location(data, input);
1593}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001594
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001595static int
1596frame_motion_handler(struct widget *widget,
1597 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001598 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001599{
1600 return frame_get_pointer_image_for_location(data, input);
1601}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001602
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001604frame_button_handler(struct widget *widget,
1605 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001606 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001607
1608{
1609 struct frame *frame = data;
1610 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001611 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001612 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001613
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614 location = frame_get_pointer_location(frame, input->sx, input->sy);
1615
1616 if (window->display->shell && button == BTN_LEFT && state == 1) {
1617 switch (location) {
1618 case WINDOW_TITLEBAR:
1619 if (!window->shell_surface)
1620 break;
1621 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001622 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001623 wl_shell_surface_move(window->shell_surface,
1624 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001625 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001626 break;
1627 case WINDOW_RESIZING_TOP:
1628 case WINDOW_RESIZING_BOTTOM:
1629 case WINDOW_RESIZING_LEFT:
1630 case WINDOW_RESIZING_RIGHT:
1631 case WINDOW_RESIZING_TOP_LEFT:
1632 case WINDOW_RESIZING_TOP_RIGHT:
1633 case WINDOW_RESIZING_BOTTOM_LEFT:
1634 case WINDOW_RESIZING_BOTTOM_RIGHT:
1635 if (!window->shell_surface)
1636 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001637 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001638
1639 if (!display->dpy) {
1640 /* If we're using shm, allocate a big
1641 pool to create buffers out of while
1642 we resize. We should probably base
1643 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001644 window->pool =
1645 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001646 }
1647
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001648 wl_shell_surface_resize(window->shell_surface,
1649 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001650 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001651 break;
1652 }
1653 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001654 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001655 }
1656}
1657
1658struct widget *
1659frame_create(struct window *window, void *data)
1660{
1661 struct frame *frame;
1662
1663 frame = malloc(sizeof *frame);
1664 memset(frame, 0, sizeof *frame);
1665
1666 frame->widget = window_add_widget(window, frame);
1667 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001668 frame->margin = 32;
Martin Minarik1998b152012-05-10 02:04:35 +02001669 frame->width = 6;
1670 frame->titlebar_height = 27;
1671
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001672 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1673 widget_set_resize_handler(frame->widget, frame_resize_handler);
1674 widget_set_enter_handler(frame->widget, frame_enter_handler);
1675 widget_set_motion_handler(frame->widget, frame_motion_handler);
1676 widget_set_button_handler(frame->widget, frame_button_handler);
1677
Martin Minarik1998b152012-05-10 02:04:35 +02001678 /* Create empty list for frame buttons */
1679 wl_list_init(&frame->buttons_list);
1680
1681 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1682 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1683
1684 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1685 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1686
1687 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1688 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1689
1690 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1691 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1692
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001693 window->frame = frame;
1694
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001695 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001696}
1697
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001698static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001699frame_destroy(struct frame *frame)
1700{
Martin Minarik1998b152012-05-10 02:04:35 +02001701 struct frame_button *button, *tmp;
1702
1703 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1704 frame_button_destroy(button);
1705
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001706 /* frame->child must be destroyed by the application */
1707 widget_destroy(frame->widget);
1708 free(frame);
1709}
1710
1711static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001712input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001713 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001714{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001715 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001716 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001717
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001718 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001719 return;
1720
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001721 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001722 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001723 widget = old;
1724 if (input->grab)
1725 widget = input->grab;
1726 if (widget->leave_handler)
1727 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001728 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001729 }
1730
1731 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001732 widget = focus;
1733 if (input->grab)
1734 widget = input->grab;
1735 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001736 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001737 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001738 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001739
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001740 input_set_pointer_image(input, input->pointer_enter_serial,
1741 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001742 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001743}
1744
1745static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001746input_handle_motion(void *data, struct wl_input_device *input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001747 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001748{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001749 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001750 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001751 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001752 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001753 float sx = wl_fixed_to_double(sx_w);
1754 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001755
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001756 input->sx = sx;
1757 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001758
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001759 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001760 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001761 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001762 }
1763
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001764 if (input->grab)
1765 widget = input->grab;
1766 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001767 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001768 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001769 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001770 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001771 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001772
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001773 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001774}
1775
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001776void
1777input_grab(struct input *input, struct widget *widget, uint32_t button)
1778{
1779 input->grab = widget;
1780 input->grab_button = button;
1781}
1782
1783void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001784input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001785{
1786 struct widget *widget;
1787
1788 input->grab = NULL;
1789 if (input->pointer_focus) {
1790 widget = widget_find_widget(input->pointer_focus->widget,
1791 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001792 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001793 }
1794}
1795
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001796static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001797input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001798 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001799 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001800{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001801 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001802 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001803
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001804 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001805 if (input->focus_widget && input->grab == NULL && state)
1806 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001807
Neil Roberts6b28aad2012-01-23 19:11:18 +00001808 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001809 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001810 (*widget->button_handler)(widget,
1811 input, time,
1812 button, state,
1813 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001814
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001815 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001816 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001817}
1818
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001819static void
Scott Moreau210d0792012-03-22 10:47:01 -06001820input_handle_axis(void *data,
1821 struct wl_input_device *input_device,
1822 uint32_t time, uint32_t axis, int32_t value)
1823{
1824}
1825
1826static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001827input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001828 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001829{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001830 struct input *input = data;
1831 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001832 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001833 uint32_t code, num_syms;
1834 const xkb_keysym_t *syms;
1835 xkb_keysym_t sym;
1836 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001837
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001838 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001839 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001840 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001841 return;
1842
Kristian Høgsberg70163132012-05-08 15:55:39 -04001843 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1844 xkb_state_update_key(d->xkb.state, code,
1845 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001846
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001847 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001848 XKB_STATE_DEPRESSED |
1849 XKB_STATE_LATCHED);
1850 input->modifiers = 0;
1851 if (mask & input->display->xkb.control_mask)
1852 input->modifiers |= MOD_CONTROL_MASK;
1853 if (mask & input->display->xkb.alt_mask)
1854 input->modifiers |= MOD_ALT_MASK;
1855 if (mask & input->display->xkb.shift_mask)
1856 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001857
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001858 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001859 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001860 if (state)
1861 window_set_maximized(window,
1862 window->type != TYPE_MAXIMIZED);
1863 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001864 if (num_syms == 1)
1865 sym = syms[0];
1866 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001867 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001868
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001869 (*window->key_handler)(window, input, time, key,
1870 sym, state, window->user_data);
1871 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001872}
1873
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001874static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001875input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001876{
1877 struct window *window = input->pointer_focus;
1878
1879 if (!window)
1880 return;
1881
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001882 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001883
Pekka Paalanene1207c72011-12-16 12:02:09 +02001884 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001885 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001886}
1887
1888static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001889input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001890 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001891 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001892 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001893{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001894 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001895 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001896 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001897 float sx = wl_fixed_to_double(sx_w);
1898 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001899
Martin Minarik1998b152012-05-10 02:04:35 +02001900 if (!surface) {
1901 /* enter event for a window we've just destroyed */
1902 return;
1903 }
1904
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001905 input->display->serial = serial;
1906 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001907 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001908 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001909
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001910 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001911 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001912 window->pool = NULL;
1913 /* Schedule a redraw to free the pool */
1914 window_schedule_redraw(window);
1915 }
1916
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001917 input->sx = sx;
1918 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001919
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001920 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001921 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001922}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001923
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001924static void
1925input_handle_pointer_leave(void *data,
1926 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001927 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001928{
1929 struct input *input = data;
1930
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001931 input->display->serial = serial;
1932 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001933}
1934
1935static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001936input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001937{
1938 struct window *window = input->keyboard_focus;
1939
1940 if (!window)
1941 return;
1942
1943 window->keyboard_device = NULL;
1944 if (window->keyboard_focus_handler)
1945 (*window->keyboard_focus_handler)(window, NULL,
1946 window->user_data);
1947
1948 input->keyboard_focus = NULL;
1949}
1950
1951static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001952input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001953 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001954 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001955 struct wl_surface *surface,
1956 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001957{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001958 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001959 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001960
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001961 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001962 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001963
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001964 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001965 window->keyboard_device = input;
1966 if (window->keyboard_focus_handler)
1967 (*window->keyboard_focus_handler)(window,
1968 window->keyboard_device,
1969 window->user_data);
1970}
1971
1972static void
1973input_handle_keyboard_leave(void *data,
1974 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001975 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001976 struct wl_surface *surface)
1977{
1978 struct input *input = data;
1979
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001980 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001981 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001982}
1983
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001984static void
1985input_handle_touch_down(void *data,
1986 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001987 uint32_t serial, uint32_t time,
1988 struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001989 int32_t id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001990{
1991}
1992
1993static void
1994input_handle_touch_up(void *data,
1995 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001996 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001997{
1998}
1999
2000static void
2001input_handle_touch_motion(void *data,
2002 struct wl_input_device *wl_input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002003 uint32_t time, int32_t id,
2004 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002005{
2006}
2007
2008static void
2009input_handle_touch_frame(void *data,
2010 struct wl_input_device *wl_input_device)
2011{
2012}
2013
2014static void
2015input_handle_touch_cancel(void *data,
2016 struct wl_input_device *wl_input_device)
2017{
2018}
2019
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002020static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002021 input_handle_motion,
2022 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06002023 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002024 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002025 input_handle_pointer_enter,
2026 input_handle_pointer_leave,
2027 input_handle_keyboard_enter,
2028 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002029 input_handle_touch_down,
2030 input_handle_touch_up,
2031 input_handle_touch_motion,
2032 input_handle_touch_frame,
2033 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002034};
2035
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002036void
2037input_get_position(struct input *input, int32_t *x, int32_t *y)
2038{
2039 *x = input->sx;
2040 *y = input->sy;
2041}
2042
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002043struct wl_input_device *
2044input_get_input_device(struct input *input)
2045{
2046 return input->input_device;
2047}
2048
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002049uint32_t
2050input_get_modifiers(struct input *input)
2051{
2052 return input->modifiers;
2053}
2054
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002055struct widget *
2056input_get_focus_widget(struct input *input)
2057{
2058 return input->focus_widget;
2059}
2060
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002061struct data_offer {
2062 struct wl_data_offer *offer;
2063 struct input *input;
2064 struct wl_array types;
2065 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002066
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002067 struct task io_task;
2068 int fd;
2069 data_func_t func;
2070 int32_t x, y;
2071 void *user_data;
2072};
2073
2074static void
2075data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2076{
2077 struct data_offer *offer = data;
2078 char **p;
2079
2080 p = wl_array_add(&offer->types, sizeof *p);
2081 *p = strdup(type);
2082}
2083
2084static const struct wl_data_offer_listener data_offer_listener = {
2085 data_offer_offer,
2086};
2087
2088static void
2089data_offer_destroy(struct data_offer *offer)
2090{
2091 char **p;
2092
2093 offer->refcount--;
2094 if (offer->refcount == 0) {
2095 wl_data_offer_destroy(offer->offer);
2096 for (p = offer->types.data; *p; p++)
2097 free(*p);
2098 wl_array_release(&offer->types);
2099 free(offer);
2100 }
2101}
2102
2103static void
2104data_device_data_offer(void *data,
2105 struct wl_data_device *data_device, uint32_t id)
2106{
2107 struct data_offer *offer;
2108
2109 offer = malloc(sizeof *offer);
2110
2111 wl_array_init(&offer->types);
2112 offer->refcount = 1;
2113 offer->input = data;
2114
2115 /* FIXME: Generate typesafe wrappers for this */
2116 offer->offer = (struct wl_data_offer *)
2117 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2118 id, &wl_data_offer_interface);
2119
2120 wl_data_offer_add_listener(offer->offer,
2121 &data_offer_listener, offer);
2122}
2123
2124static void
2125data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002126 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002127 wl_fixed_t x_w, wl_fixed_t y_w,
2128 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002129{
2130 struct input *input = data;
2131 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002132 float x = wl_fixed_to_double(x_w);
2133 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002134 char **p;
2135
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002136 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002137 input->drag_offer = wl_data_offer_get_user_data(offer);
2138 window = wl_surface_get_user_data(surface);
2139 input->pointer_focus = window;
2140
2141 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2142 *p = NULL;
2143
2144 window = input->pointer_focus;
2145 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002146 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002147 input->drag_offer->types.data,
2148 window->user_data);
2149}
2150
2151static void
2152data_device_leave(void *data, struct wl_data_device *data_device)
2153{
2154 struct input *input = data;
2155
2156 data_offer_destroy(input->drag_offer);
2157 input->drag_offer = NULL;
2158}
2159
2160static void
2161data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002162 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002163{
2164 struct input *input = data;
2165 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002166 float x = wl_fixed_to_double(x_w);
2167 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002168
2169 input->sx = x;
2170 input->sy = y;
2171
2172 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002173 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002174 input->drag_offer->types.data,
2175 window->user_data);
2176}
2177
2178static void
2179data_device_drop(void *data, struct wl_data_device *data_device)
2180{
2181 struct input *input = data;
2182 struct window *window = input->pointer_focus;
2183
2184 if (window->drop_handler)
2185 window->drop_handler(window, input,
2186 input->sx, input->sy, window->user_data);
2187}
2188
2189static void
2190data_device_selection(void *data,
2191 struct wl_data_device *wl_data_device,
2192 struct wl_data_offer *offer)
2193{
2194 struct input *input = data;
2195 char **p;
2196
2197 if (input->selection_offer)
2198 data_offer_destroy(input->selection_offer);
2199
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002200 if (offer) {
2201 input->selection_offer = wl_data_offer_get_user_data(offer);
2202 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2203 *p = NULL;
2204 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002205}
2206
2207static const struct wl_data_device_listener data_device_listener = {
2208 data_device_data_offer,
2209 data_device_enter,
2210 data_device_leave,
2211 data_device_motion,
2212 data_device_drop,
2213 data_device_selection
2214};
2215
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002216void
2217input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2218{
2219 struct display *display = input->display;
2220 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002221 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002222
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002223 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224 return;
2225
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002226 if (display->cursors[pointer].n_images == 0)
2227 return;
2228
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002229 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002230
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002231 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002232 return;
2233
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002234 input->current_cursor = pointer;
2235 buffer = display_get_buffer_for_surface(display, image->surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002236 wl_input_device_attach(input->input_device, time, buffer,
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002237 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002238}
2239
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002240struct wl_data_device *
2241input_get_data_device(struct input *input)
2242{
2243 return input->data_device;
2244}
2245
2246void
2247input_set_selection(struct input *input,
2248 struct wl_data_source *source, uint32_t time)
2249{
2250 wl_data_device_set_selection(input->data_device, source, time);
2251}
2252
2253void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002254input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002255{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002256 wl_data_offer_accept(input->drag_offer->offer,
2257 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002258}
2259
2260static void
2261offer_io_func(struct task *task, uint32_t events)
2262{
2263 struct data_offer *offer =
2264 container_of(task, struct data_offer, io_task);
2265 unsigned int len;
2266 char buffer[4096];
2267
2268 len = read(offer->fd, buffer, sizeof buffer);
2269 offer->func(buffer, len,
2270 offer->x, offer->y, offer->user_data);
2271
2272 if (len == 0) {
2273 close(offer->fd);
2274 data_offer_destroy(offer);
2275 }
2276}
2277
2278static void
2279data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2280 data_func_t func, void *user_data)
2281{
2282 int p[2];
2283
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002284 if (pipe2(p, O_CLOEXEC) == -1)
2285 return;
2286
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002287 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2288 close(p[1]);
2289
2290 offer->io_task.run = offer_io_func;
2291 offer->fd = p[0];
2292 offer->func = func;
2293 offer->refcount++;
2294 offer->user_data = user_data;
2295
2296 display_watch_fd(offer->input->display,
2297 offer->fd, EPOLLIN, &offer->io_task);
2298}
2299
2300void
2301input_receive_drag_data(struct input *input, const char *mime_type,
2302 data_func_t func, void *data)
2303{
2304 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2305 input->drag_offer->x = input->sx;
2306 input->drag_offer->y = input->sy;
2307}
2308
2309int
2310input_receive_selection_data(struct input *input, const char *mime_type,
2311 data_func_t func, void *data)
2312{
2313 char **p;
2314
2315 if (input->selection_offer == NULL)
2316 return -1;
2317
2318 for (p = input->selection_offer->types.data; *p; p++)
2319 if (strcmp(mime_type, *p) == 0)
2320 break;
2321
2322 if (*p == NULL)
2323 return -1;
2324
2325 data_offer_receive_data(input->selection_offer,
2326 mime_type, func, data);
2327 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002328}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002329
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002330int
2331input_receive_selection_data_to_fd(struct input *input,
2332 const char *mime_type, int fd)
2333{
2334 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2335
2336 return 0;
2337}
2338
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002339void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002340window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002341{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002342 if (!window->shell_surface)
2343 return;
2344
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002345 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002346 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002347}
2348
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002349static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002350idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002351{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002352 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002353
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002354 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002355 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002356 widget_set_allocation(widget,
2357 window->pending_allocation.x,
2358 window->pending_allocation.y,
2359 window->pending_allocation.width,
2360 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002361
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002362 if (window->input_region) {
2363 wl_region_destroy(window->input_region);
2364 window->input_region = NULL;
2365 }
2366
2367 if (window->opaque_region) {
2368 wl_region_destroy(window->opaque_region);
2369 window->opaque_region = NULL;
2370 }
2371
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002372 if (widget->resize_handler)
2373 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002374 widget->allocation.width,
2375 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002376 widget->user_data);
2377
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002378 if (window->allocation.width != widget->allocation.width ||
2379 window->allocation.height != widget->allocation.height) {
2380 window->allocation = widget->allocation;
2381 window_schedule_redraw(window);
2382 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002383}
2384
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002385void
2386window_schedule_resize(struct window *window, int width, int height)
2387{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002388 window->pending_allocation.x = 0;
2389 window->pending_allocation.y = 0;
2390 window->pending_allocation.width = width;
2391 window->pending_allocation.height = height;
2392
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002393 window->resize_needed = 1;
2394 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002395}
2396
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002397void
2398widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2399{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002400 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002401}
2402
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002403static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002404handle_ping(void *data, struct wl_shell_surface *shell_surface,
2405 uint32_t serial)
2406{
2407 wl_shell_surface_pong(shell_surface, serial);
2408}
2409
2410static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002411handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002412 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002413{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002414 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002415
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002416 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002417 return;
2418
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002419 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002420 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002421}
2422
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002423static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002424menu_destroy(struct menu *menu)
2425{
2426 widget_destroy(menu->widget);
2427 window_destroy(menu->window);
2428 free(menu);
2429}
2430
2431static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002432handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2433{
2434 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002435 struct menu *menu = window->widget->user_data;
2436
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002437 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002438 * device. Or just use wl_callback. And this really needs to
2439 * be a window vfunc that the menu can set. And we need the
2440 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002441
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002442 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002443 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002444 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002445}
2446
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002447static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002448 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002449 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002450 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002451};
2452
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002453void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002454window_get_allocation(struct window *window,
2455 struct rectangle *allocation)
2456{
2457 *allocation = window->allocation;
2458}
2459
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002460static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002461widget_redraw(struct widget *widget)
2462{
2463 struct widget *child;
2464
2465 if (widget->redraw_handler)
2466 widget->redraw_handler(widget, widget->user_data);
2467 wl_list_for_each(child, &widget->child_list, link)
2468 widget_redraw(child);
2469}
2470
2471static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002472frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2473{
2474 struct window *window = data;
2475
2476 wl_callback_destroy(callback);
2477 window->redraw_scheduled = 0;
2478 if (window->redraw_needed)
2479 window_schedule_redraw(window);
2480}
2481
2482static const struct wl_callback_listener listener = {
2483 frame_callback
2484};
2485
2486static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002487idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002488{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002489 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002490 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002491
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002492 if (window->resize_needed)
2493 idle_resize(window);
2494
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002495 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002496 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002497 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002498 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002499 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002500
2501 callback = wl_surface_frame(window->surface);
2502 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002503}
2504
2505void
2506window_schedule_redraw(struct window *window)
2507{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002508 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002509 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002510 window->redraw_task.run = idle_redraw;
2511 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002512 window->redraw_scheduled = 1;
2513 }
2514}
2515
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002516void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002517window_set_custom(struct window *window)
2518{
2519 window->type = TYPE_CUSTOM;
2520}
2521
2522void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002523window_set_fullscreen(struct window *window, int fullscreen)
2524{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002525 if (!window->display->shell)
2526 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002527
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002528 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002529 return;
2530
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002531 if (fullscreen) {
2532 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002533 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002534 wl_shell_surface_set_fullscreen(window->shell_surface,
2535 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2536 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002537 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002538 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002539 wl_shell_surface_set_toplevel(window->shell_surface);
2540 window_schedule_resize(window,
2541 window->saved_allocation.width,
2542 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002543 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002544}
2545
2546void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002547window_set_maximized(struct window *window, int maximized)
2548{
2549 if (!window->display->shell)
2550 return;
2551
2552 if ((window->type == TYPE_MAXIMIZED) == maximized)
2553 return;
2554
2555 if (window->type == TYPE_TOPLEVEL) {
2556 window->saved_allocation = window->allocation;
2557 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2558 window->type = TYPE_MAXIMIZED;
2559 } else {
2560 wl_shell_surface_set_toplevel(window->shell_surface);
2561 window->type = TYPE_TOPLEVEL;
2562 window_schedule_resize(window,
2563 window->saved_allocation.width,
2564 window->saved_allocation.height);
2565 }
2566}
2567
2568void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002569window_set_user_data(struct window *window, void *data)
2570{
2571 window->user_data = data;
2572}
2573
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002574void *
2575window_get_user_data(struct window *window)
2576{
2577 return window->user_data;
2578}
2579
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002580void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002581window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002582 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002583{
2584 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002585}
2586
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002587void
2588window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002589 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002590{
2591 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002592}
2593
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002594void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002595window_set_data_handler(struct window *window, window_data_handler_t handler)
2596{
2597 window->data_handler = handler;
2598}
2599
2600void
2601window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2602{
2603 window->drop_handler = handler;
2604}
2605
2606void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002607window_set_close_handler(struct window *window,
2608 window_close_handler_t handler)
2609{
2610 window->close_handler = handler;
2611}
2612
2613void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002614window_set_title(struct window *window, const char *title)
2615{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002616 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002617 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002618 if (window->shell_surface)
2619 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002620}
2621
2622const char *
2623window_get_title(struct window *window)
2624{
2625 return window->title;
2626}
2627
2628void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002629window_damage(struct window *window, int32_t x, int32_t y,
2630 int32_t width, int32_t height)
2631{
2632 wl_surface_damage(window->surface, x, y, width, height);
2633}
2634
Casey Dahlin9074db52012-04-19 22:50:09 -04002635static void
2636surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002637 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002638{
Rob Bradford7507b572012-05-15 17:55:34 +01002639 struct window *window = data;
2640 struct output *output;
2641 struct output *output_found = NULL;
2642 struct window_output *window_output;
2643
2644 wl_list_for_each(output, &window->display->output_list, link) {
2645 if (output->output == wl_output) {
2646 output_found = output;
2647 break;
2648 }
2649 }
2650
2651 if (!output_found)
2652 return;
2653
2654 window_output = malloc (sizeof *window_output);
2655 window_output->output = output_found;
2656
2657 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002658}
2659
2660static void
2661surface_leave(void *data,
2662 struct wl_surface *wl_surface, struct wl_output *output)
2663{
Rob Bradford7507b572012-05-15 17:55:34 +01002664 struct window *window = data;
2665 struct window_output *window_output;
2666 struct window_output *window_output_found = NULL;
2667
2668 wl_list_for_each(window_output, &window->window_output_list, link) {
2669 if (window_output->output->output == output) {
2670 window_output_found = window_output;
2671 break;
2672 }
2673 }
2674
2675 if (window_output_found) {
2676 wl_list_remove(&window_output_found->link);
2677 free(window_output_found);
2678 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002679}
2680
2681static const struct wl_surface_listener surface_listener = {
2682 surface_enter,
2683 surface_leave
2684};
2685
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002686static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002687window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002688{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002689 struct window *window;
2690
2691 window = malloc(sizeof *window);
2692 if (window == NULL)
2693 return NULL;
2694
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002695 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002696 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002697 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002698 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002699 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002700 if (display->shell) {
2701 window->shell_surface =
2702 wl_shell_get_shell_surface(display->shell,
2703 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002704 if (window->title)
2705 wl_shell_surface_set_title(window->shell_surface,
2706 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002707 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002708 window->allocation.x = 0;
2709 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002710 window->allocation.width = 0;
2711 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002712 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002713 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002714 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002715 window->input_region = NULL;
2716 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002717
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002718 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002719#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002720 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002721#else
2722 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2723#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002724 else
2725 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002726
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002727 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002728 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002729 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002730
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002731 if (window->shell_surface) {
2732 wl_shell_surface_set_user_data(window->shell_surface, window);
2733 wl_shell_surface_add_listener(window->shell_surface,
2734 &shell_surface_listener, window);
2735 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002736
Rob Bradford7507b572012-05-15 17:55:34 +01002737 wl_list_init (&window->window_output_list);
2738
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002739 return window;
2740}
2741
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002742struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002743window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002744{
2745 struct window *window;
2746
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002747 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002748 if (!window)
2749 return NULL;
2750
2751 return window;
2752}
2753
2754struct window *
2755window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002756 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002757{
2758 struct window *window;
2759
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002760 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002761 if (!window)
2762 return NULL;
2763
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002764 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002765 window->x = x;
2766 window->y = y;
2767
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002768 if (display->shell)
2769 wl_shell_surface_set_transient(window->shell_surface,
2770 window->parent->shell_surface,
2771 window->x, window->y, 0);
2772
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002773 return window;
2774}
2775
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002776static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002777menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002778{
2779 int next;
2780
2781 next = (sy - 8) / 20;
2782 if (menu->current != next) {
2783 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002784 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002785 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002786}
2787
2788static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002789menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002790 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002791 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002792{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002793 struct menu *menu = data;
2794
2795 if (widget == menu->widget)
2796 menu_set_item(data, y);
2797
2798 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002799}
2800
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002801static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002802menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002803 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002804{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002805 struct menu *menu = data;
2806
2807 if (widget == menu->widget)
2808 menu_set_item(data, y);
2809
2810 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002811}
2812
2813static void
2814menu_leave_handler(struct widget *widget, struct input *input, void *data)
2815{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002816 struct menu *menu = data;
2817
2818 if (widget == menu->widget)
2819 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002820}
2821
2822static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002823menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002824 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002825 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002826
2827{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002828 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002829
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002830 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002831 /* Either relase after press-drag-release or
2832 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002833 menu->func(menu->window->parent,
2834 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002835 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002836 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002837 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002838}
2839
2840static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002841menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002842{
2843 cairo_t *cr;
2844 const int32_t r = 3, margin = 3;
2845 struct menu *menu = data;
2846 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002847 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002848
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002849 cr = cairo_create(window->cairo_surface);
2850 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2851 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2852 cairo_paint(cr);
2853
2854 width = window->allocation.width;
2855 height = window->allocation.height;
2856 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002857
2858 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002859 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2860 cairo_fill(cr);
2861
2862 for (i = 0; i < menu->count; i++) {
2863 if (i == menu->current) {
2864 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2865 cairo_rectangle(cr, margin, i * 20 + margin,
2866 width - 2 * margin, 20);
2867 cairo_fill(cr);
2868 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2869 cairo_move_to(cr, 10, i * 20 + 16);
2870 cairo_show_text(cr, menu->entries[i]);
2871 } else {
2872 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2873 cairo_move_to(cr, 10, i * 20 + 16);
2874 cairo_show_text(cr, menu->entries[i]);
2875 }
2876 }
2877
2878 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002879}
2880
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002881void
2882window_show_menu(struct display *display,
2883 struct input *input, uint32_t time, struct window *parent,
2884 int32_t x, int32_t y,
2885 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002886{
2887 struct window *window;
2888 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002889 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002890
2891 menu = malloc(sizeof *menu);
2892 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002893 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002894
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002895 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002896 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002897 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002898
2899 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002900 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002901 menu->entries = entries;
2902 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002903 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002904 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002905 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002906 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002907 window->type = TYPE_MENU;
2908 window->x = x;
2909 window->y = y;
2910
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002911 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002912 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2913 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002914 window->parent->shell_surface,
2915 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002916
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002917 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002918 widget_set_enter_handler(menu->widget, menu_enter_handler);
2919 widget_set_leave_handler(menu->widget, menu_leave_handler);
2920 widget_set_motion_handler(menu->widget, menu_motion_handler);
2921 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002922
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002923 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002924 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002925}
2926
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002927void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002928window_set_buffer_type(struct window *window, enum window_buffer_type type)
2929{
2930 window->buffer_type = type;
2931}
2932
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002933
2934static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002935display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002936 struct wl_output *wl_output,
2937 int x, int y,
2938 int physical_width,
2939 int physical_height,
2940 int subpixel,
2941 const char *make,
2942 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002943{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002944 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002945
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002946 output->allocation.x = x;
2947 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002948}
2949
2950static void
2951display_handle_mode(void *data,
2952 struct wl_output *wl_output,
2953 uint32_t flags,
2954 int width,
2955 int height,
2956 int refresh)
2957{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002958 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002959 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002960
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002961 if (flags & WL_OUTPUT_MODE_CURRENT) {
2962 output->allocation.width = width;
2963 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002964 if (display->output_configure_handler)
2965 (*display->output_configure_handler)(
2966 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002967 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002968}
2969
2970static const struct wl_output_listener output_listener = {
2971 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002972 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002973};
2974
2975static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002976display_add_output(struct display *d, uint32_t id)
2977{
2978 struct output *output;
2979
2980 output = malloc(sizeof *output);
2981 if (output == NULL)
2982 return;
2983
2984 memset(output, 0, sizeof *output);
2985 output->display = d;
2986 output->output =
2987 wl_display_bind(d->display, id, &wl_output_interface);
2988 wl_list_insert(d->output_list.prev, &output->link);
2989
2990 wl_output_add_listener(output->output, &output_listener, output);
2991}
2992
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002993static void
2994output_destroy(struct output *output)
2995{
2996 if (output->destroy_handler)
2997 (*output->destroy_handler)(output, output->user_data);
2998
2999 wl_output_destroy(output->output);
3000 wl_list_remove(&output->link);
3001 free(output);
3002}
3003
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003004void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003005display_set_output_configure_handler(struct display *display,
3006 display_output_handler_t handler)
3007{
3008 struct output *output;
3009
3010 display->output_configure_handler = handler;
3011 if (!handler)
3012 return;
3013
3014 wl_list_for_each(output, &display->output_list, link)
3015 (*display->output_configure_handler)(output,
3016 display->user_data);
3017}
3018
3019void
3020output_set_user_data(struct output *output, void *data)
3021{
3022 output->user_data = data;
3023}
3024
3025void *
3026output_get_user_data(struct output *output)
3027{
3028 return output->user_data;
3029}
3030
3031void
3032output_set_destroy_handler(struct output *output,
3033 display_output_handler_t handler)
3034{
3035 output->destroy_handler = handler;
3036 /* FIXME: implement this, once we have way to remove outputs */
3037}
3038
3039void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003040output_get_allocation(struct output *output, struct rectangle *allocation)
3041{
3042 *allocation = output->allocation;
3043}
3044
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003045struct wl_output *
3046output_get_wl_output(struct output *output)
3047{
3048 return output->output;
3049}
3050
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003051static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003052display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003053{
3054 struct input *input;
3055
3056 input = malloc(sizeof *input);
3057 if (input == NULL)
3058 return;
3059
3060 memset(input, 0, sizeof *input);
3061 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003062 input->input_device =
3063 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003064 input->pointer_focus = NULL;
3065 input->keyboard_focus = NULL;
3066 wl_list_insert(d->input_list.prev, &input->link);
3067
3068 wl_input_device_add_listener(input->input_device,
3069 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003070 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003071
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003072 input->data_device =
3073 wl_data_device_manager_get_data_device(d->data_device_manager,
3074 input->input_device);
3075 wl_data_device_add_listener(input->data_device,
3076 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003077}
3078
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003079static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003080input_destroy(struct input *input)
3081{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003082 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003083 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003084
3085 if (input->drag_offer)
3086 data_offer_destroy(input->drag_offer);
3087
3088 if (input->selection_offer)
3089 data_offer_destroy(input->selection_offer);
3090
3091 wl_data_device_destroy(input->data_device);
3092 wl_list_remove(&input->link);
3093 wl_input_device_destroy(input->input_device);
3094 free(input);
3095}
3096
3097static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003098display_handle_global(struct wl_display *display, uint32_t id,
3099 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003100{
3101 struct display *d = data;
3102
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003103 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003104 d->compositor =
3105 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003106 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003107 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003108 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003109 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003110 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003111 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003112 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003113 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003114 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3115 d->data_device_manager =
3116 wl_display_bind(display, id,
3117 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003118 }
3119}
3120
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003121static void
3122display_render_frame(struct display *d)
3123{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003124 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003125 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003126
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003127 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003128 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3129 cr = cairo_create(d->shadow);
3130 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3131 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04003132 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003133 cairo_fill(cr);
3134 cairo_destroy(cr);
3135 blur_surface(d->shadow, 64);
3136
3137 d->active_frame =
3138 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3139 cr = cairo_create(d->active_frame);
3140 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003141
3142 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
3143 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
3144 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
3145 cairo_set_source(cr, pattern);
3146 cairo_pattern_destroy(pattern);
3147
Kristian Høgsbergec323d22012-03-21 01:07:49 -04003148 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003149 cairo_fill(cr);
3150 cairo_destroy(cr);
3151
3152 d->inactive_frame =
3153 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3154 cr = cairo_create(d->inactive_frame);
3155 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003156 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04003157 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003158 cairo_fill(cr);
3159 cairo_destroy(cr);
3160}
3161
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003162static void
3163init_xkb(struct display *d)
3164{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003165 d->xkb.names.rules = "evdev";
3166 d->xkb.names.model = "pc105";
3167 d->xkb.names.layout = (char *) option_xkb_layout;
3168 d->xkb.names.variant = (char *) option_xkb_variant;
3169 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003170
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003171 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003172 if (!d->xkb.context) {
3173 fprintf(stderr, "Failed to create XKB context\n");
3174 exit(1);
3175 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003176
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003177 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003178 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003179 fprintf(stderr, "Failed to compile keymap\n");
3180 exit(1);
3181 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003182
3183 d->xkb.state = xkb_state_new(d->xkb.keymap);
3184 if (!d->xkb.state) {
3185 fprintf(stderr, "Failed to create XKB state\n");
3186 exit(1);
3187 }
3188
3189 d->xkb.control_mask =
3190 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3191 d->xkb.alt_mask =
3192 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3193 d->xkb.shift_mask =
3194 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3195
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003196}
3197
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003198static void
3199fini_xkb(struct display *display)
3200{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003201 xkb_state_unref(display->xkb.state);
3202 xkb_map_unref(display->xkb.keymap);
3203 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003204}
3205
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003206#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003207static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003208init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003209{
3210 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003211 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003212
Rob Clark6396ed32012-03-11 19:48:41 -05003213#ifdef USE_CAIRO_GLESV2
3214# define GL_BIT EGL_OPENGL_ES2_BIT
3215#else
3216# define GL_BIT EGL_OPENGL_BIT
3217#endif
3218
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003219 static const EGLint argb_cfg_attribs[] = {
3220 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003221 EGL_RED_SIZE, 1,
3222 EGL_GREEN_SIZE, 1,
3223 EGL_BLUE_SIZE, 1,
3224 EGL_ALPHA_SIZE, 1,
3225 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003226 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003227 EGL_NONE
3228 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003229
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003230#ifdef USE_CAIRO_GLESV2
3231 static const EGLint context_attribs[] = {
3232 EGL_CONTEXT_CLIENT_VERSION, 2,
3233 EGL_NONE
3234 };
3235 EGLint api = EGL_OPENGL_ES_API;
3236#else
3237 EGLint *context_attribs = NULL;
3238 EGLint api = EGL_OPENGL_API;
3239#endif
3240
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003241 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003242 if (!eglInitialize(d->dpy, &major, &minor)) {
3243 fprintf(stderr, "failed to initialize display\n");
3244 return -1;
3245 }
3246
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003247 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003248 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3249 return -1;
3250 }
3251
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003252 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3253 &d->argb_config, 1, &n) || n != 1) {
3254 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003255 return -1;
3256 }
3257
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003258 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003259 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003260 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003261 fprintf(stderr, "failed to create context\n");
3262 return -1;
3263 }
3264
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003265 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003266 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003267 return -1;
3268 }
3269
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003270#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003271 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3272 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3273 fprintf(stderr, "failed to get cairo egl argb device\n");
3274 return -1;
3275 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003276#endif
3277
3278 return 0;
3279}
3280
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003281static void
3282fini_egl(struct display *display)
3283{
3284#ifdef HAVE_CAIRO_EGL
3285 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003286#endif
3287
3288 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3289 EGL_NO_CONTEXT);
3290
3291 eglTerminate(display->dpy);
3292 eglReleaseThread();
3293}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003294#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003295
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003296static int
3297event_mask_update(uint32_t mask, void *data)
3298{
3299 struct display *d = data;
3300
3301 d->mask = mask;
3302
3303 return 0;
3304}
3305
3306static void
3307handle_display_data(struct task *task, uint32_t events)
3308{
3309 struct display *display =
3310 container_of(task, struct display, display_task);
3311
3312 wl_display_iterate(display->display, display->mask);
3313}
3314
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003315struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003316display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003317{
3318 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003319
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003320 argc = parse_options(xkb_options,
3321 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003322
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003323 d = malloc(sizeof *d);
3324 if (d == NULL)
3325 return NULL;
3326
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003327 memset(d, 0, sizeof *d);
3328
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003329 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003330 if (d->display == NULL) {
3331 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003332 return NULL;
3333 }
3334
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003335 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3336 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3337 d->display_task.run = handle_display_data;
3338 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3339
3340 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003341 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003342 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003343
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003344 /* Set up listener so we'll catch all events. */
3345 wl_display_add_global_listener(d->display,
3346 display_handle_global, d);
3347
3348 /* Process connection events. */
3349 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003350#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003351 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003352 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003353#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003354
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003355 d->image_target_texture_2d =
3356 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3357 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3358 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3359
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003360 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003361
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003362 display_render_frame(d);
3363
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003364 wl_list_init(&d->window_list);
3365
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003366 init_xkb(d);
3367
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003368 return d;
3369}
3370
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003371static void
3372display_destroy_outputs(struct display *display)
3373{
3374 struct output *tmp;
3375 struct output *output;
3376
3377 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3378 output_destroy(output);
3379}
3380
Pekka Paalanene1207c72011-12-16 12:02:09 +02003381static void
3382display_destroy_inputs(struct display *display)
3383{
3384 struct input *tmp;
3385 struct input *input;
3386
3387 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3388 input_destroy(input);
3389}
3390
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003391void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003392display_destroy(struct display *display)
3393{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003394 if (!wl_list_empty(&display->window_list))
3395 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3396
3397 if (!wl_list_empty(&display->deferred_list))
3398 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3399
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003400 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003401 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003402
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003403 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003404
3405 cairo_surface_destroy(display->active_frame);
3406 cairo_surface_destroy(display->inactive_frame);
3407 cairo_surface_destroy(display->shadow);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003408 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003409
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003410#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003411 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003412#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003413
Pekka Paalanenc2052982011-12-16 11:41:32 +02003414 if (display->shell)
3415 wl_shell_destroy(display->shell);
3416
3417 if (display->shm)
3418 wl_shm_destroy(display->shm);
3419
3420 if (display->data_device_manager)
3421 wl_data_device_manager_destroy(display->data_device_manager);
3422
3423 wl_compositor_destroy(display->compositor);
3424
3425 close(display->epoll_fd);
3426
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003427 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003428 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003429 free(display);
3430}
3431
3432void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003433display_set_user_data(struct display *display, void *data)
3434{
3435 display->user_data = data;
3436}
3437
3438void *
3439display_get_user_data(struct display *display)
3440{
3441 return display->user_data;
3442}
3443
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003444struct wl_display *
3445display_get_display(struct display *display)
3446{
3447 return display->display;
3448}
3449
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003450struct output *
3451display_get_output(struct display *display)
3452{
3453 return container_of(display->output_list.next, struct output, link);
3454}
3455
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003456struct wl_compositor *
3457display_get_compositor(struct display *display)
3458{
3459 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003460}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003461
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003462uint32_t
3463display_get_serial(struct display *display)
3464{
3465 return display->serial;
3466}
3467
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003468EGLDisplay
3469display_get_egl_display(struct display *d)
3470{
3471 return d->dpy;
3472}
3473
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003474struct wl_data_source *
3475display_create_data_source(struct display *display)
3476{
3477 return wl_data_device_manager_create_data_source(display->data_device_manager);
3478}
3479
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003480EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003481display_get_argb_egl_config(struct display *d)
3482{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003483 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003484}
3485
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003486struct wl_shell *
3487display_get_shell(struct display *display)
3488{
3489 return display->shell;
3490}
3491
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003492int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003493display_acquire_window_surface(struct display *display,
3494 struct window *window,
3495 EGLContext ctx)
3496{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003497#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003498 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003499 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003500
3501 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003502 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003503 device = cairo_surface_get_device(window->cairo_surface);
3504 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003505 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003506
Benjamin Franzke0c991632011-09-27 21:57:31 +02003507 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003508 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003509 ctx = display->argb_ctx;
3510 else
3511 assert(0);
3512 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003513
3514 data = cairo_surface_get_user_data(window->cairo_surface,
3515 &surface_data_key);
3516
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003517 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003518 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003519 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3520 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003521
3522 return 0;
3523#else
3524 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003525#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003526}
3527
3528void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003529display_release_window_surface(struct display *display,
3530 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003531{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003532#ifdef HAVE_CAIRO_EGL
3533 cairo_device_t *device;
3534
3535 device = cairo_surface_get_device(window->cairo_surface);
3536 if (!device)
3537 return;
3538
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003539 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003540 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003541 cairo_device_release(device);
3542#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003543}
3544
3545void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003546display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003547{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003548 wl_list_insert(&display->deferred_list, &task->link);
3549}
3550
3551void
3552display_watch_fd(struct display *display,
3553 int fd, uint32_t events, struct task *task)
3554{
3555 struct epoll_event ep;
3556
3557 ep.events = events;
3558 ep.data.ptr = task;
3559 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3560}
3561
3562void
3563display_run(struct display *display)
3564{
3565 struct task *task;
3566 struct epoll_event ep[16];
3567 int i, count;
3568
Pekka Paalanen826d7952011-12-15 10:14:07 +02003569 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003570 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003571 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003572
3573 while (!wl_list_empty(&display->deferred_list)) {
3574 task = container_of(display->deferred_list.next,
3575 struct task, link);
3576 wl_list_remove(&task->link);
3577 task->run(task, 0);
3578 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003579
3580 if (!display->running)
3581 break;
3582
3583 wl_display_flush(display->display);
3584
3585 count = epoll_wait(display->epoll_fd,
3586 ep, ARRAY_LENGTH(ep), -1);
3587 for (i = 0; i < count; i++) {
3588 task = ep[i].data.ptr;
3589 task->run(task, ep[i].events);
3590 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003591 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003592}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003593
3594void
3595display_exit(struct display *display)
3596{
3597 display->running = 0;
3598}