blob: d7b2e5c2ff028261fdfc7add75daadde978745cb [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øgsberg5a315bc2012-05-15 22:33:43 -040062#include "../shared/cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050063
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050065
Ander Conselvan de 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øgsberg291c69c2012-05-15 22:12:54 -040093
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040094 struct theme *theme;
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;
Daniel Stone37816df2012-05-16 18:45:18 +0100189 struct wl_seat *seat;
190 struct wl_pointer *pointer;
191 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400192 struct window *pointer_focus;
193 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300194 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400195 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400196 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400197 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400198 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400199
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500200 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500201 struct widget *grab;
202 uint32_t grab_button;
203
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400204 struct wl_data_device *data_device;
205 struct data_offer *drag_offer;
206 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400207};
208
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500209struct output {
210 struct display *display;
211 struct wl_output *output;
212 struct rectangle allocation;
213 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200214
215 display_output_handler_t destroy_handler;
216 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500217};
218
Martin Minarik1998b152012-05-10 02:04:35 +0200219enum frame_button_action {
220 FRAME_BUTTON_NULL = 0,
221 FRAME_BUTTON_ICON = 1,
222 FRAME_BUTTON_CLOSE = 2,
223 FRAME_BUTTON_MINIMIZE = 3,
224 FRAME_BUTTON_MAXIMIZE = 4,
225};
226
227enum frame_button_pointer {
228 FRAME_BUTTON_DEFAULT = 0,
229 FRAME_BUTTON_OVER = 1,
230 FRAME_BUTTON_ACTIVE = 2,
231};
232
233enum frame_button_align {
234 FRAME_BUTTON_RIGHT = 0,
235 FRAME_BUTTON_LEFT = 1,
236};
237
238enum frame_button_decoration {
239 FRAME_BUTTON_NONE = 0,
240 FRAME_BUTTON_FANCY = 1,
241};
242
243struct frame_button {
244 struct widget *widget;
245 struct frame *frame;
246 cairo_surface_t *icon;
247 enum frame_button_action type;
248 enum frame_button_pointer state;
249 struct wl_list link; /* buttons_list */
250 enum frame_button_align align;
251 enum frame_button_decoration decoration;
252};
253
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500254struct frame {
255 struct widget *widget;
256 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200257 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500258};
259
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500260struct menu {
261 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500262 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500263 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500264 const char **entries;
265 uint32_t time;
266 int current;
267 int count;
268 menu_func_t func;
269};
270
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300271struct cursor_image {
272 cairo_surface_t *surface;
273 int width, height;
274 int hotspot_x, hotspot_y;
275 int delay;
276};
277
278struct cursor {
279 int n_images;
280 struct cursor_image *images;
281};
282
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700283struct shm_pool {
284 struct wl_shm_pool *pool;
285 size_t size;
286 size_t used;
287 void *data;
288};
289
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400290enum {
291 POINTER_DEFAULT = 100,
292 POINTER_UNSET
293};
294
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500295enum window_location {
296 WINDOW_INTERIOR = 0,
297 WINDOW_RESIZING_TOP = 1,
298 WINDOW_RESIZING_BOTTOM = 2,
299 WINDOW_RESIZING_LEFT = 4,
300 WINDOW_RESIZING_TOP_LEFT = 5,
301 WINDOW_RESIZING_BOTTOM_LEFT = 6,
302 WINDOW_RESIZING_RIGHT = 8,
303 WINDOW_RESIZING_TOP_RIGHT = 9,
304 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
305 WINDOW_RESIZING_MASK = 15,
306 WINDOW_EXTERIOR = 16,
307 WINDOW_TITLEBAR = 17,
308 WINDOW_CLIENT_AREA = 18,
309};
310
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400311const char *option_xkb_layout = "us";
312const char *option_xkb_variant = "";
313const char *option_xkb_options = "";
314
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400315static const struct weston_option xkb_options[] = {
316 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
317 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
318 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400319};
320
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400321static const cairo_user_data_key_t surface_data_key;
322struct surface_data {
323 struct wl_buffer *buffer;
324};
325
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500326#define MULT(_d,c,a,t) \
327 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
328
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500329#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100331struct egl_window_surface_data {
332 struct display *display;
333 struct wl_surface *surface;
334 struct wl_egl_window *window;
335 EGLSurface surf;
336};
337
338static void
339egl_window_surface_data_destroy(void *p)
340{
341 struct egl_window_surface_data *data = p;
342 struct display *d = data->display;
343
344 eglDestroySurface(d->dpy, data->surf);
345 wl_egl_window_destroy(data->window);
346 data->surface = NULL;
347
348 free(p);
349}
350
351static cairo_surface_t *
352display_create_egl_window_surface(struct display *display,
353 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400354 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100355 struct rectangle *rectangle)
356{
357 cairo_surface_t *cairo_surface;
358 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400359 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200360 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100361
362 data = malloc(sizeof *data);
363 if (data == NULL)
364 return NULL;
365
366 data->display = display;
367 data->surface = surface;
368
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500369 config = display->argb_config;
370 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400371
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400372 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100373 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100375
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500377 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378
Benjamin Franzke0c991632011-09-27 21:57:31 +0200379 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100380 data->surf,
381 rectangle->width,
382 rectangle->height);
383
384 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
385 data, egl_window_surface_data_destroy);
386
387 return cairo_surface;
388}
389
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390#endif
391
392struct wl_buffer *
393display_get_buffer_for_surface(struct display *display,
394 cairo_surface_t *surface)
395{
396 struct surface_data *data;
397
398 data = cairo_surface_get_user_data (surface, &surface_data_key);
399
400 return data->buffer;
401}
402
403struct shm_surface_data {
404 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700405 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406};
407
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500408static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700409shm_pool_destroy(struct shm_pool *pool);
410
411static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412shm_surface_data_destroy(void *p)
413{
414 struct shm_surface_data *data = p;
415
416 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700417 if (data->pool)
418 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419}
420
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300421static void
422shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
423{
424 void *dest = cairo_image_surface_get_data(surface);
425
426 memcpy(dest, data, count);
427}
428
Kristian Høgsberg16626282012-04-03 11:21:27 -0400429static struct wl_shm_pool *
430make_shm_pool(struct display *display, int size, void **data)
431{
432 char filename[] = "/tmp/wayland-shm-XXXXXX";
433 struct wl_shm_pool *pool;
434 int fd;
435
436 fd = mkstemp(filename);
437 if (fd < 0) {
438 fprintf(stderr, "open %s failed: %m\n", filename);
439 return NULL;
440 }
441 if (ftruncate(fd, size) < 0) {
442 fprintf(stderr, "ftruncate failed: %m\n");
443 close(fd);
444 return NULL;
445 }
446
447 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
448 unlink(filename);
449
450 if (*data == MAP_FAILED) {
451 fprintf(stderr, "mmap failed: %m\n");
452 close(fd);
453 return NULL;
454 }
455
456 pool = wl_shm_create_pool(display->shm, fd, size);
457
458 close(fd);
459
460 return pool;
461}
462
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700463static struct shm_pool *
464shm_pool_create(struct display *display, size_t size)
465{
466 struct shm_pool *pool = malloc(sizeof *pool);
467
468 if (!pool)
469 return NULL;
470
471 pool->pool = make_shm_pool(display, size, &pool->data);
472 if (!pool->pool) {
473 free(pool);
474 return NULL;
475 }
476
477 pool->size = size;
478 pool->used = 0;
479
480 return pool;
481}
482
483static void *
484shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
485{
486 if (pool->used + size > pool->size)
487 return NULL;
488
489 *offset = pool->used;
490 pool->used += size;
491
492 return (char *) pool->data + *offset;
493}
494
495/* destroy the pool. this does not unmap the memory though */
496static void
497shm_pool_destroy(struct shm_pool *pool)
498{
499 munmap(pool->data, pool->size);
500 wl_shm_pool_destroy(pool->pool);
501 free(pool);
502}
503
504/* Start allocating from the beginning of the pool again */
505static void
506shm_pool_reset(struct shm_pool *pool)
507{
508 pool->used = 0;
509}
510
511static int
512data_length_for_shm_surface(struct rectangle *rect)
513{
514 int stride;
515
516 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
517 rect->width);
518 return stride * rect->height;
519}
520
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500521static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700522display_create_shm_surface_from_pool(struct display *display,
523 struct rectangle *rectangle,
524 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400525{
526 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400527 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400528 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700529 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400530 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400531
532 data = malloc(sizeof *data);
533 if (data == NULL)
534 return NULL;
535
536 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
537 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700538 length = stride * rectangle->height;
539 data->pool = NULL;
540 map = shm_pool_allocate(pool, length, &offset);
541
542 if (!map) {
543 free(data);
544 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400545 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400547 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548 CAIRO_FORMAT_ARGB32,
549 rectangle->width,
550 rectangle->height,
551 stride);
552
553 cairo_surface_set_user_data (surface, &surface_data_key,
554 data, shm_surface_data_destroy);
555
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400556 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500557 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400558 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500559 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400560
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700561 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400562 rectangle->width,
563 rectangle->height,
564 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400565
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700566 return surface;
567}
568
569static cairo_surface_t *
570display_create_shm_surface(struct display *display,
571 struct rectangle *rectangle, uint32_t flags,
572 struct window *window)
573{
574 struct shm_surface_data *data;
575 struct shm_pool *pool;
576 cairo_surface_t *surface;
577
578 if (window && window->pool) {
579 shm_pool_reset(window->pool);
580 surface = display_create_shm_surface_from_pool(display,
581 rectangle,
582 flags,
583 window->pool);
584 if (surface)
585 return surface;
586 }
587
588 pool = shm_pool_create(display,
589 data_length_for_shm_surface(rectangle));
590 if (!pool)
591 return NULL;
592
593 surface =
594 display_create_shm_surface_from_pool(display, rectangle,
595 flags, pool);
596
597 if (!surface) {
598 shm_pool_destroy(pool);
599 return NULL;
600 }
601
602 /* make sure we destroy the pool when the surface is destroyed */
603 data = cairo_surface_get_user_data(surface, &surface_data_key);
604 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400605
606 return surface;
607}
608
nobled7b87cb02011-02-01 18:51:47 +0000609static int
610check_size(struct rectangle *rect)
611{
612 if (rect->width && rect->height)
613 return 0;
614
615 fprintf(stderr, "tried to create surface of "
616 "width: %d, height: %d\n", rect->width, rect->height);
617 return -1;
618}
619
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400620cairo_surface_t *
621display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100622 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400623 struct rectangle *rectangle,
624 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400625{
nobled7b87cb02011-02-01 18:51:47 +0000626 if (check_size(rectangle) < 0)
627 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500628#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400629 if (display->dpy)
630 return display_create_egl_window_surface(display,
631 surface,
632 flags,
633 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400635 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400636}
637
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300638static const char *cursors[] = {
639 "bottom_left_corner",
640 "bottom_right_corner",
641 "bottom_side",
642 "grabbing",
643 "left_ptr",
644 "left_side",
645 "right_side",
646 "top_left_corner",
647 "top_right_corner",
648 "top_side",
649 "xterm",
650 "hand1",
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400651};
652
653static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300654create_cursor_from_images(struct display *display, struct cursor *cursor,
655 XcursorImages *images)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400656{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300657 int i;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400658 struct rectangle rect;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300659 XcursorImage *image;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400660
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300661 cursor->images = malloc(images->nimage * sizeof *cursor->images);
662 cursor->n_images = images->nimage;
663
664 for (i = 0; i < images->nimage; i++) {
665 image = images->images[i];
666
667 rect.width = image->width;
668 rect.height = image->height;
669
670 cursor->images[i].surface =
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700671 display_create_shm_surface_from_pool(display, &rect, 0,
672 display->cursor_shm_pool);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300673
674 shm_surface_write(cursor->images[i].surface,
675 (unsigned char *) image->pixels,
676 image->width * image->height * sizeof image->pixels[0]);
677
678 cursor->images[i].width = image->width;
679 cursor->images[i].height = image->height;
680 cursor->images[i].hotspot_x = image->xhot;
681 cursor->images[i].hotspot_y = image->yhot;
682 cursor->images[i].delay = image->delay;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400683 }
684
685}
686
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700687static size_t
688data_length_for_cursor_images(XcursorImages *images)
689{
690 int i;
691 size_t length = 0;
692 struct rectangle rect;
693
694 for (i = 0; i < images->nimage; i++) {
695 rect.width = images->images[i]->width;
696 rect.height = images->images[i]->height;
697 length += data_length_for_shm_surface(&rect);
698 }
699
700 return length;
701}
702
Pekka Paalanen325bb602011-12-19 10:31:45 +0200703static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300704create_cursors(struct display *display)
705{
706 int i, count;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700707 size_t pool_size = 0;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300708 struct cursor *cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700709 XcursorImages **images;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300710
711 count = ARRAY_LENGTH(cursors);
712 display->cursors = malloc(count * sizeof *display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700713 images = malloc(count * sizeof images[0]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300714
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700715 for (i = 0; i < count; i++) {
716 images[i] = XcursorLibraryLoadImages(cursors[i], NULL, 32);
Rafal Mielniczuk87e4c932012-05-08 22:10:44 +0200717 if (!images[i]) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300718 fprintf(stderr, "Error loading cursor: %s\n",
719 cursors[i]);
720 continue;
721 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700722 pool_size += data_length_for_cursor_images(images[i]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300723 }
724
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700725 display->cursor_shm_pool = shm_pool_create(display, pool_size);
726
727 for (i = 0; i < count; i++) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700728 cursor = &display->cursors[i];
Dima Ryazanovff1c2d72012-05-08 21:02:33 -0700729
730 if (!images[i]) {
731 cursor->n_images = 0;
732 cursor->images = NULL;
733 continue;
734 }
735
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700736 create_cursor_from_images(display, cursor, images[i]);
737
738 XcursorImagesDestroy(images[i]);
739 }
740
741 free(images);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300742}
743
744static void
745destroy_cursor_images(struct cursor *cursor)
746{
747 int i;
748
749 for (i = 0; i < cursor->n_images; i++)
750 if (cursor->images[i].surface)
751 cairo_surface_destroy(cursor->images[i].surface);
752
753 free(cursor->images);
754}
755
756static void
757destroy_cursors(struct display *display)
Pekka Paalanen325bb602011-12-19 10:31:45 +0200758{
759 int i, count;
760
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300761 count = ARRAY_LENGTH(cursors);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200762 for (i = 0; i < count; ++i) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300763 destroy_cursor_images(&display->cursors[i]);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200764 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700765
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300766 free(display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700767 shm_pool_destroy(display->cursor_shm_pool);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200768}
769
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400770cairo_surface_t *
771display_get_pointer_surface(struct display *display, int pointer,
772 int *width, int *height,
773 int *hotspot_x, int *hotspot_y)
774{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300775 struct cursor *cursor = &display->cursors[pointer];
776 cairo_surface_t *surface = cursor->images[0].surface;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400777
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300778 /* FIXME returning information for the first image. Something better
779 * is needed for animated cursors */
780
nobledf8475c92011-01-05 17:41:55 +0000781 *width = cairo_image_surface_get_width(surface);
782 *height = cairo_image_surface_get_height(surface);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300783
784 *hotspot_x = cursor->images[0].hotspot_x;
785 *hotspot_y = cursor->images[0].hotspot_y;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400786
787 return cairo_surface_reference(surface);
788}
789
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400790static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200791window_get_resize_dx_dy(struct window *window, int *x, int *y)
792{
793 if (window->resize_edges & WINDOW_RESIZING_LEFT)
794 *x = window->server_allocation.width - window->allocation.width;
795 else
796 *x = 0;
797
798 if (window->resize_edges & WINDOW_RESIZING_TOP)
799 *y = window->server_allocation.height -
800 window->allocation.height;
801 else
802 *y = 0;
803
804 window->resize_edges = 0;
805}
806
807static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500808window_attach_surface(struct window *window)
809{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400810 struct display *display = window->display;
811 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000812#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100813 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000814#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500815 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400817 if (window->type == TYPE_NONE) {
818 window->type = TYPE_TOPLEVEL;
819 if (display->shell)
820 wl_shell_surface_set_toplevel(window->shell_surface);
821 }
822
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000824#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
826 data = cairo_surface_get_user_data(window->cairo_surface,
827 &surface_data_key);
828
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 cairo_gl_surface_swapbuffers(window->cairo_surface);
830 wl_egl_window_get_attached_size(data->window,
831 &window->server_allocation.width,
832 &window->server_allocation.height);
833 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000834#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100836 buffer =
837 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400838 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200840 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100841 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400842 wl_surface_damage(window->surface, 0, 0,
843 window->allocation.width,
844 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100845 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400846 cairo_surface_destroy(window->cairo_surface);
847 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100848 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000849 default:
850 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500852
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500853 if (window->input_region) {
854 wl_surface_set_input_region(window->surface,
855 window->input_region);
856 wl_region_destroy(window->input_region);
857 window->input_region = NULL;
858 }
859
860 if (window->opaque_region) {
861 wl_surface_set_opaque_region(window->surface,
862 window->opaque_region);
863 wl_region_destroy(window->opaque_region);
864 window->opaque_region = NULL;
865 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500866}
867
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500868void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400869window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500870{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400871 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100872 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500873}
874
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400875void
876window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400877{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500878 cairo_surface_reference(surface);
879
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400880 if (window->cairo_surface != NULL)
881 cairo_surface_destroy(window->cairo_surface);
882
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500883 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400884}
885
Benjamin Franzke22d54812011-07-16 19:50:32 +0000886#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100887static void
888window_resize_cairo_window_surface(struct window *window)
889{
890 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200891 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100892
893 data = cairo_surface_get_user_data(window->cairo_surface,
894 &surface_data_key);
895
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200896 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100897 wl_egl_window_resize(data->window,
898 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200899 window->allocation.height,
900 x,y);
901
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100902 cairo_gl_surface_set_size(window->cairo_surface,
903 window->allocation.width,
904 window->allocation.height);
905}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000906#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100907
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400908struct display *
909window_get_display(struct window *window)
910{
911 return window->display;
912}
913
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400914void
915window_create_surface(struct window *window)
916{
917 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400918 uint32_t flags = 0;
919
920 if (!window->transparent)
921 flags = SURFACE_OPAQUE;
922
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400923 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500924#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100925 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
926 if (window->cairo_surface) {
927 window_resize_cairo_window_surface(window);
928 return;
929 }
930 surface = display_create_surface(window->display,
931 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400932 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100933 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400934#endif
935 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400936 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400937 &window->allocation,
938 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400939 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800940 default:
941 surface = NULL;
942 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400943 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400944
945 window_set_surface(window, surface);
946 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947}
948
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200949static void frame_destroy(struct frame *frame);
950
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400951void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500952window_destroy(struct window *window)
953{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200954 struct display *display = window->display;
955 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100956 struct window_output *window_output;
957 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200958
959 if (window->redraw_scheduled)
960 wl_list_remove(&window->redraw_task.link);
961
962 wl_list_for_each(input, &display->input_list, link) {
963 if (input->pointer_focus == window)
964 input->pointer_focus = NULL;
965 if (input->keyboard_focus == window)
966 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500967 if (input->focus_widget &&
968 input->focus_widget->window == window)
969 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200970 }
971
Rob Bradford7507b572012-05-15 17:55:34 +0100972 wl_list_for_each_safe(window_output, window_output_tmp,
973 &window->window_output_list, link) {
974 free (window_output);
975 }
976
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500977 if (window->input_region)
978 wl_region_destroy(window->input_region);
979 if (window->opaque_region)
980 wl_region_destroy(window->opaque_region);
981
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200982 if (window->frame)
983 frame_destroy(window->frame);
984
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200985 if (window->shell_surface)
986 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500987 wl_surface_destroy(window->surface);
988 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200989
990 if (window->cairo_surface != NULL)
991 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200992
993 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500994 free(window);
995}
996
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500997static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500998widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400999{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001000 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001001
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001002 wl_list_for_each(child, &widget->child_list, link) {
1003 target = widget_find_widget(child, x, y);
1004 if (target)
1005 return target;
1006 }
1007
1008 if (widget->allocation.x <= x &&
1009 x < widget->allocation.x + widget->allocation.width &&
1010 widget->allocation.y <= y &&
1011 y < widget->allocation.y + widget->allocation.height) {
1012 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001013 }
1014
1015 return NULL;
1016}
1017
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001018static struct widget *
1019widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001020{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001021 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001022
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001023 widget = malloc(sizeof *widget);
1024 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001025 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001026 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001027 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001028 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001029 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001030
1031 return widget;
1032}
1033
1034struct widget *
1035window_add_widget(struct window *window, void *data)
1036{
1037 window->widget = widget_create(window, data);
1038 wl_list_init(&window->widget->link);
1039
1040 return window->widget;
1041}
1042
1043struct widget *
1044widget_add_widget(struct widget *parent, void *data)
1045{
1046 struct widget *widget;
1047
1048 widget = widget_create(parent->window, data);
1049 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001050
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001051 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001052}
1053
1054void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001055widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001056{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001057 struct display *display = widget->window->display;
1058 struct input *input;
1059
1060 wl_list_for_each(input, &display->input_list, link) {
1061 if (input->focus_widget == widget)
1062 input->focus_widget = NULL;
1063 }
1064
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001065 wl_list_remove(&widget->link);
1066 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001067}
1068
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001069void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001070widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001072 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073}
1074
1075void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001076widget_set_size(struct widget *widget, int32_t width, int32_t height)
1077{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001078 widget->allocation.width = width;
1079 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001080}
1081
1082void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001083widget_set_allocation(struct widget *widget,
1084 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001085{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086 widget->allocation.x = x;
1087 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001088 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001089}
1090
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001091void
1092widget_set_transparent(struct widget *widget, int transparent)
1093{
1094 widget->opaque = !transparent;
1095}
1096
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001100 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001101}
1102
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001103void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001104widget_set_resize_handler(struct widget *widget,
1105 widget_resize_handler_t handler)
1106{
1107 widget->resize_handler = handler;
1108}
1109
1110void
1111widget_set_redraw_handler(struct widget *widget,
1112 widget_redraw_handler_t handler)
1113{
1114 widget->redraw_handler = handler;
1115}
1116
1117void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001118widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001119{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001120 widget->enter_handler = handler;
1121}
1122
1123void
1124widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1125{
1126 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001127}
1128
1129void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001130widget_set_motion_handler(struct widget *widget,
1131 widget_motion_handler_t handler)
1132{
1133 widget->motion_handler = handler;
1134}
1135
1136void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001137widget_set_button_handler(struct widget *widget,
1138 widget_button_handler_t handler)
1139{
1140 widget->button_handler = handler;
1141}
1142
1143void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001144widget_schedule_redraw(struct widget *widget)
1145{
1146 window_schedule_redraw(widget->window);
1147}
1148
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001149cairo_surface_t *
1150window_get_surface(struct window *window)
1151{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001152 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001153}
1154
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001155struct wl_surface *
1156window_get_wl_surface(struct window *window)
1157{
1158 return window->surface;
1159}
1160
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001161struct wl_shell_surface *
1162window_get_wl_shell_surface(struct window *window)
1163{
1164 return window->shell_surface;
1165}
1166
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001167static void
1168frame_resize_handler(struct widget *widget,
1169 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001170{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001171 struct frame *frame = data;
1172 struct widget *child = frame->child;
1173 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001174 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001175 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001176 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001177 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øgsberg291c69c2012-05-15 22:12:54 -04001182 decoration_width = (t->width + t->margin) * 2;
1183 decoration_height = t->width +
1184 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001185
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001186 allocation.x = t->width + t->margin;
1187 allocation.y = t->titlebar_height + t->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øgsberg291c69c2012-05-15 22:12:54 -04001191 opaque_margin = t->margin + t->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,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001224 t->margin, t->margin,
1225 width - 2 * t->margin,
1226 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001227
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 */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001240 x_r = frame->widget->allocation.width - t->width - t->margin;
1241 x_l = t->width + t->margin;
1242 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001243 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{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001424 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001425 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001426 struct theme *t = window->display->theme;
1427 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001428
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001429 if (window->type == TYPE_FULLSCREEN)
1430 return;
1431
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001432 cr = cairo_create(window->cairo_surface);
1433
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001434 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001435 flags |= THEME_FRAME_ACTIVE;
1436 theme_render_frame(t, cr, widget->allocation.width,
1437 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001438
1439 cairo_destroy(cr);
1440}
1441
1442static int
1443frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1444{
1445 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001446 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001447 const int grip_size = 8;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001448 struct theme *t = widget->window->display->theme;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001449
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001450 if (x < t->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001451 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001452 else if (t->margin <= x && x < t->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001453 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001454 else if (x < widget->allocation.width - t->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001455 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001456 else if (x < widget->allocation.width - t->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001457 hlocation = WINDOW_RESIZING_RIGHT;
1458 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001459 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001460
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001461 if (y < t->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001462 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001463 else if (t->margin <= y && y < t->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001464 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001465 else if (y < widget->allocation.height - t->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001466 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001467 else if (y < widget->allocation.height - t->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001468 vlocation = WINDOW_RESIZING_BOTTOM;
1469 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001470 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001471
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001472 location = vlocation | hlocation;
1473 if (location & WINDOW_EXTERIOR)
1474 location = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001475 if (location == WINDOW_INTERIOR && y < t->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001476 location = WINDOW_TITLEBAR;
1477 else if (location == WINDOW_INTERIOR)
1478 location = WINDOW_CLIENT_AREA;
1479
1480 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001481}
1482
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001483static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001484frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001485{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001486 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001487
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001488 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001489 switch (location) {
1490 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001491 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001492 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001493 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001494 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001495 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001496 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001497 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001498 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001499 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001500 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001501 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001502 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001503 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001504 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001505 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001506 case WINDOW_EXTERIOR:
1507 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001508 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001509 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001510 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001511}
1512
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001513static void
1514frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001515{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001516 switch (index) {
1517 case 0: /* close */
1518 if (window->close_handler)
1519 window->close_handler(window->parent,
1520 window->user_data);
1521 else
1522 display_exit(window->display);
1523 break;
1524 case 1: /* fullscreen */
1525 /* we don't have a way to get out of fullscreen for now */
1526 window_set_fullscreen(window, 1);
1527 break;
1528 case 2: /* rotate */
1529 case 3: /* scale */
1530 break;
1531 }
1532}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001533
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001534void
1535window_show_frame_menu(struct window *window,
1536 struct input *input, uint32_t time)
1537{
1538 int32_t x, y;
1539
1540 static const char *entries[] = {
1541 "Close", "Fullscreen", "Rotate", "Scale"
1542 };
1543
1544 input_get_position(input, &x, &y);
1545 window_show_menu(window->display, input, time, window,
1546 x - 10, y - 10, frame_menu_func, entries, 4);
1547}
1548
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001549static int
1550frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001551 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001552{
1553 return frame_get_pointer_image_for_location(data, input);
1554}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001555
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001556static int
1557frame_motion_handler(struct widget *widget,
1558 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001559 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001560{
1561 return frame_get_pointer_image_for_location(data, input);
1562}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001563
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001564static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001565frame_button_handler(struct widget *widget,
1566 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001567 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001568
1569{
1570 struct frame *frame = data;
1571 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001572 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001573 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001574
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001575 location = frame_get_pointer_location(frame, input->sx, input->sy);
1576
1577 if (window->display->shell && button == BTN_LEFT && state == 1) {
1578 switch (location) {
1579 case WINDOW_TITLEBAR:
1580 if (!window->shell_surface)
1581 break;
1582 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001583 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001584 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001585 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001586 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001587 break;
1588 case WINDOW_RESIZING_TOP:
1589 case WINDOW_RESIZING_BOTTOM:
1590 case WINDOW_RESIZING_LEFT:
1591 case WINDOW_RESIZING_RIGHT:
1592 case WINDOW_RESIZING_TOP_LEFT:
1593 case WINDOW_RESIZING_TOP_RIGHT:
1594 case WINDOW_RESIZING_BOTTOM_LEFT:
1595 case WINDOW_RESIZING_BOTTOM_RIGHT:
1596 if (!window->shell_surface)
1597 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001598 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001599
1600 if (!display->dpy) {
1601 /* If we're using shm, allocate a big
1602 pool to create buffers out of while
1603 we resize. We should probably base
1604 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001605 window->pool =
1606 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001607 }
1608
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001609 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001610 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001611 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001612 break;
1613 }
1614 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001615 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001616 }
1617}
1618
1619struct widget *
1620frame_create(struct window *window, void *data)
1621{
1622 struct frame *frame;
1623
1624 frame = malloc(sizeof *frame);
1625 memset(frame, 0, sizeof *frame);
1626
1627 frame->widget = window_add_widget(window, frame);
1628 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001629
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001630 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1631 widget_set_resize_handler(frame->widget, frame_resize_handler);
1632 widget_set_enter_handler(frame->widget, frame_enter_handler);
1633 widget_set_motion_handler(frame->widget, frame_motion_handler);
1634 widget_set_button_handler(frame->widget, frame_button_handler);
1635
Martin Minarik1998b152012-05-10 02:04:35 +02001636 /* Create empty list for frame buttons */
1637 wl_list_init(&frame->buttons_list);
1638
1639 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1640 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1641
1642 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1643 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1644
1645 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1646 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1647
1648 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1649 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1650
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001651 window->frame = frame;
1652
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001653 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001654}
1655
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001656static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001657frame_destroy(struct frame *frame)
1658{
Martin Minarik1998b152012-05-10 02:04:35 +02001659 struct frame_button *button, *tmp;
1660
1661 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1662 frame_button_destroy(button);
1663
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001664 /* frame->child must be destroyed by the application */
1665 widget_destroy(frame->widget);
1666 free(frame);
1667}
1668
1669static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001670input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001671 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001672{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001673 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001674 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001675
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001676 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001677 return;
1678
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001679 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001680 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001681 widget = old;
1682 if (input->grab)
1683 widget = input->grab;
1684 if (widget->leave_handler)
1685 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001686 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001687 }
1688
1689 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001690 widget = focus;
1691 if (input->grab)
1692 widget = input->grab;
1693 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001694 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001695 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001696 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001697
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001698 input_set_pointer_image(input, input->pointer_enter_serial,
1699 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001700 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001701}
1702
1703static void
Daniel Stone37816df2012-05-16 18:45:18 +01001704pointer_handle_motion(void *data, struct wl_pointer *pointer,
1705 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001706{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001707 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001708 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001709 struct widget *widget;
Daniel Stone37816df2012-05-16 18:45:18 +01001710 int cursor = POINTER_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001711 float sx = wl_fixed_to_double(sx_w);
1712 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001713
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001714 input->sx = sx;
1715 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001716
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001717 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001718 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001719 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001720 }
1721
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001722 if (input->grab)
1723 widget = input->grab;
1724 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001725 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001726 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001727 cursor = widget->motion_handler(input->focus_widget,
1728 input, time, sx, sy,
1729 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001730
Daniel Stone37816df2012-05-16 18:45:18 +01001731 input_set_pointer_image(input, time, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001732}
1733
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001734void
1735input_grab(struct input *input, struct widget *widget, uint32_t button)
1736{
1737 input->grab = widget;
1738 input->grab_button = button;
1739}
1740
1741void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001742input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001743{
1744 struct widget *widget;
1745
1746 input->grab = NULL;
1747 if (input->pointer_focus) {
1748 widget = widget_find_widget(input->pointer_focus->widget,
1749 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001750 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001751 }
1752}
1753
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001754static void
Daniel Stone37816df2012-05-16 18:45:18 +01001755pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1756 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001757{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001758 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001759 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001760
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001761 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001762 if (input->focus_widget && input->grab == NULL && state)
1763 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001764
Neil Roberts6b28aad2012-01-23 19:11:18 +00001765 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001766 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001767 (*widget->button_handler)(widget,
1768 input, time,
1769 button, state,
1770 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001771
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001772 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001773 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001774}
1775
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001776static void
Daniel Stone37816df2012-05-16 18:45:18 +01001777pointer_handle_axis(void *data, struct wl_pointer *pointer,
Scott Moreau210d0792012-03-22 10:47:01 -06001778 uint32_t time, uint32_t axis, int32_t value)
1779{
1780}
1781
1782static void
Daniel Stone37816df2012-05-16 18:45:18 +01001783keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1784 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001785{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001786 struct input *input = data;
1787 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001788 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001789 uint32_t code, num_syms;
1790 const xkb_keysym_t *syms;
1791 xkb_keysym_t sym;
1792 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001793
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001794 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001795 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001796 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001797 return;
1798
Kristian Høgsberg70163132012-05-08 15:55:39 -04001799 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1800 xkb_state_update_key(d->xkb.state, code,
1801 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001802
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001803 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001804 XKB_STATE_DEPRESSED |
1805 XKB_STATE_LATCHED);
1806 input->modifiers = 0;
1807 if (mask & input->display->xkb.control_mask)
1808 input->modifiers |= MOD_CONTROL_MASK;
1809 if (mask & input->display->xkb.alt_mask)
1810 input->modifiers |= MOD_ALT_MASK;
1811 if (mask & input->display->xkb.shift_mask)
1812 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001813
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001814 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001815 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001816 if (state)
1817 window_set_maximized(window,
1818 window->type != TYPE_MAXIMIZED);
1819 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001820 if (num_syms == 1)
1821 sym = syms[0];
1822 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001823 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001824
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001825 (*window->key_handler)(window, input, time, key,
1826 sym, state, window->user_data);
1827 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001828}
1829
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001830static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001831input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001832{
1833 struct window *window = input->pointer_focus;
1834
1835 if (!window)
1836 return;
1837
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001838 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001839
Pekka Paalanene1207c72011-12-16 12:02:09 +02001840 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001841 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001842}
1843
1844static void
Daniel Stone37816df2012-05-16 18:45:18 +01001845pointer_handle_enter(void *data, struct wl_pointer *pointer,
1846 uint32_t serial, struct wl_surface *surface,
1847 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001848{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001849 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001850 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001851 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001852 float sx = wl_fixed_to_double(sx_w);
1853 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001854
Martin Minarik1998b152012-05-10 02:04:35 +02001855 if (!surface) {
1856 /* enter event for a window we've just destroyed */
1857 return;
1858 }
1859
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001860 input->display->serial = serial;
1861 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001862 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001863 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001864
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001865 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001866 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001867 window->pool = NULL;
1868 /* Schedule a redraw to free the pool */
1869 window_schedule_redraw(window);
1870 }
1871
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001872 input->sx = sx;
1873 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001874
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001875 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001876 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001877}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001878
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001879static void
Daniel Stone37816df2012-05-16 18:45:18 +01001880pointer_handle_leave(void *data, struct wl_pointer *pointer,
1881 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001882{
1883 struct input *input = data;
1884
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001885 input->display->serial = serial;
1886 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001887}
1888
1889static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001890input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001891{
1892 struct window *window = input->keyboard_focus;
1893
1894 if (!window)
1895 return;
1896
1897 window->keyboard_device = NULL;
1898 if (window->keyboard_focus_handler)
1899 (*window->keyboard_focus_handler)(window, NULL,
1900 window->user_data);
1901
1902 input->keyboard_focus = NULL;
1903}
1904
1905static void
Daniel Stone37816df2012-05-16 18:45:18 +01001906keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1907 uint32_t serial, struct wl_surface *surface,
1908 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001909{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001910 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001911 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001912
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001913 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001914 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001915
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001916 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001917 window->keyboard_device = input;
1918 if (window->keyboard_focus_handler)
1919 (*window->keyboard_focus_handler)(window,
1920 window->keyboard_device,
1921 window->user_data);
1922}
1923
1924static void
Daniel Stone37816df2012-05-16 18:45:18 +01001925keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1926 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001927{
1928 struct input *input = data;
1929
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001930 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001931 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001932}
1933
Daniel Stone37816df2012-05-16 18:45:18 +01001934static const struct wl_pointer_listener pointer_listener = {
1935 pointer_handle_enter,
1936 pointer_handle_leave,
1937 pointer_handle_motion,
1938 pointer_handle_button,
1939 pointer_handle_axis,
1940};
1941
1942static const struct wl_keyboard_listener keyboard_listener = {
1943 keyboard_handle_enter,
1944 keyboard_handle_leave,
1945 keyboard_handle_key,
1946};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001947
1948static void
Daniel Stone37816df2012-05-16 18:45:18 +01001949seat_handle_capabilities(void *data, struct wl_seat *seat,
1950 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001951{
Daniel Stone37816df2012-05-16 18:45:18 +01001952 struct input *input = data;
1953
1954 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
1955 input->pointer = wl_seat_get_pointer(seat);
1956 wl_pointer_set_user_data(input->pointer, input);
1957 wl_pointer_add_listener(input->pointer, &pointer_listener,
1958 input);
1959 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
1960 wl_pointer_destroy(input->pointer);
1961 input->pointer = NULL;
1962 }
1963
1964 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
1965 input->keyboard = wl_seat_get_keyboard(seat);
1966 wl_keyboard_set_user_data(input->keyboard, input);
1967 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
1968 input);
1969 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
1970 wl_keyboard_destroy(input->keyboard);
1971 input->keyboard = NULL;
1972 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001973}
1974
Daniel Stone37816df2012-05-16 18:45:18 +01001975static const struct wl_seat_listener seat_listener = {
1976 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001977};
1978
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001979void
1980input_get_position(struct input *input, int32_t *x, int32_t *y)
1981{
1982 *x = input->sx;
1983 *y = input->sy;
1984}
1985
Daniel Stone37816df2012-05-16 18:45:18 +01001986struct wl_seat *
1987input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001988{
Daniel Stone37816df2012-05-16 18:45:18 +01001989 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001990}
1991
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001992uint32_t
1993input_get_modifiers(struct input *input)
1994{
1995 return input->modifiers;
1996}
1997
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001998struct widget *
1999input_get_focus_widget(struct input *input)
2000{
2001 return input->focus_widget;
2002}
2003
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002004struct data_offer {
2005 struct wl_data_offer *offer;
2006 struct input *input;
2007 struct wl_array types;
2008 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002009
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002010 struct task io_task;
2011 int fd;
2012 data_func_t func;
2013 int32_t x, y;
2014 void *user_data;
2015};
2016
2017static void
2018data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2019{
2020 struct data_offer *offer = data;
2021 char **p;
2022
2023 p = wl_array_add(&offer->types, sizeof *p);
2024 *p = strdup(type);
2025}
2026
2027static const struct wl_data_offer_listener data_offer_listener = {
2028 data_offer_offer,
2029};
2030
2031static void
2032data_offer_destroy(struct data_offer *offer)
2033{
2034 char **p;
2035
2036 offer->refcount--;
2037 if (offer->refcount == 0) {
2038 wl_data_offer_destroy(offer->offer);
2039 for (p = offer->types.data; *p; p++)
2040 free(*p);
2041 wl_array_release(&offer->types);
2042 free(offer);
2043 }
2044}
2045
2046static void
2047data_device_data_offer(void *data,
2048 struct wl_data_device *data_device, uint32_t id)
2049{
2050 struct data_offer *offer;
2051
2052 offer = malloc(sizeof *offer);
2053
2054 wl_array_init(&offer->types);
2055 offer->refcount = 1;
2056 offer->input = data;
2057
2058 /* FIXME: Generate typesafe wrappers for this */
2059 offer->offer = (struct wl_data_offer *)
2060 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2061 id, &wl_data_offer_interface);
2062
2063 wl_data_offer_add_listener(offer->offer,
2064 &data_offer_listener, offer);
2065}
2066
2067static void
2068data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002069 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002070 wl_fixed_t x_w, wl_fixed_t y_w,
2071 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002072{
2073 struct input *input = data;
2074 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002075 float x = wl_fixed_to_double(x_w);
2076 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002077 char **p;
2078
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002079 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002080 input->drag_offer = wl_data_offer_get_user_data(offer);
2081 window = wl_surface_get_user_data(surface);
2082 input->pointer_focus = window;
2083
2084 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2085 *p = NULL;
2086
2087 window = input->pointer_focus;
2088 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002089 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002090 input->drag_offer->types.data,
2091 window->user_data);
2092}
2093
2094static void
2095data_device_leave(void *data, struct wl_data_device *data_device)
2096{
2097 struct input *input = data;
2098
2099 data_offer_destroy(input->drag_offer);
2100 input->drag_offer = NULL;
2101}
2102
2103static void
2104data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002105 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002106{
2107 struct input *input = data;
2108 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002109 float x = wl_fixed_to_double(x_w);
2110 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002111
2112 input->sx = x;
2113 input->sy = y;
2114
2115 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002116 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002117 input->drag_offer->types.data,
2118 window->user_data);
2119}
2120
2121static void
2122data_device_drop(void *data, struct wl_data_device *data_device)
2123{
2124 struct input *input = data;
2125 struct window *window = input->pointer_focus;
2126
2127 if (window->drop_handler)
2128 window->drop_handler(window, input,
2129 input->sx, input->sy, window->user_data);
2130}
2131
2132static void
2133data_device_selection(void *data,
2134 struct wl_data_device *wl_data_device,
2135 struct wl_data_offer *offer)
2136{
2137 struct input *input = data;
2138 char **p;
2139
2140 if (input->selection_offer)
2141 data_offer_destroy(input->selection_offer);
2142
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002143 if (offer) {
2144 input->selection_offer = wl_data_offer_get_user_data(offer);
2145 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2146 *p = NULL;
2147 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002148}
2149
2150static const struct wl_data_device_listener data_device_listener = {
2151 data_device_data_offer,
2152 data_device_enter,
2153 data_device_leave,
2154 data_device_motion,
2155 data_device_drop,
2156 data_device_selection
2157};
2158
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002159void
2160input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2161{
2162 struct display *display = input->display;
2163 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002164 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002165
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002166 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002167 return;
2168
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002169 if (display->cursors[pointer].n_images == 0)
2170 return;
2171
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002172 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002173
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002174 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002175 return;
2176
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002177 input->current_cursor = pointer;
2178 buffer = display_get_buffer_for_surface(display, image->surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002179 wl_pointer_attach(input->pointer, time, buffer,
2180 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002181}
2182
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002183struct wl_data_device *
2184input_get_data_device(struct input *input)
2185{
2186 return input->data_device;
2187}
2188
2189void
2190input_set_selection(struct input *input,
2191 struct wl_data_source *source, uint32_t time)
2192{
2193 wl_data_device_set_selection(input->data_device, source, time);
2194}
2195
2196void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002197input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002198{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002199 wl_data_offer_accept(input->drag_offer->offer,
2200 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002201}
2202
2203static void
2204offer_io_func(struct task *task, uint32_t events)
2205{
2206 struct data_offer *offer =
2207 container_of(task, struct data_offer, io_task);
2208 unsigned int len;
2209 char buffer[4096];
2210
2211 len = read(offer->fd, buffer, sizeof buffer);
2212 offer->func(buffer, len,
2213 offer->x, offer->y, offer->user_data);
2214
2215 if (len == 0) {
2216 close(offer->fd);
2217 data_offer_destroy(offer);
2218 }
2219}
2220
2221static void
2222data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2223 data_func_t func, void *user_data)
2224{
2225 int p[2];
2226
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002227 if (pipe2(p, O_CLOEXEC) == -1)
2228 return;
2229
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002230 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2231 close(p[1]);
2232
2233 offer->io_task.run = offer_io_func;
2234 offer->fd = p[0];
2235 offer->func = func;
2236 offer->refcount++;
2237 offer->user_data = user_data;
2238
2239 display_watch_fd(offer->input->display,
2240 offer->fd, EPOLLIN, &offer->io_task);
2241}
2242
2243void
2244input_receive_drag_data(struct input *input, const char *mime_type,
2245 data_func_t func, void *data)
2246{
2247 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2248 input->drag_offer->x = input->sx;
2249 input->drag_offer->y = input->sy;
2250}
2251
2252int
2253input_receive_selection_data(struct input *input, const char *mime_type,
2254 data_func_t func, void *data)
2255{
2256 char **p;
2257
2258 if (input->selection_offer == NULL)
2259 return -1;
2260
2261 for (p = input->selection_offer->types.data; *p; p++)
2262 if (strcmp(mime_type, *p) == 0)
2263 break;
2264
2265 if (*p == NULL)
2266 return -1;
2267
2268 data_offer_receive_data(input->selection_offer,
2269 mime_type, func, data);
2270 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002271}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002272
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002273int
2274input_receive_selection_data_to_fd(struct input *input,
2275 const char *mime_type, int fd)
2276{
2277 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2278
2279 return 0;
2280}
2281
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002282void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002283window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002284{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002285 if (!window->shell_surface)
2286 return;
2287
Daniel Stone37816df2012-05-16 18:45:18 +01002288 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002289}
2290
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002291static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002292idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002293{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002294 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002295
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002296 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002297 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002298 widget_set_allocation(widget,
2299 window->pending_allocation.x,
2300 window->pending_allocation.y,
2301 window->pending_allocation.width,
2302 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002303
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002304 if (window->input_region) {
2305 wl_region_destroy(window->input_region);
2306 window->input_region = NULL;
2307 }
2308
2309 if (window->opaque_region) {
2310 wl_region_destroy(window->opaque_region);
2311 window->opaque_region = NULL;
2312 }
2313
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002314 if (widget->resize_handler)
2315 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002316 widget->allocation.width,
2317 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002318 widget->user_data);
2319
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002320 if (window->allocation.width != widget->allocation.width ||
2321 window->allocation.height != widget->allocation.height) {
2322 window->allocation = widget->allocation;
2323 window_schedule_redraw(window);
2324 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002325}
2326
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002327void
2328window_schedule_resize(struct window *window, int width, int height)
2329{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002330 window->pending_allocation.x = 0;
2331 window->pending_allocation.y = 0;
2332 window->pending_allocation.width = width;
2333 window->pending_allocation.height = height;
2334
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002335 window->resize_needed = 1;
2336 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002337}
2338
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002339void
2340widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2341{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002342 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002343}
2344
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002345static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002346handle_ping(void *data, struct wl_shell_surface *shell_surface,
2347 uint32_t serial)
2348{
2349 wl_shell_surface_pong(shell_surface, serial);
2350}
2351
2352static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002353handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002354 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002355{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002356 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002357
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002358 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002359 return;
2360
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002361 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002362 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002363}
2364
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002365static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002366menu_destroy(struct menu *menu)
2367{
2368 widget_destroy(menu->widget);
2369 window_destroy(menu->window);
2370 free(menu);
2371}
2372
2373static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002374handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2375{
2376 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002377 struct menu *menu = window->widget->user_data;
2378
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002379 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002380 * device. Or just use wl_callback. And this really needs to
2381 * be a window vfunc that the menu can set. And we need the
2382 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002383
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002384 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002385 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002386 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002387}
2388
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002389static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002390 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002391 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002392 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002393};
2394
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002395void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002396window_get_allocation(struct window *window,
2397 struct rectangle *allocation)
2398{
2399 *allocation = window->allocation;
2400}
2401
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002402static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002403widget_redraw(struct widget *widget)
2404{
2405 struct widget *child;
2406
2407 if (widget->redraw_handler)
2408 widget->redraw_handler(widget, widget->user_data);
2409 wl_list_for_each(child, &widget->child_list, link)
2410 widget_redraw(child);
2411}
2412
2413static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002414frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2415{
2416 struct window *window = data;
2417
2418 wl_callback_destroy(callback);
2419 window->redraw_scheduled = 0;
2420 if (window->redraw_needed)
2421 window_schedule_redraw(window);
2422}
2423
2424static const struct wl_callback_listener listener = {
2425 frame_callback
2426};
2427
2428static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002429idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002430{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002431 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002432 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002433
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002434 if (window->resize_needed)
2435 idle_resize(window);
2436
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002437 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002438 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002439 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002440 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002441 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002442
2443 callback = wl_surface_frame(window->surface);
2444 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002445}
2446
2447void
2448window_schedule_redraw(struct window *window)
2449{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002450 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002451 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002452 window->redraw_task.run = idle_redraw;
2453 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002454 window->redraw_scheduled = 1;
2455 }
2456}
2457
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002458void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002459window_set_custom(struct window *window)
2460{
2461 window->type = TYPE_CUSTOM;
2462}
2463
2464void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002465window_set_fullscreen(struct window *window, int fullscreen)
2466{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002467 if (!window->display->shell)
2468 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002469
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002470 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002471 return;
2472
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002473 if (fullscreen) {
2474 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002475 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002476 wl_shell_surface_set_fullscreen(window->shell_surface,
2477 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2478 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002479 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002480 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002481 wl_shell_surface_set_toplevel(window->shell_surface);
2482 window_schedule_resize(window,
2483 window->saved_allocation.width,
2484 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002485 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002486}
2487
2488void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002489window_set_maximized(struct window *window, int maximized)
2490{
2491 if (!window->display->shell)
2492 return;
2493
2494 if ((window->type == TYPE_MAXIMIZED) == maximized)
2495 return;
2496
2497 if (window->type == TYPE_TOPLEVEL) {
2498 window->saved_allocation = window->allocation;
2499 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2500 window->type = TYPE_MAXIMIZED;
2501 } else {
2502 wl_shell_surface_set_toplevel(window->shell_surface);
2503 window->type = TYPE_TOPLEVEL;
2504 window_schedule_resize(window,
2505 window->saved_allocation.width,
2506 window->saved_allocation.height);
2507 }
2508}
2509
2510void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002511window_set_user_data(struct window *window, void *data)
2512{
2513 window->user_data = data;
2514}
2515
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002516void *
2517window_get_user_data(struct window *window)
2518{
2519 return window->user_data;
2520}
2521
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002522void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002523window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002524 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002525{
2526 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002527}
2528
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002529void
2530window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002531 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002532{
2533 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002534}
2535
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002536void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002537window_set_data_handler(struct window *window, window_data_handler_t handler)
2538{
2539 window->data_handler = handler;
2540}
2541
2542void
2543window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2544{
2545 window->drop_handler = handler;
2546}
2547
2548void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002549window_set_close_handler(struct window *window,
2550 window_close_handler_t handler)
2551{
2552 window->close_handler = handler;
2553}
2554
2555void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002556window_set_title(struct window *window, const char *title)
2557{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002558 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002559 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002560 if (window->shell_surface)
2561 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002562}
2563
2564const char *
2565window_get_title(struct window *window)
2566{
2567 return window->title;
2568}
2569
2570void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002571window_damage(struct window *window, int32_t x, int32_t y,
2572 int32_t width, int32_t height)
2573{
2574 wl_surface_damage(window->surface, x, y, width, height);
2575}
2576
Casey Dahlin9074db52012-04-19 22:50:09 -04002577static void
2578surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002579 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002580{
Rob Bradford7507b572012-05-15 17:55:34 +01002581 struct window *window = data;
2582 struct output *output;
2583 struct output *output_found = NULL;
2584 struct window_output *window_output;
2585
2586 wl_list_for_each(output, &window->display->output_list, link) {
2587 if (output->output == wl_output) {
2588 output_found = output;
2589 break;
2590 }
2591 }
2592
2593 if (!output_found)
2594 return;
2595
2596 window_output = malloc (sizeof *window_output);
2597 window_output->output = output_found;
2598
2599 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002600}
2601
2602static void
2603surface_leave(void *data,
2604 struct wl_surface *wl_surface, struct wl_output *output)
2605{
Rob Bradford7507b572012-05-15 17:55:34 +01002606 struct window *window = data;
2607 struct window_output *window_output;
2608 struct window_output *window_output_found = NULL;
2609
2610 wl_list_for_each(window_output, &window->window_output_list, link) {
2611 if (window_output->output->output == output) {
2612 window_output_found = window_output;
2613 break;
2614 }
2615 }
2616
2617 if (window_output_found) {
2618 wl_list_remove(&window_output_found->link);
2619 free(window_output_found);
2620 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002621}
2622
2623static const struct wl_surface_listener surface_listener = {
2624 surface_enter,
2625 surface_leave
2626};
2627
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002628static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002629window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002630{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002631 struct window *window;
2632
2633 window = malloc(sizeof *window);
2634 if (window == NULL)
2635 return NULL;
2636
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002637 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002638 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002639 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002640 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002641 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002642 if (display->shell) {
2643 window->shell_surface =
2644 wl_shell_get_shell_surface(display->shell,
2645 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002646 if (window->title)
2647 wl_shell_surface_set_title(window->shell_surface,
2648 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002649 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002650 window->allocation.x = 0;
2651 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002652 window->allocation.width = 0;
2653 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002654 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002655 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002656 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002657 window->input_region = NULL;
2658 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002659
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002660 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002661#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002662 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002663#else
2664 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2665#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002666 else
2667 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002668
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002669 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002670 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002671 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002672
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002673 if (window->shell_surface) {
2674 wl_shell_surface_set_user_data(window->shell_surface, window);
2675 wl_shell_surface_add_listener(window->shell_surface,
2676 &shell_surface_listener, window);
2677 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002678
Rob Bradford7507b572012-05-15 17:55:34 +01002679 wl_list_init (&window->window_output_list);
2680
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002681 return window;
2682}
2683
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002684struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002685window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002686{
2687 struct window *window;
2688
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002689 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002690 if (!window)
2691 return NULL;
2692
2693 return window;
2694}
2695
2696struct window *
2697window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002698 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002699{
2700 struct window *window;
2701
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002702 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002703 if (!window)
2704 return NULL;
2705
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002706 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002707 window->x = x;
2708 window->y = y;
2709
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002710 if (display->shell)
2711 wl_shell_surface_set_transient(window->shell_surface,
2712 window->parent->shell_surface,
2713 window->x, window->y, 0);
2714
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002715 return window;
2716}
2717
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002718static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002719menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002720{
2721 int next;
2722
2723 next = (sy - 8) / 20;
2724 if (menu->current != next) {
2725 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002726 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002727 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002728}
2729
2730static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002731menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002732 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002733 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002734{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002735 struct menu *menu = data;
2736
2737 if (widget == menu->widget)
2738 menu_set_item(data, y);
2739
2740 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002741}
2742
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002743static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002744menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002745 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002746{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002747 struct menu *menu = data;
2748
2749 if (widget == menu->widget)
2750 menu_set_item(data, y);
2751
2752 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002753}
2754
2755static void
2756menu_leave_handler(struct widget *widget, struct input *input, void *data)
2757{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002758 struct menu *menu = data;
2759
2760 if (widget == menu->widget)
2761 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002762}
2763
2764static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002765menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002766 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002767 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002768
2769{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002770 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002771
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002772 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002773 /* Either relase after press-drag-release or
2774 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002775 menu->func(menu->window->parent,
2776 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002777 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002778 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002779 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002780}
2781
2782static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002783menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002784{
2785 cairo_t *cr;
2786 const int32_t r = 3, margin = 3;
2787 struct menu *menu = data;
2788 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002789 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002790
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002791 cr = cairo_create(window->cairo_surface);
2792 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2793 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2794 cairo_paint(cr);
2795
2796 width = window->allocation.width;
2797 height = window->allocation.height;
2798 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002799
2800 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002801 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2802 cairo_fill(cr);
2803
2804 for (i = 0; i < menu->count; i++) {
2805 if (i == menu->current) {
2806 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2807 cairo_rectangle(cr, margin, i * 20 + margin,
2808 width - 2 * margin, 20);
2809 cairo_fill(cr);
2810 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2811 cairo_move_to(cr, 10, i * 20 + 16);
2812 cairo_show_text(cr, menu->entries[i]);
2813 } else {
2814 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2815 cairo_move_to(cr, 10, i * 20 + 16);
2816 cairo_show_text(cr, menu->entries[i]);
2817 }
2818 }
2819
2820 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002821}
2822
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002823void
2824window_show_menu(struct display *display,
2825 struct input *input, uint32_t time, struct window *parent,
2826 int32_t x, int32_t y,
2827 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002828{
2829 struct window *window;
2830 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002831 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002832
2833 menu = malloc(sizeof *menu);
2834 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002835 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002836
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002837 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002838 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002839 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002840
2841 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002842 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002843 menu->entries = entries;
2844 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002845 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002846 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002847 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002848 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002849 window->type = TYPE_MENU;
2850 window->x = x;
2851 window->y = y;
2852
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002853 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002854 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002855 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002856 window->parent->shell_surface,
2857 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002858
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002859 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002860 widget_set_enter_handler(menu->widget, menu_enter_handler);
2861 widget_set_leave_handler(menu->widget, menu_leave_handler);
2862 widget_set_motion_handler(menu->widget, menu_motion_handler);
2863 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002864
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002865 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002866 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002867}
2868
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002869void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002870window_set_buffer_type(struct window *window, enum window_buffer_type type)
2871{
2872 window->buffer_type = type;
2873}
2874
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002875
2876static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002877display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002878 struct wl_output *wl_output,
2879 int x, int y,
2880 int physical_width,
2881 int physical_height,
2882 int subpixel,
2883 const char *make,
2884 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002885{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002886 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002887
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002888 output->allocation.x = x;
2889 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002890}
2891
2892static void
2893display_handle_mode(void *data,
2894 struct wl_output *wl_output,
2895 uint32_t flags,
2896 int width,
2897 int height,
2898 int refresh)
2899{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002900 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002901 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002902
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002903 if (flags & WL_OUTPUT_MODE_CURRENT) {
2904 output->allocation.width = width;
2905 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002906 if (display->output_configure_handler)
2907 (*display->output_configure_handler)(
2908 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002909 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002910}
2911
2912static const struct wl_output_listener output_listener = {
2913 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002914 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002915};
2916
2917static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002918display_add_output(struct display *d, uint32_t id)
2919{
2920 struct output *output;
2921
2922 output = malloc(sizeof *output);
2923 if (output == NULL)
2924 return;
2925
2926 memset(output, 0, sizeof *output);
2927 output->display = d;
2928 output->output =
2929 wl_display_bind(d->display, id, &wl_output_interface);
2930 wl_list_insert(d->output_list.prev, &output->link);
2931
2932 wl_output_add_listener(output->output, &output_listener, output);
2933}
2934
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002935static void
2936output_destroy(struct output *output)
2937{
2938 if (output->destroy_handler)
2939 (*output->destroy_handler)(output, output->user_data);
2940
2941 wl_output_destroy(output->output);
2942 wl_list_remove(&output->link);
2943 free(output);
2944}
2945
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002946void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002947display_set_output_configure_handler(struct display *display,
2948 display_output_handler_t handler)
2949{
2950 struct output *output;
2951
2952 display->output_configure_handler = handler;
2953 if (!handler)
2954 return;
2955
2956 wl_list_for_each(output, &display->output_list, link)
2957 (*display->output_configure_handler)(output,
2958 display->user_data);
2959}
2960
2961void
2962output_set_user_data(struct output *output, void *data)
2963{
2964 output->user_data = data;
2965}
2966
2967void *
2968output_get_user_data(struct output *output)
2969{
2970 return output->user_data;
2971}
2972
2973void
2974output_set_destroy_handler(struct output *output,
2975 display_output_handler_t handler)
2976{
2977 output->destroy_handler = handler;
2978 /* FIXME: implement this, once we have way to remove outputs */
2979}
2980
2981void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002982output_get_allocation(struct output *output, struct rectangle *allocation)
2983{
2984 *allocation = output->allocation;
2985}
2986
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002987struct wl_output *
2988output_get_wl_output(struct output *output)
2989{
2990 return output->output;
2991}
2992
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002993static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002994display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002995{
2996 struct input *input;
2997
2998 input = malloc(sizeof *input);
2999 if (input == NULL)
3000 return;
3001
3002 memset(input, 0, sizeof *input);
3003 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003004 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003005 input->pointer_focus = NULL;
3006 input->keyboard_focus = NULL;
3007 wl_list_insert(d->input_list.prev, &input->link);
3008
Daniel Stone37816df2012-05-16 18:45:18 +01003009 wl_seat_add_listener(input->seat, &seat_listener, input);
3010 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003011
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003012 input->data_device =
3013 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003014 input->seat);
3015 wl_data_device_add_listener(input->data_device, &data_device_listener,
3016 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003017}
3018
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003019static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003020input_destroy(struct input *input)
3021{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003022 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003023 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003024
3025 if (input->drag_offer)
3026 data_offer_destroy(input->drag_offer);
3027
3028 if (input->selection_offer)
3029 data_offer_destroy(input->selection_offer);
3030
3031 wl_data_device_destroy(input->data_device);
3032 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003033 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003034 free(input);
3035}
3036
3037static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003038display_handle_global(struct wl_display *display, uint32_t id,
3039 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003040{
3041 struct display *d = data;
3042
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003043 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003044 d->compositor =
3045 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003046 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003047 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003048 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003049 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003050 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003051 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003052 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003053 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003054 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3055 d->data_device_manager =
3056 wl_display_bind(display, id,
3057 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003058 }
3059}
3060
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003061static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003062init_xkb(struct display *d)
3063{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003064 d->xkb.names.rules = "evdev";
3065 d->xkb.names.model = "pc105";
3066 d->xkb.names.layout = (char *) option_xkb_layout;
3067 d->xkb.names.variant = (char *) option_xkb_variant;
3068 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003069
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003070 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003071 if (!d->xkb.context) {
3072 fprintf(stderr, "Failed to create XKB context\n");
3073 exit(1);
3074 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003075
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003076 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003077 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003078 fprintf(stderr, "Failed to compile keymap\n");
3079 exit(1);
3080 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003081
3082 d->xkb.state = xkb_state_new(d->xkb.keymap);
3083 if (!d->xkb.state) {
3084 fprintf(stderr, "Failed to create XKB state\n");
3085 exit(1);
3086 }
3087
3088 d->xkb.control_mask =
3089 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3090 d->xkb.alt_mask =
3091 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3092 d->xkb.shift_mask =
3093 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3094
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003095}
3096
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003097static void
3098fini_xkb(struct display *display)
3099{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003100 xkb_state_unref(display->xkb.state);
3101 xkb_map_unref(display->xkb.keymap);
3102 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003103}
3104
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003105#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003106static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003107init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003108{
3109 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003110 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003111
Rob Clark6396ed32012-03-11 19:48:41 -05003112#ifdef USE_CAIRO_GLESV2
3113# define GL_BIT EGL_OPENGL_ES2_BIT
3114#else
3115# define GL_BIT EGL_OPENGL_BIT
3116#endif
3117
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003118 static const EGLint argb_cfg_attribs[] = {
3119 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003120 EGL_RED_SIZE, 1,
3121 EGL_GREEN_SIZE, 1,
3122 EGL_BLUE_SIZE, 1,
3123 EGL_ALPHA_SIZE, 1,
3124 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003125 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003126 EGL_NONE
3127 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003128
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003129#ifdef USE_CAIRO_GLESV2
3130 static const EGLint context_attribs[] = {
3131 EGL_CONTEXT_CLIENT_VERSION, 2,
3132 EGL_NONE
3133 };
3134 EGLint api = EGL_OPENGL_ES_API;
3135#else
3136 EGLint *context_attribs = NULL;
3137 EGLint api = EGL_OPENGL_API;
3138#endif
3139
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003140 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003141 if (!eglInitialize(d->dpy, &major, &minor)) {
3142 fprintf(stderr, "failed to initialize display\n");
3143 return -1;
3144 }
3145
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003146 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003147 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3148 return -1;
3149 }
3150
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003151 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3152 &d->argb_config, 1, &n) || n != 1) {
3153 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003154 return -1;
3155 }
3156
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003157 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003158 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003159 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003160 fprintf(stderr, "failed to create context\n");
3161 return -1;
3162 }
3163
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003164 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003165 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003166 return -1;
3167 }
3168
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003169#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003170 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3171 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3172 fprintf(stderr, "failed to get cairo egl argb device\n");
3173 return -1;
3174 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003175#endif
3176
3177 return 0;
3178}
3179
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003180static void
3181fini_egl(struct display *display)
3182{
3183#ifdef HAVE_CAIRO_EGL
3184 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003185#endif
3186
3187 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3188 EGL_NO_CONTEXT);
3189
3190 eglTerminate(display->dpy);
3191 eglReleaseThread();
3192}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003193#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003194
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003195static int
3196event_mask_update(uint32_t mask, void *data)
3197{
3198 struct display *d = data;
3199
3200 d->mask = mask;
3201
3202 return 0;
3203}
3204
3205static void
3206handle_display_data(struct task *task, uint32_t events)
3207{
3208 struct display *display =
3209 container_of(task, struct display, display_task);
3210
3211 wl_display_iterate(display->display, display->mask);
3212}
3213
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003214struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003215display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003216{
3217 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003218
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003219 argc = parse_options(xkb_options,
3220 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003221
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003222 d = malloc(sizeof *d);
3223 if (d == NULL)
3224 return NULL;
3225
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003226 memset(d, 0, sizeof *d);
3227
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003228 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003229 if (d->display == NULL) {
3230 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003231 return NULL;
3232 }
3233
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003234 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3235 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3236 d->display_task.run = handle_display_data;
3237 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3238
3239 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003240 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003241 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003242
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003243 /* Set up listener so we'll catch all events. */
3244 wl_display_add_global_listener(d->display,
3245 display_handle_global, d);
3246
3247 /* Process connection events. */
3248 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003249#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003250 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003251 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003252#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003253
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003254 d->image_target_texture_2d =
3255 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3256 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3257 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3258
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003259 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003260
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003261 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003262
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003263 wl_list_init(&d->window_list);
3264
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003265 init_xkb(d);
3266
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003267 return d;
3268}
3269
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003270static void
3271display_destroy_outputs(struct display *display)
3272{
3273 struct output *tmp;
3274 struct output *output;
3275
3276 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3277 output_destroy(output);
3278}
3279
Pekka Paalanene1207c72011-12-16 12:02:09 +02003280static void
3281display_destroy_inputs(struct display *display)
3282{
3283 struct input *tmp;
3284 struct input *input;
3285
3286 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3287 input_destroy(input);
3288}
3289
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003290void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003291display_destroy(struct display *display)
3292{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003293 if (!wl_list_empty(&display->window_list))
3294 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3295
3296 if (!wl_list_empty(&display->deferred_list))
3297 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3298
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003299 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003300 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003301
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003302 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003303
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003304 theme_destroy(display->theme);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003305 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003306
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003307#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003308 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003309#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003310
Pekka Paalanenc2052982011-12-16 11:41:32 +02003311 if (display->shell)
3312 wl_shell_destroy(display->shell);
3313
3314 if (display->shm)
3315 wl_shm_destroy(display->shm);
3316
3317 if (display->data_device_manager)
3318 wl_data_device_manager_destroy(display->data_device_manager);
3319
3320 wl_compositor_destroy(display->compositor);
3321
3322 close(display->epoll_fd);
3323
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003324 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003325 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003326 free(display);
3327}
3328
3329void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003330display_set_user_data(struct display *display, void *data)
3331{
3332 display->user_data = data;
3333}
3334
3335void *
3336display_get_user_data(struct display *display)
3337{
3338 return display->user_data;
3339}
3340
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003341struct wl_display *
3342display_get_display(struct display *display)
3343{
3344 return display->display;
3345}
3346
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003347struct output *
3348display_get_output(struct display *display)
3349{
3350 return container_of(display->output_list.next, struct output, link);
3351}
3352
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003353struct wl_compositor *
3354display_get_compositor(struct display *display)
3355{
3356 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003357}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003358
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003359uint32_t
3360display_get_serial(struct display *display)
3361{
3362 return display->serial;
3363}
3364
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003365EGLDisplay
3366display_get_egl_display(struct display *d)
3367{
3368 return d->dpy;
3369}
3370
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003371struct wl_data_source *
3372display_create_data_source(struct display *display)
3373{
3374 return wl_data_device_manager_create_data_source(display->data_device_manager);
3375}
3376
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003377EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003378display_get_argb_egl_config(struct display *d)
3379{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003380 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003381}
3382
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003383struct wl_shell *
3384display_get_shell(struct display *display)
3385{
3386 return display->shell;
3387}
3388
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003389int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003390display_acquire_window_surface(struct display *display,
3391 struct window *window,
3392 EGLContext ctx)
3393{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003394#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003395 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003396 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003397
3398 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003399 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003400 device = cairo_surface_get_device(window->cairo_surface);
3401 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003402 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003403
Benjamin Franzke0c991632011-09-27 21:57:31 +02003404 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003405 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003406 ctx = display->argb_ctx;
3407 else
3408 assert(0);
3409 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003410
3411 data = cairo_surface_get_user_data(window->cairo_surface,
3412 &surface_data_key);
3413
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003414 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003415 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003416 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3417 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003418
3419 return 0;
3420#else
3421 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003422#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003423}
3424
3425void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003426display_release_window_surface(struct display *display,
3427 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003428{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003429#ifdef HAVE_CAIRO_EGL
3430 cairo_device_t *device;
3431
3432 device = cairo_surface_get_device(window->cairo_surface);
3433 if (!device)
3434 return;
3435
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003436 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003437 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003438 cairo_device_release(device);
3439#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003440}
3441
3442void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003443display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003444{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003445 wl_list_insert(&display->deferred_list, &task->link);
3446}
3447
3448void
3449display_watch_fd(struct display *display,
3450 int fd, uint32_t events, struct task *task)
3451{
3452 struct epoll_event ep;
3453
3454 ep.events = events;
3455 ep.data.ptr = task;
3456 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3457}
3458
3459void
3460display_run(struct display *display)
3461{
3462 struct task *task;
3463 struct epoll_event ep[16];
3464 int i, count;
3465
Pekka Paalanen826d7952011-12-15 10:14:07 +02003466 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003467 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003468 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003469
3470 while (!wl_list_empty(&display->deferred_list)) {
3471 task = container_of(display->deferred_list.next,
3472 struct task, link);
3473 wl_list_remove(&task->link);
3474 task->run(task, 0);
3475 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003476
3477 if (!display->running)
3478 break;
3479
3480 wl_display_flush(display->display);
3481
3482 count = epoll_wait(display->epoll_fd,
3483 ep, ARRAY_LENGTH(ep), -1);
3484 for (i = 0; i < count; i++) {
3485 task = ep[i].data.ptr;
3486 task->run(task, ep[i].events);
3487 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003488 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003489}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003490
3491void
3492display_exit(struct display *display)
3493{
3494 display->running = 0;
3495}