blob: 2dcf421fc56edd0f83e74258d87c5c75d8555b38 [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>
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030060#include <X11/Xcursor/Xcursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040061
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050062#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020063#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040064#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050065
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050066#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050067
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030068struct cursor;
69
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050070struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050071 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050072 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040073 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040074 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040075 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040076 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050077 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020078 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020079 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040080 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040081
82 int display_fd;
83 uint32_t mask;
84 struct task display_task;
85
86 int epoll_fd;
87 struct wl_list deferred_list;
88
Pekka Paalanen826d7952011-12-15 10:14:07 +020089 int running;
90
Kristian Høgsberg478d9262010-06-08 20:34:11 -040091 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040092 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050093 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040094 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050095 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040096 struct xkb_desc *xkb;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030097 struct cursor *cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040098
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050099 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
100 PFNEGLCREATEIMAGEKHRPROC create_image;
101 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200102
103 display_output_handler_t output_configure_handler;
104
105 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500106};
107
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400108enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400109 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400110 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400111 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500112 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500114 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 TYPE_CUSTOM
116};
117
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500118struct window {
119 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500120 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500121 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200122 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500123 struct wl_region *input_region;
124 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500125 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500126 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500127 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500128 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400129 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400130 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400131 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400132 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400133 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400134 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400135 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400136 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400137 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500138
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400139 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500140
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400141 struct wl_shm_pool *pool;
142 size_t pool_size;
143 void *pool_data;
144
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500145 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500146 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400147 window_data_handler_t data_handler;
148 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500149 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400150
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200151 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500152 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500153
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500154 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400155 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500156};
157
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500158struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500159 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500160 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161 struct wl_list link;
162 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500163 widget_resize_handler_t resize_handler;
164 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500165 widget_enter_handler_t enter_handler;
166 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500167 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500168 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400169 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500170 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400171};
172
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400173struct input {
174 struct display *display;
175 struct wl_input_device *input_device;
176 struct window *pointer_focus;
177 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300178 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400179 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400180 uint32_t pointer_enter_serial;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200181 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400182 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400183
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500184 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500185 struct widget *grab;
186 uint32_t grab_button;
187
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400188 struct wl_data_device *data_device;
189 struct data_offer *drag_offer;
190 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400191};
192
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500193struct output {
194 struct display *display;
195 struct wl_output *output;
196 struct rectangle allocation;
197 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200198
199 display_output_handler_t destroy_handler;
200 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500201};
202
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500203struct frame {
204 struct widget *widget;
205 struct widget *child;
206 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400207 int width;
208 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500209};
210
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500211struct menu {
212 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500213 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500214 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500215 const char **entries;
216 uint32_t time;
217 int current;
218 int count;
219 menu_func_t func;
220};
221
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300222struct cursor_image {
223 cairo_surface_t *surface;
224 int width, height;
225 int hotspot_x, hotspot_y;
226 int delay;
227};
228
229struct cursor {
230 int n_images;
231 struct cursor_image *images;
232};
233
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400234enum {
235 POINTER_DEFAULT = 100,
236 POINTER_UNSET
237};
238
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500239enum window_location {
240 WINDOW_INTERIOR = 0,
241 WINDOW_RESIZING_TOP = 1,
242 WINDOW_RESIZING_BOTTOM = 2,
243 WINDOW_RESIZING_LEFT = 4,
244 WINDOW_RESIZING_TOP_LEFT = 5,
245 WINDOW_RESIZING_BOTTOM_LEFT = 6,
246 WINDOW_RESIZING_RIGHT = 8,
247 WINDOW_RESIZING_TOP_RIGHT = 9,
248 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
249 WINDOW_RESIZING_MASK = 15,
250 WINDOW_EXTERIOR = 16,
251 WINDOW_TITLEBAR = 17,
252 WINDOW_CLIENT_AREA = 18,
253};
254
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400255const char *option_xkb_layout = "us";
256const char *option_xkb_variant = "";
257const char *option_xkb_options = "";
258
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400259static const struct weston_option xkb_options[] = {
260 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
261 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
262 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400263};
264
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400265static const cairo_user_data_key_t surface_data_key;
266struct surface_data {
267 struct wl_buffer *buffer;
268};
269
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500270#define MULT(_d,c,a,t) \
271 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
272
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500273#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400274
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100275struct egl_window_surface_data {
276 struct display *display;
277 struct wl_surface *surface;
278 struct wl_egl_window *window;
279 EGLSurface surf;
280};
281
282static void
283egl_window_surface_data_destroy(void *p)
284{
285 struct egl_window_surface_data *data = p;
286 struct display *d = data->display;
287
288 eglDestroySurface(d->dpy, data->surf);
289 wl_egl_window_destroy(data->window);
290 data->surface = NULL;
291
292 free(p);
293}
294
295static cairo_surface_t *
296display_create_egl_window_surface(struct display *display,
297 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400298 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100299 struct rectangle *rectangle)
300{
301 cairo_surface_t *cairo_surface;
302 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400303 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200304 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100305
306 data = malloc(sizeof *data);
307 if (data == NULL)
308 return NULL;
309
310 data->display = display;
311 data->surface = surface;
312
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500313 config = display->argb_config;
314 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400315
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400316 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100317 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400318 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100319
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400320 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500321 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100322
Benjamin Franzke0c991632011-09-27 21:57:31 +0200323 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100324 data->surf,
325 rectangle->width,
326 rectangle->height);
327
328 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
329 data, egl_window_surface_data_destroy);
330
331 return cairo_surface;
332}
333
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400334#endif
335
336struct wl_buffer *
337display_get_buffer_for_surface(struct display *display,
338 cairo_surface_t *surface)
339{
340 struct surface_data *data;
341
342 data = cairo_surface_get_user_data (surface, &surface_data_key);
343
344 return data->buffer;
345}
346
347struct shm_surface_data {
348 struct surface_data data;
349 void *map;
350 size_t length;
351};
352
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500353static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354shm_surface_data_destroy(void *p)
355{
356 struct shm_surface_data *data = p;
357
358 wl_buffer_destroy(data->data.buffer);
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400359 if (data->map)
360 munmap(data->map, data->length);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400361}
362
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300363static void
364shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
365{
366 void *dest = cairo_image_surface_get_data(surface);
367
368 memcpy(dest, data, count);
369}
370
Kristian Høgsberg16626282012-04-03 11:21:27 -0400371static struct wl_shm_pool *
372make_shm_pool(struct display *display, int size, void **data)
373{
374 char filename[] = "/tmp/wayland-shm-XXXXXX";
375 struct wl_shm_pool *pool;
376 int fd;
377
378 fd = mkstemp(filename);
379 if (fd < 0) {
380 fprintf(stderr, "open %s failed: %m\n", filename);
381 return NULL;
382 }
383 if (ftruncate(fd, size) < 0) {
384 fprintf(stderr, "ftruncate failed: %m\n");
385 close(fd);
386 return NULL;
387 }
388
389 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
390 unlink(filename);
391
392 if (*data == MAP_FAILED) {
393 fprintf(stderr, "mmap failed: %m\n");
394 close(fd);
395 return NULL;
396 }
397
398 pool = wl_shm_create_pool(display->shm, fd, size);
399
400 close(fd);
401
402 return pool;
403}
404
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500405static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406display_create_shm_surface(struct display *display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400407 struct rectangle *rectangle, uint32_t flags,
408 struct window *window)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409{
410 struct shm_surface_data *data;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400411 struct wl_shm_pool *pool = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400412 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400413 cairo_surface_t *surface;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400414 void *map;
Kristian Høgsberg16626282012-04-03 11:21:27 -0400415 int stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400416
417 data = malloc(sizeof *data);
418 if (data == NULL)
419 return NULL;
420
421 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
422 rectangle->width);
423 data->length = stride * rectangle->height;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400424 if (window && window->pool && data->length < window->pool_size) {
425 pool = window->pool;
426 map = window->pool_data;
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400427 data->map = NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400428 } else {
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400429 pool = make_shm_pool(display, data->length, &map);
430 data->map = map;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400431 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400432
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400433 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400434 CAIRO_FORMAT_ARGB32,
435 rectangle->width,
436 rectangle->height,
437 stride);
438
439 cairo_surface_set_user_data (surface, &surface_data_key,
440 data, shm_surface_data_destroy);
441
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400442 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500443 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400444 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500445 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400446
Kristian Høgsberg16626282012-04-03 11:21:27 -0400447 data->data.buffer = wl_shm_pool_create_buffer(pool, 0,
448 rectangle->width,
449 rectangle->height,
450 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400451
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400452 if (data->map)
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400453 wl_shm_pool_destroy(pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454
455 return surface;
456}
457
nobled7b87cb02011-02-01 18:51:47 +0000458static int
459check_size(struct rectangle *rect)
460{
461 if (rect->width && rect->height)
462 return 0;
463
464 fprintf(stderr, "tried to create surface of "
465 "width: %d, height: %d\n", rect->width, rect->height);
466 return -1;
467}
468
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400469cairo_surface_t *
470display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100471 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400472 struct rectangle *rectangle,
473 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400474{
nobled7b87cb02011-02-01 18:51:47 +0000475 if (check_size(rectangle) < 0)
476 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500477#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400478 if (display->dpy)
479 return display_create_egl_window_surface(display,
480 surface,
481 flags,
482 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400483#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400484 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400485}
486
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300487static const char *cursors[] = {
488 "bottom_left_corner",
489 "bottom_right_corner",
490 "bottom_side",
491 "grabbing",
492 "left_ptr",
493 "left_side",
494 "right_side",
495 "top_left_corner",
496 "top_right_corner",
497 "top_side",
498 "xterm",
499 "hand1",
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400500};
501
502static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300503create_cursor_from_images(struct display *display, struct cursor *cursor,
504 XcursorImages *images)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400505{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300506 int i;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400507 struct rectangle rect;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300508 XcursorImage *image;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400509
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300510 cursor->images = malloc(images->nimage * sizeof *cursor->images);
511 cursor->n_images = images->nimage;
512
513 for (i = 0; i < images->nimage; i++) {
514 image = images->images[i];
515
516 rect.width = image->width;
517 rect.height = image->height;
518
519 cursor->images[i].surface =
520 display_create_shm_surface(display, &rect, 0, NULL);
521
522 shm_surface_write(cursor->images[i].surface,
523 (unsigned char *) image->pixels,
524 image->width * image->height * sizeof image->pixels[0]);
525
526 cursor->images[i].width = image->width;
527 cursor->images[i].height = image->height;
528 cursor->images[i].hotspot_x = image->xhot;
529 cursor->images[i].hotspot_y = image->yhot;
530 cursor->images[i].delay = image->delay;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400531 }
532
533}
534
Pekka Paalanen325bb602011-12-19 10:31:45 +0200535static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300536create_cursors(struct display *display)
537{
538 int i, count;
539 struct cursor *cursor;
540 XcursorImages *images;
541
542 count = ARRAY_LENGTH(cursors);
543 display->cursors = malloc(count * sizeof *display->cursors);
544 for (i = 0; i < count; i++) {
545 images = XcursorLibraryLoadImages(cursors[i], NULL, 32);
546
547 if (!images) {
548 fprintf(stderr, "Error loading cursor: %s\n",
549 cursors[i]);
550 continue;
551 }
552
553 cursor = &display->cursors[i];
554 create_cursor_from_images(display, cursor, images);
555
556 XcursorImagesDestroy(images);
557 }
558
559}
560
561static void
562destroy_cursor_images(struct cursor *cursor)
563{
564 int i;
565
566 for (i = 0; i < cursor->n_images; i++)
567 if (cursor->images[i].surface)
568 cairo_surface_destroy(cursor->images[i].surface);
569
570 free(cursor->images);
571}
572
573static void
574destroy_cursors(struct display *display)
Pekka Paalanen325bb602011-12-19 10:31:45 +0200575{
576 int i, count;
577
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300578 count = ARRAY_LENGTH(cursors);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200579 for (i = 0; i < count; ++i) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300580 destroy_cursor_images(&display->cursors[i]);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200581 }
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300582 free(display->cursors);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200583}
584
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400585cairo_surface_t *
586display_get_pointer_surface(struct display *display, int pointer,
587 int *width, int *height,
588 int *hotspot_x, int *hotspot_y)
589{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300590 struct cursor *cursor = &display->cursors[pointer];
591 cairo_surface_t *surface = cursor->images[0].surface;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400592
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300593 /* FIXME returning information for the first image. Something better
594 * is needed for animated cursors */
595
nobledf8475c92011-01-05 17:41:55 +0000596 *width = cairo_image_surface_get_width(surface);
597 *height = cairo_image_surface_get_height(surface);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300598
599 *hotspot_x = cursor->images[0].hotspot_x;
600 *hotspot_y = cursor->images[0].hotspot_y;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400601
602 return cairo_surface_reference(surface);
603}
604
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400605static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200606window_get_resize_dx_dy(struct window *window, int *x, int *y)
607{
608 if (window->resize_edges & WINDOW_RESIZING_LEFT)
609 *x = window->server_allocation.width - window->allocation.width;
610 else
611 *x = 0;
612
613 if (window->resize_edges & WINDOW_RESIZING_TOP)
614 *y = window->server_allocation.height -
615 window->allocation.height;
616 else
617 *y = 0;
618
619 window->resize_edges = 0;
620}
621
622static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500623window_attach_surface(struct window *window)
624{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400625 struct display *display = window->display;
626 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000627#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100628 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000629#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500630 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400632 if (window->type == TYPE_NONE) {
633 window->type = TYPE_TOPLEVEL;
634 if (display->shell)
635 wl_shell_surface_set_toplevel(window->shell_surface);
636 }
637
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000639#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100640 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
641 data = cairo_surface_get_user_data(window->cairo_surface,
642 &surface_data_key);
643
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100644 cairo_gl_surface_swapbuffers(window->cairo_surface);
645 wl_egl_window_get_attached_size(data->window,
646 &window->server_allocation.width,
647 &window->server_allocation.height);
648 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000649#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100651 buffer =
652 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400653 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100654
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200655 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100656 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400657 wl_surface_damage(window->surface, 0, 0,
658 window->allocation.width,
659 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100660 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400661 cairo_surface_destroy(window->cairo_surface);
662 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100663 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000664 default:
665 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100666 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500667
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500668 if (window->input_region) {
669 wl_surface_set_input_region(window->surface,
670 window->input_region);
671 wl_region_destroy(window->input_region);
672 window->input_region = NULL;
673 }
674
675 if (window->opaque_region) {
676 wl_surface_set_opaque_region(window->surface,
677 window->opaque_region);
678 wl_region_destroy(window->opaque_region);
679 window->opaque_region = NULL;
680 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500681}
682
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500683void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400684window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500685{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400686 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100687 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500688}
689
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400690void
691window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400692{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500693 cairo_surface_reference(surface);
694
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400695 if (window->cairo_surface != NULL)
696 cairo_surface_destroy(window->cairo_surface);
697
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500698 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400699}
700
Benjamin Franzke22d54812011-07-16 19:50:32 +0000701#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100702static void
703window_resize_cairo_window_surface(struct window *window)
704{
705 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200706 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100707
708 data = cairo_surface_get_user_data(window->cairo_surface,
709 &surface_data_key);
710
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200711 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100712 wl_egl_window_resize(data->window,
713 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200714 window->allocation.height,
715 x,y);
716
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100717 cairo_gl_surface_set_size(window->cairo_surface,
718 window->allocation.width,
719 window->allocation.height);
720}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000721#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400723struct display *
724window_get_display(struct window *window)
725{
726 return window->display;
727}
728
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400729void
730window_create_surface(struct window *window)
731{
732 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400733 uint32_t flags = 0;
734
735 if (!window->transparent)
736 flags = SURFACE_OPAQUE;
737
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400738 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500739#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
741 if (window->cairo_surface) {
742 window_resize_cairo_window_surface(window);
743 return;
744 }
745 surface = display_create_surface(window->display,
746 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400747 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100748 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400749#endif
750 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400751 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400752 &window->allocation,
753 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400754 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800755 default:
756 surface = NULL;
757 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400758 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400759
760 window_set_surface(window, surface);
761 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400762}
763
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200764static void frame_destroy(struct frame *frame);
765
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400766void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500767window_destroy(struct window *window)
768{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200769 struct display *display = window->display;
770 struct input *input;
771
772 if (window->redraw_scheduled)
773 wl_list_remove(&window->redraw_task.link);
774
775 wl_list_for_each(input, &display->input_list, link) {
776 if (input->pointer_focus == window)
777 input->pointer_focus = NULL;
778 if (input->keyboard_focus == window)
779 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500780 if (input->focus_widget &&
781 input->focus_widget->window == window)
782 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200783 }
784
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500785 if (window->input_region)
786 wl_region_destroy(window->input_region);
787 if (window->opaque_region)
788 wl_region_destroy(window->opaque_region);
789
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200790 if (window->frame)
791 frame_destroy(window->frame);
792
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200793 if (window->shell_surface)
794 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500795 wl_surface_destroy(window->surface);
796 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200797
798 if (window->cairo_surface != NULL)
799 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200800
801 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500802 free(window);
803}
804
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500805static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500806widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400807{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500808 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400809
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500810 wl_list_for_each(child, &widget->child_list, link) {
811 target = widget_find_widget(child, x, y);
812 if (target)
813 return target;
814 }
815
816 if (widget->allocation.x <= x &&
817 x < widget->allocation.x + widget->allocation.width &&
818 widget->allocation.y <= y &&
819 y < widget->allocation.y + widget->allocation.height) {
820 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400821 }
822
823 return NULL;
824}
825
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500826static struct widget *
827widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400828{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500829 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400830
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500831 widget = malloc(sizeof *widget);
832 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500833 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500834 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500835 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500836 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500837 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500838
839 return widget;
840}
841
842struct widget *
843window_add_widget(struct window *window, void *data)
844{
845 window->widget = widget_create(window, data);
846 wl_list_init(&window->widget->link);
847
848 return window->widget;
849}
850
851struct widget *
852widget_add_widget(struct widget *parent, void *data)
853{
854 struct widget *widget;
855
856 widget = widget_create(parent->window, data);
857 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400858
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500859 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400860}
861
862void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500863widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400864{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200865 struct display *display = widget->window->display;
866 struct input *input;
867
868 wl_list_for_each(input, &display->input_list, link) {
869 if (input->focus_widget == widget)
870 input->focus_widget = NULL;
871 }
872
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500873 wl_list_remove(&widget->link);
874 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400875}
876
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400877void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500878widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400879{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500880 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400881}
882
883void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500884widget_set_size(struct widget *widget, int32_t width, int32_t height)
885{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500886 widget->allocation.width = width;
887 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500888}
889
890void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500891widget_set_allocation(struct widget *widget,
892 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400893{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500894 widget->allocation.x = x;
895 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200896 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400897}
898
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500899void
900widget_set_transparent(struct widget *widget, int transparent)
901{
902 widget->opaque = !transparent;
903}
904
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400905void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500906widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400907{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500908 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400909}
910
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500911void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500912widget_set_resize_handler(struct widget *widget,
913 widget_resize_handler_t handler)
914{
915 widget->resize_handler = handler;
916}
917
918void
919widget_set_redraw_handler(struct widget *widget,
920 widget_redraw_handler_t handler)
921{
922 widget->redraw_handler = handler;
923}
924
925void
Kristian Høgsbergee143232012-01-09 08:42:24 -0500926widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500927{
Kristian Høgsbergee143232012-01-09 08:42:24 -0500928 widget->enter_handler = handler;
929}
930
931void
932widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
933{
934 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500935}
936
937void
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500938widget_set_motion_handler(struct widget *widget,
939 widget_motion_handler_t handler)
940{
941 widget->motion_handler = handler;
942}
943
944void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500945widget_set_button_handler(struct widget *widget,
946 widget_button_handler_t handler)
947{
948 widget->button_handler = handler;
949}
950
951void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500952widget_schedule_redraw(struct widget *widget)
953{
954 window_schedule_redraw(widget->window);
955}
956
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400957cairo_surface_t *
958window_get_surface(struct window *window)
959{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400960 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400961}
962
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100963struct wl_surface *
964window_get_wl_surface(struct window *window)
965{
966 return window->surface;
967}
968
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200969struct wl_shell_surface *
970window_get_wl_shell_surface(struct window *window)
971{
972 return window->shell_surface;
973}
974
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500975static void
976frame_resize_handler(struct widget *widget,
977 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400978{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500979 struct frame *frame = data;
980 struct widget *child = frame->child;
981 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500982 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500983 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500984 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500985
Kristian Høgsbergb435e842012-03-05 20:38:08 -0500986 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400987 decoration_width = (frame->width + frame->margin) * 2;
988 decoration_height = frame->width +
989 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500990
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400991 allocation.x = frame->width + frame->margin;
992 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -0500993 allocation.width = width - decoration_width;
994 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500995
996 widget->window->input_region =
997 wl_compositor_create_region(display->compositor);
998 wl_region_add(widget->window->input_region,
999 frame->margin, frame->margin,
1000 width - 2 * frame->margin,
1001 height - 2 * frame->margin);
1002
1003 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001004 } else {
1005 decoration_width = 0;
1006 decoration_height = 0;
1007
1008 allocation.x = 0;
1009 allocation.y = 0;
1010 allocation.width = width;
1011 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001012 opaque_margin = 0;
1013 }
1014
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001015 widget_set_allocation(child, allocation.x, allocation.y,
1016 allocation.width, allocation.height);
1017
1018 if (child->resize_handler)
1019 child->resize_handler(child,
1020 allocation.width,
1021 allocation.height,
1022 child->user_data);
1023
1024 widget_set_allocation(widget, 0, 0,
1025 child->allocation.width + decoration_width,
1026 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001027
1028 if (child->opaque) {
1029 widget->window->opaque_region =
1030 wl_compositor_create_region(display->compositor);
1031 wl_region_add(widget->window->opaque_region,
1032 opaque_margin, opaque_margin,
1033 widget->allocation.width - 2 * opaque_margin,
1034 widget->allocation.height - 2 * opaque_margin);
1035 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001036}
1037
1038static void
1039frame_redraw_handler(struct widget *widget, void *data)
1040{
1041 struct frame *frame = data;
1042 cairo_t *cr;
1043 cairo_text_extents_t extents;
1044 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001045 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001046 struct window *window = widget->window;
1047
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001048 if (window->type == TYPE_FULLSCREEN)
1049 return;
1050
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001051 width = widget->allocation.width;
1052 height = widget->allocation.height;
1053
1054 cr = cairo_create(window->cairo_surface);
1055
1056 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1057 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1058 cairo_paint(cr);
1059
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001060 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001061 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001062 2, 2, width + 8, height + 8,
1063 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001064
1065 if (window->keyboard_device)
1066 source = window->display->active_frame;
1067 else
1068 source = window->display->inactive_frame;
1069
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001070 tile_source(cr, source,
1071 frame->margin, frame->margin,
1072 width - frame->margin * 2, height - frame->margin * 2,
1073 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001074
1075 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001076 cairo_select_font_face(cr, "sans",
1077 CAIRO_FONT_SLANT_NORMAL,
1078 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001079 cairo_set_font_size(cr, 14);
1080 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001081 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001082 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001083 if (window->keyboard_device) {
1084 cairo_move_to(cr, x + 1, y + 1);
1085 cairo_set_source_rgb(cr, 1, 1, 1);
1086 cairo_show_text(cr, window->title);
1087 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001088 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001089 cairo_show_text(cr, window->title);
1090 } else {
1091 cairo_move_to(cr, x, y);
1092 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1093 cairo_show_text(cr, window->title);
1094 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001095
1096 cairo_destroy(cr);
1097}
1098
1099static int
1100frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1101{
1102 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001103 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001104 const int grip_size = 8;
1105
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001106 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001107 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001108 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001109 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001110 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001111 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001112 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001113 hlocation = WINDOW_RESIZING_RIGHT;
1114 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001115 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001116
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001117 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001118 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001119 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001120 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001121 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001122 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001123 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001124 vlocation = WINDOW_RESIZING_BOTTOM;
1125 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001126 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001127
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001128 location = vlocation | hlocation;
1129 if (location & WINDOW_EXTERIOR)
1130 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001131 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001132 location = WINDOW_TITLEBAR;
1133 else if (location == WINDOW_INTERIOR)
1134 location = WINDOW_CLIENT_AREA;
1135
1136 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001137}
1138
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001139static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001140frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001141{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001142 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001143
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001144 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001145 switch (location) {
1146 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001147 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001148 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001149 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001150 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001151 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001152 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001153 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001154 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001155 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001156 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001157 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001158 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001159 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001160 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001161 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001162 case WINDOW_EXTERIOR:
1163 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001164 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001165 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001166 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001167}
1168
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169static void
1170frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001171{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001172 switch (index) {
1173 case 0: /* close */
1174 if (window->close_handler)
1175 window->close_handler(window->parent,
1176 window->user_data);
1177 else
1178 display_exit(window->display);
1179 break;
1180 case 1: /* fullscreen */
1181 /* we don't have a way to get out of fullscreen for now */
1182 window_set_fullscreen(window, 1);
1183 break;
1184 case 2: /* rotate */
1185 case 3: /* scale */
1186 break;
1187 }
1188}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001189
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001190void
1191window_show_frame_menu(struct window *window,
1192 struct input *input, uint32_t time)
1193{
1194 int32_t x, y;
1195
1196 static const char *entries[] = {
1197 "Close", "Fullscreen", "Rotate", "Scale"
1198 };
1199
1200 input_get_position(input, &x, &y);
1201 window_show_menu(window->display, input, time, window,
1202 x - 10, y - 10, frame_menu_func, entries, 4);
1203}
1204
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001205static int
1206frame_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001207 struct input *input, int32_t x, int32_t y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001208{
1209 return frame_get_pointer_image_for_location(data, input);
1210}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001211
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001212static int
1213frame_motion_handler(struct widget *widget,
1214 struct input *input, uint32_t time,
1215 int32_t x, int32_t y, void *data)
1216{
1217 return frame_get_pointer_image_for_location(data, input);
1218}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001219
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001220static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001221frame_button_handler(struct widget *widget,
1222 struct input *input, uint32_t time,
1223 int button, int state, void *data)
1224
1225{
1226 struct frame *frame = data;
1227 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001228 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001229 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001230
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001231 location = frame_get_pointer_location(frame, input->sx, input->sy);
1232
1233 if (window->display->shell && button == BTN_LEFT && state == 1) {
1234 switch (location) {
1235 case WINDOW_TITLEBAR:
1236 if (!window->shell_surface)
1237 break;
1238 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001239 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001240 wl_shell_surface_move(window->shell_surface,
1241 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001242 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001243 break;
1244 case WINDOW_RESIZING_TOP:
1245 case WINDOW_RESIZING_BOTTOM:
1246 case WINDOW_RESIZING_LEFT:
1247 case WINDOW_RESIZING_RIGHT:
1248 case WINDOW_RESIZING_TOP_LEFT:
1249 case WINDOW_RESIZING_TOP_RIGHT:
1250 case WINDOW_RESIZING_BOTTOM_LEFT:
1251 case WINDOW_RESIZING_BOTTOM_RIGHT:
1252 if (!window->shell_surface)
1253 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001254 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001255
1256 if (!display->dpy) {
1257 /* If we're using shm, allocate a big
1258 pool to create buffers out of while
1259 we resize. We should probably base
1260 this number on the size of the output. */
1261 window->pool_size = 6 * 1024 * 1024;
1262 window->pool = make_shm_pool(display,
1263 window->pool_size,
1264 &window->pool_data);
1265 }
1266
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001267 wl_shell_surface_resize(window->shell_surface,
1268 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001269 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001270 break;
1271 }
1272 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001273 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001274 }
1275}
1276
1277struct widget *
1278frame_create(struct window *window, void *data)
1279{
1280 struct frame *frame;
1281
1282 frame = malloc(sizeof *frame);
1283 memset(frame, 0, sizeof *frame);
1284
1285 frame->widget = window_add_widget(window, frame);
1286 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001287 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001288 frame->width = 4;
1289 frame->titlebar_height = 30
1290;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001291 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1292 widget_set_resize_handler(frame->widget, frame_resize_handler);
1293 widget_set_enter_handler(frame->widget, frame_enter_handler);
1294 widget_set_motion_handler(frame->widget, frame_motion_handler);
1295 widget_set_button_handler(frame->widget, frame_button_handler);
1296
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001297 window->frame = frame;
1298
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001299 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001300}
1301
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001302static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001303frame_destroy(struct frame *frame)
1304{
1305 /* frame->child must be destroyed by the application */
1306 widget_destroy(frame->widget);
1307 free(frame);
1308}
1309
1310static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001311input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001312 int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001313{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001314 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001315 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001316
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001317 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001318 return;
1319
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001320 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001321 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001322 widget = old;
1323 if (input->grab)
1324 widget = input->grab;
1325 if (widget->leave_handler)
1326 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001327 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001328 }
1329
1330 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001331 widget = focus;
1332 if (input->grab)
1333 widget = input->grab;
1334 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001335 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001336 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001337 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001338
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001339 input_set_pointer_image(input, input->pointer_enter_serial,
1340 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001341 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001342}
1343
1344static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001345input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001346 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001347{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001348 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001349 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001350 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001351 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001352
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001353 input->sx = sx;
1354 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001355
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001356 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001357 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001358 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001359 }
1360
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001361 if (input->grab)
1362 widget = input->grab;
1363 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001364 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001365 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001366 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001367 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001368 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001369
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001370 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001371}
1372
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001373void
1374input_grab(struct input *input, struct widget *widget, uint32_t button)
1375{
1376 input->grab = widget;
1377 input->grab_button = button;
1378}
1379
1380void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001381input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001382{
1383 struct widget *widget;
1384
1385 input->grab = NULL;
1386 if (input->pointer_focus) {
1387 widget = widget_find_widget(input->pointer_focus->widget,
1388 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001389 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001390 }
1391}
1392
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001393static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001394input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001395 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001396 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001397{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001398 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001399 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001400
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001401 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001402 if (input->focus_widget && input->grab == NULL && state)
1403 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001404
Neil Roberts6b28aad2012-01-23 19:11:18 +00001405 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001406 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001407 (*widget->button_handler)(widget,
1408 input, time,
1409 button, state,
1410 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001411
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001412 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001413 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001414}
1415
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001416static void
Scott Moreau210d0792012-03-22 10:47:01 -06001417input_handle_axis(void *data,
1418 struct wl_input_device *input_device,
1419 uint32_t time, uint32_t axis, int32_t value)
1420{
1421}
1422
1423static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001424input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001425 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001426{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001427 struct input *input = data;
1428 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001429 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001430 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001431
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001432 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001433 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001434 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001435 return;
1436
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001437 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001438 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001439 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1440 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001441
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001442 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1443
1444 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001445 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001446 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001447 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001448
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001449 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1450 if (state)
1451 window_set_maximized(window,
1452 window->type != TYPE_MAXIMIZED);
1453 } else if (window->key_handler) {
1454 (*window->key_handler)(window, input, time, key,
1455 sym, state, window->user_data);
1456 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001457}
1458
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001459static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001460input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001461{
1462 struct window *window = input->pointer_focus;
1463
1464 if (!window)
1465 return;
1466
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001467 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001468
Pekka Paalanene1207c72011-12-16 12:02:09 +02001469 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001470 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001471}
1472
1473static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001474input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001475 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001476 uint32_t serial, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001477 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001478{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001479 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001480 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001481 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001482
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001483 input->display->serial = serial;
1484 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001485 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001486 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001487
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001488 if (window->pool) {
1489 wl_shm_pool_destroy(window->pool);
1490 munmap(window->pool_data, window->pool_size);
1491 window->pool = NULL;
1492 /* Schedule a redraw to free the pool */
1493 window_schedule_redraw(window);
1494 }
1495
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001496 input->sx = sx;
1497 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001498
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001499 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001500 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001501}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001502
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001503static void
1504input_handle_pointer_leave(void *data,
1505 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001506 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001507{
1508 struct input *input = data;
1509
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001510 input->display->serial = serial;
1511 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001512}
1513
1514static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001515input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001516{
1517 struct window *window = input->keyboard_focus;
1518
1519 if (!window)
1520 return;
1521
1522 window->keyboard_device = NULL;
1523 if (window->keyboard_focus_handler)
1524 (*window->keyboard_focus_handler)(window, NULL,
1525 window->user_data);
1526
1527 input->keyboard_focus = NULL;
1528}
1529
1530static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001531input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001532 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001533 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001534 struct wl_surface *surface,
1535 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001536{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001537 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001538 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001539 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001540 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001541
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001542 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001543 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001544
1545 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001546 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001547 for (k = keys->data; k < end; k++)
1548 input->modifiers |= d->xkb->map->modmap[*k];
1549
1550 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001551 window->keyboard_device = input;
1552 if (window->keyboard_focus_handler)
1553 (*window->keyboard_focus_handler)(window,
1554 window->keyboard_device,
1555 window->user_data);
1556}
1557
1558static void
1559input_handle_keyboard_leave(void *data,
1560 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001561 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001562 struct wl_surface *surface)
1563{
1564 struct input *input = data;
1565
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001566 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001567 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001568}
1569
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001570static void
1571input_handle_touch_down(void *data,
1572 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001573 uint32_t serial, uint32_t time,
1574 struct wl_surface *surface,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001575 int32_t id, int32_t x, int32_t y)
1576{
1577}
1578
1579static void
1580input_handle_touch_up(void *data,
1581 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001582 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001583{
1584}
1585
1586static void
1587input_handle_touch_motion(void *data,
1588 struct wl_input_device *wl_input_device,
1589 uint32_t time, int32_t id, int32_t x, int32_t y)
1590{
1591}
1592
1593static void
1594input_handle_touch_frame(void *data,
1595 struct wl_input_device *wl_input_device)
1596{
1597}
1598
1599static void
1600input_handle_touch_cancel(void *data,
1601 struct wl_input_device *wl_input_device)
1602{
1603}
1604
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001605static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001606 input_handle_motion,
1607 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001608 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001609 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001610 input_handle_pointer_enter,
1611 input_handle_pointer_leave,
1612 input_handle_keyboard_enter,
1613 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001614 input_handle_touch_down,
1615 input_handle_touch_up,
1616 input_handle_touch_motion,
1617 input_handle_touch_frame,
1618 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001619};
1620
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001621void
1622input_get_position(struct input *input, int32_t *x, int32_t *y)
1623{
1624 *x = input->sx;
1625 *y = input->sy;
1626}
1627
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001628struct wl_input_device *
1629input_get_input_device(struct input *input)
1630{
1631 return input->input_device;
1632}
1633
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001634uint32_t
1635input_get_modifiers(struct input *input)
1636{
1637 return input->modifiers;
1638}
1639
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001640struct widget *
1641input_get_focus_widget(struct input *input)
1642{
1643 return input->focus_widget;
1644}
1645
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001646struct data_offer {
1647 struct wl_data_offer *offer;
1648 struct input *input;
1649 struct wl_array types;
1650 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001651
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001652 struct task io_task;
1653 int fd;
1654 data_func_t func;
1655 int32_t x, y;
1656 void *user_data;
1657};
1658
1659static void
1660data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1661{
1662 struct data_offer *offer = data;
1663 char **p;
1664
1665 p = wl_array_add(&offer->types, sizeof *p);
1666 *p = strdup(type);
1667}
1668
1669static const struct wl_data_offer_listener data_offer_listener = {
1670 data_offer_offer,
1671};
1672
1673static void
1674data_offer_destroy(struct data_offer *offer)
1675{
1676 char **p;
1677
1678 offer->refcount--;
1679 if (offer->refcount == 0) {
1680 wl_data_offer_destroy(offer->offer);
1681 for (p = offer->types.data; *p; p++)
1682 free(*p);
1683 wl_array_release(&offer->types);
1684 free(offer);
1685 }
1686}
1687
1688static void
1689data_device_data_offer(void *data,
1690 struct wl_data_device *data_device, uint32_t id)
1691{
1692 struct data_offer *offer;
1693
1694 offer = malloc(sizeof *offer);
1695
1696 wl_array_init(&offer->types);
1697 offer->refcount = 1;
1698 offer->input = data;
1699
1700 /* FIXME: Generate typesafe wrappers for this */
1701 offer->offer = (struct wl_data_offer *)
1702 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1703 id, &wl_data_offer_interface);
1704
1705 wl_data_offer_add_listener(offer->offer,
1706 &data_offer_listener, offer);
1707}
1708
1709static void
1710data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001711 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001712 int32_t x, int32_t y, struct wl_data_offer *offer)
1713{
1714 struct input *input = data;
1715 struct window *window;
1716 char **p;
1717
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001718 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001719 input->drag_offer = wl_data_offer_get_user_data(offer);
1720 window = wl_surface_get_user_data(surface);
1721 input->pointer_focus = window;
1722
1723 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1724 *p = NULL;
1725
1726 window = input->pointer_focus;
1727 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001728 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001729 input->drag_offer->types.data,
1730 window->user_data);
1731}
1732
1733static void
1734data_device_leave(void *data, struct wl_data_device *data_device)
1735{
1736 struct input *input = data;
1737
1738 data_offer_destroy(input->drag_offer);
1739 input->drag_offer = NULL;
1740}
1741
1742static void
1743data_device_motion(void *data, struct wl_data_device *data_device,
1744 uint32_t time, int32_t x, int32_t y)
1745{
1746 struct input *input = data;
1747 struct window *window = input->pointer_focus;
1748
1749 input->sx = x;
1750 input->sy = y;
1751
1752 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001753 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001754 input->drag_offer->types.data,
1755 window->user_data);
1756}
1757
1758static void
1759data_device_drop(void *data, struct wl_data_device *data_device)
1760{
1761 struct input *input = data;
1762 struct window *window = input->pointer_focus;
1763
1764 if (window->drop_handler)
1765 window->drop_handler(window, input,
1766 input->sx, input->sy, window->user_data);
1767}
1768
1769static void
1770data_device_selection(void *data,
1771 struct wl_data_device *wl_data_device,
1772 struct wl_data_offer *offer)
1773{
1774 struct input *input = data;
1775 char **p;
1776
1777 if (input->selection_offer)
1778 data_offer_destroy(input->selection_offer);
1779
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001780 if (offer) {
1781 input->selection_offer = wl_data_offer_get_user_data(offer);
1782 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1783 *p = NULL;
1784 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001785}
1786
1787static const struct wl_data_device_listener data_device_listener = {
1788 data_device_data_offer,
1789 data_device_enter,
1790 data_device_leave,
1791 data_device_motion,
1792 data_device_drop,
1793 data_device_selection
1794};
1795
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001796void
1797input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1798{
1799 struct display *display = input->display;
1800 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001801 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001802
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001803 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001804 return;
1805
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001806 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001807
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001808 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001809 return;
1810
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001811 input->current_cursor = pointer;
1812 buffer = display_get_buffer_for_surface(display, image->surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001813 wl_input_device_attach(input->input_device, time, buffer,
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001814 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001815}
1816
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001817struct wl_data_device *
1818input_get_data_device(struct input *input)
1819{
1820 return input->data_device;
1821}
1822
1823void
1824input_set_selection(struct input *input,
1825 struct wl_data_source *source, uint32_t time)
1826{
1827 wl_data_device_set_selection(input->data_device, source, time);
1828}
1829
1830void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001831input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001832{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001833 wl_data_offer_accept(input->drag_offer->offer,
1834 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001835}
1836
1837static void
1838offer_io_func(struct task *task, uint32_t events)
1839{
1840 struct data_offer *offer =
1841 container_of(task, struct data_offer, io_task);
1842 unsigned int len;
1843 char buffer[4096];
1844
1845 len = read(offer->fd, buffer, sizeof buffer);
1846 offer->func(buffer, len,
1847 offer->x, offer->y, offer->user_data);
1848
1849 if (len == 0) {
1850 close(offer->fd);
1851 data_offer_destroy(offer);
1852 }
1853}
1854
1855static void
1856data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1857 data_func_t func, void *user_data)
1858{
1859 int p[2];
1860
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02001861 if (pipe2(p, O_CLOEXEC) == -1)
1862 return;
1863
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001864 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1865 close(p[1]);
1866
1867 offer->io_task.run = offer_io_func;
1868 offer->fd = p[0];
1869 offer->func = func;
1870 offer->refcount++;
1871 offer->user_data = user_data;
1872
1873 display_watch_fd(offer->input->display,
1874 offer->fd, EPOLLIN, &offer->io_task);
1875}
1876
1877void
1878input_receive_drag_data(struct input *input, const char *mime_type,
1879 data_func_t func, void *data)
1880{
1881 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1882 input->drag_offer->x = input->sx;
1883 input->drag_offer->y = input->sy;
1884}
1885
1886int
1887input_receive_selection_data(struct input *input, const char *mime_type,
1888 data_func_t func, void *data)
1889{
1890 char **p;
1891
1892 if (input->selection_offer == NULL)
1893 return -1;
1894
1895 for (p = input->selection_offer->types.data; *p; p++)
1896 if (strcmp(mime_type, *p) == 0)
1897 break;
1898
1899 if (*p == NULL)
1900 return -1;
1901
1902 data_offer_receive_data(input->selection_offer,
1903 mime_type, func, data);
1904 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001905}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001906
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001907int
1908input_receive_selection_data_to_fd(struct input *input,
1909 const char *mime_type, int fd)
1910{
1911 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1912
1913 return 0;
1914}
1915
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001916void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001917window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001918{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001919 if (!window->shell_surface)
1920 return;
1921
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001922 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001923 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001924}
1925
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001926static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001927idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001928{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001929 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001930
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001931 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001932 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001933 widget_set_allocation(widget,
1934 window->pending_allocation.x,
1935 window->pending_allocation.y,
1936 window->pending_allocation.width,
1937 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001938
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001939 if (window->input_region) {
1940 wl_region_destroy(window->input_region);
1941 window->input_region = NULL;
1942 }
1943
1944 if (window->opaque_region) {
1945 wl_region_destroy(window->opaque_region);
1946 window->opaque_region = NULL;
1947 }
1948
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001949 if (widget->resize_handler)
1950 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001951 widget->allocation.width,
1952 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001953 widget->user_data);
1954
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05001955 if (window->allocation.width != widget->allocation.width ||
1956 window->allocation.height != widget->allocation.height) {
1957 window->allocation = widget->allocation;
1958 window_schedule_redraw(window);
1959 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001960}
1961
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001962void
1963window_schedule_resize(struct window *window, int width, int height)
1964{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001965 window->pending_allocation.x = 0;
1966 window->pending_allocation.y = 0;
1967 window->pending_allocation.width = width;
1968 window->pending_allocation.height = height;
1969
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001970 window->resize_needed = 1;
1971 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001972}
1973
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001974void
1975widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
1976{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001977 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001978}
1979
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001980static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001981handle_ping(void *data, struct wl_shell_surface *shell_surface,
1982 uint32_t serial)
1983{
1984 wl_shell_surface_pong(shell_surface, serial);
1985}
1986
1987static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001988handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001989 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001990{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001991 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001992
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001993 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001994 return;
1995
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001996 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001997 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001998}
1999
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002000static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002001menu_destroy(struct menu *menu)
2002{
2003 widget_destroy(menu->widget);
2004 window_destroy(menu->window);
2005 free(menu);
2006}
2007
2008static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002009handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2010{
2011 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002012 struct menu *menu = window->widget->user_data;
2013
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002014 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002015 * device. Or just use wl_callback. And this really needs to
2016 * be a window vfunc that the menu can set. And we need the
2017 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002018
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002019 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002020 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002021 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002022}
2023
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002024static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002025 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002026 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002027 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002028};
2029
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002030void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002031window_get_allocation(struct window *window,
2032 struct rectangle *allocation)
2033{
2034 *allocation = window->allocation;
2035}
2036
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002037static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002038widget_redraw(struct widget *widget)
2039{
2040 struct widget *child;
2041
2042 if (widget->redraw_handler)
2043 widget->redraw_handler(widget, widget->user_data);
2044 wl_list_for_each(child, &widget->child_list, link)
2045 widget_redraw(child);
2046}
2047
2048static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002049frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2050{
2051 struct window *window = data;
2052
2053 wl_callback_destroy(callback);
2054 window->redraw_scheduled = 0;
2055 if (window->redraw_needed)
2056 window_schedule_redraw(window);
2057}
2058
2059static const struct wl_callback_listener listener = {
2060 frame_callback
2061};
2062
2063static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002064idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002065{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002066 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002067 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002068
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002069 if (window->resize_needed)
2070 idle_resize(window);
2071
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002072 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002073 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002074 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002075 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002076 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002077
2078 callback = wl_surface_frame(window->surface);
2079 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002080}
2081
2082void
2083window_schedule_redraw(struct window *window)
2084{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002085 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002086 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002087 window->redraw_task.run = idle_redraw;
2088 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002089 window->redraw_scheduled = 1;
2090 }
2091}
2092
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002093void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002094window_set_custom(struct window *window)
2095{
2096 window->type = TYPE_CUSTOM;
2097}
2098
2099void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002100window_set_fullscreen(struct window *window, int fullscreen)
2101{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002102 if (!window->display->shell)
2103 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002104
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002105 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002106 return;
2107
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002108 if (fullscreen) {
2109 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002110 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002111 wl_shell_surface_set_fullscreen(window->shell_surface,
2112 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2113 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002114 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002115 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002116 wl_shell_surface_set_toplevel(window->shell_surface);
2117 window_schedule_resize(window,
2118 window->saved_allocation.width,
2119 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002120 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002121}
2122
2123void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002124window_set_maximized(struct window *window, int maximized)
2125{
2126 if (!window->display->shell)
2127 return;
2128
2129 if ((window->type == TYPE_MAXIMIZED) == maximized)
2130 return;
2131
2132 if (window->type == TYPE_TOPLEVEL) {
2133 window->saved_allocation = window->allocation;
2134 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2135 window->type = TYPE_MAXIMIZED;
2136 } else {
2137 wl_shell_surface_set_toplevel(window->shell_surface);
2138 window->type = TYPE_TOPLEVEL;
2139 window_schedule_resize(window,
2140 window->saved_allocation.width,
2141 window->saved_allocation.height);
2142 }
2143}
2144
2145void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002146window_set_user_data(struct window *window, void *data)
2147{
2148 window->user_data = data;
2149}
2150
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002151void *
2152window_get_user_data(struct window *window)
2153{
2154 return window->user_data;
2155}
2156
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002157void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002158window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002159 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002160{
2161 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002162}
2163
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002164void
2165window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002166 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002167{
2168 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002169}
2170
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002171void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002172window_set_data_handler(struct window *window, window_data_handler_t handler)
2173{
2174 window->data_handler = handler;
2175}
2176
2177void
2178window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2179{
2180 window->drop_handler = handler;
2181}
2182
2183void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002184window_set_close_handler(struct window *window,
2185 window_close_handler_t handler)
2186{
2187 window->close_handler = handler;
2188}
2189
2190void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002191window_set_title(struct window *window, const char *title)
2192{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002193 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002194 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002195 if (window->shell_surface)
2196 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002197}
2198
2199const char *
2200window_get_title(struct window *window)
2201{
2202 return window->title;
2203}
2204
2205void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002206window_damage(struct window *window, int32_t x, int32_t y,
2207 int32_t width, int32_t height)
2208{
2209 wl_surface_damage(window->surface, x, y, width, height);
2210}
2211
Casey Dahlin9074db52012-04-19 22:50:09 -04002212static void
2213surface_enter(void *data,
2214 struct wl_surface *wl_surface, struct wl_output *output)
2215{
2216}
2217
2218static void
2219surface_leave(void *data,
2220 struct wl_surface *wl_surface, struct wl_output *output)
2221{
2222}
2223
2224static const struct wl_surface_listener surface_listener = {
2225 surface_enter,
2226 surface_leave
2227};
2228
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002229static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002230window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002231{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002232 struct window *window;
2233
2234 window = malloc(sizeof *window);
2235 if (window == NULL)
2236 return NULL;
2237
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002238 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002239 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002240 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002241 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002242 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002243 if (display->shell) {
2244 window->shell_surface =
2245 wl_shell_get_shell_surface(display->shell,
2246 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002247 if (window->title)
2248 wl_shell_surface_set_title(window->shell_surface,
2249 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002250 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002251 window->allocation.x = 0;
2252 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002253 window->allocation.width = 0;
2254 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002255 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002256 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002257 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002258 window->input_region = NULL;
2259 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002260
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002261 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002262#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002263 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002264#else
2265 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2266#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002267 else
2268 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002269
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002270 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002271 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002272 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002273
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002274 if (window->shell_surface) {
2275 wl_shell_surface_set_user_data(window->shell_surface, window);
2276 wl_shell_surface_add_listener(window->shell_surface,
2277 &shell_surface_listener, window);
2278 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002279
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002280 return window;
2281}
2282
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002283struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002284window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002285{
2286 struct window *window;
2287
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002288 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002289 if (!window)
2290 return NULL;
2291
2292 return window;
2293}
2294
2295struct window *
2296window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002297 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002298{
2299 struct window *window;
2300
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002301 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002302 if (!window)
2303 return NULL;
2304
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002305 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002306 window->x = x;
2307 window->y = y;
2308
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002309 if (display->shell)
2310 wl_shell_surface_set_transient(window->shell_surface,
2311 window->parent->shell_surface,
2312 window->x, window->y, 0);
2313
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002314 return window;
2315}
2316
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002317static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002318menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002319{
2320 int next;
2321
2322 next = (sy - 8) / 20;
2323 if (menu->current != next) {
2324 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002325 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002326 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002327}
2328
2329static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002330menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002331 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002332 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002333{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002334 struct menu *menu = data;
2335
2336 if (widget == menu->widget)
2337 menu_set_item(data, y);
2338
2339 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002340}
2341
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002342static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002343menu_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002344 struct input *input, int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002345{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002346 struct menu *menu = data;
2347
2348 if (widget == menu->widget)
2349 menu_set_item(data, y);
2350
2351 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002352}
2353
2354static void
2355menu_leave_handler(struct widget *widget, struct input *input, void *data)
2356{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002357 struct menu *menu = data;
2358
2359 if (widget == menu->widget)
2360 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002361}
2362
2363static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002364menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002365 struct input *input, uint32_t time,
2366 int button, int state, void *data)
2367
2368{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002369 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002370
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002371 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002372 /* Either relase after press-drag-release or
2373 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002374 menu->func(menu->window->parent,
2375 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002376 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002377 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002378 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002379}
2380
2381static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002382menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002383{
2384 cairo_t *cr;
2385 const int32_t r = 3, margin = 3;
2386 struct menu *menu = data;
2387 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002388 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002389
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002390 cr = cairo_create(window->cairo_surface);
2391 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2392 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2393 cairo_paint(cr);
2394
2395 width = window->allocation.width;
2396 height = window->allocation.height;
2397 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002398
2399 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002400 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2401 cairo_fill(cr);
2402
2403 for (i = 0; i < menu->count; i++) {
2404 if (i == menu->current) {
2405 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2406 cairo_rectangle(cr, margin, i * 20 + margin,
2407 width - 2 * margin, 20);
2408 cairo_fill(cr);
2409 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2410 cairo_move_to(cr, 10, i * 20 + 16);
2411 cairo_show_text(cr, menu->entries[i]);
2412 } else {
2413 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2414 cairo_move_to(cr, 10, i * 20 + 16);
2415 cairo_show_text(cr, menu->entries[i]);
2416 }
2417 }
2418
2419 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002420}
2421
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002422void
2423window_show_menu(struct display *display,
2424 struct input *input, uint32_t time, struct window *parent,
2425 int32_t x, int32_t y,
2426 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002427{
2428 struct window *window;
2429 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002430 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002431
2432 menu = malloc(sizeof *menu);
2433 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002434 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002435
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002436 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002437 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002438 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002439
2440 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002441 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002442 menu->entries = entries;
2443 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002444 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002445 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002446 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002447 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002448 window->type = TYPE_MENU;
2449 window->x = x;
2450 window->y = y;
2451
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002452 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002453 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2454 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002455 window->parent->shell_surface,
2456 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002457
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002458 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002459 widget_set_enter_handler(menu->widget, menu_enter_handler);
2460 widget_set_leave_handler(menu->widget, menu_leave_handler);
2461 widget_set_motion_handler(menu->widget, menu_motion_handler);
2462 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002463
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002464 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002465 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002466}
2467
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002468void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002469window_set_buffer_type(struct window *window, enum window_buffer_type type)
2470{
2471 window->buffer_type = type;
2472}
2473
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002474
2475static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002476display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002477 struct wl_output *wl_output,
2478 int x, int y,
2479 int physical_width,
2480 int physical_height,
2481 int subpixel,
2482 const char *make,
2483 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002484{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002485 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002486
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002487 output->allocation.x = x;
2488 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002489}
2490
2491static void
2492display_handle_mode(void *data,
2493 struct wl_output *wl_output,
2494 uint32_t flags,
2495 int width,
2496 int height,
2497 int refresh)
2498{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002499 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002500 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002501
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002502 if (flags & WL_OUTPUT_MODE_CURRENT) {
2503 output->allocation.width = width;
2504 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002505 if (display->output_configure_handler)
2506 (*display->output_configure_handler)(
2507 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002508 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002509}
2510
2511static const struct wl_output_listener output_listener = {
2512 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002513 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002514};
2515
2516static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002517display_add_output(struct display *d, uint32_t id)
2518{
2519 struct output *output;
2520
2521 output = malloc(sizeof *output);
2522 if (output == NULL)
2523 return;
2524
2525 memset(output, 0, sizeof *output);
2526 output->display = d;
2527 output->output =
2528 wl_display_bind(d->display, id, &wl_output_interface);
2529 wl_list_insert(d->output_list.prev, &output->link);
2530
2531 wl_output_add_listener(output->output, &output_listener, output);
2532}
2533
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002534static void
2535output_destroy(struct output *output)
2536{
2537 if (output->destroy_handler)
2538 (*output->destroy_handler)(output, output->user_data);
2539
2540 wl_output_destroy(output->output);
2541 wl_list_remove(&output->link);
2542 free(output);
2543}
2544
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002545void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002546display_set_output_configure_handler(struct display *display,
2547 display_output_handler_t handler)
2548{
2549 struct output *output;
2550
2551 display->output_configure_handler = handler;
2552 if (!handler)
2553 return;
2554
2555 wl_list_for_each(output, &display->output_list, link)
2556 (*display->output_configure_handler)(output,
2557 display->user_data);
2558}
2559
2560void
2561output_set_user_data(struct output *output, void *data)
2562{
2563 output->user_data = data;
2564}
2565
2566void *
2567output_get_user_data(struct output *output)
2568{
2569 return output->user_data;
2570}
2571
2572void
2573output_set_destroy_handler(struct output *output,
2574 display_output_handler_t handler)
2575{
2576 output->destroy_handler = handler;
2577 /* FIXME: implement this, once we have way to remove outputs */
2578}
2579
2580void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002581output_get_allocation(struct output *output, struct rectangle *allocation)
2582{
2583 *allocation = output->allocation;
2584}
2585
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002586struct wl_output *
2587output_get_wl_output(struct output *output)
2588{
2589 return output->output;
2590}
2591
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002592static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002593display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002594{
2595 struct input *input;
2596
2597 input = malloc(sizeof *input);
2598 if (input == NULL)
2599 return;
2600
2601 memset(input, 0, sizeof *input);
2602 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002603 input->input_device =
2604 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002605 input->pointer_focus = NULL;
2606 input->keyboard_focus = NULL;
2607 wl_list_insert(d->input_list.prev, &input->link);
2608
2609 wl_input_device_add_listener(input->input_device,
2610 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002611 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002612
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002613 input->data_device =
2614 wl_data_device_manager_get_data_device(d->data_device_manager,
2615 input->input_device);
2616 wl_data_device_add_listener(input->data_device,
2617 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002618}
2619
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002620static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002621input_destroy(struct input *input)
2622{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002623 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002624 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002625
2626 if (input->drag_offer)
2627 data_offer_destroy(input->drag_offer);
2628
2629 if (input->selection_offer)
2630 data_offer_destroy(input->selection_offer);
2631
2632 wl_data_device_destroy(input->data_device);
2633 wl_list_remove(&input->link);
2634 wl_input_device_destroy(input->input_device);
2635 free(input);
2636}
2637
2638static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002639display_handle_global(struct wl_display *display, uint32_t id,
2640 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002641{
2642 struct display *d = data;
2643
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002644 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002645 d->compositor =
2646 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002647 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002648 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002649 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002650 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002651 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002652 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002653 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002654 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002655 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2656 d->data_device_manager =
2657 wl_display_bind(display, id,
2658 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002659 }
2660}
2661
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002662static void
2663display_render_frame(struct display *d)
2664{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002665 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002666 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002667
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002668 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002669 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2670 cr = cairo_create(d->shadow);
2671 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2672 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002673 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002674 cairo_fill(cr);
2675 cairo_destroy(cr);
2676 blur_surface(d->shadow, 64);
2677
2678 d->active_frame =
2679 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2680 cr = cairo_create(d->active_frame);
2681 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002682
2683 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2684 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2685 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2686 cairo_set_source(cr, pattern);
2687 cairo_pattern_destroy(pattern);
2688
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002689 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002690 cairo_fill(cr);
2691 cairo_destroy(cr);
2692
2693 d->inactive_frame =
2694 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2695 cr = cairo_create(d->inactive_frame);
2696 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002697 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002698 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002699 cairo_fill(cr);
2700 cairo_destroy(cr);
2701}
2702
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002703static void
2704init_xkb(struct display *d)
2705{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002706 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002707
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002708 names.rules = "evdev";
2709 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002710 names.layout = option_xkb_layout;
2711 names.variant = option_xkb_variant;
2712 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002713
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002714 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002715 if (!d->xkb) {
2716 fprintf(stderr, "Failed to compile keymap\n");
2717 exit(1);
2718 }
2719}
2720
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002721static void
2722fini_xkb(struct display *display)
2723{
2724 xkb_free_keymap(display->xkb);
2725}
2726
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002727#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002728static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002729init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002730{
2731 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002732 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002733
Rob Clark6396ed32012-03-11 19:48:41 -05002734#ifdef USE_CAIRO_GLESV2
2735# define GL_BIT EGL_OPENGL_ES2_BIT
2736#else
2737# define GL_BIT EGL_OPENGL_BIT
2738#endif
2739
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002740 static const EGLint argb_cfg_attribs[] = {
2741 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002742 EGL_RED_SIZE, 1,
2743 EGL_GREEN_SIZE, 1,
2744 EGL_BLUE_SIZE, 1,
2745 EGL_ALPHA_SIZE, 1,
2746 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002747 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002748 EGL_NONE
2749 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002750
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002751#ifdef USE_CAIRO_GLESV2
2752 static const EGLint context_attribs[] = {
2753 EGL_CONTEXT_CLIENT_VERSION, 2,
2754 EGL_NONE
2755 };
2756 EGLint api = EGL_OPENGL_ES_API;
2757#else
2758 EGLint *context_attribs = NULL;
2759 EGLint api = EGL_OPENGL_API;
2760#endif
2761
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002762 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002763 if (!eglInitialize(d->dpy, &major, &minor)) {
2764 fprintf(stderr, "failed to initialize display\n");
2765 return -1;
2766 }
2767
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002768 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002769 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2770 return -1;
2771 }
2772
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002773 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2774 &d->argb_config, 1, &n) || n != 1) {
2775 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002776 return -1;
2777 }
2778
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002779 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002780 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002781 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002782 fprintf(stderr, "failed to create context\n");
2783 return -1;
2784 }
2785
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002786 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002787 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002788 return -1;
2789 }
2790
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002791#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002792 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2793 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2794 fprintf(stderr, "failed to get cairo egl argb device\n");
2795 return -1;
2796 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002797#endif
2798
2799 return 0;
2800}
2801
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002802static void
2803fini_egl(struct display *display)
2804{
2805#ifdef HAVE_CAIRO_EGL
2806 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002807#endif
2808
2809 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2810 EGL_NO_CONTEXT);
2811
2812 eglTerminate(display->dpy);
2813 eglReleaseThread();
2814}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002815#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002816
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002817static int
2818event_mask_update(uint32_t mask, void *data)
2819{
2820 struct display *d = data;
2821
2822 d->mask = mask;
2823
2824 return 0;
2825}
2826
2827static void
2828handle_display_data(struct task *task, uint32_t events)
2829{
2830 struct display *display =
2831 container_of(task, struct display, display_task);
2832
2833 wl_display_iterate(display->display, display->mask);
2834}
2835
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002836struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002837display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002838{
2839 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002840
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002841 argc = parse_options(xkb_options,
2842 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002843
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002844 d = malloc(sizeof *d);
2845 if (d == NULL)
2846 return NULL;
2847
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002848 memset(d, 0, sizeof *d);
2849
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002850 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002851 if (d->display == NULL) {
2852 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002853 return NULL;
2854 }
2855
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002856 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2857 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2858 d->display_task.run = handle_display_data;
2859 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2860
2861 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002862 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002863 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002864
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002865 /* Set up listener so we'll catch all events. */
2866 wl_display_add_global_listener(d->display,
2867 display_handle_global, d);
2868
2869 /* Process connection events. */
2870 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002871#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002872 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002873 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002874#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002875
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002876 d->image_target_texture_2d =
2877 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2878 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2879 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2880
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002881 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002882
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002883 display_render_frame(d);
2884
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002885 wl_list_init(&d->window_list);
2886
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002887 init_xkb(d);
2888
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002889 return d;
2890}
2891
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002892static void
2893display_destroy_outputs(struct display *display)
2894{
2895 struct output *tmp;
2896 struct output *output;
2897
2898 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2899 output_destroy(output);
2900}
2901
Pekka Paalanene1207c72011-12-16 12:02:09 +02002902static void
2903display_destroy_inputs(struct display *display)
2904{
2905 struct input *tmp;
2906 struct input *input;
2907
2908 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2909 input_destroy(input);
2910}
2911
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002912void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002913display_destroy(struct display *display)
2914{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002915 if (!wl_list_empty(&display->window_list))
2916 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2917
2918 if (!wl_list_empty(&display->deferred_list))
2919 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2920
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002921 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002922 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002923
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002924 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002925
2926 cairo_surface_destroy(display->active_frame);
2927 cairo_surface_destroy(display->inactive_frame);
2928 cairo_surface_destroy(display->shadow);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002929 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002930
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002931#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002932 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002933#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002934
Pekka Paalanenc2052982011-12-16 11:41:32 +02002935 if (display->shell)
2936 wl_shell_destroy(display->shell);
2937
2938 if (display->shm)
2939 wl_shm_destroy(display->shm);
2940
2941 if (display->data_device_manager)
2942 wl_data_device_manager_destroy(display->data_device_manager);
2943
2944 wl_compositor_destroy(display->compositor);
2945
2946 close(display->epoll_fd);
2947
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002948 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05002949 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002950 free(display);
2951}
2952
2953void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002954display_set_user_data(struct display *display, void *data)
2955{
2956 display->user_data = data;
2957}
2958
2959void *
2960display_get_user_data(struct display *display)
2961{
2962 return display->user_data;
2963}
2964
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002965struct wl_display *
2966display_get_display(struct display *display)
2967{
2968 return display->display;
2969}
2970
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002971struct output *
2972display_get_output(struct display *display)
2973{
2974 return container_of(display->output_list.next, struct output, link);
2975}
2976
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002977struct wl_compositor *
2978display_get_compositor(struct display *display)
2979{
2980 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002981}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002982
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002983uint32_t
2984display_get_serial(struct display *display)
2985{
2986 return display->serial;
2987}
2988
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002989EGLDisplay
2990display_get_egl_display(struct display *d)
2991{
2992 return d->dpy;
2993}
2994
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002995struct wl_data_source *
2996display_create_data_source(struct display *display)
2997{
2998 return wl_data_device_manager_create_data_source(display->data_device_manager);
2999}
3000
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003001EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003002display_get_argb_egl_config(struct display *d)
3003{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003004 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003005}
3006
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003007struct wl_shell *
3008display_get_shell(struct display *display)
3009{
3010 return display->shell;
3011}
3012
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003013int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003014display_acquire_window_surface(struct display *display,
3015 struct window *window,
3016 EGLContext ctx)
3017{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003018#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003019 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003020 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003021
3022 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003023 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003024 device = cairo_surface_get_device(window->cairo_surface);
3025 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003026 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003027
Benjamin Franzke0c991632011-09-27 21:57:31 +02003028 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003029 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003030 ctx = display->argb_ctx;
3031 else
3032 assert(0);
3033 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003034
3035 data = cairo_surface_get_user_data(window->cairo_surface,
3036 &surface_data_key);
3037
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003038 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003039 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003040 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3041 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003042
3043 return 0;
3044#else
3045 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003046#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003047}
3048
3049void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003050display_release_window_surface(struct display *display,
3051 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003052{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003053#ifdef HAVE_CAIRO_EGL
3054 cairo_device_t *device;
3055
3056 device = cairo_surface_get_device(window->cairo_surface);
3057 if (!device)
3058 return;
3059
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003060 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003061 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003062 cairo_device_release(device);
3063#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003064}
3065
3066void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003067display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003068{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003069 wl_list_insert(&display->deferred_list, &task->link);
3070}
3071
3072void
3073display_watch_fd(struct display *display,
3074 int fd, uint32_t events, struct task *task)
3075{
3076 struct epoll_event ep;
3077
3078 ep.events = events;
3079 ep.data.ptr = task;
3080 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3081}
3082
3083void
3084display_run(struct display *display)
3085{
3086 struct task *task;
3087 struct epoll_event ep[16];
3088 int i, count;
3089
Pekka Paalanen826d7952011-12-15 10:14:07 +02003090 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003091 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003092 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003093
3094 while (!wl_list_empty(&display->deferred_list)) {
3095 task = container_of(display->deferred_list.next,
3096 struct task, link);
3097 wl_list_remove(&task->link);
3098 task->run(task, 0);
3099 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003100
3101 if (!display->running)
3102 break;
3103
3104 wl_display_flush(display->display);
3105
3106 count = epoll_wait(display->epoll_fd,
3107 ep, ARRAY_LENGTH(ep), -1);
3108 for (i = 0; i < count; i++) {
3109 task = ep[i].data.ptr;
3110 task->run(task, ep[i].events);
3111 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003112 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003113}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003114
3115void
3116display_exit(struct display *display)
3117{
3118 display->running = 0;
3119}