blob: c7a05822f1345c1ef2bf2ccb86da239e1d57aadf [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øgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050059#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040063#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050067struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050068 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050069 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040070 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040071 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040072 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040073 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050074 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020075 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020076 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040077
78 int display_fd;
79 uint32_t mask;
80 struct task display_task;
81
82 int epoll_fd;
83 struct wl_list deferred_list;
84
Pekka Paalanen826d7952011-12-15 10:14:07 +020085 int running;
86
Kristian Høgsberg478d9262010-06-08 20:34:11 -040087 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040088 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050089 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040090 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050091 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040092 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040093 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040094
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050095 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
96 PFNEGLCREATEIMAGEKHRPROC create_image;
97 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020098
99 display_output_handler_t output_configure_handler;
100
101 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500102};
103
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400106 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500107 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400108 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500109 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400110 TYPE_CUSTOM
111};
112
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500113struct window {
114 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500115 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500116 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500118 struct wl_region *input_region;
119 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500120 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500121 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500122 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500123 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400124 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400125 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400126 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400127 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400128 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400129 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400130 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400131 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400132 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500133
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400134 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500135
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400136 struct wl_shm_pool *pool;
137 size_t pool_size;
138 void *pool_data;
139
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500140 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500141 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400142 window_data_handler_t data_handler;
143 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500144 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400145
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200146 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500147 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500148
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500149 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400150 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500151};
152
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500153struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500154 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500155 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400156 struct wl_list link;
157 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500158 widget_resize_handler_t resize_handler;
159 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500160 widget_enter_handler_t enter_handler;
161 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500162 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500163 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400164 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500165 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400166};
167
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400168struct input {
169 struct display *display;
170 struct wl_input_device *input_device;
171 struct window *pointer_focus;
172 struct window *keyboard_focus;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400173 int current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400174 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200175 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400176 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400177
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500178 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500179 struct widget *grab;
180 uint32_t grab_button;
181
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400182 struct wl_data_device *data_device;
183 struct data_offer *drag_offer;
184 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400185};
186
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500187struct output {
188 struct display *display;
189 struct wl_output *output;
190 struct rectangle allocation;
191 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200192
193 display_output_handler_t destroy_handler;
194 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500195};
196
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500197struct frame {
198 struct widget *widget;
199 struct widget *child;
200 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400201 int width;
202 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500203};
204
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500205struct menu {
206 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500207 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500208 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500209 const char **entries;
210 uint32_t time;
211 int current;
212 int count;
213 menu_func_t func;
214};
215
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400216enum {
217 POINTER_DEFAULT = 100,
218 POINTER_UNSET
219};
220
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500221enum window_location {
222 WINDOW_INTERIOR = 0,
223 WINDOW_RESIZING_TOP = 1,
224 WINDOW_RESIZING_BOTTOM = 2,
225 WINDOW_RESIZING_LEFT = 4,
226 WINDOW_RESIZING_TOP_LEFT = 5,
227 WINDOW_RESIZING_BOTTOM_LEFT = 6,
228 WINDOW_RESIZING_RIGHT = 8,
229 WINDOW_RESIZING_TOP_RIGHT = 9,
230 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
231 WINDOW_RESIZING_MASK = 15,
232 WINDOW_EXTERIOR = 16,
233 WINDOW_TITLEBAR = 17,
234 WINDOW_CLIENT_AREA = 18,
235};
236
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400237const char *option_xkb_layout = "us";
238const char *option_xkb_variant = "";
239const char *option_xkb_options = "";
240
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400241static const struct weston_option xkb_options[] = {
242 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
243 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
244 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400245};
246
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400247static const cairo_user_data_key_t surface_data_key;
248struct surface_data {
249 struct wl_buffer *buffer;
250};
251
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500252#define MULT(_d,c,a,t) \
253 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
254
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500255#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400256
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100257struct egl_window_surface_data {
258 struct display *display;
259 struct wl_surface *surface;
260 struct wl_egl_window *window;
261 EGLSurface surf;
262};
263
264static void
265egl_window_surface_data_destroy(void *p)
266{
267 struct egl_window_surface_data *data = p;
268 struct display *d = data->display;
269
270 eglDestroySurface(d->dpy, data->surf);
271 wl_egl_window_destroy(data->window);
272 data->surface = NULL;
273
274 free(p);
275}
276
277static cairo_surface_t *
278display_create_egl_window_surface(struct display *display,
279 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400280 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100281 struct rectangle *rectangle)
282{
283 cairo_surface_t *cairo_surface;
284 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400285 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200286 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100287
288 data = malloc(sizeof *data);
289 if (data == NULL)
290 return NULL;
291
292 data->display = display;
293 data->surface = surface;
294
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500295 config = display->argb_config;
296 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400298 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100299 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400300 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100301
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400302 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500303 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100304
Benjamin Franzke0c991632011-09-27 21:57:31 +0200305 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100306 data->surf,
307 rectangle->width,
308 rectangle->height);
309
310 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
311 data, egl_window_surface_data_destroy);
312
313 return cairo_surface;
314}
315
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316#endif
317
318struct wl_buffer *
319display_get_buffer_for_surface(struct display *display,
320 cairo_surface_t *surface)
321{
322 struct surface_data *data;
323
324 data = cairo_surface_get_user_data (surface, &surface_data_key);
325
326 return data->buffer;
327}
328
329struct shm_surface_data {
330 struct surface_data data;
331 void *map;
332 size_t length;
333};
334
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500335static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336shm_surface_data_destroy(void *p)
337{
338 struct shm_surface_data *data = p;
339
340 wl_buffer_destroy(data->data.buffer);
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400341 if (data->map)
342 munmap(data->map, data->length);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400343}
344
Kristian Høgsberg16626282012-04-03 11:21:27 -0400345static struct wl_shm_pool *
346make_shm_pool(struct display *display, int size, void **data)
347{
348 char filename[] = "/tmp/wayland-shm-XXXXXX";
349 struct wl_shm_pool *pool;
350 int fd;
351
352 fd = mkstemp(filename);
353 if (fd < 0) {
354 fprintf(stderr, "open %s failed: %m\n", filename);
355 return NULL;
356 }
357 if (ftruncate(fd, size) < 0) {
358 fprintf(stderr, "ftruncate failed: %m\n");
359 close(fd);
360 return NULL;
361 }
362
363 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
364 unlink(filename);
365
366 if (*data == MAP_FAILED) {
367 fprintf(stderr, "mmap failed: %m\n");
368 close(fd);
369 return NULL;
370 }
371
372 pool = wl_shm_create_pool(display->shm, fd, size);
373
374 close(fd);
375
376 return pool;
377}
378
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500379static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400380display_create_shm_surface(struct display *display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400381 struct rectangle *rectangle, uint32_t flags,
382 struct window *window)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400383{
384 struct shm_surface_data *data;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400385 struct wl_shm_pool *pool = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400386 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400387 cairo_surface_t *surface;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400388 void *map;
Kristian Høgsberg16626282012-04-03 11:21:27 -0400389 int stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390
391 data = malloc(sizeof *data);
392 if (data == NULL)
393 return NULL;
394
395 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
396 rectangle->width);
397 data->length = stride * rectangle->height;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400398 if (window && window->pool && data->length < window->pool_size) {
399 pool = window->pool;
400 map = window->pool_data;
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400401 data->map = NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400402 } else {
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400403 pool = make_shm_pool(display, data->length, &map);
404 data->map = map;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400405 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400407 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400408 CAIRO_FORMAT_ARGB32,
409 rectangle->width,
410 rectangle->height,
411 stride);
412
413 cairo_surface_set_user_data (surface, &surface_data_key,
414 data, shm_surface_data_destroy);
415
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400416 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500417 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400418 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500419 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400420
Kristian Høgsberg16626282012-04-03 11:21:27 -0400421 data->data.buffer = wl_shm_pool_create_buffer(pool, 0,
422 rectangle->width,
423 rectangle->height,
424 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400425
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400426 if (data->map)
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400427 wl_shm_pool_destroy(pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428
429 return surface;
430}
431
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500432static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400433display_create_shm_surface_from_file(struct display *display,
434 const char *filename,
435 struct rectangle *rect)
436{
437 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400438 pixman_image_t *image;
439 void *dest;
440 int size;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400441
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400442 image = load_image(filename);
Kristian Høgsberg610a3f22012-04-03 14:49:50 -0400443 if (image == NULL)
444 return NULL;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400446 surface = display_create_shm_surface(display, rect, 0, NULL);
nobled7b87cb02011-02-01 18:51:47 +0000447 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400448 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000449 return NULL;
450 }
451
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400452 size = pixman_image_get_stride(image) * pixman_image_get_height(image);
453 dest = cairo_image_surface_get_data(surface);
454 memcpy(dest, pixman_image_get_data(image), size);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400455
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400456 pixman_image_unref(image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457
458 return surface;
459}
460
nobled7b87cb02011-02-01 18:51:47 +0000461static int
462check_size(struct rectangle *rect)
463{
464 if (rect->width && rect->height)
465 return 0;
466
467 fprintf(stderr, "tried to create surface of "
468 "width: %d, height: %d\n", rect->width, rect->height);
469 return -1;
470}
471
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400472cairo_surface_t *
473display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100474 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400475 struct rectangle *rectangle,
476 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400477{
nobled7b87cb02011-02-01 18:51:47 +0000478 if (check_size(rectangle) < 0)
479 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500480#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400481 if (display->dpy)
482 return display_create_egl_window_surface(display,
483 surface,
484 flags,
485 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400486#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400487 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400488}
489
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500490static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400491display_create_surface_from_file(struct display *display,
492 const char *filename,
493 struct rectangle *rectangle)
494{
nobled7b87cb02011-02-01 18:51:47 +0000495 if (check_size(rectangle) < 0)
496 return NULL;
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400497
Yuval Fledel45568f62010-12-06 09:18:12 -0500498 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400499}
Yuval Fledel45568f62010-12-06 09:18:12 -0500500 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400501 const char *filename;
502 int hotspot_x, hotspot_y;
503} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500504 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
505 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
506 { DATADIR "/weston/bottom_side.png", 16, 20 },
507 { DATADIR "/weston/grabbing.png", 20, 17 },
508 { DATADIR "/weston/left_ptr.png", 10, 5 },
509 { DATADIR "/weston/left_side.png", 10, 20 },
510 { DATADIR "/weston/right_side.png", 30, 19 },
511 { DATADIR "/weston/top_left_corner.png", 8, 8 },
512 { DATADIR "/weston/top_right_corner.png", 26, 8 },
513 { DATADIR "/weston/top_side.png", 18, 8 },
514 { DATADIR "/weston/xterm.png", 15, 15 },
515 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400516};
517
518static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400519create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400520{
521 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400522 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400523 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400524
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400525 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400526 display->pointer_surfaces =
527 malloc(count * sizeof *display->pointer_surfaces);
528 rect.width = width;
529 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400530 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400531 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400532 display_create_surface_from_file(display,
533 pointer_images[i].filename,
534 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100535 if (!display->pointer_surfaces[i]) {
536 fprintf(stderr, "Error loading pointer image: %s\n",
537 pointer_images[i].filename);
538 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400539 }
540
541}
542
Pekka Paalanen325bb602011-12-19 10:31:45 +0200543static void
544destroy_pointer_surfaces(struct display *display)
545{
546 int i, count;
547
548 count = ARRAY_LENGTH(pointer_images);
549 for (i = 0; i < count; ++i) {
550 if (display->pointer_surfaces[i])
551 cairo_surface_destroy(display->pointer_surfaces[i]);
552 }
553 free(display->pointer_surfaces);
554}
555
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400556cairo_surface_t *
557display_get_pointer_surface(struct display *display, int pointer,
558 int *width, int *height,
559 int *hotspot_x, int *hotspot_y)
560{
561 cairo_surface_t *surface;
562
563 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500564#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400565 *width = cairo_gl_surface_get_width(surface);
566 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000567#else
568 *width = cairo_image_surface_get_width(surface);
569 *height = cairo_image_surface_get_height(surface);
570#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400571 *hotspot_x = pointer_images[pointer].hotspot_x;
572 *hotspot_y = pointer_images[pointer].hotspot_y;
573
574 return cairo_surface_reference(surface);
575}
576
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400577static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200578window_get_resize_dx_dy(struct window *window, int *x, int *y)
579{
580 if (window->resize_edges & WINDOW_RESIZING_LEFT)
581 *x = window->server_allocation.width - window->allocation.width;
582 else
583 *x = 0;
584
585 if (window->resize_edges & WINDOW_RESIZING_TOP)
586 *y = window->server_allocation.height -
587 window->allocation.height;
588 else
589 *y = 0;
590
591 window->resize_edges = 0;
592}
593
594static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500595window_attach_surface(struct window *window)
596{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400597 struct display *display = window->display;
598 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000599#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100600 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000601#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500602 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100603
604 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000605#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100606 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
607 data = cairo_surface_get_user_data(window->cairo_surface,
608 &surface_data_key);
609
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100610 cairo_gl_surface_swapbuffers(window->cairo_surface);
611 wl_egl_window_get_attached_size(data->window,
612 &window->server_allocation.width,
613 &window->server_allocation.height);
614 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000615#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100616 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100617 buffer =
618 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400619 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100620
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200621 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100622 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400623 wl_surface_damage(window->surface, 0, 0,
624 window->allocation.width,
625 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100626 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400627 cairo_surface_destroy(window->cairo_surface);
628 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100629 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000630 default:
631 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100632 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500633
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500634 if (window->input_region) {
635 wl_surface_set_input_region(window->surface,
636 window->input_region);
637 wl_region_destroy(window->input_region);
638 window->input_region = NULL;
639 }
640
641 if (window->opaque_region) {
642 wl_surface_set_opaque_region(window->surface,
643 window->opaque_region);
644 wl_region_destroy(window->opaque_region);
645 window->opaque_region = NULL;
646 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500647}
648
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500649void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400650window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500651{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400652 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100653 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500654}
655
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400656void
657window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500659 cairo_surface_reference(surface);
660
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400661 if (window->cairo_surface != NULL)
662 cairo_surface_destroy(window->cairo_surface);
663
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500664 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400665}
666
Benjamin Franzke22d54812011-07-16 19:50:32 +0000667#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100668static void
669window_resize_cairo_window_surface(struct window *window)
670{
671 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200672 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100673
674 data = cairo_surface_get_user_data(window->cairo_surface,
675 &surface_data_key);
676
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200677 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100678 wl_egl_window_resize(data->window,
679 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200680 window->allocation.height,
681 x,y);
682
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100683 cairo_gl_surface_set_size(window->cairo_surface,
684 window->allocation.width,
685 window->allocation.height);
686}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000687#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100688
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400689struct display *
690window_get_display(struct window *window)
691{
692 return window->display;
693}
694
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400695void
696window_create_surface(struct window *window)
697{
698 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400699 uint32_t flags = 0;
700
701 if (!window->transparent)
702 flags = SURFACE_OPAQUE;
703
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500705#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100706 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
707 if (window->cairo_surface) {
708 window_resize_cairo_window_surface(window);
709 return;
710 }
711 surface = display_create_surface(window->display,
712 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400713 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100714 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400715#endif
716 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400717 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400718 &window->allocation,
719 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400720 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800721 default:
722 surface = NULL;
723 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400724 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400725
726 window_set_surface(window, surface);
727 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400728}
729
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200730static void frame_destroy(struct frame *frame);
731
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400732void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500733window_destroy(struct window *window)
734{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200735 struct display *display = window->display;
736 struct input *input;
737
738 if (window->redraw_scheduled)
739 wl_list_remove(&window->redraw_task.link);
740
741 wl_list_for_each(input, &display->input_list, link) {
742 if (input->pointer_focus == window)
743 input->pointer_focus = NULL;
744 if (input->keyboard_focus == window)
745 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500746 if (input->focus_widget &&
747 input->focus_widget->window == window)
748 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200749 }
750
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500751 if (window->input_region)
752 wl_region_destroy(window->input_region);
753 if (window->opaque_region)
754 wl_region_destroy(window->opaque_region);
755
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200756 if (window->frame)
757 frame_destroy(window->frame);
758
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200759 if (window->shell_surface)
760 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500761 wl_surface_destroy(window->surface);
762 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200763
764 if (window->cairo_surface != NULL)
765 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200766
767 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500768 free(window);
769}
770
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500771static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500772widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400773{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500774 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400775
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500776 wl_list_for_each(child, &widget->child_list, link) {
777 target = widget_find_widget(child, x, y);
778 if (target)
779 return target;
780 }
781
782 if (widget->allocation.x <= x &&
783 x < widget->allocation.x + widget->allocation.width &&
784 widget->allocation.y <= y &&
785 y < widget->allocation.y + widget->allocation.height) {
786 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400787 }
788
789 return NULL;
790}
791
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500792static struct widget *
793widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400794{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500795 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400796
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500797 widget = malloc(sizeof *widget);
798 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500799 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500800 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500801 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500802 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500803 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500804
805 return widget;
806}
807
808struct widget *
809window_add_widget(struct window *window, void *data)
810{
811 window->widget = widget_create(window, data);
812 wl_list_init(&window->widget->link);
813
814 return window->widget;
815}
816
817struct widget *
818widget_add_widget(struct widget *parent, void *data)
819{
820 struct widget *widget;
821
822 widget = widget_create(parent->window, data);
823 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400824
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500825 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400826}
827
828void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500829widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400830{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200831 struct display *display = widget->window->display;
832 struct input *input;
833
834 wl_list_for_each(input, &display->input_list, link) {
835 if (input->focus_widget == widget)
836 input->focus_widget = NULL;
837 }
838
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500839 wl_list_remove(&widget->link);
840 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400841}
842
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400843void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500844widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400845{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500846 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400847}
848
849void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500850widget_set_size(struct widget *widget, int32_t width, int32_t height)
851{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500852 widget->allocation.width = width;
853 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500854}
855
856void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500857widget_set_allocation(struct widget *widget,
858 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400859{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500860 widget->allocation.x = x;
861 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200862 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400863}
864
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500865void
866widget_set_transparent(struct widget *widget, int transparent)
867{
868 widget->opaque = !transparent;
869}
870
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400871void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500872widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400873{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500874 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400875}
876
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500877void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500878widget_set_resize_handler(struct widget *widget,
879 widget_resize_handler_t handler)
880{
881 widget->resize_handler = handler;
882}
883
884void
885widget_set_redraw_handler(struct widget *widget,
886 widget_redraw_handler_t handler)
887{
888 widget->redraw_handler = handler;
889}
890
891void
Kristian Høgsbergee143232012-01-09 08:42:24 -0500892widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500893{
Kristian Høgsbergee143232012-01-09 08:42:24 -0500894 widget->enter_handler = handler;
895}
896
897void
898widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
899{
900 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500901}
902
903void
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500904widget_set_motion_handler(struct widget *widget,
905 widget_motion_handler_t handler)
906{
907 widget->motion_handler = handler;
908}
909
910void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500911widget_set_button_handler(struct widget *widget,
912 widget_button_handler_t handler)
913{
914 widget->button_handler = handler;
915}
916
917void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500918widget_schedule_redraw(struct widget *widget)
919{
920 window_schedule_redraw(widget->window);
921}
922
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400923cairo_surface_t *
924window_get_surface(struct window *window)
925{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400926 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400927}
928
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100929struct wl_surface *
930window_get_wl_surface(struct window *window)
931{
932 return window->surface;
933}
934
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200935struct wl_shell_surface *
936window_get_wl_shell_surface(struct window *window)
937{
938 return window->shell_surface;
939}
940
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500941static void
942frame_resize_handler(struct widget *widget,
943 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400944{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500945 struct frame *frame = data;
946 struct widget *child = frame->child;
947 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500948 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500949 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500950 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500951
Kristian Høgsbergb435e842012-03-05 20:38:08 -0500952 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400953 decoration_width = (frame->width + frame->margin) * 2;
954 decoration_height = frame->width +
955 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500956
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400957 allocation.x = frame->width + frame->margin;
958 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -0500959 allocation.width = width - decoration_width;
960 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500961
962 widget->window->input_region =
963 wl_compositor_create_region(display->compositor);
964 wl_region_add(widget->window->input_region,
965 frame->margin, frame->margin,
966 width - 2 * frame->margin,
967 height - 2 * frame->margin);
968
969 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -0500970 } else {
971 decoration_width = 0;
972 decoration_height = 0;
973
974 allocation.x = 0;
975 allocation.y = 0;
976 allocation.width = width;
977 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500978 opaque_margin = 0;
979 }
980
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500981 widget_set_allocation(child, allocation.x, allocation.y,
982 allocation.width, allocation.height);
983
984 if (child->resize_handler)
985 child->resize_handler(child,
986 allocation.width,
987 allocation.height,
988 child->user_data);
989
990 widget_set_allocation(widget, 0, 0,
991 child->allocation.width + decoration_width,
992 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -0500993
994 if (child->opaque) {
995 widget->window->opaque_region =
996 wl_compositor_create_region(display->compositor);
997 wl_region_add(widget->window->opaque_region,
998 opaque_margin, opaque_margin,
999 widget->allocation.width - 2 * opaque_margin,
1000 widget->allocation.height - 2 * opaque_margin);
1001 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001002}
1003
1004static void
1005frame_redraw_handler(struct widget *widget, void *data)
1006{
1007 struct frame *frame = data;
1008 cairo_t *cr;
1009 cairo_text_extents_t extents;
1010 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001011 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001012 struct window *window = widget->window;
1013
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001014 if (window->type == TYPE_FULLSCREEN)
1015 return;
1016
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001017 width = widget->allocation.width;
1018 height = widget->allocation.height;
1019
1020 cr = cairo_create(window->cairo_surface);
1021
1022 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1023 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1024 cairo_paint(cr);
1025
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001026 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001027 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001028 2, 2, width + 8, height + 8,
1029 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001030
1031 if (window->keyboard_device)
1032 source = window->display->active_frame;
1033 else
1034 source = window->display->inactive_frame;
1035
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001036 tile_source(cr, source,
1037 frame->margin, frame->margin,
1038 width - frame->margin * 2, height - frame->margin * 2,
1039 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001040
1041 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001042 cairo_select_font_face(cr, "sans",
1043 CAIRO_FONT_SLANT_NORMAL,
1044 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001045 cairo_set_font_size(cr, 14);
1046 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001047 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001048 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001049 if (window->keyboard_device) {
1050 cairo_move_to(cr, x + 1, y + 1);
1051 cairo_set_source_rgb(cr, 1, 1, 1);
1052 cairo_show_text(cr, window->title);
1053 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001054 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001055 cairo_show_text(cr, window->title);
1056 } else {
1057 cairo_move_to(cr, x, y);
1058 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1059 cairo_show_text(cr, window->title);
1060 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001061
1062 cairo_destroy(cr);
1063}
1064
1065static int
1066frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1067{
1068 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001069 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001070 const int grip_size = 8;
1071
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001072 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001073 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001074 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001075 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001076 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001077 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001078 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001079 hlocation = WINDOW_RESIZING_RIGHT;
1080 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001081 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001082
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001083 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001084 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001085 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001086 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001087 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001088 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001089 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001090 vlocation = WINDOW_RESIZING_BOTTOM;
1091 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001092 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001093
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001094 location = vlocation | hlocation;
1095 if (location & WINDOW_EXTERIOR)
1096 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001097 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001098 location = WINDOW_TITLEBAR;
1099 else if (location == WINDOW_INTERIOR)
1100 location = WINDOW_CLIENT_AREA;
1101
1102 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001103}
1104
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001105static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001106frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001107{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001108 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001109
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001110 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001111 switch (location) {
1112 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001113 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001114 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001115 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001116 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001117 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001118 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001119 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001120 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001121 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001122 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001123 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001124 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001125 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001126 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001127 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001128 case WINDOW_EXTERIOR:
1129 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001130 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001131 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001132 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001133}
1134
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001135static void
1136frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001137{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001138 switch (index) {
1139 case 0: /* close */
1140 if (window->close_handler)
1141 window->close_handler(window->parent,
1142 window->user_data);
1143 else
1144 display_exit(window->display);
1145 break;
1146 case 1: /* fullscreen */
1147 /* we don't have a way to get out of fullscreen for now */
1148 window_set_fullscreen(window, 1);
1149 break;
1150 case 2: /* rotate */
1151 case 3: /* scale */
1152 break;
1153 }
1154}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001155
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001156void
1157window_show_frame_menu(struct window *window,
1158 struct input *input, uint32_t time)
1159{
1160 int32_t x, y;
1161
1162 static const char *entries[] = {
1163 "Close", "Fullscreen", "Rotate", "Scale"
1164 };
1165
1166 input_get_position(input, &x, &y);
1167 window_show_menu(window->display, input, time, window,
1168 x - 10, y - 10, frame_menu_func, entries, 4);
1169}
1170
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001171static int
1172frame_enter_handler(struct widget *widget,
1173 struct input *input, uint32_t time,
1174 int32_t x, int32_t y, void *data)
1175{
1176 return frame_get_pointer_image_for_location(data, input);
1177}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001178
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001179static int
1180frame_motion_handler(struct widget *widget,
1181 struct input *input, uint32_t time,
1182 int32_t x, int32_t y, void *data)
1183{
1184 return frame_get_pointer_image_for_location(data, input);
1185}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001186
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001187static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001188frame_button_handler(struct widget *widget,
1189 struct input *input, uint32_t time,
1190 int button, int state, void *data)
1191
1192{
1193 struct frame *frame = data;
1194 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001195 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001196 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001197
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001198 location = frame_get_pointer_location(frame, input->sx, input->sy);
1199
1200 if (window->display->shell && button == BTN_LEFT && state == 1) {
1201 switch (location) {
1202 case WINDOW_TITLEBAR:
1203 if (!window->shell_surface)
1204 break;
1205 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001206 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001207 wl_shell_surface_move(window->shell_surface,
1208 input_get_input_device(input),
1209 time);
1210 break;
1211 case WINDOW_RESIZING_TOP:
1212 case WINDOW_RESIZING_BOTTOM:
1213 case WINDOW_RESIZING_LEFT:
1214 case WINDOW_RESIZING_RIGHT:
1215 case WINDOW_RESIZING_TOP_LEFT:
1216 case WINDOW_RESIZING_TOP_RIGHT:
1217 case WINDOW_RESIZING_BOTTOM_LEFT:
1218 case WINDOW_RESIZING_BOTTOM_RIGHT:
1219 if (!window->shell_surface)
1220 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001221 input_ungrab(input, time);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001222
1223 if (!display->dpy) {
1224 /* If we're using shm, allocate a big
1225 pool to create buffers out of while
1226 we resize. We should probably base
1227 this number on the size of the output. */
1228 window->pool_size = 6 * 1024 * 1024;
1229 window->pool = make_shm_pool(display,
1230 window->pool_size,
1231 &window->pool_data);
1232 }
1233
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001234 wl_shell_surface_resize(window->shell_surface,
1235 input_get_input_device(input),
1236 time, location);
1237 break;
1238 }
1239 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001240 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001241 }
1242}
1243
1244struct widget *
1245frame_create(struct window *window, void *data)
1246{
1247 struct frame *frame;
1248
1249 frame = malloc(sizeof *frame);
1250 memset(frame, 0, sizeof *frame);
1251
1252 frame->widget = window_add_widget(window, frame);
1253 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001254 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001255 frame->width = 4;
1256 frame->titlebar_height = 30
1257;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1259 widget_set_resize_handler(frame->widget, frame_resize_handler);
1260 widget_set_enter_handler(frame->widget, frame_enter_handler);
1261 widget_set_motion_handler(frame->widget, frame_motion_handler);
1262 widget_set_button_handler(frame->widget, frame_button_handler);
1263
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001264 window->frame = frame;
1265
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001266 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001267}
1268
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001269static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001270frame_destroy(struct frame *frame)
1271{
1272 /* frame->child must be destroyed by the application */
1273 widget_destroy(frame->widget);
1274 free(frame);
1275}
1276
1277static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001278input_set_focus_widget(struct input *input, struct widget *focus,
1279 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001280{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001281 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001282 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001283
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001284 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001285 return;
1286
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001287 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001288 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001289 widget = old;
1290 if (input->grab)
1291 widget = input->grab;
1292 if (widget->leave_handler)
1293 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001294 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001295 }
1296
1297 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001298 widget = focus;
1299 if (input->grab)
1300 widget = input->grab;
1301 if (widget->enter_handler)
1302 pointer = widget->enter_handler(focus, input, time,
1303 x, y,
1304 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001305 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001306
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001307 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001308 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001309}
1310
1311static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001312input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001313 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001314{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001315 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001316 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001317 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001318 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001319
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001320 input->sx = sx;
1321 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001322
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001323 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001324 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001325 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001326 }
1327
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001328 if (input->grab)
1329 widget = input->grab;
1330 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001331 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001332 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001333 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001334 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001335 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001336
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001337 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001338}
1339
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001340void
1341input_grab(struct input *input, struct widget *widget, uint32_t button)
1342{
1343 input->grab = widget;
1344 input->grab_button = button;
1345}
1346
1347void
1348input_ungrab(struct input *input, uint32_t time)
1349{
1350 struct widget *widget;
1351
1352 input->grab = NULL;
1353 if (input->pointer_focus) {
1354 widget = widget_find_widget(input->pointer_focus->widget,
1355 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001356 input_set_focus_widget(input, widget,
1357 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001358 }
1359}
1360
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001361static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001362input_handle_button(void *data,
1363 struct wl_input_device *input_device,
1364 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001365{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001366 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001367 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001368
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001369 if (input->focus_widget && input->grab == NULL && state)
1370 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001371
Neil Roberts6b28aad2012-01-23 19:11:18 +00001372 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001373 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001374 (*widget->button_handler)(widget,
1375 input, time,
1376 button, state,
1377 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001378
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001379 if (input->grab && input->grab_button == button && !state)
1380 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001381}
1382
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001383static void
Scott Moreau210d0792012-03-22 10:47:01 -06001384input_handle_axis(void *data,
1385 struct wl_input_device *input_device,
1386 uint32_t time, uint32_t axis, int32_t value)
1387{
1388}
1389
1390static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001391input_handle_key(void *data, struct wl_input_device *input_device,
1392 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001393{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001394 struct input *input = data;
1395 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001396 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001397 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001398
Daniel Stone0d5a5092012-02-16 12:48:00 +00001399 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001400 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001401 return;
1402
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001403 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001404 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001405 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1406 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001407
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001408 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1409
1410 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001411 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001412 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001413 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001414
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001415 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1416 if (state)
1417 window_set_maximized(window,
1418 window->type != TYPE_MAXIMIZED);
1419 } else if (window->key_handler) {
1420 (*window->key_handler)(window, input, time, key,
1421 sym, state, window->user_data);
1422 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001423}
1424
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001425static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001426input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001427{
1428 struct window *window = input->pointer_focus;
1429
1430 if (!window)
1431 return;
1432
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001433 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001434
Pekka Paalanene1207c72011-12-16 12:02:09 +02001435 input->pointer_focus = NULL;
1436 input->current_pointer_image = POINTER_UNSET;
1437}
1438
1439static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001440input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001441 struct wl_input_device *input_device,
1442 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001443 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001444{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001445 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001446 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001447 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001448
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001449 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001450 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001451
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001452 if (window->pool) {
1453 wl_shm_pool_destroy(window->pool);
1454 munmap(window->pool_data, window->pool_size);
1455 window->pool = NULL;
1456 /* Schedule a redraw to free the pool */
1457 window_schedule_redraw(window);
1458 }
1459
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001460 input->sx = sx;
1461 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001462
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001463 widget = widget_find_widget(window->widget, sx, sy);
1464 input_set_focus_widget(input, widget, time, sx, sy);
1465}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001466
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001467static void
1468input_handle_pointer_leave(void *data,
1469 struct wl_input_device *input_device,
1470 uint32_t time, struct wl_surface *surface)
1471{
1472 struct input *input = data;
1473
1474 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001475}
1476
1477static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001478input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001479{
1480 struct window *window = input->keyboard_focus;
1481
1482 if (!window)
1483 return;
1484
1485 window->keyboard_device = NULL;
1486 if (window->keyboard_focus_handler)
1487 (*window->keyboard_focus_handler)(window, NULL,
1488 window->user_data);
1489
1490 input->keyboard_focus = NULL;
1491}
1492
1493static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001494input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001495 struct wl_input_device *input_device,
1496 uint32_t time,
1497 struct wl_surface *surface,
1498 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001499{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001500 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001501 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001502 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001503 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001504
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001505 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001506
1507 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001508 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001509 for (k = keys->data; k < end; k++)
1510 input->modifiers |= d->xkb->map->modmap[*k];
1511
1512 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001513 window->keyboard_device = input;
1514 if (window->keyboard_focus_handler)
1515 (*window->keyboard_focus_handler)(window,
1516 window->keyboard_device,
1517 window->user_data);
1518}
1519
1520static void
1521input_handle_keyboard_leave(void *data,
1522 struct wl_input_device *input_device,
1523 uint32_t time,
1524 struct wl_surface *surface)
1525{
1526 struct input *input = data;
1527
1528 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001529}
1530
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001531static void
1532input_handle_touch_down(void *data,
1533 struct wl_input_device *wl_input_device,
1534 uint32_t time, struct wl_surface *surface,
1535 int32_t id, int32_t x, int32_t y)
1536{
1537}
1538
1539static void
1540input_handle_touch_up(void *data,
1541 struct wl_input_device *wl_input_device,
1542 uint32_t time, int32_t id)
1543{
1544}
1545
1546static void
1547input_handle_touch_motion(void *data,
1548 struct wl_input_device *wl_input_device,
1549 uint32_t time, int32_t id, int32_t x, int32_t y)
1550{
1551}
1552
1553static void
1554input_handle_touch_frame(void *data,
1555 struct wl_input_device *wl_input_device)
1556{
1557}
1558
1559static void
1560input_handle_touch_cancel(void *data,
1561 struct wl_input_device *wl_input_device)
1562{
1563}
1564
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001565static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001566 input_handle_motion,
1567 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001568 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001569 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001570 input_handle_pointer_enter,
1571 input_handle_pointer_leave,
1572 input_handle_keyboard_enter,
1573 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001574 input_handle_touch_down,
1575 input_handle_touch_up,
1576 input_handle_touch_motion,
1577 input_handle_touch_frame,
1578 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001579};
1580
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001581void
1582input_get_position(struct input *input, int32_t *x, int32_t *y)
1583{
1584 *x = input->sx;
1585 *y = input->sy;
1586}
1587
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001588struct wl_input_device *
1589input_get_input_device(struct input *input)
1590{
1591 return input->input_device;
1592}
1593
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001594uint32_t
1595input_get_modifiers(struct input *input)
1596{
1597 return input->modifiers;
1598}
1599
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001600struct widget *
1601input_get_focus_widget(struct input *input)
1602{
1603 return input->focus_widget;
1604}
1605
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001606struct data_offer {
1607 struct wl_data_offer *offer;
1608 struct input *input;
1609 struct wl_array types;
1610 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001611
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001612 struct task io_task;
1613 int fd;
1614 data_func_t func;
1615 int32_t x, y;
1616 void *user_data;
1617};
1618
1619static void
1620data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1621{
1622 struct data_offer *offer = data;
1623 char **p;
1624
1625 p = wl_array_add(&offer->types, sizeof *p);
1626 *p = strdup(type);
1627}
1628
1629static const struct wl_data_offer_listener data_offer_listener = {
1630 data_offer_offer,
1631};
1632
1633static void
1634data_offer_destroy(struct data_offer *offer)
1635{
1636 char **p;
1637
1638 offer->refcount--;
1639 if (offer->refcount == 0) {
1640 wl_data_offer_destroy(offer->offer);
1641 for (p = offer->types.data; *p; p++)
1642 free(*p);
1643 wl_array_release(&offer->types);
1644 free(offer);
1645 }
1646}
1647
1648static void
1649data_device_data_offer(void *data,
1650 struct wl_data_device *data_device, uint32_t id)
1651{
1652 struct data_offer *offer;
1653
1654 offer = malloc(sizeof *offer);
1655
1656 wl_array_init(&offer->types);
1657 offer->refcount = 1;
1658 offer->input = data;
1659
1660 /* FIXME: Generate typesafe wrappers for this */
1661 offer->offer = (struct wl_data_offer *)
1662 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1663 id, &wl_data_offer_interface);
1664
1665 wl_data_offer_add_listener(offer->offer,
1666 &data_offer_listener, offer);
1667}
1668
1669static void
1670data_device_enter(void *data, struct wl_data_device *data_device,
1671 uint32_t time, struct wl_surface *surface,
1672 int32_t x, int32_t y, struct wl_data_offer *offer)
1673{
1674 struct input *input = data;
1675 struct window *window;
1676 char **p;
1677
1678 input->drag_offer = wl_data_offer_get_user_data(offer);
1679 window = wl_surface_get_user_data(surface);
1680 input->pointer_focus = window;
1681
1682 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1683 *p = NULL;
1684
1685 window = input->pointer_focus;
1686 if (window->data_handler)
1687 window->data_handler(window, input, time, x, y,
1688 input->drag_offer->types.data,
1689 window->user_data);
1690}
1691
1692static void
1693data_device_leave(void *data, struct wl_data_device *data_device)
1694{
1695 struct input *input = data;
1696
1697 data_offer_destroy(input->drag_offer);
1698 input->drag_offer = NULL;
1699}
1700
1701static void
1702data_device_motion(void *data, struct wl_data_device *data_device,
1703 uint32_t time, int32_t x, int32_t y)
1704{
1705 struct input *input = data;
1706 struct window *window = input->pointer_focus;
1707
1708 input->sx = x;
1709 input->sy = y;
1710
1711 if (window->data_handler)
1712 window->data_handler(window, input, time, x, y,
1713 input->drag_offer->types.data,
1714 window->user_data);
1715}
1716
1717static void
1718data_device_drop(void *data, struct wl_data_device *data_device)
1719{
1720 struct input *input = data;
1721 struct window *window = input->pointer_focus;
1722
1723 if (window->drop_handler)
1724 window->drop_handler(window, input,
1725 input->sx, input->sy, window->user_data);
1726}
1727
1728static void
1729data_device_selection(void *data,
1730 struct wl_data_device *wl_data_device,
1731 struct wl_data_offer *offer)
1732{
1733 struct input *input = data;
1734 char **p;
1735
1736 if (input->selection_offer)
1737 data_offer_destroy(input->selection_offer);
1738
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001739 if (offer) {
1740 input->selection_offer = wl_data_offer_get_user_data(offer);
1741 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1742 *p = NULL;
1743 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001744}
1745
1746static const struct wl_data_device_listener data_device_listener = {
1747 data_device_data_offer,
1748 data_device_enter,
1749 data_device_leave,
1750 data_device_motion,
1751 data_device_drop,
1752 data_device_selection
1753};
1754
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001755void
1756input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1757{
1758 struct display *display = input->display;
1759 struct wl_buffer *buffer;
1760 cairo_surface_t *surface;
1761
1762 if (pointer == input->current_pointer_image)
1763 return;
1764
1765 input->current_pointer_image = pointer;
1766 surface = display->pointer_surfaces[pointer];
1767
1768 if (!surface)
1769 return;
1770
1771 buffer = display_get_buffer_for_surface(display, surface);
1772 wl_input_device_attach(input->input_device, time, buffer,
1773 pointer_images[pointer].hotspot_x,
1774 pointer_images[pointer].hotspot_y);
1775}
1776
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001777struct wl_data_device *
1778input_get_data_device(struct input *input)
1779{
1780 return input->data_device;
1781}
1782
1783void
1784input_set_selection(struct input *input,
1785 struct wl_data_source *source, uint32_t time)
1786{
1787 wl_data_device_set_selection(input->data_device, source, time);
1788}
1789
1790void
1791input_accept(struct input *input, uint32_t time, const char *type)
1792{
1793 wl_data_offer_accept(input->drag_offer->offer, time, type);
1794}
1795
1796static void
1797offer_io_func(struct task *task, uint32_t events)
1798{
1799 struct data_offer *offer =
1800 container_of(task, struct data_offer, io_task);
1801 unsigned int len;
1802 char buffer[4096];
1803
1804 len = read(offer->fd, buffer, sizeof buffer);
1805 offer->func(buffer, len,
1806 offer->x, offer->y, offer->user_data);
1807
1808 if (len == 0) {
1809 close(offer->fd);
1810 data_offer_destroy(offer);
1811 }
1812}
1813
1814static void
1815data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1816 data_func_t func, void *user_data)
1817{
1818 int p[2];
1819
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02001820 if (pipe2(p, O_CLOEXEC) == -1)
1821 return;
1822
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001823 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1824 close(p[1]);
1825
1826 offer->io_task.run = offer_io_func;
1827 offer->fd = p[0];
1828 offer->func = func;
1829 offer->refcount++;
1830 offer->user_data = user_data;
1831
1832 display_watch_fd(offer->input->display,
1833 offer->fd, EPOLLIN, &offer->io_task);
1834}
1835
1836void
1837input_receive_drag_data(struct input *input, const char *mime_type,
1838 data_func_t func, void *data)
1839{
1840 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1841 input->drag_offer->x = input->sx;
1842 input->drag_offer->y = input->sy;
1843}
1844
1845int
1846input_receive_selection_data(struct input *input, const char *mime_type,
1847 data_func_t func, void *data)
1848{
1849 char **p;
1850
1851 if (input->selection_offer == NULL)
1852 return -1;
1853
1854 for (p = input->selection_offer->types.data; *p; p++)
1855 if (strcmp(mime_type, *p) == 0)
1856 break;
1857
1858 if (*p == NULL)
1859 return -1;
1860
1861 data_offer_receive_data(input->selection_offer,
1862 mime_type, func, data);
1863 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001864}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001865
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001866int
1867input_receive_selection_data_to_fd(struct input *input,
1868 const char *mime_type, int fd)
1869{
1870 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1871
1872 return 0;
1873}
1874
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001875void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001876window_move(struct window *window, struct input *input, uint32_t time)
1877{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001878 if (!window->shell_surface)
1879 return;
1880
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001881 wl_shell_surface_move(window->shell_surface,
1882 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001883}
1884
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001885static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001886idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001887{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001888 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001889
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001890 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001891 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001892 widget_set_allocation(widget,
1893 window->pending_allocation.x,
1894 window->pending_allocation.y,
1895 window->pending_allocation.width,
1896 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001897
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001898 if (window->input_region) {
1899 wl_region_destroy(window->input_region);
1900 window->input_region = NULL;
1901 }
1902
1903 if (window->opaque_region) {
1904 wl_region_destroy(window->opaque_region);
1905 window->opaque_region = NULL;
1906 }
1907
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001908 if (widget->resize_handler)
1909 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001910 widget->allocation.width,
1911 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001912 widget->user_data);
1913
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05001914 if (window->allocation.width != widget->allocation.width ||
1915 window->allocation.height != widget->allocation.height) {
1916 window->allocation = widget->allocation;
1917 window_schedule_redraw(window);
1918 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001919}
1920
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001921void
1922window_schedule_resize(struct window *window, int width, int height)
1923{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001924 window->pending_allocation.x = 0;
1925 window->pending_allocation.y = 0;
1926 window->pending_allocation.width = width;
1927 window->pending_allocation.height = height;
1928
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001929 window->resize_needed = 1;
1930 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001931}
1932
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001933void
1934widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
1935{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001936 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001937}
1938
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001939static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001940handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001941 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001942 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001943{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001944 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001945
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001946 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001947 return;
1948
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001949 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001950 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001951}
1952
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001953static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02001954menu_destroy(struct menu *menu)
1955{
1956 widget_destroy(menu->widget);
1957 window_destroy(menu->window);
1958 free(menu);
1959}
1960
1961static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001962handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1963{
1964 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001965 struct menu *menu = window->widget->user_data;
1966
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001967 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001968 * device. Or just use wl_callback. And this really needs to
1969 * be a window vfunc that the menu can set. And we need the
1970 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001971
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001972 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001973 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02001974 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001975}
1976
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001977static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001978 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001979 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001980};
1981
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001982void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001983window_get_allocation(struct window *window,
1984 struct rectangle *allocation)
1985{
1986 *allocation = window->allocation;
1987}
1988
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001989static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001990widget_redraw(struct widget *widget)
1991{
1992 struct widget *child;
1993
1994 if (widget->redraw_handler)
1995 widget->redraw_handler(widget, widget->user_data);
1996 wl_list_for_each(child, &widget->child_list, link)
1997 widget_redraw(child);
1998}
1999
2000static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002001frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2002{
2003 struct window *window = data;
2004
2005 wl_callback_destroy(callback);
2006 window->redraw_scheduled = 0;
2007 if (window->redraw_needed)
2008 window_schedule_redraw(window);
2009}
2010
2011static const struct wl_callback_listener listener = {
2012 frame_callback
2013};
2014
2015static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002016idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002017{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002018 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002019 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002020
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002021 if (window->resize_needed)
2022 idle_resize(window);
2023
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002024 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002025 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002026 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002027 window->redraw_needed = 0;
2028
2029 callback = wl_surface_frame(window->surface);
2030 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002031}
2032
2033void
2034window_schedule_redraw(struct window *window)
2035{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002036 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002037 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002038 window->redraw_task.run = idle_redraw;
2039 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002040 window->redraw_scheduled = 1;
2041 }
2042}
2043
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002044void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002045window_set_custom(struct window *window)
2046{
2047 window->type = TYPE_CUSTOM;
2048}
2049
2050void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002051window_set_fullscreen(struct window *window, int fullscreen)
2052{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002053 if (!window->display->shell)
2054 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002055
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002056 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002057 return;
2058
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002059 if (fullscreen) {
2060 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002061 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002062 wl_shell_surface_set_fullscreen(window->shell_surface,
2063 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2064 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002065 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002066 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002067 wl_shell_surface_set_toplevel(window->shell_surface);
2068 window_schedule_resize(window,
2069 window->saved_allocation.width,
2070 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002071 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002072}
2073
2074void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002075window_set_maximized(struct window *window, int maximized)
2076{
2077 if (!window->display->shell)
2078 return;
2079
2080 if ((window->type == TYPE_MAXIMIZED) == maximized)
2081 return;
2082
2083 if (window->type == TYPE_TOPLEVEL) {
2084 window->saved_allocation = window->allocation;
2085 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2086 window->type = TYPE_MAXIMIZED;
2087 } else {
2088 wl_shell_surface_set_toplevel(window->shell_surface);
2089 window->type = TYPE_TOPLEVEL;
2090 window_schedule_resize(window,
2091 window->saved_allocation.width,
2092 window->saved_allocation.height);
2093 }
2094}
2095
2096void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002097window_set_user_data(struct window *window, void *data)
2098{
2099 window->user_data = data;
2100}
2101
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002102void *
2103window_get_user_data(struct window *window)
2104{
2105 return window->user_data;
2106}
2107
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002108void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002109window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002110 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002111{
2112 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002113}
2114
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002115void
2116window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002117 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002118{
2119 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002120}
2121
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002122void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002123window_set_data_handler(struct window *window, window_data_handler_t handler)
2124{
2125 window->data_handler = handler;
2126}
2127
2128void
2129window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2130{
2131 window->drop_handler = handler;
2132}
2133
2134void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002135window_set_close_handler(struct window *window,
2136 window_close_handler_t handler)
2137{
2138 window->close_handler = handler;
2139}
2140
2141void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002142window_set_title(struct window *window, const char *title)
2143{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002144 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002145 window->title = strdup(title);
2146}
2147
2148const char *
2149window_get_title(struct window *window)
2150{
2151 return window->title;
2152}
2153
2154void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002155window_damage(struct window *window, int32_t x, int32_t y,
2156 int32_t width, int32_t height)
2157{
2158 wl_surface_damage(window->surface, x, y, width, height);
2159}
2160
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002161static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002162window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002163{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002164 struct window *window;
2165
2166 window = malloc(sizeof *window);
2167 if (window == NULL)
2168 return NULL;
2169
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002170 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002171 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002172 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002173 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002174 if (display->shell) {
2175 window->shell_surface =
2176 wl_shell_get_shell_surface(display->shell,
2177 window->surface);
2178 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002179 window->allocation.x = 0;
2180 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002181 window->allocation.width = 0;
2182 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002183 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002184 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002185 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002186 window->input_region = NULL;
2187 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002188
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002189 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002190#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002191 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002192#else
2193 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2194#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002195 else
2196 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002197
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002198 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002199 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002200
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002201 if (window->shell_surface) {
2202 wl_shell_surface_set_user_data(window->shell_surface, window);
2203 wl_shell_surface_add_listener(window->shell_surface,
2204 &shell_surface_listener, window);
2205 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002206
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002207 return window;
2208}
2209
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002210struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002211window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002212{
2213 struct window *window;
2214
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002215 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002216 if (!window)
2217 return NULL;
2218
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002219 window->type = TYPE_TOPLEVEL;
2220 if (display->shell)
2221 wl_shell_surface_set_toplevel(window->shell_surface);
2222
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002223 return window;
2224}
2225
2226struct window *
2227window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002228 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002229{
2230 struct window *window;
2231
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002232 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002233 if (!window)
2234 return NULL;
2235
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002236 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002237 window->x = x;
2238 window->y = y;
2239
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002240 if (display->shell)
2241 wl_shell_surface_set_transient(window->shell_surface,
2242 window->parent->shell_surface,
2243 window->x, window->y, 0);
2244
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002245 return window;
2246}
2247
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002248static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002249menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002250{
2251 int next;
2252
2253 next = (sy - 8) / 20;
2254 if (menu->current != next) {
2255 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002256 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002257 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002258}
2259
2260static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002261menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002262 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002263 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002264{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002265 struct menu *menu = data;
2266
2267 if (widget == menu->widget)
2268 menu_set_item(data, y);
2269
2270 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002271}
2272
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002273static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002274menu_enter_handler(struct widget *widget,
2275 struct input *input, uint32_t time,
2276 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002277{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002278 struct menu *menu = data;
2279
2280 if (widget == menu->widget)
2281 menu_set_item(data, y);
2282
2283 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002284}
2285
2286static void
2287menu_leave_handler(struct widget *widget, struct input *input, void *data)
2288{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002289 struct menu *menu = data;
2290
2291 if (widget == menu->widget)
2292 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002293}
2294
2295static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002296menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002297 struct input *input, uint32_t time,
2298 int button, int state, void *data)
2299
2300{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002301 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002302
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002303 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002304 /* Either relase after press-drag-release or
2305 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002306 menu->func(menu->window->parent,
2307 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002308 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002309 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002310 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002311}
2312
2313static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002314menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002315{
2316 cairo_t *cr;
2317 const int32_t r = 3, margin = 3;
2318 struct menu *menu = data;
2319 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002320 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002321
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002322 cr = cairo_create(window->cairo_surface);
2323 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2324 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2325 cairo_paint(cr);
2326
2327 width = window->allocation.width;
2328 height = window->allocation.height;
2329 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002330
2331 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002332 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2333 cairo_fill(cr);
2334
2335 for (i = 0; i < menu->count; i++) {
2336 if (i == menu->current) {
2337 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2338 cairo_rectangle(cr, margin, i * 20 + margin,
2339 width - 2 * margin, 20);
2340 cairo_fill(cr);
2341 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2342 cairo_move_to(cr, 10, i * 20 + 16);
2343 cairo_show_text(cr, menu->entries[i]);
2344 } else {
2345 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2346 cairo_move_to(cr, 10, i * 20 + 16);
2347 cairo_show_text(cr, menu->entries[i]);
2348 }
2349 }
2350
2351 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002352}
2353
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002354void
2355window_show_menu(struct display *display,
2356 struct input *input, uint32_t time, struct window *parent,
2357 int32_t x, int32_t y,
2358 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002359{
2360 struct window *window;
2361 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002362 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002363
2364 menu = malloc(sizeof *menu);
2365 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002366 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002367
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002368 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002369 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002370 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002371
2372 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002373 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002374 menu->entries = entries;
2375 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002376 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002377 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002378 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002379 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002380 window->type = TYPE_MENU;
2381 window->x = x;
2382 window->y = y;
2383
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002384 wl_shell_surface_set_popup(window->shell_surface,
2385 input->input_device, time,
2386 window->parent->shell_surface,
2387 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002388
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002389 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002390 widget_set_enter_handler(menu->widget, menu_enter_handler);
2391 widget_set_leave_handler(menu->widget, menu_leave_handler);
2392 widget_set_motion_handler(menu->widget, menu_motion_handler);
2393 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002394
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002395 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002396 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002397}
2398
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002399void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002400window_set_buffer_type(struct window *window, enum window_buffer_type type)
2401{
2402 window->buffer_type = type;
2403}
2404
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002405
2406static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002407display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002408 struct wl_output *wl_output,
2409 int x, int y,
2410 int physical_width,
2411 int physical_height,
2412 int subpixel,
2413 const char *make,
2414 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002415{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002416 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002417
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002418 output->allocation.x = x;
2419 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002420}
2421
2422static void
2423display_handle_mode(void *data,
2424 struct wl_output *wl_output,
2425 uint32_t flags,
2426 int width,
2427 int height,
2428 int refresh)
2429{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002430 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002431 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002432
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002433 if (flags & WL_OUTPUT_MODE_CURRENT) {
2434 output->allocation.width = width;
2435 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002436 if (display->output_configure_handler)
2437 (*display->output_configure_handler)(
2438 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002439 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002440}
2441
2442static const struct wl_output_listener output_listener = {
2443 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002444 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002445};
2446
2447static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002448display_add_output(struct display *d, uint32_t id)
2449{
2450 struct output *output;
2451
2452 output = malloc(sizeof *output);
2453 if (output == NULL)
2454 return;
2455
2456 memset(output, 0, sizeof *output);
2457 output->display = d;
2458 output->output =
2459 wl_display_bind(d->display, id, &wl_output_interface);
2460 wl_list_insert(d->output_list.prev, &output->link);
2461
2462 wl_output_add_listener(output->output, &output_listener, output);
2463}
2464
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002465static void
2466output_destroy(struct output *output)
2467{
2468 if (output->destroy_handler)
2469 (*output->destroy_handler)(output, output->user_data);
2470
2471 wl_output_destroy(output->output);
2472 wl_list_remove(&output->link);
2473 free(output);
2474}
2475
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002476void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002477display_set_output_configure_handler(struct display *display,
2478 display_output_handler_t handler)
2479{
2480 struct output *output;
2481
2482 display->output_configure_handler = handler;
2483 if (!handler)
2484 return;
2485
2486 wl_list_for_each(output, &display->output_list, link)
2487 (*display->output_configure_handler)(output,
2488 display->user_data);
2489}
2490
2491void
2492output_set_user_data(struct output *output, void *data)
2493{
2494 output->user_data = data;
2495}
2496
2497void *
2498output_get_user_data(struct output *output)
2499{
2500 return output->user_data;
2501}
2502
2503void
2504output_set_destroy_handler(struct output *output,
2505 display_output_handler_t handler)
2506{
2507 output->destroy_handler = handler;
2508 /* FIXME: implement this, once we have way to remove outputs */
2509}
2510
2511void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002512output_get_allocation(struct output *output, struct rectangle *allocation)
2513{
2514 *allocation = output->allocation;
2515}
2516
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002517struct wl_output *
2518output_get_wl_output(struct output *output)
2519{
2520 return output->output;
2521}
2522
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002523static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002524display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002525{
2526 struct input *input;
2527
2528 input = malloc(sizeof *input);
2529 if (input == NULL)
2530 return;
2531
2532 memset(input, 0, sizeof *input);
2533 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002534 input->input_device =
2535 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002536 input->pointer_focus = NULL;
2537 input->keyboard_focus = NULL;
2538 wl_list_insert(d->input_list.prev, &input->link);
2539
2540 wl_input_device_add_listener(input->input_device,
2541 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002542 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002543
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002544 input->data_device =
2545 wl_data_device_manager_get_data_device(d->data_device_manager,
2546 input->input_device);
2547 wl_data_device_add_listener(input->data_device,
2548 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002549}
2550
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002551static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002552input_destroy(struct input *input)
2553{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002554 input_remove_keyboard_focus(input);
2555 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002556
2557 if (input->drag_offer)
2558 data_offer_destroy(input->drag_offer);
2559
2560 if (input->selection_offer)
2561 data_offer_destroy(input->selection_offer);
2562
2563 wl_data_device_destroy(input->data_device);
2564 wl_list_remove(&input->link);
2565 wl_input_device_destroy(input->input_device);
2566 free(input);
2567}
2568
2569static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002570display_handle_global(struct wl_display *display, uint32_t id,
2571 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002572{
2573 struct display *d = data;
2574
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002575 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002576 d->compositor =
2577 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002578 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002579 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002580 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002581 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002582 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002583 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002584 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002585 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002586 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2587 d->data_device_manager =
2588 wl_display_bind(display, id,
2589 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002590 }
2591}
2592
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002593static void
2594display_render_frame(struct display *d)
2595{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002596 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002597 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002598
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002599 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002600 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2601 cr = cairo_create(d->shadow);
2602 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2603 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002604 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002605 cairo_fill(cr);
2606 cairo_destroy(cr);
2607 blur_surface(d->shadow, 64);
2608
2609 d->active_frame =
2610 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2611 cr = cairo_create(d->active_frame);
2612 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002613
2614 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2615 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2616 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2617 cairo_set_source(cr, pattern);
2618 cairo_pattern_destroy(pattern);
2619
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002620 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002621 cairo_fill(cr);
2622 cairo_destroy(cr);
2623
2624 d->inactive_frame =
2625 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2626 cr = cairo_create(d->inactive_frame);
2627 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002628 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002629 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002630 cairo_fill(cr);
2631 cairo_destroy(cr);
2632}
2633
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002634static void
2635init_xkb(struct display *d)
2636{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002637 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002638
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002639 names.rules = "evdev";
2640 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002641 names.layout = option_xkb_layout;
2642 names.variant = option_xkb_variant;
2643 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002644
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002645 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002646 if (!d->xkb) {
2647 fprintf(stderr, "Failed to compile keymap\n");
2648 exit(1);
2649 }
2650}
2651
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002652static void
2653fini_xkb(struct display *display)
2654{
2655 xkb_free_keymap(display->xkb);
2656}
2657
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002658#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002659static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002660init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002661{
2662 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002663 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002664
Rob Clark6396ed32012-03-11 19:48:41 -05002665#ifdef USE_CAIRO_GLESV2
2666# define GL_BIT EGL_OPENGL_ES2_BIT
2667#else
2668# define GL_BIT EGL_OPENGL_BIT
2669#endif
2670
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002671 static const EGLint argb_cfg_attribs[] = {
2672 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002673 EGL_RED_SIZE, 1,
2674 EGL_GREEN_SIZE, 1,
2675 EGL_BLUE_SIZE, 1,
2676 EGL_ALPHA_SIZE, 1,
2677 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002678 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002679 EGL_NONE
2680 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002681
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002682#ifdef USE_CAIRO_GLESV2
2683 static const EGLint context_attribs[] = {
2684 EGL_CONTEXT_CLIENT_VERSION, 2,
2685 EGL_NONE
2686 };
2687 EGLint api = EGL_OPENGL_ES_API;
2688#else
2689 EGLint *context_attribs = NULL;
2690 EGLint api = EGL_OPENGL_API;
2691#endif
2692
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002693 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002694 if (!eglInitialize(d->dpy, &major, &minor)) {
2695 fprintf(stderr, "failed to initialize display\n");
2696 return -1;
2697 }
2698
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002699 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002700 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2701 return -1;
2702 }
2703
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002704 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2705 &d->argb_config, 1, &n) || n != 1) {
2706 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002707 return -1;
2708 }
2709
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002710 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002711 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002712 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002713 fprintf(stderr, "failed to create context\n");
2714 return -1;
2715 }
2716
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002717 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002718 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002719 return -1;
2720 }
2721
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002722#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002723 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2724 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2725 fprintf(stderr, "failed to get cairo egl argb device\n");
2726 return -1;
2727 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002728#endif
2729
2730 return 0;
2731}
2732
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002733static void
2734fini_egl(struct display *display)
2735{
2736#ifdef HAVE_CAIRO_EGL
2737 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002738#endif
2739
2740 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2741 EGL_NO_CONTEXT);
2742
2743 eglTerminate(display->dpy);
2744 eglReleaseThread();
2745}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002746#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002747
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002748static int
2749event_mask_update(uint32_t mask, void *data)
2750{
2751 struct display *d = data;
2752
2753 d->mask = mask;
2754
2755 return 0;
2756}
2757
2758static void
2759handle_display_data(struct task *task, uint32_t events)
2760{
2761 struct display *display =
2762 container_of(task, struct display, display_task);
2763
2764 wl_display_iterate(display->display, display->mask);
2765}
2766
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002767struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002768display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002769{
2770 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002771
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002772 argc = parse_options(xkb_options,
2773 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002774
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002775 d = malloc(sizeof *d);
2776 if (d == NULL)
2777 return NULL;
2778
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002779 memset(d, 0, sizeof *d);
2780
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002781 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002782 if (d->display == NULL) {
2783 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002784 return NULL;
2785 }
2786
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002787 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2788 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2789 d->display_task.run = handle_display_data;
2790 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2791
2792 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002793 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002794 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002795
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002796 /* Set up listener so we'll catch all events. */
2797 wl_display_add_global_listener(d->display,
2798 display_handle_global, d);
2799
2800 /* Process connection events. */
2801 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002802#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002803 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002804 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002805#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002806
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002807 d->image_target_texture_2d =
2808 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2809 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2810 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2811
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002812 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002813
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002814 display_render_frame(d);
2815
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002816 wl_list_init(&d->window_list);
2817
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002818 init_xkb(d);
2819
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002820 return d;
2821}
2822
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002823static void
2824display_destroy_outputs(struct display *display)
2825{
2826 struct output *tmp;
2827 struct output *output;
2828
2829 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2830 output_destroy(output);
2831}
2832
Pekka Paalanene1207c72011-12-16 12:02:09 +02002833static void
2834display_destroy_inputs(struct display *display)
2835{
2836 struct input *tmp;
2837 struct input *input;
2838
2839 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2840 input_destroy(input);
2841}
2842
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002843void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002844display_destroy(struct display *display)
2845{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002846 if (!wl_list_empty(&display->window_list))
2847 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2848
2849 if (!wl_list_empty(&display->deferred_list))
2850 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2851
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002852 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002853 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002854
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002855 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002856
2857 cairo_surface_destroy(display->active_frame);
2858 cairo_surface_destroy(display->inactive_frame);
2859 cairo_surface_destroy(display->shadow);
2860 destroy_pointer_surfaces(display);
2861
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002862#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002863 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002864#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002865
Pekka Paalanenc2052982011-12-16 11:41:32 +02002866 if (display->shell)
2867 wl_shell_destroy(display->shell);
2868
2869 if (display->shm)
2870 wl_shm_destroy(display->shm);
2871
2872 if (display->data_device_manager)
2873 wl_data_device_manager_destroy(display->data_device_manager);
2874
2875 wl_compositor_destroy(display->compositor);
2876
2877 close(display->epoll_fd);
2878
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002879 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05002880 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002881 free(display);
2882}
2883
2884void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002885display_set_user_data(struct display *display, void *data)
2886{
2887 display->user_data = data;
2888}
2889
2890void *
2891display_get_user_data(struct display *display)
2892{
2893 return display->user_data;
2894}
2895
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002896struct wl_display *
2897display_get_display(struct display *display)
2898{
2899 return display->display;
2900}
2901
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002902struct output *
2903display_get_output(struct display *display)
2904{
2905 return container_of(display->output_list.next, struct output, link);
2906}
2907
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002908struct wl_compositor *
2909display_get_compositor(struct display *display)
2910{
2911 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002912}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002913
2914EGLDisplay
2915display_get_egl_display(struct display *d)
2916{
2917 return d->dpy;
2918}
2919
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002920struct wl_data_source *
2921display_create_data_source(struct display *display)
2922{
2923 return wl_data_device_manager_create_data_source(display->data_device_manager);
2924}
2925
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002926EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002927display_get_argb_egl_config(struct display *d)
2928{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002929 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002930}
2931
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002932struct wl_shell *
2933display_get_shell(struct display *display)
2934{
2935 return display->shell;
2936}
2937
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002938int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002939display_acquire_window_surface(struct display *display,
2940 struct window *window,
2941 EGLContext ctx)
2942{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002943#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002944 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002945 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002946
2947 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002948 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002949 device = cairo_surface_get_device(window->cairo_surface);
2950 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002951 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002952
Benjamin Franzke0c991632011-09-27 21:57:31 +02002953 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002954 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02002955 ctx = display->argb_ctx;
2956 else
2957 assert(0);
2958 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002959
2960 data = cairo_surface_get_user_data(window->cairo_surface,
2961 &surface_data_key);
2962
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002963 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002964 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002965 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2966 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002967
2968 return 0;
2969#else
2970 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002971#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002972}
2973
2974void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002975display_release_window_surface(struct display *display,
2976 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002977{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002978#ifdef HAVE_CAIRO_EGL
2979 cairo_device_t *device;
2980
2981 device = cairo_surface_get_device(window->cairo_surface);
2982 if (!device)
2983 return;
2984
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002985 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002986 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002987 cairo_device_release(device);
2988#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002989}
2990
2991void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002992display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002993{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002994 wl_list_insert(&display->deferred_list, &task->link);
2995}
2996
2997void
2998display_watch_fd(struct display *display,
2999 int fd, uint32_t events, struct task *task)
3000{
3001 struct epoll_event ep;
3002
3003 ep.events = events;
3004 ep.data.ptr = task;
3005 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3006}
3007
3008void
3009display_run(struct display *display)
3010{
3011 struct task *task;
3012 struct epoll_event ep[16];
3013 int i, count;
3014
Pekka Paalanen826d7952011-12-15 10:14:07 +02003015 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003016 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003017 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003018
3019 while (!wl_list_empty(&display->deferred_list)) {
3020 task = container_of(display->deferred_list.next,
3021 struct task, link);
3022 wl_list_remove(&task->link);
3023 task->run(task, 0);
3024 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003025
3026 if (!display->running)
3027 break;
3028
3029 wl_display_flush(display->display);
3030
3031 count = epoll_wait(display->epoll_fd,
3032 ep, ARRAY_LENGTH(ep), -1);
3033 for (i = 0; i < count; i++) {
3034 task = ep[i].data.ptr;
3035 task->run(task, ep[i].events);
3036 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003037 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003038}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003039
3040void
3041display_exit(struct display *display)
3042{
3043 display->running = 0;
3044}