blob: 286a8ce5aaa8d2eb0ed0c494268d7a4361a2d00c [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040039
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040040#include <pixman.h>
41
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Rob Clark6396ed32012-03-11 19:48:41 -050044#ifdef USE_CAIRO_GLESV2
45#include <GLES2/gl2.h>
46#include <GLES2/gl2ext.h>
47#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040048#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050049#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <EGL/egl.h>
51#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040052
Kristian Høgsberg8def2642011-01-14 17:41:33 -050053#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050056
Daniel Stone9d4f0302012-02-15 16:33:21 +000057#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030058#include <X11/Xcursor/Xcursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040059
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020061#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040062#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050063
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050065
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030066struct cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070067struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030068
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050069struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050070 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050071 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040072 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040073 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040074 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040075 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050076 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020077 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020078 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040079 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040080
81 int display_fd;
82 uint32_t mask;
83 struct task display_task;
84
85 int epoll_fd;
86 struct wl_list deferred_list;
87
Pekka Paalanen826d7952011-12-15 10:14:07 +020088 int running;
89
Kristian Høgsberg478d9262010-06-08 20:34:11 -040090 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040091 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050092 struct wl_list output_list;
Kristian Hø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;
189 struct wl_input_device *input_device;
190 struct window *pointer_focus;
191 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300192 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400194 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400195 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400196 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400197
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500198 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500199 struct widget *grab;
200 uint32_t grab_button;
201
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400202 struct wl_data_device *data_device;
203 struct data_offer *drag_offer;
204 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400205};
206
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500207struct output {
208 struct display *display;
209 struct wl_output *output;
210 struct rectangle allocation;
211 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200212
213 display_output_handler_t destroy_handler;
214 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500215};
216
Martin Minarik1998b152012-05-10 02:04:35 +0200217enum frame_button_action {
218 FRAME_BUTTON_NULL = 0,
219 FRAME_BUTTON_ICON = 1,
220 FRAME_BUTTON_CLOSE = 2,
221 FRAME_BUTTON_MINIMIZE = 3,
222 FRAME_BUTTON_MAXIMIZE = 4,
223};
224
225enum frame_button_pointer {
226 FRAME_BUTTON_DEFAULT = 0,
227 FRAME_BUTTON_OVER = 1,
228 FRAME_BUTTON_ACTIVE = 2,
229};
230
231enum frame_button_align {
232 FRAME_BUTTON_RIGHT = 0,
233 FRAME_BUTTON_LEFT = 1,
234};
235
236enum frame_button_decoration {
237 FRAME_BUTTON_NONE = 0,
238 FRAME_BUTTON_FANCY = 1,
239};
240
241struct frame_button {
242 struct widget *widget;
243 struct frame *frame;
244 cairo_surface_t *icon;
245 enum frame_button_action type;
246 enum frame_button_pointer state;
247 struct wl_list link; /* buttons_list */
248 enum frame_button_align align;
249 enum frame_button_decoration decoration;
250};
251
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500252struct frame {
253 struct widget *widget;
254 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200255 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500256};
257
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500258struct menu {
259 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500260 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500261 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500262 const char **entries;
263 uint32_t time;
264 int current;
265 int count;
266 menu_func_t func;
267};
268
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300269struct cursor_image {
270 cairo_surface_t *surface;
271 int width, height;
272 int hotspot_x, hotspot_y;
273 int delay;
274};
275
276struct cursor {
277 int n_images;
278 struct cursor_image *images;
279};
280
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700281struct shm_pool {
282 struct wl_shm_pool *pool;
283 size_t size;
284 size_t used;
285 void *data;
286};
287
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400288enum {
289 POINTER_DEFAULT = 100,
290 POINTER_UNSET
291};
292
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500293enum window_location {
294 WINDOW_INTERIOR = 0,
295 WINDOW_RESIZING_TOP = 1,
296 WINDOW_RESIZING_BOTTOM = 2,
297 WINDOW_RESIZING_LEFT = 4,
298 WINDOW_RESIZING_TOP_LEFT = 5,
299 WINDOW_RESIZING_BOTTOM_LEFT = 6,
300 WINDOW_RESIZING_RIGHT = 8,
301 WINDOW_RESIZING_TOP_RIGHT = 9,
302 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
303 WINDOW_RESIZING_MASK = 15,
304 WINDOW_EXTERIOR = 16,
305 WINDOW_TITLEBAR = 17,
306 WINDOW_CLIENT_AREA = 18,
307};
308
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400309const char *option_xkb_layout = "us";
310const char *option_xkb_variant = "";
311const char *option_xkb_options = "";
312
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400313static const struct weston_option xkb_options[] = {
314 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
315 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
316 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400317};
318
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319static const cairo_user_data_key_t surface_data_key;
320struct surface_data {
321 struct wl_buffer *buffer;
322};
323
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500324#define MULT(_d,c,a,t) \
325 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
326
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500327#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100329struct egl_window_surface_data {
330 struct display *display;
331 struct wl_surface *surface;
332 struct wl_egl_window *window;
333 EGLSurface surf;
334};
335
336static void
337egl_window_surface_data_destroy(void *p)
338{
339 struct egl_window_surface_data *data = p;
340 struct display *d = data->display;
341
342 eglDestroySurface(d->dpy, data->surf);
343 wl_egl_window_destroy(data->window);
344 data->surface = NULL;
345
346 free(p);
347}
348
349static cairo_surface_t *
350display_create_egl_window_surface(struct display *display,
351 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400352 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100353 struct rectangle *rectangle)
354{
355 cairo_surface_t *cairo_surface;
356 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400357 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200358 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100359
360 data = malloc(sizeof *data);
361 if (data == NULL)
362 return NULL;
363
364 data->display = display;
365 data->surface = surface;
366
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500367 config = display->argb_config;
368 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400369
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400370 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100371 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400372 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100373
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500375 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100376
Benjamin Franzke0c991632011-09-27 21:57:31 +0200377 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378 data->surf,
379 rectangle->width,
380 rectangle->height);
381
382 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
383 data, egl_window_surface_data_destroy);
384
385 return cairo_surface;
386}
387
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400388#endif
389
390struct wl_buffer *
391display_get_buffer_for_surface(struct display *display,
392 cairo_surface_t *surface)
393{
394 struct surface_data *data;
395
396 data = cairo_surface_get_user_data (surface, &surface_data_key);
397
398 return data->buffer;
399}
400
401struct shm_surface_data {
402 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700403 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404};
405
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500406static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700407shm_pool_destroy(struct shm_pool *pool);
408
409static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410shm_surface_data_destroy(void *p)
411{
412 struct shm_surface_data *data = p;
413
414 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700415 if (data->pool)
416 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417}
418
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300419static void
420shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
421{
422 void *dest = cairo_image_surface_get_data(surface);
423
424 memcpy(dest, data, count);
425}
426
Kristian Høgsberg16626282012-04-03 11:21:27 -0400427static struct wl_shm_pool *
428make_shm_pool(struct display *display, int size, void **data)
429{
430 char filename[] = "/tmp/wayland-shm-XXXXXX";
431 struct wl_shm_pool *pool;
432 int fd;
433
434 fd = mkstemp(filename);
435 if (fd < 0) {
436 fprintf(stderr, "open %s failed: %m\n", filename);
437 return NULL;
438 }
439 if (ftruncate(fd, size) < 0) {
440 fprintf(stderr, "ftruncate failed: %m\n");
441 close(fd);
442 return NULL;
443 }
444
445 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
446 unlink(filename);
447
448 if (*data == MAP_FAILED) {
449 fprintf(stderr, "mmap failed: %m\n");
450 close(fd);
451 return NULL;
452 }
453
454 pool = wl_shm_create_pool(display->shm, fd, size);
455
456 close(fd);
457
458 return pool;
459}
460
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700461static struct shm_pool *
462shm_pool_create(struct display *display, size_t size)
463{
464 struct shm_pool *pool = malloc(sizeof *pool);
465
466 if (!pool)
467 return NULL;
468
469 pool->pool = make_shm_pool(display, size, &pool->data);
470 if (!pool->pool) {
471 free(pool);
472 return NULL;
473 }
474
475 pool->size = size;
476 pool->used = 0;
477
478 return pool;
479}
480
481static void *
482shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
483{
484 if (pool->used + size > pool->size)
485 return NULL;
486
487 *offset = pool->used;
488 pool->used += size;
489
490 return (char *) pool->data + *offset;
491}
492
493/* destroy the pool. this does not unmap the memory though */
494static void
495shm_pool_destroy(struct shm_pool *pool)
496{
497 munmap(pool->data, pool->size);
498 wl_shm_pool_destroy(pool->pool);
499 free(pool);
500}
501
502/* Start allocating from the beginning of the pool again */
503static void
504shm_pool_reset(struct shm_pool *pool)
505{
506 pool->used = 0;
507}
508
509static int
510data_length_for_shm_surface(struct rectangle *rect)
511{
512 int stride;
513
514 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
515 rect->width);
516 return stride * rect->height;
517}
518
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500519static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700520display_create_shm_surface_from_pool(struct display *display,
521 struct rectangle *rectangle,
522 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523{
524 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400525 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400526 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700527 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400528 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400529
530 data = malloc(sizeof *data);
531 if (data == NULL)
532 return NULL;
533
534 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
535 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700536 length = stride * rectangle->height;
537 data->pool = NULL;
538 map = shm_pool_allocate(pool, length, &offset);
539
540 if (!map) {
541 free(data);
542 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400543 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400544
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400545 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546 CAIRO_FORMAT_ARGB32,
547 rectangle->width,
548 rectangle->height,
549 stride);
550
551 cairo_surface_set_user_data (surface, &surface_data_key,
552 data, shm_surface_data_destroy);
553
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400554 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500555 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400556 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500557 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400558
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700559 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400560 rectangle->width,
561 rectangle->height,
562 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700564 return surface;
565}
566
567static cairo_surface_t *
568display_create_shm_surface(struct display *display,
569 struct rectangle *rectangle, uint32_t flags,
570 struct window *window)
571{
572 struct shm_surface_data *data;
573 struct shm_pool *pool;
574 cairo_surface_t *surface;
575
576 if (window && window->pool) {
577 shm_pool_reset(window->pool);
578 surface = display_create_shm_surface_from_pool(display,
579 rectangle,
580 flags,
581 window->pool);
582 if (surface)
583 return surface;
584 }
585
586 pool = shm_pool_create(display,
587 data_length_for_shm_surface(rectangle));
588 if (!pool)
589 return NULL;
590
591 surface =
592 display_create_shm_surface_from_pool(display, rectangle,
593 flags, pool);
594
595 if (!surface) {
596 shm_pool_destroy(pool);
597 return NULL;
598 }
599
600 /* make sure we destroy the pool when the surface is destroyed */
601 data = cairo_surface_get_user_data(surface, &surface_data_key);
602 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400603
604 return surface;
605}
606
nobled7b87cb02011-02-01 18:51:47 +0000607static int
608check_size(struct rectangle *rect)
609{
610 if (rect->width && rect->height)
611 return 0;
612
613 fprintf(stderr, "tried to create surface of "
614 "width: %d, height: %d\n", rect->width, rect->height);
615 return -1;
616}
617
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618cairo_surface_t *
619display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100620 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400621 struct rectangle *rectangle,
622 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400623{
nobled7b87cb02011-02-01 18:51:47 +0000624 if (check_size(rectangle) < 0)
625 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500626#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400627 if (display->dpy)
628 return display_create_egl_window_surface(display,
629 surface,
630 flags,
631 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400633 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634}
635
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300636static const char *cursors[] = {
637 "bottom_left_corner",
638 "bottom_right_corner",
639 "bottom_side",
640 "grabbing",
641 "left_ptr",
642 "left_side",
643 "right_side",
644 "top_left_corner",
645 "top_right_corner",
646 "top_side",
647 "xterm",
648 "hand1",
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400649};
650
651static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300652create_cursor_from_images(struct display *display, struct cursor *cursor,
653 XcursorImages *images)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400654{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300655 int i;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400656 struct rectangle rect;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300657 XcursorImage *image;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400658
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300659 cursor->images = malloc(images->nimage * sizeof *cursor->images);
660 cursor->n_images = images->nimage;
661
662 for (i = 0; i < images->nimage; i++) {
663 image = images->images[i];
664
665 rect.width = image->width;
666 rect.height = image->height;
667
668 cursor->images[i].surface =
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700669 display_create_shm_surface_from_pool(display, &rect, 0,
670 display->cursor_shm_pool);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300671
672 shm_surface_write(cursor->images[i].surface,
673 (unsigned char *) image->pixels,
674 image->width * image->height * sizeof image->pixels[0]);
675
676 cursor->images[i].width = image->width;
677 cursor->images[i].height = image->height;
678 cursor->images[i].hotspot_x = image->xhot;
679 cursor->images[i].hotspot_y = image->yhot;
680 cursor->images[i].delay = image->delay;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400681 }
682
683}
684
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700685static size_t
686data_length_for_cursor_images(XcursorImages *images)
687{
688 int i;
689 size_t length = 0;
690 struct rectangle rect;
691
692 for (i = 0; i < images->nimage; i++) {
693 rect.width = images->images[i]->width;
694 rect.height = images->images[i]->height;
695 length += data_length_for_shm_surface(&rect);
696 }
697
698 return length;
699}
700
Pekka Paalanen325bb602011-12-19 10:31:45 +0200701static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300702create_cursors(struct display *display)
703{
704 int i, count;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700705 size_t pool_size = 0;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300706 struct cursor *cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700707 XcursorImages **images;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300708
709 count = ARRAY_LENGTH(cursors);
710 display->cursors = malloc(count * sizeof *display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700711 images = malloc(count * sizeof images[0]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300712
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700713 for (i = 0; i < count; i++) {
714 images[i] = XcursorLibraryLoadImages(cursors[i], NULL, 32);
Rafal Mielniczuk87e4c932012-05-08 22:10:44 +0200715 if (!images[i]) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300716 fprintf(stderr, "Error loading cursor: %s\n",
717 cursors[i]);
718 continue;
719 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700720 pool_size += data_length_for_cursor_images(images[i]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300721 }
722
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723 display->cursor_shm_pool = shm_pool_create(display, pool_size);
724
725 for (i = 0; i < count; i++) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700726 cursor = &display->cursors[i];
Dima Ryazanovff1c2d72012-05-08 21:02:33 -0700727
728 if (!images[i]) {
729 cursor->n_images = 0;
730 cursor->images = NULL;
731 continue;
732 }
733
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700734 create_cursor_from_images(display, cursor, images[i]);
735
736 XcursorImagesDestroy(images[i]);
737 }
738
739 free(images);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300740}
741
742static void
743destroy_cursor_images(struct cursor *cursor)
744{
745 int i;
746
747 for (i = 0; i < cursor->n_images; i++)
748 if (cursor->images[i].surface)
749 cairo_surface_destroy(cursor->images[i].surface);
750
751 free(cursor->images);
752}
753
754static void
755destroy_cursors(struct display *display)
Pekka Paalanen325bb602011-12-19 10:31:45 +0200756{
757 int i, count;
758
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300759 count = ARRAY_LENGTH(cursors);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200760 for (i = 0; i < count; ++i) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300761 destroy_cursor_images(&display->cursors[i]);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200762 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700763
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300764 free(display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700765 shm_pool_destroy(display->cursor_shm_pool);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200766}
767
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400768cairo_surface_t *
769display_get_pointer_surface(struct display *display, int pointer,
770 int *width, int *height,
771 int *hotspot_x, int *hotspot_y)
772{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300773 struct cursor *cursor = &display->cursors[pointer];
774 cairo_surface_t *surface = cursor->images[0].surface;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400775
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300776 /* FIXME returning information for the first image. Something better
777 * is needed for animated cursors */
778
nobledf8475c92011-01-05 17:41:55 +0000779 *width = cairo_image_surface_get_width(surface);
780 *height = cairo_image_surface_get_height(surface);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300781
782 *hotspot_x = cursor->images[0].hotspot_x;
783 *hotspot_y = cursor->images[0].hotspot_y;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400784
785 return cairo_surface_reference(surface);
786}
787
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400788static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200789window_get_resize_dx_dy(struct window *window, int *x, int *y)
790{
791 if (window->resize_edges & WINDOW_RESIZING_LEFT)
792 *x = window->server_allocation.width - window->allocation.width;
793 else
794 *x = 0;
795
796 if (window->resize_edges & WINDOW_RESIZING_TOP)
797 *y = window->server_allocation.height -
798 window->allocation.height;
799 else
800 *y = 0;
801
802 window->resize_edges = 0;
803}
804
805static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500806window_attach_surface(struct window *window)
807{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400808 struct display *display = window->display;
809 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000810#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000812#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500813 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400815 if (window->type == TYPE_NONE) {
816 window->type = TYPE_TOPLEVEL;
817 if (display->shell)
818 wl_shell_surface_set_toplevel(window->shell_surface);
819 }
820
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
824 data = cairo_surface_get_user_data(window->cairo_surface,
825 &surface_data_key);
826
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 cairo_gl_surface_swapbuffers(window->cairo_surface);
828 wl_egl_window_get_attached_size(data->window,
829 &window->server_allocation.width,
830 &window->server_allocation.height);
831 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000832#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100833 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834 buffer =
835 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400836 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200838 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400840 wl_surface_damage(window->surface, 0, 0,
841 window->allocation.width,
842 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400844 cairo_surface_destroy(window->cairo_surface);
845 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000847 default:
848 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500850
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500851 if (window->input_region) {
852 wl_surface_set_input_region(window->surface,
853 window->input_region);
854 wl_region_destroy(window->input_region);
855 window->input_region = NULL;
856 }
857
858 if (window->opaque_region) {
859 wl_surface_set_opaque_region(window->surface,
860 window->opaque_region);
861 wl_region_destroy(window->opaque_region);
862 window->opaque_region = NULL;
863 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500864}
865
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500866void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400867window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500868{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400869 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100870 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500871}
872
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400873void
874window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400875{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500876 cairo_surface_reference(surface);
877
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400878 if (window->cairo_surface != NULL)
879 cairo_surface_destroy(window->cairo_surface);
880
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500881 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400882}
883
Benjamin Franzke22d54812011-07-16 19:50:32 +0000884#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100885static void
886window_resize_cairo_window_surface(struct window *window)
887{
888 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200889 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100890
891 data = cairo_surface_get_user_data(window->cairo_surface,
892 &surface_data_key);
893
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200894 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100895 wl_egl_window_resize(data->window,
896 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200897 window->allocation.height,
898 x,y);
899
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100900 cairo_gl_surface_set_size(window->cairo_surface,
901 window->allocation.width,
902 window->allocation.height);
903}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000904#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400906struct display *
907window_get_display(struct window *window)
908{
909 return window->display;
910}
911
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400912void
913window_create_surface(struct window *window)
914{
915 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400916 uint32_t flags = 0;
917
918 if (!window->transparent)
919 flags = SURFACE_OPAQUE;
920
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400921 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500922#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100923 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
924 if (window->cairo_surface) {
925 window_resize_cairo_window_surface(window);
926 return;
927 }
928 surface = display_create_surface(window->display,
929 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400930 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100931 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400932#endif
933 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400934 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400935 &window->allocation,
936 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400937 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800938 default:
939 surface = NULL;
940 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400941 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400942
943 window_set_surface(window, surface);
944 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945}
946
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200947static void frame_destroy(struct frame *frame);
948
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400949void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500950window_destroy(struct window *window)
951{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200952 struct display *display = window->display;
953 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100954 struct window_output *window_output;
955 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200956
957 if (window->redraw_scheduled)
958 wl_list_remove(&window->redraw_task.link);
959
960 wl_list_for_each(input, &display->input_list, link) {
961 if (input->pointer_focus == window)
962 input->pointer_focus = NULL;
963 if (input->keyboard_focus == window)
964 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500965 if (input->focus_widget &&
966 input->focus_widget->window == window)
967 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200968 }
969
Rob Bradford7507b572012-05-15 17:55:34 +0100970 wl_list_for_each_safe(window_output, window_output_tmp,
971 &window->window_output_list, link) {
972 free (window_output);
973 }
974
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500975 if (window->input_region)
976 wl_region_destroy(window->input_region);
977 if (window->opaque_region)
978 wl_region_destroy(window->opaque_region);
979
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200980 if (window->frame)
981 frame_destroy(window->frame);
982
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200983 if (window->shell_surface)
984 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500985 wl_surface_destroy(window->surface);
986 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200987
988 if (window->cairo_surface != NULL)
989 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200990
991 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500992 free(window);
993}
994
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500995static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500996widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400997{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500998 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400999
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001000 wl_list_for_each(child, &widget->child_list, link) {
1001 target = widget_find_widget(child, x, y);
1002 if (target)
1003 return target;
1004 }
1005
1006 if (widget->allocation.x <= x &&
1007 x < widget->allocation.x + widget->allocation.width &&
1008 widget->allocation.y <= y &&
1009 y < widget->allocation.y + widget->allocation.height) {
1010 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001011 }
1012
1013 return NULL;
1014}
1015
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001016static struct widget *
1017widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001018{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001019 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001020
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001021 widget = malloc(sizeof *widget);
1022 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001023 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001024 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001025 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001026 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001027 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001028
1029 return widget;
1030}
1031
1032struct widget *
1033window_add_widget(struct window *window, void *data)
1034{
1035 window->widget = widget_create(window, data);
1036 wl_list_init(&window->widget->link);
1037
1038 return window->widget;
1039}
1040
1041struct widget *
1042widget_add_widget(struct widget *parent, void *data)
1043{
1044 struct widget *widget;
1045
1046 widget = widget_create(parent->window, data);
1047 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001048
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001049 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001050}
1051
1052void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001053widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001054{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001055 struct display *display = widget->window->display;
1056 struct input *input;
1057
1058 wl_list_for_each(input, &display->input_list, link) {
1059 if (input->focus_widget == widget)
1060 input->focus_widget = NULL;
1061 }
1062
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001063 wl_list_remove(&widget->link);
1064 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001065}
1066
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001067void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001068widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001069{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001070 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071}
1072
1073void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001074widget_set_size(struct widget *widget, int32_t width, int32_t height)
1075{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001076 widget->allocation.width = width;
1077 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001078}
1079
1080void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001081widget_set_allocation(struct widget *widget,
1082 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001083{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001084 widget->allocation.x = x;
1085 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001086 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087}
1088
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001089void
1090widget_set_transparent(struct widget *widget, int transparent)
1091{
1092 widget->opaque = !transparent;
1093}
1094
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001095void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099}
1100
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001101void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001102widget_set_resize_handler(struct widget *widget,
1103 widget_resize_handler_t handler)
1104{
1105 widget->resize_handler = handler;
1106}
1107
1108void
1109widget_set_redraw_handler(struct widget *widget,
1110 widget_redraw_handler_t handler)
1111{
1112 widget->redraw_handler = handler;
1113}
1114
1115void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001116widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001117{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001118 widget->enter_handler = handler;
1119}
1120
1121void
1122widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1123{
1124 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001125}
1126
1127void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001128widget_set_motion_handler(struct widget *widget,
1129 widget_motion_handler_t handler)
1130{
1131 widget->motion_handler = handler;
1132}
1133
1134void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001135widget_set_button_handler(struct widget *widget,
1136 widget_button_handler_t handler)
1137{
1138 widget->button_handler = handler;
1139}
1140
1141void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001142widget_schedule_redraw(struct widget *widget)
1143{
1144 window_schedule_redraw(widget->window);
1145}
1146
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001147cairo_surface_t *
1148window_get_surface(struct window *window)
1149{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001150 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001151}
1152
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001153struct wl_surface *
1154window_get_wl_surface(struct window *window)
1155{
1156 return window->surface;
1157}
1158
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001159struct wl_shell_surface *
1160window_get_wl_shell_surface(struct window *window)
1161{
1162 return window->shell_surface;
1163}
1164
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001165static void
1166frame_resize_handler(struct widget *widget,
1167 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001168{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169 struct frame *frame = data;
1170 struct widget *child = frame->child;
1171 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001172 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001173 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001174 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001175 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001176 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001177 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001178
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001179 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001180 decoration_width = (t->width + t->margin) * 2;
1181 decoration_height = t->width +
1182 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001183
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001184 allocation.x = t->width + t->margin;
1185 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001186 allocation.width = width - decoration_width;
1187 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001188
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001189 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001190
1191 wl_list_for_each(button, &frame->buttons_list, link)
1192 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001193 } else {
1194 decoration_width = 0;
1195 decoration_height = 0;
1196
1197 allocation.x = 0;
1198 allocation.y = 0;
1199 allocation.width = width;
1200 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001201 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001202
1203 wl_list_for_each(button, &frame->buttons_list, link)
1204 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001205 }
1206
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001207 widget_set_allocation(child, allocation.x, allocation.y,
1208 allocation.width, allocation.height);
1209
1210 if (child->resize_handler)
1211 child->resize_handler(child,
1212 allocation.width,
1213 allocation.height,
1214 child->user_data);
1215
Scott Moreauf7e498c2012-05-14 11:39:29 -06001216 width = child->allocation.width + decoration_width;
1217 height = child->allocation.height + decoration_height;
1218
1219 widget->window->input_region =
1220 wl_compositor_create_region(display->compositor);
1221 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001222 t->margin, t->margin,
1223 width - 2 * t->margin,
1224 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001225
1226 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001227
1228 if (child->opaque) {
1229 widget->window->opaque_region =
1230 wl_compositor_create_region(display->compositor);
1231 wl_region_add(widget->window->opaque_region,
1232 opaque_margin, opaque_margin,
1233 widget->allocation.width - 2 * opaque_margin,
1234 widget->allocation.height - 2 * opaque_margin);
1235 }
Martin Minarik1998b152012-05-10 02:04:35 +02001236
1237 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001238 x_r = frame->widget->allocation.width - t->width - t->margin;
1239 x_l = t->width + t->margin;
1240 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001241 wl_list_for_each(button, &frame->buttons_list, link) {
1242 const int button_padding = 4;
1243 w = cairo_image_surface_get_width(button->icon);
1244 h = cairo_image_surface_get_height(button->icon);
1245
1246 if (button->decoration == FRAME_BUTTON_FANCY)
1247 w += 10;
1248
1249 if (button->align == FRAME_BUTTON_LEFT) {
1250 widget_set_allocation(button->widget,
1251 x_l, y , w + 1, h + 1);
1252 x_l += w;
1253 x_l += button_padding;
1254 } else {
1255 x_r -= w;
1256 widget_set_allocation(button->widget,
1257 x_r, y , w + 1, h + 1);
1258 x_r -= button_padding;
1259 }
1260 }
1261}
1262
1263static int
1264frame_button_enter_handler(struct widget *widget,
1265 struct input *input, float x, float y, void *data)
1266{
1267 struct frame_button *frame_button = data;
1268
1269 widget_schedule_redraw(frame_button->widget);
1270 frame_button->state = FRAME_BUTTON_OVER;
1271
1272 return POINTER_LEFT_PTR;
1273}
1274
1275static void
1276frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1277{
1278 struct frame_button *frame_button = data;
1279
1280 widget_schedule_redraw(frame_button->widget);
1281 frame_button->state = FRAME_BUTTON_DEFAULT;
1282}
1283
1284static void
1285frame_button_button_handler(struct widget *widget,
1286 struct input *input, uint32_t time,
1287 uint32_t button, uint32_t state, void *data)
1288{
1289 struct frame_button *frame_button = data;
1290 struct window *window = widget->window;
1291
1292 if (button != BTN_LEFT)
1293 return;
1294
1295 switch (state) {
1296 case 1:
1297 frame_button->state = FRAME_BUTTON_ACTIVE;
1298 widget_schedule_redraw(frame_button->widget);
1299
1300 if (frame_button->type == FRAME_BUTTON_ICON)
1301 window_show_frame_menu(window, input, time);
1302 return;
1303 case 0:
1304 frame_button->state = FRAME_BUTTON_DEFAULT;
1305 widget_schedule_redraw(frame_button->widget);
1306 break;
1307 }
1308
1309 switch (frame_button->type) {
1310 case FRAME_BUTTON_CLOSE:
1311 if (window->close_handler)
1312 window->close_handler(window->parent,
1313 window->user_data);
1314 else
1315 display_exit(window->display);
1316 break;
1317 case FRAME_BUTTON_MINIMIZE:
1318 fprintf(stderr,"Minimize stub\n");
1319 break;
1320 case FRAME_BUTTON_MAXIMIZE:
1321 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1322 break;
1323 default:
1324 /* Unknown operation */
1325 break;
1326 }
1327}
1328
1329static void
1330frame_button_redraw_handler(struct widget *widget, void *data)
1331{
1332 struct frame_button *frame_button = data;
1333 cairo_t *cr;
1334 int width, height, x, y;
1335 struct window *window = widget->window;
1336
1337 x = widget->allocation.x;
1338 y = widget->allocation.y;
1339 width = widget->allocation.width;
1340 height = widget->allocation.height;
1341
1342 if (!width)
1343 return;
1344 if (!height)
1345 return;
1346 if (widget->opaque)
1347 return;
1348
1349 cr = cairo_create(window->cairo_surface);
1350
1351 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1352 cairo_set_line_width(cr, 1);
1353
1354 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1355 cairo_rectangle (cr, x, y, 25, 16);
1356
1357 cairo_stroke_preserve(cr);
1358
1359 switch (frame_button->state) {
1360 case FRAME_BUTTON_DEFAULT:
1361 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1362 break;
1363 case FRAME_BUTTON_OVER:
1364 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1365 break;
1366 case FRAME_BUTTON_ACTIVE:
1367 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1368 break;
1369 }
1370
1371 cairo_fill (cr);
1372
1373 x += 4;
1374 }
1375
1376 cairo_set_source_surface(cr, frame_button->icon, x, y);
1377 cairo_paint(cr);
1378
1379 cairo_destroy(cr);
1380}
1381
1382static struct widget *
1383frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1384 enum frame_button_align align, enum frame_button_decoration style)
1385{
1386 struct frame_button *frame_button;
1387 const char *icon = data;
1388
1389 frame_button = malloc (sizeof *frame_button);
1390 memset(frame_button, 0, sizeof *frame_button);
1391
1392 frame_button->icon = cairo_image_surface_create_from_png(icon);
1393 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1394 frame_button->frame = frame;
1395 frame_button->type = type;
1396 frame_button->align = align;
1397 frame_button->decoration = style;
1398
1399 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1400
1401 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1402 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1403 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1404 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1405 return frame_button->widget;
1406}
1407
1408static void
1409frame_button_destroy(struct frame_button *frame_button)
1410{
1411 widget_destroy(frame_button->widget);
1412 wl_list_remove(&frame_button->link);
1413 cairo_surface_destroy(frame_button->icon);
1414 free(frame_button);
1415
1416 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001417}
1418
1419static void
1420frame_redraw_handler(struct widget *widget, void *data)
1421{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001422 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001423 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001424 struct theme *t = window->display->theme;
1425 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001426
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001427 if (window->type == TYPE_FULLSCREEN)
1428 return;
1429
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001430 cr = cairo_create(window->cairo_surface);
1431
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001432 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001433 flags |= THEME_FRAME_ACTIVE;
1434 theme_render_frame(t, cr, widget->allocation.width,
1435 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001436
1437 cairo_destroy(cr);
1438}
1439
1440static int
1441frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1442{
1443 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001444 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001445 const int grip_size = 8;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001446 struct theme *t = widget->window->display->theme;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001447
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001448 if (x < t->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001449 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001450 else if (t->margin <= x && x < t->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001451 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001452 else if (x < widget->allocation.width - t->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001453 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001454 else if (x < widget->allocation.width - t->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001455 hlocation = WINDOW_RESIZING_RIGHT;
1456 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001457 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001458
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001459 if (y < t->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001460 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001461 else if (t->margin <= y && y < t->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001462 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001463 else if (y < widget->allocation.height - t->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001464 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001465 else if (y < widget->allocation.height - t->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001466 vlocation = WINDOW_RESIZING_BOTTOM;
1467 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001468 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001469
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001470 location = vlocation | hlocation;
1471 if (location & WINDOW_EXTERIOR)
1472 location = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001473 if (location == WINDOW_INTERIOR && y < t->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001474 location = WINDOW_TITLEBAR;
1475 else if (location == WINDOW_INTERIOR)
1476 location = WINDOW_CLIENT_AREA;
1477
1478 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001479}
1480
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001481static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001482frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001483{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001484 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001485
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001486 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001487 switch (location) {
1488 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001489 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001490 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001491 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001492 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001493 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001494 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001495 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001496 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001497 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001498 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001499 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001500 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001501 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001502 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001503 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001504 case WINDOW_EXTERIOR:
1505 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001506 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001507 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001508 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001509}
1510
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001511static void
1512frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001513{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001514 switch (index) {
1515 case 0: /* close */
1516 if (window->close_handler)
1517 window->close_handler(window->parent,
1518 window->user_data);
1519 else
1520 display_exit(window->display);
1521 break;
1522 case 1: /* fullscreen */
1523 /* we don't have a way to get out of fullscreen for now */
1524 window_set_fullscreen(window, 1);
1525 break;
1526 case 2: /* rotate */
1527 case 3: /* scale */
1528 break;
1529 }
1530}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001531
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001532void
1533window_show_frame_menu(struct window *window,
1534 struct input *input, uint32_t time)
1535{
1536 int32_t x, y;
1537
1538 static const char *entries[] = {
1539 "Close", "Fullscreen", "Rotate", "Scale"
1540 };
1541
1542 input_get_position(input, &x, &y);
1543 window_show_menu(window->display, input, time, window,
1544 x - 10, y - 10, frame_menu_func, entries, 4);
1545}
1546
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001547static int
1548frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001549 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001550{
1551 return frame_get_pointer_image_for_location(data, input);
1552}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001553
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001554static int
1555frame_motion_handler(struct widget *widget,
1556 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001557 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001558{
1559 return frame_get_pointer_image_for_location(data, input);
1560}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001561
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001562static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001563frame_button_handler(struct widget *widget,
1564 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001565 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001566
1567{
1568 struct frame *frame = data;
1569 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001570 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001571 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001572
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001573 location = frame_get_pointer_location(frame, input->sx, input->sy);
1574
1575 if (window->display->shell && button == BTN_LEFT && state == 1) {
1576 switch (location) {
1577 case WINDOW_TITLEBAR:
1578 if (!window->shell_surface)
1579 break;
1580 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001581 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001582 wl_shell_surface_move(window->shell_surface,
1583 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001584 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001585 break;
1586 case WINDOW_RESIZING_TOP:
1587 case WINDOW_RESIZING_BOTTOM:
1588 case WINDOW_RESIZING_LEFT:
1589 case WINDOW_RESIZING_RIGHT:
1590 case WINDOW_RESIZING_TOP_LEFT:
1591 case WINDOW_RESIZING_TOP_RIGHT:
1592 case WINDOW_RESIZING_BOTTOM_LEFT:
1593 case WINDOW_RESIZING_BOTTOM_RIGHT:
1594 if (!window->shell_surface)
1595 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001596 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001597
1598 if (!display->dpy) {
1599 /* If we're using shm, allocate a big
1600 pool to create buffers out of while
1601 we resize. We should probably base
1602 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001603 window->pool =
1604 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001605 }
1606
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001607 wl_shell_surface_resize(window->shell_surface,
1608 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001609 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610 break;
1611 }
1612 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001613 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614 }
1615}
1616
1617struct widget *
1618frame_create(struct window *window, void *data)
1619{
1620 struct frame *frame;
1621
1622 frame = malloc(sizeof *frame);
1623 memset(frame, 0, sizeof *frame);
1624
1625 frame->widget = window_add_widget(window, frame);
1626 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001627
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001628 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1629 widget_set_resize_handler(frame->widget, frame_resize_handler);
1630 widget_set_enter_handler(frame->widget, frame_enter_handler);
1631 widget_set_motion_handler(frame->widget, frame_motion_handler);
1632 widget_set_button_handler(frame->widget, frame_button_handler);
1633
Martin Minarik1998b152012-05-10 02:04:35 +02001634 /* Create empty list for frame buttons */
1635 wl_list_init(&frame->buttons_list);
1636
1637 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1638 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1639
1640 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1641 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1642
1643 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1644 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1645
1646 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1647 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1648
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001649 window->frame = frame;
1650
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001651 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001652}
1653
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001654static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001655frame_destroy(struct frame *frame)
1656{
Martin Minarik1998b152012-05-10 02:04:35 +02001657 struct frame_button *button, *tmp;
1658
1659 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1660 frame_button_destroy(button);
1661
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001662 /* frame->child must be destroyed by the application */
1663 widget_destroy(frame->widget);
1664 free(frame);
1665}
1666
1667static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001668input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001669 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001670{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001671 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001672 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001673
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001674 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001675 return;
1676
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001677 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001678 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001679 widget = old;
1680 if (input->grab)
1681 widget = input->grab;
1682 if (widget->leave_handler)
1683 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001684 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001685 }
1686
1687 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001688 widget = focus;
1689 if (input->grab)
1690 widget = input->grab;
1691 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001692 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001693 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001694 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001695
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001696 input_set_pointer_image(input, input->pointer_enter_serial,
1697 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001698 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001699}
1700
1701static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001702input_handle_motion(void *data, struct wl_input_device *input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001703 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001704{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001705 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001706 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001707 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001708 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001709 float sx = wl_fixed_to_double(sx_w);
1710 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001711
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001712 input->sx = sx;
1713 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001714
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001715 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001716 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001717 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001718 }
1719
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001720 if (input->grab)
1721 widget = input->grab;
1722 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001723 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001724 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001725 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001726 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001727 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001728
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001729 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001730}
1731
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001732void
1733input_grab(struct input *input, struct widget *widget, uint32_t button)
1734{
1735 input->grab = widget;
1736 input->grab_button = button;
1737}
1738
1739void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001740input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001741{
1742 struct widget *widget;
1743
1744 input->grab = NULL;
1745 if (input->pointer_focus) {
1746 widget = widget_find_widget(input->pointer_focus->widget,
1747 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001748 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001749 }
1750}
1751
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001752static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001753input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001754 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001755 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001756{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001757 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001758 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001759
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001760 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001761 if (input->focus_widget && input->grab == NULL && state)
1762 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001763
Neil Roberts6b28aad2012-01-23 19:11:18 +00001764 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001765 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001766 (*widget->button_handler)(widget,
1767 input, time,
1768 button, state,
1769 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001770
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001771 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001772 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001773}
1774
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001775static void
Scott Moreau210d0792012-03-22 10:47:01 -06001776input_handle_axis(void *data,
1777 struct wl_input_device *input_device,
1778 uint32_t time, uint32_t axis, int32_t value)
1779{
1780}
1781
1782static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001783input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001784 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
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001845input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001846 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001847 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001848 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001849{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001850 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001851 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001852 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001853 float sx = wl_fixed_to_double(sx_w);
1854 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001855
Martin Minarik1998b152012-05-10 02:04:35 +02001856 if (!surface) {
1857 /* enter event for a window we've just destroyed */
1858 return;
1859 }
1860
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001861 input->display->serial = serial;
1862 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001863 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001864 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001865
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001866 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001867 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001868 window->pool = NULL;
1869 /* Schedule a redraw to free the pool */
1870 window_schedule_redraw(window);
1871 }
1872
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001873 input->sx = sx;
1874 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001875
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001876 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001877 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001878}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001879
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001880static void
1881input_handle_pointer_leave(void *data,
1882 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001883 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001884{
1885 struct input *input = data;
1886
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001887 input->display->serial = serial;
1888 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001889}
1890
1891static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001892input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001893{
1894 struct window *window = input->keyboard_focus;
1895
1896 if (!window)
1897 return;
1898
1899 window->keyboard_device = NULL;
1900 if (window->keyboard_focus_handler)
1901 (*window->keyboard_focus_handler)(window, NULL,
1902 window->user_data);
1903
1904 input->keyboard_focus = NULL;
1905}
1906
1907static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001908input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001909 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001910 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001911 struct wl_surface *surface,
1912 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001913{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001914 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001915 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001916
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001917 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001918 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001919
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001920 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001921 window->keyboard_device = input;
1922 if (window->keyboard_focus_handler)
1923 (*window->keyboard_focus_handler)(window,
1924 window->keyboard_device,
1925 window->user_data);
1926}
1927
1928static void
1929input_handle_keyboard_leave(void *data,
1930 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001931 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001932 struct wl_surface *surface)
1933{
1934 struct input *input = data;
1935
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001936 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001937 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001938}
1939
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001940static void
1941input_handle_touch_down(void *data,
1942 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001943 uint32_t serial, uint32_t time,
1944 struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001945 int32_t id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001946{
1947}
1948
1949static void
1950input_handle_touch_up(void *data,
1951 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001952 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001953{
1954}
1955
1956static void
1957input_handle_touch_motion(void *data,
1958 struct wl_input_device *wl_input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001959 uint32_t time, int32_t id,
1960 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001961{
1962}
1963
1964static void
1965input_handle_touch_frame(void *data,
1966 struct wl_input_device *wl_input_device)
1967{
1968}
1969
1970static void
1971input_handle_touch_cancel(void *data,
1972 struct wl_input_device *wl_input_device)
1973{
1974}
1975
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001976static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001977 input_handle_motion,
1978 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001979 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001980 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001981 input_handle_pointer_enter,
1982 input_handle_pointer_leave,
1983 input_handle_keyboard_enter,
1984 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001985 input_handle_touch_down,
1986 input_handle_touch_up,
1987 input_handle_touch_motion,
1988 input_handle_touch_frame,
1989 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001990};
1991
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001992void
1993input_get_position(struct input *input, int32_t *x, int32_t *y)
1994{
1995 *x = input->sx;
1996 *y = input->sy;
1997}
1998
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001999struct wl_input_device *
2000input_get_input_device(struct input *input)
2001{
2002 return input->input_device;
2003}
2004
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002005uint32_t
2006input_get_modifiers(struct input *input)
2007{
2008 return input->modifiers;
2009}
2010
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002011struct widget *
2012input_get_focus_widget(struct input *input)
2013{
2014 return input->focus_widget;
2015}
2016
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002017struct data_offer {
2018 struct wl_data_offer *offer;
2019 struct input *input;
2020 struct wl_array types;
2021 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002022
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002023 struct task io_task;
2024 int fd;
2025 data_func_t func;
2026 int32_t x, y;
2027 void *user_data;
2028};
2029
2030static void
2031data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2032{
2033 struct data_offer *offer = data;
2034 char **p;
2035
2036 p = wl_array_add(&offer->types, sizeof *p);
2037 *p = strdup(type);
2038}
2039
2040static const struct wl_data_offer_listener data_offer_listener = {
2041 data_offer_offer,
2042};
2043
2044static void
2045data_offer_destroy(struct data_offer *offer)
2046{
2047 char **p;
2048
2049 offer->refcount--;
2050 if (offer->refcount == 0) {
2051 wl_data_offer_destroy(offer->offer);
2052 for (p = offer->types.data; *p; p++)
2053 free(*p);
2054 wl_array_release(&offer->types);
2055 free(offer);
2056 }
2057}
2058
2059static void
2060data_device_data_offer(void *data,
2061 struct wl_data_device *data_device, uint32_t id)
2062{
2063 struct data_offer *offer;
2064
2065 offer = malloc(sizeof *offer);
2066
2067 wl_array_init(&offer->types);
2068 offer->refcount = 1;
2069 offer->input = data;
2070
2071 /* FIXME: Generate typesafe wrappers for this */
2072 offer->offer = (struct wl_data_offer *)
2073 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2074 id, &wl_data_offer_interface);
2075
2076 wl_data_offer_add_listener(offer->offer,
2077 &data_offer_listener, offer);
2078}
2079
2080static void
2081data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002082 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002083 wl_fixed_t x_w, wl_fixed_t y_w,
2084 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002085{
2086 struct input *input = data;
2087 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002088 float x = wl_fixed_to_double(x_w);
2089 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002090 char **p;
2091
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002092 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002093 input->drag_offer = wl_data_offer_get_user_data(offer);
2094 window = wl_surface_get_user_data(surface);
2095 input->pointer_focus = window;
2096
2097 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2098 *p = NULL;
2099
2100 window = input->pointer_focus;
2101 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002102 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002103 input->drag_offer->types.data,
2104 window->user_data);
2105}
2106
2107static void
2108data_device_leave(void *data, struct wl_data_device *data_device)
2109{
2110 struct input *input = data;
2111
2112 data_offer_destroy(input->drag_offer);
2113 input->drag_offer = NULL;
2114}
2115
2116static void
2117data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002118 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002119{
2120 struct input *input = data;
2121 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002122 float x = wl_fixed_to_double(x_w);
2123 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002124
2125 input->sx = x;
2126 input->sy = y;
2127
2128 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002129 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002130 input->drag_offer->types.data,
2131 window->user_data);
2132}
2133
2134static void
2135data_device_drop(void *data, struct wl_data_device *data_device)
2136{
2137 struct input *input = data;
2138 struct window *window = input->pointer_focus;
2139
2140 if (window->drop_handler)
2141 window->drop_handler(window, input,
2142 input->sx, input->sy, window->user_data);
2143}
2144
2145static void
2146data_device_selection(void *data,
2147 struct wl_data_device *wl_data_device,
2148 struct wl_data_offer *offer)
2149{
2150 struct input *input = data;
2151 char **p;
2152
2153 if (input->selection_offer)
2154 data_offer_destroy(input->selection_offer);
2155
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002156 if (offer) {
2157 input->selection_offer = wl_data_offer_get_user_data(offer);
2158 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2159 *p = NULL;
2160 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002161}
2162
2163static const struct wl_data_device_listener data_device_listener = {
2164 data_device_data_offer,
2165 data_device_enter,
2166 data_device_leave,
2167 data_device_motion,
2168 data_device_drop,
2169 data_device_selection
2170};
2171
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002172void
2173input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2174{
2175 struct display *display = input->display;
2176 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002177 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002178
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002179 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002180 return;
2181
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002182 if (display->cursors[pointer].n_images == 0)
2183 return;
2184
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002185 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002186
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002187 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002188 return;
2189
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002190 input->current_cursor = pointer;
2191 buffer = display_get_buffer_for_surface(display, image->surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192 wl_input_device_attach(input->input_device, time, buffer,
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002193 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194}
2195
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002196struct wl_data_device *
2197input_get_data_device(struct input *input)
2198{
2199 return input->data_device;
2200}
2201
2202void
2203input_set_selection(struct input *input,
2204 struct wl_data_source *source, uint32_t time)
2205{
2206 wl_data_device_set_selection(input->data_device, source, time);
2207}
2208
2209void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002210input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002211{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002212 wl_data_offer_accept(input->drag_offer->offer,
2213 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002214}
2215
2216static void
2217offer_io_func(struct task *task, uint32_t events)
2218{
2219 struct data_offer *offer =
2220 container_of(task, struct data_offer, io_task);
2221 unsigned int len;
2222 char buffer[4096];
2223
2224 len = read(offer->fd, buffer, sizeof buffer);
2225 offer->func(buffer, len,
2226 offer->x, offer->y, offer->user_data);
2227
2228 if (len == 0) {
2229 close(offer->fd);
2230 data_offer_destroy(offer);
2231 }
2232}
2233
2234static void
2235data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2236 data_func_t func, void *user_data)
2237{
2238 int p[2];
2239
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002240 if (pipe2(p, O_CLOEXEC) == -1)
2241 return;
2242
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002243 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2244 close(p[1]);
2245
2246 offer->io_task.run = offer_io_func;
2247 offer->fd = p[0];
2248 offer->func = func;
2249 offer->refcount++;
2250 offer->user_data = user_data;
2251
2252 display_watch_fd(offer->input->display,
2253 offer->fd, EPOLLIN, &offer->io_task);
2254}
2255
2256void
2257input_receive_drag_data(struct input *input, const char *mime_type,
2258 data_func_t func, void *data)
2259{
2260 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2261 input->drag_offer->x = input->sx;
2262 input->drag_offer->y = input->sy;
2263}
2264
2265int
2266input_receive_selection_data(struct input *input, const char *mime_type,
2267 data_func_t func, void *data)
2268{
2269 char **p;
2270
2271 if (input->selection_offer == NULL)
2272 return -1;
2273
2274 for (p = input->selection_offer->types.data; *p; p++)
2275 if (strcmp(mime_type, *p) == 0)
2276 break;
2277
2278 if (*p == NULL)
2279 return -1;
2280
2281 data_offer_receive_data(input->selection_offer,
2282 mime_type, func, data);
2283 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002284}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002285
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002286int
2287input_receive_selection_data_to_fd(struct input *input,
2288 const char *mime_type, int fd)
2289{
2290 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2291
2292 return 0;
2293}
2294
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002295void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002296window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002297{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002298 if (!window->shell_surface)
2299 return;
2300
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002301 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002302 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002303}
2304
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002305static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002306idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002307{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002308 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002309
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002310 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002311 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002312 widget_set_allocation(widget,
2313 window->pending_allocation.x,
2314 window->pending_allocation.y,
2315 window->pending_allocation.width,
2316 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002317
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002318 if (window->input_region) {
2319 wl_region_destroy(window->input_region);
2320 window->input_region = NULL;
2321 }
2322
2323 if (window->opaque_region) {
2324 wl_region_destroy(window->opaque_region);
2325 window->opaque_region = NULL;
2326 }
2327
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002328 if (widget->resize_handler)
2329 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002330 widget->allocation.width,
2331 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002332 widget->user_data);
2333
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002334 if (window->allocation.width != widget->allocation.width ||
2335 window->allocation.height != widget->allocation.height) {
2336 window->allocation = widget->allocation;
2337 window_schedule_redraw(window);
2338 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002339}
2340
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002341void
2342window_schedule_resize(struct window *window, int width, int height)
2343{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002344 window->pending_allocation.x = 0;
2345 window->pending_allocation.y = 0;
2346 window->pending_allocation.width = width;
2347 window->pending_allocation.height = height;
2348
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002349 window->resize_needed = 1;
2350 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002351}
2352
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002353void
2354widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2355{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002356 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002357}
2358
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002359static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002360handle_ping(void *data, struct wl_shell_surface *shell_surface,
2361 uint32_t serial)
2362{
2363 wl_shell_surface_pong(shell_surface, serial);
2364}
2365
2366static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002367handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002368 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002369{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002370 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002371
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002372 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002373 return;
2374
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002375 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002376 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002377}
2378
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002379static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002380menu_destroy(struct menu *menu)
2381{
2382 widget_destroy(menu->widget);
2383 window_destroy(menu->window);
2384 free(menu);
2385}
2386
2387static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002388handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2389{
2390 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002391 struct menu *menu = window->widget->user_data;
2392
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002393 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002394 * device. Or just use wl_callback. And this really needs to
2395 * be a window vfunc that the menu can set. And we need the
2396 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002397
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002398 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002399 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002400 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002401}
2402
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002403static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002404 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002405 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002406 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002407};
2408
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002409void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002410window_get_allocation(struct window *window,
2411 struct rectangle *allocation)
2412{
2413 *allocation = window->allocation;
2414}
2415
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002416static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002417widget_redraw(struct widget *widget)
2418{
2419 struct widget *child;
2420
2421 if (widget->redraw_handler)
2422 widget->redraw_handler(widget, widget->user_data);
2423 wl_list_for_each(child, &widget->child_list, link)
2424 widget_redraw(child);
2425}
2426
2427static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002428frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2429{
2430 struct window *window = data;
2431
2432 wl_callback_destroy(callback);
2433 window->redraw_scheduled = 0;
2434 if (window->redraw_needed)
2435 window_schedule_redraw(window);
2436}
2437
2438static const struct wl_callback_listener listener = {
2439 frame_callback
2440};
2441
2442static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002443idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002444{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002445 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002446 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002447
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002448 if (window->resize_needed)
2449 idle_resize(window);
2450
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002451 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002452 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002453 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002454 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002455 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002456
2457 callback = wl_surface_frame(window->surface);
2458 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002459}
2460
2461void
2462window_schedule_redraw(struct window *window)
2463{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002464 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002465 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002466 window->redraw_task.run = idle_redraw;
2467 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002468 window->redraw_scheduled = 1;
2469 }
2470}
2471
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002472void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002473window_set_custom(struct window *window)
2474{
2475 window->type = TYPE_CUSTOM;
2476}
2477
2478void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002479window_set_fullscreen(struct window *window, int fullscreen)
2480{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002481 if (!window->display->shell)
2482 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002483
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002484 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002485 return;
2486
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002487 if (fullscreen) {
2488 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002489 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002490 wl_shell_surface_set_fullscreen(window->shell_surface,
2491 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2492 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002493 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002494 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002495 wl_shell_surface_set_toplevel(window->shell_surface);
2496 window_schedule_resize(window,
2497 window->saved_allocation.width,
2498 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002499 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002500}
2501
2502void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002503window_set_maximized(struct window *window, int maximized)
2504{
2505 if (!window->display->shell)
2506 return;
2507
2508 if ((window->type == TYPE_MAXIMIZED) == maximized)
2509 return;
2510
2511 if (window->type == TYPE_TOPLEVEL) {
2512 window->saved_allocation = window->allocation;
2513 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2514 window->type = TYPE_MAXIMIZED;
2515 } else {
2516 wl_shell_surface_set_toplevel(window->shell_surface);
2517 window->type = TYPE_TOPLEVEL;
2518 window_schedule_resize(window,
2519 window->saved_allocation.width,
2520 window->saved_allocation.height);
2521 }
2522}
2523
2524void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002525window_set_user_data(struct window *window, void *data)
2526{
2527 window->user_data = data;
2528}
2529
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002530void *
2531window_get_user_data(struct window *window)
2532{
2533 return window->user_data;
2534}
2535
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002536void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002537window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002538 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002539{
2540 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002541}
2542
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002543void
2544window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002545 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002546{
2547 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002548}
2549
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002550void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002551window_set_data_handler(struct window *window, window_data_handler_t handler)
2552{
2553 window->data_handler = handler;
2554}
2555
2556void
2557window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2558{
2559 window->drop_handler = handler;
2560}
2561
2562void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002563window_set_close_handler(struct window *window,
2564 window_close_handler_t handler)
2565{
2566 window->close_handler = handler;
2567}
2568
2569void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002570window_set_title(struct window *window, const char *title)
2571{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002572 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002573 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002574 if (window->shell_surface)
2575 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002576}
2577
2578const char *
2579window_get_title(struct window *window)
2580{
2581 return window->title;
2582}
2583
2584void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002585window_damage(struct window *window, int32_t x, int32_t y,
2586 int32_t width, int32_t height)
2587{
2588 wl_surface_damage(window->surface, x, y, width, height);
2589}
2590
Casey Dahlin9074db52012-04-19 22:50:09 -04002591static void
2592surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002593 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002594{
Rob Bradford7507b572012-05-15 17:55:34 +01002595 struct window *window = data;
2596 struct output *output;
2597 struct output *output_found = NULL;
2598 struct window_output *window_output;
2599
2600 wl_list_for_each(output, &window->display->output_list, link) {
2601 if (output->output == wl_output) {
2602 output_found = output;
2603 break;
2604 }
2605 }
2606
2607 if (!output_found)
2608 return;
2609
2610 window_output = malloc (sizeof *window_output);
2611 window_output->output = output_found;
2612
2613 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002614}
2615
2616static void
2617surface_leave(void *data,
2618 struct wl_surface *wl_surface, struct wl_output *output)
2619{
Rob Bradford7507b572012-05-15 17:55:34 +01002620 struct window *window = data;
2621 struct window_output *window_output;
2622 struct window_output *window_output_found = NULL;
2623
2624 wl_list_for_each(window_output, &window->window_output_list, link) {
2625 if (window_output->output->output == output) {
2626 window_output_found = window_output;
2627 break;
2628 }
2629 }
2630
2631 if (window_output_found) {
2632 wl_list_remove(&window_output_found->link);
2633 free(window_output_found);
2634 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002635}
2636
2637static const struct wl_surface_listener surface_listener = {
2638 surface_enter,
2639 surface_leave
2640};
2641
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002642static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002643window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002644{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002645 struct window *window;
2646
2647 window = malloc(sizeof *window);
2648 if (window == NULL)
2649 return NULL;
2650
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002651 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002652 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002653 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002654 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002655 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002656 if (display->shell) {
2657 window->shell_surface =
2658 wl_shell_get_shell_surface(display->shell,
2659 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002660 if (window->title)
2661 wl_shell_surface_set_title(window->shell_surface,
2662 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002663 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002664 window->allocation.x = 0;
2665 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002666 window->allocation.width = 0;
2667 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002668 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002669 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002670 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002671 window->input_region = NULL;
2672 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002673
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002674 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002675#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002676 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002677#else
2678 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2679#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002680 else
2681 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002682
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002683 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002684 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002685 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002686
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002687 if (window->shell_surface) {
2688 wl_shell_surface_set_user_data(window->shell_surface, window);
2689 wl_shell_surface_add_listener(window->shell_surface,
2690 &shell_surface_listener, window);
2691 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002692
Rob Bradford7507b572012-05-15 17:55:34 +01002693 wl_list_init (&window->window_output_list);
2694
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002695 return window;
2696}
2697
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002698struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002699window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002700{
2701 struct window *window;
2702
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002703 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002704 if (!window)
2705 return NULL;
2706
2707 return window;
2708}
2709
2710struct window *
2711window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002712 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002713{
2714 struct window *window;
2715
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002716 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002717 if (!window)
2718 return NULL;
2719
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002720 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002721 window->x = x;
2722 window->y = y;
2723
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002724 if (display->shell)
2725 wl_shell_surface_set_transient(window->shell_surface,
2726 window->parent->shell_surface,
2727 window->x, window->y, 0);
2728
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002729 return window;
2730}
2731
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002732static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002733menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002734{
2735 int next;
2736
2737 next = (sy - 8) / 20;
2738 if (menu->current != next) {
2739 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002740 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002741 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002742}
2743
2744static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002745menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002746 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002747 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002748{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002749 struct menu *menu = data;
2750
2751 if (widget == menu->widget)
2752 menu_set_item(data, y);
2753
2754 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002755}
2756
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002757static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002758menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002759 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002760{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002761 struct menu *menu = data;
2762
2763 if (widget == menu->widget)
2764 menu_set_item(data, y);
2765
2766 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002767}
2768
2769static void
2770menu_leave_handler(struct widget *widget, struct input *input, void *data)
2771{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002772 struct menu *menu = data;
2773
2774 if (widget == menu->widget)
2775 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002776}
2777
2778static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002779menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002780 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002781 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002782
2783{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002784 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002785
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002786 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002787 /* Either relase after press-drag-release or
2788 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002789 menu->func(menu->window->parent,
2790 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002791 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002792 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002793 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002794}
2795
2796static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002797menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002798{
2799 cairo_t *cr;
2800 const int32_t r = 3, margin = 3;
2801 struct menu *menu = data;
2802 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002803 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002804
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002805 cr = cairo_create(window->cairo_surface);
2806 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2807 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2808 cairo_paint(cr);
2809
2810 width = window->allocation.width;
2811 height = window->allocation.height;
2812 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002813
2814 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002815 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2816 cairo_fill(cr);
2817
2818 for (i = 0; i < menu->count; i++) {
2819 if (i == menu->current) {
2820 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2821 cairo_rectangle(cr, margin, i * 20 + margin,
2822 width - 2 * margin, 20);
2823 cairo_fill(cr);
2824 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2825 cairo_move_to(cr, 10, i * 20 + 16);
2826 cairo_show_text(cr, menu->entries[i]);
2827 } else {
2828 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2829 cairo_move_to(cr, 10, i * 20 + 16);
2830 cairo_show_text(cr, menu->entries[i]);
2831 }
2832 }
2833
2834 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002835}
2836
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002837void
2838window_show_menu(struct display *display,
2839 struct input *input, uint32_t time, struct window *parent,
2840 int32_t x, int32_t y,
2841 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002842{
2843 struct window *window;
2844 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002845 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002846
2847 menu = malloc(sizeof *menu);
2848 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002849 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002850
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002851 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002852 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002853 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002854
2855 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002856 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002857 menu->entries = entries;
2858 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002859 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002860 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002861 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002862 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002863 window->type = TYPE_MENU;
2864 window->x = x;
2865 window->y = y;
2866
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002867 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002868 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2869 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002870 window->parent->shell_surface,
2871 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002872
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002873 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002874 widget_set_enter_handler(menu->widget, menu_enter_handler);
2875 widget_set_leave_handler(menu->widget, menu_leave_handler);
2876 widget_set_motion_handler(menu->widget, menu_motion_handler);
2877 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002878
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002879 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002880 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002881}
2882
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002883void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002884window_set_buffer_type(struct window *window, enum window_buffer_type type)
2885{
2886 window->buffer_type = type;
2887}
2888
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002889
2890static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002891display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002892 struct wl_output *wl_output,
2893 int x, int y,
2894 int physical_width,
2895 int physical_height,
2896 int subpixel,
2897 const char *make,
2898 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002899{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002900 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002901
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002902 output->allocation.x = x;
2903 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002904}
2905
2906static void
2907display_handle_mode(void *data,
2908 struct wl_output *wl_output,
2909 uint32_t flags,
2910 int width,
2911 int height,
2912 int refresh)
2913{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002914 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002915 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002916
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002917 if (flags & WL_OUTPUT_MODE_CURRENT) {
2918 output->allocation.width = width;
2919 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002920 if (display->output_configure_handler)
2921 (*display->output_configure_handler)(
2922 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002923 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002924}
2925
2926static const struct wl_output_listener output_listener = {
2927 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002928 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002929};
2930
2931static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002932display_add_output(struct display *d, uint32_t id)
2933{
2934 struct output *output;
2935
2936 output = malloc(sizeof *output);
2937 if (output == NULL)
2938 return;
2939
2940 memset(output, 0, sizeof *output);
2941 output->display = d;
2942 output->output =
2943 wl_display_bind(d->display, id, &wl_output_interface);
2944 wl_list_insert(d->output_list.prev, &output->link);
2945
2946 wl_output_add_listener(output->output, &output_listener, output);
2947}
2948
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002949static void
2950output_destroy(struct output *output)
2951{
2952 if (output->destroy_handler)
2953 (*output->destroy_handler)(output, output->user_data);
2954
2955 wl_output_destroy(output->output);
2956 wl_list_remove(&output->link);
2957 free(output);
2958}
2959
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002960void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002961display_set_output_configure_handler(struct display *display,
2962 display_output_handler_t handler)
2963{
2964 struct output *output;
2965
2966 display->output_configure_handler = handler;
2967 if (!handler)
2968 return;
2969
2970 wl_list_for_each(output, &display->output_list, link)
2971 (*display->output_configure_handler)(output,
2972 display->user_data);
2973}
2974
2975void
2976output_set_user_data(struct output *output, void *data)
2977{
2978 output->user_data = data;
2979}
2980
2981void *
2982output_get_user_data(struct output *output)
2983{
2984 return output->user_data;
2985}
2986
2987void
2988output_set_destroy_handler(struct output *output,
2989 display_output_handler_t handler)
2990{
2991 output->destroy_handler = handler;
2992 /* FIXME: implement this, once we have way to remove outputs */
2993}
2994
2995void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002996output_get_allocation(struct output *output, struct rectangle *allocation)
2997{
2998 *allocation = output->allocation;
2999}
3000
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003001struct wl_output *
3002output_get_wl_output(struct output *output)
3003{
3004 return output->output;
3005}
3006
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003007static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003008display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003009{
3010 struct input *input;
3011
3012 input = malloc(sizeof *input);
3013 if (input == NULL)
3014 return;
3015
3016 memset(input, 0, sizeof *input);
3017 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003018 input->input_device =
3019 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003020 input->pointer_focus = NULL;
3021 input->keyboard_focus = NULL;
3022 wl_list_insert(d->input_list.prev, &input->link);
3023
3024 wl_input_device_add_listener(input->input_device,
3025 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003026 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003027
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003028 input->data_device =
3029 wl_data_device_manager_get_data_device(d->data_device_manager,
3030 input->input_device);
3031 wl_data_device_add_listener(input->data_device,
3032 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003033}
3034
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003035static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003036input_destroy(struct input *input)
3037{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003038 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003039 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003040
3041 if (input->drag_offer)
3042 data_offer_destroy(input->drag_offer);
3043
3044 if (input->selection_offer)
3045 data_offer_destroy(input->selection_offer);
3046
3047 wl_data_device_destroy(input->data_device);
3048 wl_list_remove(&input->link);
3049 wl_input_device_destroy(input->input_device);
3050 free(input);
3051}
3052
3053static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003054display_handle_global(struct wl_display *display, uint32_t id,
3055 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003056{
3057 struct display *d = data;
3058
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003059 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003060 d->compositor =
3061 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003062 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003063 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003064 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003065 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003066 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003067 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003068 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003069 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003070 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3071 d->data_device_manager =
3072 wl_display_bind(display, id,
3073 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003074 }
3075}
3076
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003077static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003078init_xkb(struct display *d)
3079{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003080 d->xkb.names.rules = "evdev";
3081 d->xkb.names.model = "pc105";
3082 d->xkb.names.layout = (char *) option_xkb_layout;
3083 d->xkb.names.variant = (char *) option_xkb_variant;
3084 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003085
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003086 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003087 if (!d->xkb.context) {
3088 fprintf(stderr, "Failed to create XKB context\n");
3089 exit(1);
3090 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003091
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003092 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003093 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003094 fprintf(stderr, "Failed to compile keymap\n");
3095 exit(1);
3096 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003097
3098 d->xkb.state = xkb_state_new(d->xkb.keymap);
3099 if (!d->xkb.state) {
3100 fprintf(stderr, "Failed to create XKB state\n");
3101 exit(1);
3102 }
3103
3104 d->xkb.control_mask =
3105 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3106 d->xkb.alt_mask =
3107 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3108 d->xkb.shift_mask =
3109 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3110
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003111}
3112
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003113static void
3114fini_xkb(struct display *display)
3115{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003116 xkb_state_unref(display->xkb.state);
3117 xkb_map_unref(display->xkb.keymap);
3118 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003119}
3120
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003121#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003122static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003123init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003124{
3125 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003126 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003127
Rob Clark6396ed32012-03-11 19:48:41 -05003128#ifdef USE_CAIRO_GLESV2
3129# define GL_BIT EGL_OPENGL_ES2_BIT
3130#else
3131# define GL_BIT EGL_OPENGL_BIT
3132#endif
3133
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003134 static const EGLint argb_cfg_attribs[] = {
3135 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003136 EGL_RED_SIZE, 1,
3137 EGL_GREEN_SIZE, 1,
3138 EGL_BLUE_SIZE, 1,
3139 EGL_ALPHA_SIZE, 1,
3140 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003141 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003142 EGL_NONE
3143 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003144
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003145#ifdef USE_CAIRO_GLESV2
3146 static const EGLint context_attribs[] = {
3147 EGL_CONTEXT_CLIENT_VERSION, 2,
3148 EGL_NONE
3149 };
3150 EGLint api = EGL_OPENGL_ES_API;
3151#else
3152 EGLint *context_attribs = NULL;
3153 EGLint api = EGL_OPENGL_API;
3154#endif
3155
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003156 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003157 if (!eglInitialize(d->dpy, &major, &minor)) {
3158 fprintf(stderr, "failed to initialize display\n");
3159 return -1;
3160 }
3161
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003162 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003163 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3164 return -1;
3165 }
3166
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003167 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3168 &d->argb_config, 1, &n) || n != 1) {
3169 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003170 return -1;
3171 }
3172
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003173 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003174 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003175 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003176 fprintf(stderr, "failed to create context\n");
3177 return -1;
3178 }
3179
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003180 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003181 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003182 return -1;
3183 }
3184
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003185#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003186 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3187 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3188 fprintf(stderr, "failed to get cairo egl argb device\n");
3189 return -1;
3190 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003191#endif
3192
3193 return 0;
3194}
3195
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003196static void
3197fini_egl(struct display *display)
3198{
3199#ifdef HAVE_CAIRO_EGL
3200 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003201#endif
3202
3203 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3204 EGL_NO_CONTEXT);
3205
3206 eglTerminate(display->dpy);
3207 eglReleaseThread();
3208}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003209#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003210
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003211static int
3212event_mask_update(uint32_t mask, void *data)
3213{
3214 struct display *d = data;
3215
3216 d->mask = mask;
3217
3218 return 0;
3219}
3220
3221static void
3222handle_display_data(struct task *task, uint32_t events)
3223{
3224 struct display *display =
3225 container_of(task, struct display, display_task);
3226
3227 wl_display_iterate(display->display, display->mask);
3228}
3229
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003230struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003231display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003232{
3233 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003234
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003235 argc = parse_options(xkb_options,
3236 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003237
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003238 d = malloc(sizeof *d);
3239 if (d == NULL)
3240 return NULL;
3241
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003242 memset(d, 0, sizeof *d);
3243
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003244 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003245 if (d->display == NULL) {
3246 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003247 return NULL;
3248 }
3249
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003250 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3251 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3252 d->display_task.run = handle_display_data;
3253 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3254
3255 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003256 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003257 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003258
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003259 /* Set up listener so we'll catch all events. */
3260 wl_display_add_global_listener(d->display,
3261 display_handle_global, d);
3262
3263 /* Process connection events. */
3264 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003265#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003266 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003267 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003268#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003269
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003270 d->image_target_texture_2d =
3271 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3272 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3273 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3274
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003275 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003276
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003277 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003278
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003279 wl_list_init(&d->window_list);
3280
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003281 init_xkb(d);
3282
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003283 return d;
3284}
3285
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003286static void
3287display_destroy_outputs(struct display *display)
3288{
3289 struct output *tmp;
3290 struct output *output;
3291
3292 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3293 output_destroy(output);
3294}
3295
Pekka Paalanene1207c72011-12-16 12:02:09 +02003296static void
3297display_destroy_inputs(struct display *display)
3298{
3299 struct input *tmp;
3300 struct input *input;
3301
3302 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3303 input_destroy(input);
3304}
3305
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003306void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003307display_destroy(struct display *display)
3308{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003309 if (!wl_list_empty(&display->window_list))
3310 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3311
3312 if (!wl_list_empty(&display->deferred_list))
3313 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3314
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003315 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003316 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003317
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003318 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003319
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003320 theme_destroy(display->theme);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003321 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003322
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003323#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003324 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003325#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003326
Pekka Paalanenc2052982011-12-16 11:41:32 +02003327 if (display->shell)
3328 wl_shell_destroy(display->shell);
3329
3330 if (display->shm)
3331 wl_shm_destroy(display->shm);
3332
3333 if (display->data_device_manager)
3334 wl_data_device_manager_destroy(display->data_device_manager);
3335
3336 wl_compositor_destroy(display->compositor);
3337
3338 close(display->epoll_fd);
3339
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003340 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003341 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003342 free(display);
3343}
3344
3345void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003346display_set_user_data(struct display *display, void *data)
3347{
3348 display->user_data = data;
3349}
3350
3351void *
3352display_get_user_data(struct display *display)
3353{
3354 return display->user_data;
3355}
3356
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003357struct wl_display *
3358display_get_display(struct display *display)
3359{
3360 return display->display;
3361}
3362
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003363struct output *
3364display_get_output(struct display *display)
3365{
3366 return container_of(display->output_list.next, struct output, link);
3367}
3368
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003369struct wl_compositor *
3370display_get_compositor(struct display *display)
3371{
3372 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003373}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003374
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003375uint32_t
3376display_get_serial(struct display *display)
3377{
3378 return display->serial;
3379}
3380
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003381EGLDisplay
3382display_get_egl_display(struct display *d)
3383{
3384 return d->dpy;
3385}
3386
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003387struct wl_data_source *
3388display_create_data_source(struct display *display)
3389{
3390 return wl_data_device_manager_create_data_source(display->data_device_manager);
3391}
3392
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003393EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003394display_get_argb_egl_config(struct display *d)
3395{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003396 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003397}
3398
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003399struct wl_shell *
3400display_get_shell(struct display *display)
3401{
3402 return display->shell;
3403}
3404
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003405int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003406display_acquire_window_surface(struct display *display,
3407 struct window *window,
3408 EGLContext ctx)
3409{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003410#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003411 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003412 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003413
3414 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003415 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003416 device = cairo_surface_get_device(window->cairo_surface);
3417 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003418 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003419
Benjamin Franzke0c991632011-09-27 21:57:31 +02003420 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003421 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003422 ctx = display->argb_ctx;
3423 else
3424 assert(0);
3425 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003426
3427 data = cairo_surface_get_user_data(window->cairo_surface,
3428 &surface_data_key);
3429
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003430 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003431 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003432 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3433 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003434
3435 return 0;
3436#else
3437 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003438#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003439}
3440
3441void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003442display_release_window_surface(struct display *display,
3443 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003444{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003445#ifdef HAVE_CAIRO_EGL
3446 cairo_device_t *device;
3447
3448 device = cairo_surface_get_device(window->cairo_surface);
3449 if (!device)
3450 return;
3451
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003452 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003453 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003454 cairo_device_release(device);
3455#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003456}
3457
3458void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003459display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003460{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003461 wl_list_insert(&display->deferred_list, &task->link);
3462}
3463
3464void
3465display_watch_fd(struct display *display,
3466 int fd, uint32_t events, struct task *task)
3467{
3468 struct epoll_event ep;
3469
3470 ep.events = events;
3471 ep.data.ptr = task;
3472 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3473}
3474
3475void
3476display_run(struct display *display)
3477{
3478 struct task *task;
3479 struct epoll_event ep[16];
3480 int i, count;
3481
Pekka Paalanen826d7952011-12-15 10:14:07 +02003482 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003483 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003484 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003485
3486 while (!wl_list_empty(&display->deferred_list)) {
3487 task = container_of(display->deferred_list.next,
3488 struct task, link);
3489 wl_list_remove(&task->link);
3490 task->run(task, 0);
3491 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003492
3493 if (!display->running)
3494 break;
3495
3496 wl_display_flush(display->display);
3497
3498 count = epoll_wait(display->epoll_fd,
3499 ep, ARRAY_LENGTH(ep), -1);
3500 for (i = 0; i < count; i++) {
3501 task = ep[i].data.ptr;
3502 task->run(task, ep[i].events);
3503 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003504 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003505}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003506
3507void
3508display_exit(struct display *display)
3509{
3510 display->running = 0;
3511}