blob: ad0b710443fa16f0e23365c08d68213008c3fb65 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050059#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040063#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050067struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050068 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050069 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040070 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040071 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040072 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040073 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050074 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020075 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020076 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040077 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040078
79 int display_fd;
80 uint32_t mask;
81 struct task display_task;
82
83 int epoll_fd;
84 struct wl_list deferred_list;
85
Pekka Paalanen826d7952011-12-15 10:14:07 +020086 int running;
87
Kristian Høgsberg478d9262010-06-08 20:34:11 -040088 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040089 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050090 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040091 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050092 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040093 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040094 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040095
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050096 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
97 PFNEGLCREATEIMAGEKHRPROC create_image;
98 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020099
100 display_output_handler_t output_configure_handler;
101
102 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500103};
104
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400106 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400107 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500108 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400109 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500110 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400111 TYPE_CUSTOM
112};
113
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500114struct window {
115 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500116 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500117 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200118 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500119 struct wl_region *input_region;
120 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500121 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500122 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500123 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500124 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400125 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400126 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400127 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400128 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400129 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400130 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400131 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400132 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400133 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500134
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400135 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500136
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400137 struct wl_shm_pool *pool;
138 size_t pool_size;
139 void *pool_data;
140
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500141 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500142 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400143 window_data_handler_t data_handler;
144 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500145 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400146
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200147 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500148 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500149
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500150 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400151 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500152};
153
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500154struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500155 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500156 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400157 struct wl_list link;
158 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500159 widget_resize_handler_t resize_handler;
160 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500161 widget_enter_handler_t enter_handler;
162 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500163 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500164 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400165 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500166 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400167};
168
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400169struct input {
170 struct display *display;
171 struct wl_input_device *input_device;
172 struct window *pointer_focus;
173 struct window *keyboard_focus;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400174 int current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400175 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400176 uint32_t pointer_enter_serial;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200177 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400178 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400179
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500180 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500181 struct widget *grab;
182 uint32_t grab_button;
183
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400184 struct wl_data_device *data_device;
185 struct data_offer *drag_offer;
186 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400187};
188
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500189struct output {
190 struct display *display;
191 struct wl_output *output;
192 struct rectangle allocation;
193 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200194
195 display_output_handler_t destroy_handler;
196 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500197};
198
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500199struct frame {
200 struct widget *widget;
201 struct widget *child;
202 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400203 int width;
204 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500205};
206
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500207struct menu {
208 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500209 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500210 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500211 const char **entries;
212 uint32_t time;
213 int current;
214 int count;
215 menu_func_t func;
216};
217
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400218enum {
219 POINTER_DEFAULT = 100,
220 POINTER_UNSET
221};
222
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500223enum window_location {
224 WINDOW_INTERIOR = 0,
225 WINDOW_RESIZING_TOP = 1,
226 WINDOW_RESIZING_BOTTOM = 2,
227 WINDOW_RESIZING_LEFT = 4,
228 WINDOW_RESIZING_TOP_LEFT = 5,
229 WINDOW_RESIZING_BOTTOM_LEFT = 6,
230 WINDOW_RESIZING_RIGHT = 8,
231 WINDOW_RESIZING_TOP_RIGHT = 9,
232 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
233 WINDOW_RESIZING_MASK = 15,
234 WINDOW_EXTERIOR = 16,
235 WINDOW_TITLEBAR = 17,
236 WINDOW_CLIENT_AREA = 18,
237};
238
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400239const char *option_xkb_layout = "us";
240const char *option_xkb_variant = "";
241const char *option_xkb_options = "";
242
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400243static const struct weston_option xkb_options[] = {
244 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
245 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
246 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400247};
248
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400249static const cairo_user_data_key_t surface_data_key;
250struct surface_data {
251 struct wl_buffer *buffer;
252};
253
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500254#define MULT(_d,c,a,t) \
255 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
256
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500257#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400258
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100259struct egl_window_surface_data {
260 struct display *display;
261 struct wl_surface *surface;
262 struct wl_egl_window *window;
263 EGLSurface surf;
264};
265
266static void
267egl_window_surface_data_destroy(void *p)
268{
269 struct egl_window_surface_data *data = p;
270 struct display *d = data->display;
271
272 eglDestroySurface(d->dpy, data->surf);
273 wl_egl_window_destroy(data->window);
274 data->surface = NULL;
275
276 free(p);
277}
278
279static cairo_surface_t *
280display_create_egl_window_surface(struct display *display,
281 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400282 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100283 struct rectangle *rectangle)
284{
285 cairo_surface_t *cairo_surface;
286 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400287 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200288 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100289
290 data = malloc(sizeof *data);
291 if (data == NULL)
292 return NULL;
293
294 data->display = display;
295 data->surface = surface;
296
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500297 config = display->argb_config;
298 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400299
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400300 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100301 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400302 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100303
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400304 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500305 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100306
Benjamin Franzke0c991632011-09-27 21:57:31 +0200307 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100308 data->surf,
309 rectangle->width,
310 rectangle->height);
311
312 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
313 data, egl_window_surface_data_destroy);
314
315 return cairo_surface;
316}
317
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400318#endif
319
320struct wl_buffer *
321display_get_buffer_for_surface(struct display *display,
322 cairo_surface_t *surface)
323{
324 struct surface_data *data;
325
326 data = cairo_surface_get_user_data (surface, &surface_data_key);
327
328 return data->buffer;
329}
330
331struct shm_surface_data {
332 struct surface_data data;
333 void *map;
334 size_t length;
335};
336
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500337static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400338shm_surface_data_destroy(void *p)
339{
340 struct shm_surface_data *data = p;
341
342 wl_buffer_destroy(data->data.buffer);
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400343 if (data->map)
344 munmap(data->map, data->length);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400345}
346
Kristian Høgsberg16626282012-04-03 11:21:27 -0400347static struct wl_shm_pool *
348make_shm_pool(struct display *display, int size, void **data)
349{
350 char filename[] = "/tmp/wayland-shm-XXXXXX";
351 struct wl_shm_pool *pool;
352 int fd;
353
354 fd = mkstemp(filename);
355 if (fd < 0) {
356 fprintf(stderr, "open %s failed: %m\n", filename);
357 return NULL;
358 }
359 if (ftruncate(fd, size) < 0) {
360 fprintf(stderr, "ftruncate failed: %m\n");
361 close(fd);
362 return NULL;
363 }
364
365 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
366 unlink(filename);
367
368 if (*data == MAP_FAILED) {
369 fprintf(stderr, "mmap failed: %m\n");
370 close(fd);
371 return NULL;
372 }
373
374 pool = wl_shm_create_pool(display->shm, fd, size);
375
376 close(fd);
377
378 return pool;
379}
380
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500381static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400382display_create_shm_surface(struct display *display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400383 struct rectangle *rectangle, uint32_t flags,
384 struct window *window)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400385{
386 struct shm_surface_data *data;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400387 struct wl_shm_pool *pool = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400388 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400389 cairo_surface_t *surface;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400390 void *map;
Kristian Høgsberg16626282012-04-03 11:21:27 -0400391 int stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392
393 data = malloc(sizeof *data);
394 if (data == NULL)
395 return NULL;
396
397 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
398 rectangle->width);
399 data->length = stride * rectangle->height;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400400 if (window && window->pool && data->length < window->pool_size) {
401 pool = window->pool;
402 map = window->pool_data;
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400403 data->map = NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400404 } else {
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400405 pool = make_shm_pool(display, data->length, &map);
406 data->map = map;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400407 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400408
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400409 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410 CAIRO_FORMAT_ARGB32,
411 rectangle->width,
412 rectangle->height,
413 stride);
414
415 cairo_surface_set_user_data (surface, &surface_data_key,
416 data, shm_surface_data_destroy);
417
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400418 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500419 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400420 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500421 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400422
Kristian Høgsberg16626282012-04-03 11:21:27 -0400423 data->data.buffer = wl_shm_pool_create_buffer(pool, 0,
424 rectangle->width,
425 rectangle->height,
426 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400428 if (data->map)
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400429 wl_shm_pool_destroy(pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400430
431 return surface;
432}
433
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500434static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435display_create_shm_surface_from_file(struct display *display,
436 const char *filename,
437 struct rectangle *rect)
438{
439 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400440 pixman_image_t *image;
441 void *dest;
442 int size;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400443
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400444 image = load_image(filename);
Kristian Høgsberg610a3f22012-04-03 14:49:50 -0400445 if (image == NULL)
446 return NULL;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400447
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400448 surface = display_create_shm_surface(display, rect, 0, NULL);
nobled7b87cb02011-02-01 18:51:47 +0000449 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400450 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000451 return NULL;
452 }
453
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400454 size = pixman_image_get_stride(image) * pixman_image_get_height(image);
455 dest = cairo_image_surface_get_data(surface);
456 memcpy(dest, pixman_image_get_data(image), size);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400458 pixman_image_unref(image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400459
460 return surface;
461}
462
nobled7b87cb02011-02-01 18:51:47 +0000463static int
464check_size(struct rectangle *rect)
465{
466 if (rect->width && rect->height)
467 return 0;
468
469 fprintf(stderr, "tried to create surface of "
470 "width: %d, height: %d\n", rect->width, rect->height);
471 return -1;
472}
473
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400474cairo_surface_t *
475display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100476 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400477 struct rectangle *rectangle,
478 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400479{
nobled7b87cb02011-02-01 18:51:47 +0000480 if (check_size(rectangle) < 0)
481 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500482#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400483 if (display->dpy)
484 return display_create_egl_window_surface(display,
485 surface,
486 flags,
487 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400488#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400489 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400490}
491
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500492static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400493display_create_surface_from_file(struct display *display,
494 const char *filename,
495 struct rectangle *rectangle)
496{
nobled7b87cb02011-02-01 18:51:47 +0000497 if (check_size(rectangle) < 0)
498 return NULL;
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400499
Yuval Fledel45568f62010-12-06 09:18:12 -0500500 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400501}
Yuval Fledel45568f62010-12-06 09:18:12 -0500502 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400503 const char *filename;
504 int hotspot_x, hotspot_y;
505} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500506 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
507 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
508 { DATADIR "/weston/bottom_side.png", 16, 20 },
509 { DATADIR "/weston/grabbing.png", 20, 17 },
510 { DATADIR "/weston/left_ptr.png", 10, 5 },
511 { DATADIR "/weston/left_side.png", 10, 20 },
512 { DATADIR "/weston/right_side.png", 30, 19 },
513 { DATADIR "/weston/top_left_corner.png", 8, 8 },
514 { DATADIR "/weston/top_right_corner.png", 26, 8 },
515 { DATADIR "/weston/top_side.png", 18, 8 },
516 { DATADIR "/weston/xterm.png", 15, 15 },
517 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400518};
519
520static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400521create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400522{
523 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400524 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400525 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400526
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400527 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400528 display->pointer_surfaces =
529 malloc(count * sizeof *display->pointer_surfaces);
530 rect.width = width;
531 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400532 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400533 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534 display_create_surface_from_file(display,
535 pointer_images[i].filename,
536 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100537 if (!display->pointer_surfaces[i]) {
538 fprintf(stderr, "Error loading pointer image: %s\n",
539 pointer_images[i].filename);
540 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400541 }
542
543}
544
Pekka Paalanen325bb602011-12-19 10:31:45 +0200545static void
546destroy_pointer_surfaces(struct display *display)
547{
548 int i, count;
549
550 count = ARRAY_LENGTH(pointer_images);
551 for (i = 0; i < count; ++i) {
552 if (display->pointer_surfaces[i])
553 cairo_surface_destroy(display->pointer_surfaces[i]);
554 }
555 free(display->pointer_surfaces);
556}
557
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400558cairo_surface_t *
559display_get_pointer_surface(struct display *display, int pointer,
560 int *width, int *height,
561 int *hotspot_x, int *hotspot_y)
562{
563 cairo_surface_t *surface;
564
565 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500566#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400567 *width = cairo_gl_surface_get_width(surface);
568 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000569#else
570 *width = cairo_image_surface_get_width(surface);
571 *height = cairo_image_surface_get_height(surface);
572#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400573 *hotspot_x = pointer_images[pointer].hotspot_x;
574 *hotspot_y = pointer_images[pointer].hotspot_y;
575
576 return cairo_surface_reference(surface);
577}
578
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400579static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200580window_get_resize_dx_dy(struct window *window, int *x, int *y)
581{
582 if (window->resize_edges & WINDOW_RESIZING_LEFT)
583 *x = window->server_allocation.width - window->allocation.width;
584 else
585 *x = 0;
586
587 if (window->resize_edges & WINDOW_RESIZING_TOP)
588 *y = window->server_allocation.height -
589 window->allocation.height;
590 else
591 *y = 0;
592
593 window->resize_edges = 0;
594}
595
596static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500597window_attach_surface(struct window *window)
598{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400599 struct display *display = window->display;
600 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000601#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100602 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000603#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500604 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100605
606 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000607#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100608 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
609 data = cairo_surface_get_user_data(window->cairo_surface,
610 &surface_data_key);
611
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100612 cairo_gl_surface_swapbuffers(window->cairo_surface);
613 wl_egl_window_get_attached_size(data->window,
614 &window->server_allocation.width,
615 &window->server_allocation.height);
616 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000617#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100618 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100619 buffer =
620 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400621 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100622
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200623 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100624 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400625 wl_surface_damage(window->surface, 0, 0,
626 window->allocation.width,
627 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100628 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400629 cairo_surface_destroy(window->cairo_surface);
630 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000632 default:
633 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500635
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500636 if (window->input_region) {
637 wl_surface_set_input_region(window->surface,
638 window->input_region);
639 wl_region_destroy(window->input_region);
640 window->input_region = NULL;
641 }
642
643 if (window->opaque_region) {
644 wl_surface_set_opaque_region(window->surface,
645 window->opaque_region);
646 wl_region_destroy(window->opaque_region);
647 window->opaque_region = NULL;
648 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500649}
650
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500651void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400652window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500653{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400654 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100655 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500656}
657
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400658void
659window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400660{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500661 cairo_surface_reference(surface);
662
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400663 if (window->cairo_surface != NULL)
664 cairo_surface_destroy(window->cairo_surface);
665
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500666 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400667}
668
Benjamin Franzke22d54812011-07-16 19:50:32 +0000669#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100670static void
671window_resize_cairo_window_surface(struct window *window)
672{
673 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200674 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100675
676 data = cairo_surface_get_user_data(window->cairo_surface,
677 &surface_data_key);
678
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200679 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100680 wl_egl_window_resize(data->window,
681 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200682 window->allocation.height,
683 x,y);
684
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100685 cairo_gl_surface_set_size(window->cairo_surface,
686 window->allocation.width,
687 window->allocation.height);
688}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000689#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100690
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400691struct display *
692window_get_display(struct window *window)
693{
694 return window->display;
695}
696
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400697void
698window_create_surface(struct window *window)
699{
700 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400701 uint32_t flags = 0;
702
703 if (!window->transparent)
704 flags = SURFACE_OPAQUE;
705
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500707#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100708 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
709 if (window->cairo_surface) {
710 window_resize_cairo_window_surface(window);
711 return;
712 }
713 surface = display_create_surface(window->display,
714 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400715 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100716 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400717#endif
718 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400719 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400720 &window->allocation,
721 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400722 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800723 default:
724 surface = NULL;
725 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400726 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400727
728 window_set_surface(window, surface);
729 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400730}
731
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200732static void frame_destroy(struct frame *frame);
733
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400734void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500735window_destroy(struct window *window)
736{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200737 struct display *display = window->display;
738 struct input *input;
739
740 if (window->redraw_scheduled)
741 wl_list_remove(&window->redraw_task.link);
742
743 wl_list_for_each(input, &display->input_list, link) {
744 if (input->pointer_focus == window)
745 input->pointer_focus = NULL;
746 if (input->keyboard_focus == window)
747 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500748 if (input->focus_widget &&
749 input->focus_widget->window == window)
750 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200751 }
752
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500753 if (window->input_region)
754 wl_region_destroy(window->input_region);
755 if (window->opaque_region)
756 wl_region_destroy(window->opaque_region);
757
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200758 if (window->frame)
759 frame_destroy(window->frame);
760
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200761 if (window->shell_surface)
762 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500763 wl_surface_destroy(window->surface);
764 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200765
766 if (window->cairo_surface != NULL)
767 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200768
769 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500770 free(window);
771}
772
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500773static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500774widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400775{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500776 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400777
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500778 wl_list_for_each(child, &widget->child_list, link) {
779 target = widget_find_widget(child, x, y);
780 if (target)
781 return target;
782 }
783
784 if (widget->allocation.x <= x &&
785 x < widget->allocation.x + widget->allocation.width &&
786 widget->allocation.y <= y &&
787 y < widget->allocation.y + widget->allocation.height) {
788 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400789 }
790
791 return NULL;
792}
793
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500794static struct widget *
795widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400796{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500797 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400798
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500799 widget = malloc(sizeof *widget);
800 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500801 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500802 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500803 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500804 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500805 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500806
807 return widget;
808}
809
810struct widget *
811window_add_widget(struct window *window, void *data)
812{
813 window->widget = widget_create(window, data);
814 wl_list_init(&window->widget->link);
815
816 return window->widget;
817}
818
819struct widget *
820widget_add_widget(struct widget *parent, void *data)
821{
822 struct widget *widget;
823
824 widget = widget_create(parent->window, data);
825 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400826
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500827 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400828}
829
830void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500831widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400832{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200833 struct display *display = widget->window->display;
834 struct input *input;
835
836 wl_list_for_each(input, &display->input_list, link) {
837 if (input->focus_widget == widget)
838 input->focus_widget = NULL;
839 }
840
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500841 wl_list_remove(&widget->link);
842 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400843}
844
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400845void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500846widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400847{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500848 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400849}
850
851void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500852widget_set_size(struct widget *widget, int32_t width, int32_t height)
853{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500854 widget->allocation.width = width;
855 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500856}
857
858void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500859widget_set_allocation(struct widget *widget,
860 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400861{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500862 widget->allocation.x = x;
863 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200864 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400865}
866
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500867void
868widget_set_transparent(struct widget *widget, int transparent)
869{
870 widget->opaque = !transparent;
871}
872
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400873void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500874widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400875{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500876 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400877}
878
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500879void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500880widget_set_resize_handler(struct widget *widget,
881 widget_resize_handler_t handler)
882{
883 widget->resize_handler = handler;
884}
885
886void
887widget_set_redraw_handler(struct widget *widget,
888 widget_redraw_handler_t handler)
889{
890 widget->redraw_handler = handler;
891}
892
893void
Kristian Høgsbergee143232012-01-09 08:42:24 -0500894widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500895{
Kristian Høgsbergee143232012-01-09 08:42:24 -0500896 widget->enter_handler = handler;
897}
898
899void
900widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
901{
902 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500903}
904
905void
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500906widget_set_motion_handler(struct widget *widget,
907 widget_motion_handler_t handler)
908{
909 widget->motion_handler = handler;
910}
911
912void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500913widget_set_button_handler(struct widget *widget,
914 widget_button_handler_t handler)
915{
916 widget->button_handler = handler;
917}
918
919void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500920widget_schedule_redraw(struct widget *widget)
921{
922 window_schedule_redraw(widget->window);
923}
924
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400925cairo_surface_t *
926window_get_surface(struct window *window)
927{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400928 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400929}
930
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100931struct wl_surface *
932window_get_wl_surface(struct window *window)
933{
934 return window->surface;
935}
936
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200937struct wl_shell_surface *
938window_get_wl_shell_surface(struct window *window)
939{
940 return window->shell_surface;
941}
942
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500943static void
944frame_resize_handler(struct widget *widget,
945 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400946{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500947 struct frame *frame = data;
948 struct widget *child = frame->child;
949 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500950 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500951 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500952 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500953
Kristian Høgsbergb435e842012-03-05 20:38:08 -0500954 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400955 decoration_width = (frame->width + frame->margin) * 2;
956 decoration_height = frame->width +
957 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500958
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400959 allocation.x = frame->width + frame->margin;
960 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -0500961 allocation.width = width - decoration_width;
962 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500963
964 widget->window->input_region =
965 wl_compositor_create_region(display->compositor);
966 wl_region_add(widget->window->input_region,
967 frame->margin, frame->margin,
968 width - 2 * frame->margin,
969 height - 2 * frame->margin);
970
971 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -0500972 } else {
973 decoration_width = 0;
974 decoration_height = 0;
975
976 allocation.x = 0;
977 allocation.y = 0;
978 allocation.width = width;
979 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500980 opaque_margin = 0;
981 }
982
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500983 widget_set_allocation(child, allocation.x, allocation.y,
984 allocation.width, allocation.height);
985
986 if (child->resize_handler)
987 child->resize_handler(child,
988 allocation.width,
989 allocation.height,
990 child->user_data);
991
992 widget_set_allocation(widget, 0, 0,
993 child->allocation.width + decoration_width,
994 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -0500995
996 if (child->opaque) {
997 widget->window->opaque_region =
998 wl_compositor_create_region(display->compositor);
999 wl_region_add(widget->window->opaque_region,
1000 opaque_margin, opaque_margin,
1001 widget->allocation.width - 2 * opaque_margin,
1002 widget->allocation.height - 2 * opaque_margin);
1003 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001004}
1005
1006static void
1007frame_redraw_handler(struct widget *widget, void *data)
1008{
1009 struct frame *frame = data;
1010 cairo_t *cr;
1011 cairo_text_extents_t extents;
1012 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001013 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001014 struct window *window = widget->window;
1015
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001016 if (window->type == TYPE_FULLSCREEN)
1017 return;
1018
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001019 width = widget->allocation.width;
1020 height = widget->allocation.height;
1021
1022 cr = cairo_create(window->cairo_surface);
1023
1024 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1025 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1026 cairo_paint(cr);
1027
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001028 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001029 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001030 2, 2, width + 8, height + 8,
1031 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001032
1033 if (window->keyboard_device)
1034 source = window->display->active_frame;
1035 else
1036 source = window->display->inactive_frame;
1037
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001038 tile_source(cr, source,
1039 frame->margin, frame->margin,
1040 width - frame->margin * 2, height - frame->margin * 2,
1041 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001042
1043 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001044 cairo_select_font_face(cr, "sans",
1045 CAIRO_FONT_SLANT_NORMAL,
1046 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001047 cairo_set_font_size(cr, 14);
1048 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001049 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001050 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001051 if (window->keyboard_device) {
1052 cairo_move_to(cr, x + 1, y + 1);
1053 cairo_set_source_rgb(cr, 1, 1, 1);
1054 cairo_show_text(cr, window->title);
1055 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001056 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001057 cairo_show_text(cr, window->title);
1058 } else {
1059 cairo_move_to(cr, x, y);
1060 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1061 cairo_show_text(cr, window->title);
1062 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001063
1064 cairo_destroy(cr);
1065}
1066
1067static int
1068frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1069{
1070 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001071 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001072 const int grip_size = 8;
1073
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001074 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001075 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001076 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001077 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001078 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001079 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001080 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001081 hlocation = WINDOW_RESIZING_RIGHT;
1082 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001083 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001084
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001085 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001086 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001087 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001088 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001089 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001090 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001091 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001092 vlocation = WINDOW_RESIZING_BOTTOM;
1093 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001094 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001095
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001096 location = vlocation | hlocation;
1097 if (location & WINDOW_EXTERIOR)
1098 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001099 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001100 location = WINDOW_TITLEBAR;
1101 else if (location == WINDOW_INTERIOR)
1102 location = WINDOW_CLIENT_AREA;
1103
1104 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001105}
1106
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001107static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001108frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001109{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001110 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001111
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001112 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001113 switch (location) {
1114 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001115 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001116 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001117 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001118 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001119 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001120 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001121 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001122 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001123 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001124 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001125 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001126 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001127 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001128 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001129 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001130 case WINDOW_EXTERIOR:
1131 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001132 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001133 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001134 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001135}
1136
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001137static void
1138frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001139{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001140 switch (index) {
1141 case 0: /* close */
1142 if (window->close_handler)
1143 window->close_handler(window->parent,
1144 window->user_data);
1145 else
1146 display_exit(window->display);
1147 break;
1148 case 1: /* fullscreen */
1149 /* we don't have a way to get out of fullscreen for now */
1150 window_set_fullscreen(window, 1);
1151 break;
1152 case 2: /* rotate */
1153 case 3: /* scale */
1154 break;
1155 }
1156}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001157
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001158void
1159window_show_frame_menu(struct window *window,
1160 struct input *input, uint32_t time)
1161{
1162 int32_t x, y;
1163
1164 static const char *entries[] = {
1165 "Close", "Fullscreen", "Rotate", "Scale"
1166 };
1167
1168 input_get_position(input, &x, &y);
1169 window_show_menu(window->display, input, time, window,
1170 x - 10, y - 10, frame_menu_func, entries, 4);
1171}
1172
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001173static int
1174frame_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001175 struct input *input, int32_t x, int32_t y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001176{
1177 return frame_get_pointer_image_for_location(data, input);
1178}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001179
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001180static int
1181frame_motion_handler(struct widget *widget,
1182 struct input *input, uint32_t time,
1183 int32_t x, int32_t y, void *data)
1184{
1185 return frame_get_pointer_image_for_location(data, input);
1186}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001187
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001188static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001189frame_button_handler(struct widget *widget,
1190 struct input *input, uint32_t time,
1191 int button, int state, void *data)
1192
1193{
1194 struct frame *frame = data;
1195 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001196 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001197 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001198
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001199 location = frame_get_pointer_location(frame, input->sx, input->sy);
1200
1201 if (window->display->shell && button == BTN_LEFT && state == 1) {
1202 switch (location) {
1203 case WINDOW_TITLEBAR:
1204 if (!window->shell_surface)
1205 break;
1206 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001207 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001208 wl_shell_surface_move(window->shell_surface,
1209 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001210 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001211 break;
1212 case WINDOW_RESIZING_TOP:
1213 case WINDOW_RESIZING_BOTTOM:
1214 case WINDOW_RESIZING_LEFT:
1215 case WINDOW_RESIZING_RIGHT:
1216 case WINDOW_RESIZING_TOP_LEFT:
1217 case WINDOW_RESIZING_TOP_RIGHT:
1218 case WINDOW_RESIZING_BOTTOM_LEFT:
1219 case WINDOW_RESIZING_BOTTOM_RIGHT:
1220 if (!window->shell_surface)
1221 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001222 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001223
1224 if (!display->dpy) {
1225 /* If we're using shm, allocate a big
1226 pool to create buffers out of while
1227 we resize. We should probably base
1228 this number on the size of the output. */
1229 window->pool_size = 6 * 1024 * 1024;
1230 window->pool = make_shm_pool(display,
1231 window->pool_size,
1232 &window->pool_data);
1233 }
1234
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001235 wl_shell_surface_resize(window->shell_surface,
1236 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001237 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001238 break;
1239 }
1240 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001241 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242 }
1243}
1244
1245struct widget *
1246frame_create(struct window *window, void *data)
1247{
1248 struct frame *frame;
1249
1250 frame = malloc(sizeof *frame);
1251 memset(frame, 0, sizeof *frame);
1252
1253 frame->widget = window_add_widget(window, frame);
1254 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001255 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001256 frame->width = 4;
1257 frame->titlebar_height = 30
1258;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001259 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1260 widget_set_resize_handler(frame->widget, frame_resize_handler);
1261 widget_set_enter_handler(frame->widget, frame_enter_handler);
1262 widget_set_motion_handler(frame->widget, frame_motion_handler);
1263 widget_set_button_handler(frame->widget, frame_button_handler);
1264
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001265 window->frame = frame;
1266
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001267 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001268}
1269
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001270static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001271frame_destroy(struct frame *frame)
1272{
1273 /* frame->child must be destroyed by the application */
1274 widget_destroy(frame->widget);
1275 free(frame);
1276}
1277
1278static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001279input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001280 int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001281{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001282 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001283 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001284
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001285 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001286 return;
1287
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001288 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001289 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001290 widget = old;
1291 if (input->grab)
1292 widget = input->grab;
1293 if (widget->leave_handler)
1294 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001295 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001296 }
1297
1298 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001299 widget = focus;
1300 if (input->grab)
1301 widget = input->grab;
1302 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001303 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001304 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001305 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001306
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001307 input_set_pointer_image(input, input->pointer_enter_serial,
1308 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001309 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001310}
1311
1312static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001313input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001314 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001315{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001316 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001317 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001318 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001319 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001320
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001321 input->sx = sx;
1322 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001323
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001324 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001325 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001326 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001327 }
1328
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001329 if (input->grab)
1330 widget = input->grab;
1331 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001332 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001333 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001334 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001335 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001336 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001337
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001338 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001339}
1340
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001341void
1342input_grab(struct input *input, struct widget *widget, uint32_t button)
1343{
1344 input->grab = widget;
1345 input->grab_button = button;
1346}
1347
1348void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001349input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001350{
1351 struct widget *widget;
1352
1353 input->grab = NULL;
1354 if (input->pointer_focus) {
1355 widget = widget_find_widget(input->pointer_focus->widget,
1356 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001357 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001358 }
1359}
1360
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001361static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001362input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001363 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001364 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001365{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001366 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001367 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001368
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001369 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001370 if (input->focus_widget && input->grab == NULL && state)
1371 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001372
Neil Roberts6b28aad2012-01-23 19:11:18 +00001373 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001374 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001375 (*widget->button_handler)(widget,
1376 input, time,
1377 button, state,
1378 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001379
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001380 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001381 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001382}
1383
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001384static void
Scott Moreau210d0792012-03-22 10:47:01 -06001385input_handle_axis(void *data,
1386 struct wl_input_device *input_device,
1387 uint32_t time, uint32_t axis, int32_t value)
1388{
1389}
1390
1391static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001392input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001393 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001394{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001395 struct input *input = data;
1396 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001397 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001398 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001399
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001400 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001401 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001402 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001403 return;
1404
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001405 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001406 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001407 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1408 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001409
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001410 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1411
1412 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001413 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001414 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001415 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001416
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001417 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1418 if (state)
1419 window_set_maximized(window,
1420 window->type != TYPE_MAXIMIZED);
1421 } else if (window->key_handler) {
1422 (*window->key_handler)(window, input, time, key,
1423 sym, state, window->user_data);
1424 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001425}
1426
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001427static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001428input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001429{
1430 struct window *window = input->pointer_focus;
1431
1432 if (!window)
1433 return;
1434
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001435 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001436
Pekka Paalanene1207c72011-12-16 12:02:09 +02001437 input->pointer_focus = NULL;
1438 input->current_pointer_image = POINTER_UNSET;
1439}
1440
1441static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001442input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001443 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001444 uint32_t serial, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001445 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001446{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001447 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001448 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001449 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001450
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001451 input->display->serial = serial;
1452 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001453 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001454 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001455
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001456 if (window->pool) {
1457 wl_shm_pool_destroy(window->pool);
1458 munmap(window->pool_data, window->pool_size);
1459 window->pool = NULL;
1460 /* Schedule a redraw to free the pool */
1461 window_schedule_redraw(window);
1462 }
1463
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001464 input->sx = sx;
1465 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001466
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001467 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001468 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001469}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001470
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001471static void
1472input_handle_pointer_leave(void *data,
1473 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001474 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001475{
1476 struct input *input = data;
1477
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001478 input->display->serial = serial;
1479 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001480}
1481
1482static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001483input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001484{
1485 struct window *window = input->keyboard_focus;
1486
1487 if (!window)
1488 return;
1489
1490 window->keyboard_device = NULL;
1491 if (window->keyboard_focus_handler)
1492 (*window->keyboard_focus_handler)(window, NULL,
1493 window->user_data);
1494
1495 input->keyboard_focus = NULL;
1496}
1497
1498static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001499input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001500 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001501 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001502 struct wl_surface *surface,
1503 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001504{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001505 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001506 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001507 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001508 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001509
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001510 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001511 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001512
1513 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001514 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001515 for (k = keys->data; k < end; k++)
1516 input->modifiers |= d->xkb->map->modmap[*k];
1517
1518 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001519 window->keyboard_device = input;
1520 if (window->keyboard_focus_handler)
1521 (*window->keyboard_focus_handler)(window,
1522 window->keyboard_device,
1523 window->user_data);
1524}
1525
1526static void
1527input_handle_keyboard_leave(void *data,
1528 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001529 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001530 struct wl_surface *surface)
1531{
1532 struct input *input = data;
1533
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001534 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001535 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001536}
1537
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001538static void
1539input_handle_touch_down(void *data,
1540 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001541 uint32_t serial, uint32_t time,
1542 struct wl_surface *surface,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001543 int32_t id, int32_t x, int32_t y)
1544{
1545}
1546
1547static void
1548input_handle_touch_up(void *data,
1549 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001550 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001551{
1552}
1553
1554static void
1555input_handle_touch_motion(void *data,
1556 struct wl_input_device *wl_input_device,
1557 uint32_t time, int32_t id, int32_t x, int32_t y)
1558{
1559}
1560
1561static void
1562input_handle_touch_frame(void *data,
1563 struct wl_input_device *wl_input_device)
1564{
1565}
1566
1567static void
1568input_handle_touch_cancel(void *data,
1569 struct wl_input_device *wl_input_device)
1570{
1571}
1572
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001573static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001574 input_handle_motion,
1575 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001576 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001577 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001578 input_handle_pointer_enter,
1579 input_handle_pointer_leave,
1580 input_handle_keyboard_enter,
1581 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001582 input_handle_touch_down,
1583 input_handle_touch_up,
1584 input_handle_touch_motion,
1585 input_handle_touch_frame,
1586 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001587};
1588
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001589void
1590input_get_position(struct input *input, int32_t *x, int32_t *y)
1591{
1592 *x = input->sx;
1593 *y = input->sy;
1594}
1595
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001596struct wl_input_device *
1597input_get_input_device(struct input *input)
1598{
1599 return input->input_device;
1600}
1601
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001602uint32_t
1603input_get_modifiers(struct input *input)
1604{
1605 return input->modifiers;
1606}
1607
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001608struct widget *
1609input_get_focus_widget(struct input *input)
1610{
1611 return input->focus_widget;
1612}
1613
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001614struct data_offer {
1615 struct wl_data_offer *offer;
1616 struct input *input;
1617 struct wl_array types;
1618 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001619
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001620 struct task io_task;
1621 int fd;
1622 data_func_t func;
1623 int32_t x, y;
1624 void *user_data;
1625};
1626
1627static void
1628data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1629{
1630 struct data_offer *offer = data;
1631 char **p;
1632
1633 p = wl_array_add(&offer->types, sizeof *p);
1634 *p = strdup(type);
1635}
1636
1637static const struct wl_data_offer_listener data_offer_listener = {
1638 data_offer_offer,
1639};
1640
1641static void
1642data_offer_destroy(struct data_offer *offer)
1643{
1644 char **p;
1645
1646 offer->refcount--;
1647 if (offer->refcount == 0) {
1648 wl_data_offer_destroy(offer->offer);
1649 for (p = offer->types.data; *p; p++)
1650 free(*p);
1651 wl_array_release(&offer->types);
1652 free(offer);
1653 }
1654}
1655
1656static void
1657data_device_data_offer(void *data,
1658 struct wl_data_device *data_device, uint32_t id)
1659{
1660 struct data_offer *offer;
1661
1662 offer = malloc(sizeof *offer);
1663
1664 wl_array_init(&offer->types);
1665 offer->refcount = 1;
1666 offer->input = data;
1667
1668 /* FIXME: Generate typesafe wrappers for this */
1669 offer->offer = (struct wl_data_offer *)
1670 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1671 id, &wl_data_offer_interface);
1672
1673 wl_data_offer_add_listener(offer->offer,
1674 &data_offer_listener, offer);
1675}
1676
1677static void
1678data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001679 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001680 int32_t x, int32_t y, struct wl_data_offer *offer)
1681{
1682 struct input *input = data;
1683 struct window *window;
1684 char **p;
1685
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001686 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001687 input->drag_offer = wl_data_offer_get_user_data(offer);
1688 window = wl_surface_get_user_data(surface);
1689 input->pointer_focus = window;
1690
1691 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1692 *p = NULL;
1693
1694 window = input->pointer_focus;
1695 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001696 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001697 input->drag_offer->types.data,
1698 window->user_data);
1699}
1700
1701static void
1702data_device_leave(void *data, struct wl_data_device *data_device)
1703{
1704 struct input *input = data;
1705
1706 data_offer_destroy(input->drag_offer);
1707 input->drag_offer = NULL;
1708}
1709
1710static void
1711data_device_motion(void *data, struct wl_data_device *data_device,
1712 uint32_t time, int32_t x, int32_t y)
1713{
1714 struct input *input = data;
1715 struct window *window = input->pointer_focus;
1716
1717 input->sx = x;
1718 input->sy = y;
1719
1720 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001721 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001722 input->drag_offer->types.data,
1723 window->user_data);
1724}
1725
1726static void
1727data_device_drop(void *data, struct wl_data_device *data_device)
1728{
1729 struct input *input = data;
1730 struct window *window = input->pointer_focus;
1731
1732 if (window->drop_handler)
1733 window->drop_handler(window, input,
1734 input->sx, input->sy, window->user_data);
1735}
1736
1737static void
1738data_device_selection(void *data,
1739 struct wl_data_device *wl_data_device,
1740 struct wl_data_offer *offer)
1741{
1742 struct input *input = data;
1743 char **p;
1744
1745 if (input->selection_offer)
1746 data_offer_destroy(input->selection_offer);
1747
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001748 if (offer) {
1749 input->selection_offer = wl_data_offer_get_user_data(offer);
1750 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1751 *p = NULL;
1752 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001753}
1754
1755static const struct wl_data_device_listener data_device_listener = {
1756 data_device_data_offer,
1757 data_device_enter,
1758 data_device_leave,
1759 data_device_motion,
1760 data_device_drop,
1761 data_device_selection
1762};
1763
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001764void
1765input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1766{
1767 struct display *display = input->display;
1768 struct wl_buffer *buffer;
1769 cairo_surface_t *surface;
1770
1771 if (pointer == input->current_pointer_image)
1772 return;
1773
1774 input->current_pointer_image = pointer;
1775 surface = display->pointer_surfaces[pointer];
1776
1777 if (!surface)
1778 return;
1779
1780 buffer = display_get_buffer_for_surface(display, surface);
1781 wl_input_device_attach(input->input_device, time, buffer,
1782 pointer_images[pointer].hotspot_x,
1783 pointer_images[pointer].hotspot_y);
1784}
1785
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001786struct wl_data_device *
1787input_get_data_device(struct input *input)
1788{
1789 return input->data_device;
1790}
1791
1792void
1793input_set_selection(struct input *input,
1794 struct wl_data_source *source, uint32_t time)
1795{
1796 wl_data_device_set_selection(input->data_device, source, time);
1797}
1798
1799void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001800input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001801{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001802 wl_data_offer_accept(input->drag_offer->offer,
1803 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001804}
1805
1806static void
1807offer_io_func(struct task *task, uint32_t events)
1808{
1809 struct data_offer *offer =
1810 container_of(task, struct data_offer, io_task);
1811 unsigned int len;
1812 char buffer[4096];
1813
1814 len = read(offer->fd, buffer, sizeof buffer);
1815 offer->func(buffer, len,
1816 offer->x, offer->y, offer->user_data);
1817
1818 if (len == 0) {
1819 close(offer->fd);
1820 data_offer_destroy(offer);
1821 }
1822}
1823
1824static void
1825data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1826 data_func_t func, void *user_data)
1827{
1828 int p[2];
1829
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02001830 if (pipe2(p, O_CLOEXEC) == -1)
1831 return;
1832
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001833 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1834 close(p[1]);
1835
1836 offer->io_task.run = offer_io_func;
1837 offer->fd = p[0];
1838 offer->func = func;
1839 offer->refcount++;
1840 offer->user_data = user_data;
1841
1842 display_watch_fd(offer->input->display,
1843 offer->fd, EPOLLIN, &offer->io_task);
1844}
1845
1846void
1847input_receive_drag_data(struct input *input, const char *mime_type,
1848 data_func_t func, void *data)
1849{
1850 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1851 input->drag_offer->x = input->sx;
1852 input->drag_offer->y = input->sy;
1853}
1854
1855int
1856input_receive_selection_data(struct input *input, const char *mime_type,
1857 data_func_t func, void *data)
1858{
1859 char **p;
1860
1861 if (input->selection_offer == NULL)
1862 return -1;
1863
1864 for (p = input->selection_offer->types.data; *p; p++)
1865 if (strcmp(mime_type, *p) == 0)
1866 break;
1867
1868 if (*p == NULL)
1869 return -1;
1870
1871 data_offer_receive_data(input->selection_offer,
1872 mime_type, func, data);
1873 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001874}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001875
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001876int
1877input_receive_selection_data_to_fd(struct input *input,
1878 const char *mime_type, int fd)
1879{
1880 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1881
1882 return 0;
1883}
1884
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001885void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001886window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001887{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001888 if (!window->shell_surface)
1889 return;
1890
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001891 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001892 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001893}
1894
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001895static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001896idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001897{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001898 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001899
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001900 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001901 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001902 widget_set_allocation(widget,
1903 window->pending_allocation.x,
1904 window->pending_allocation.y,
1905 window->pending_allocation.width,
1906 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001907
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001908 if (window->input_region) {
1909 wl_region_destroy(window->input_region);
1910 window->input_region = NULL;
1911 }
1912
1913 if (window->opaque_region) {
1914 wl_region_destroy(window->opaque_region);
1915 window->opaque_region = NULL;
1916 }
1917
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001918 if (widget->resize_handler)
1919 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001920 widget->allocation.width,
1921 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001922 widget->user_data);
1923
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05001924 if (window->allocation.width != widget->allocation.width ||
1925 window->allocation.height != widget->allocation.height) {
1926 window->allocation = widget->allocation;
1927 window_schedule_redraw(window);
1928 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001929}
1930
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001931void
1932window_schedule_resize(struct window *window, int width, int height)
1933{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001934 window->pending_allocation.x = 0;
1935 window->pending_allocation.y = 0;
1936 window->pending_allocation.width = width;
1937 window->pending_allocation.height = height;
1938
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001939 window->resize_needed = 1;
1940 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001941}
1942
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001943void
1944widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
1945{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001946 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001947}
1948
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001949static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001950handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001951 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001952{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001953 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001954
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001955 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001956 return;
1957
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001958 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001959 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001960}
1961
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001962static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02001963menu_destroy(struct menu *menu)
1964{
1965 widget_destroy(menu->widget);
1966 window_destroy(menu->window);
1967 free(menu);
1968}
1969
1970static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001971handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1972{
1973 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001974 struct menu *menu = window->widget->user_data;
1975
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001976 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001977 * device. Or just use wl_callback. And this really needs to
1978 * be a window vfunc that the menu can set. And we need the
1979 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001980
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001981 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001982 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02001983 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001984}
1985
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001986static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001987 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001988 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001989};
1990
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001991void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001992window_get_allocation(struct window *window,
1993 struct rectangle *allocation)
1994{
1995 *allocation = window->allocation;
1996}
1997
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001998static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001999widget_redraw(struct widget *widget)
2000{
2001 struct widget *child;
2002
2003 if (widget->redraw_handler)
2004 widget->redraw_handler(widget, widget->user_data);
2005 wl_list_for_each(child, &widget->child_list, link)
2006 widget_redraw(child);
2007}
2008
2009static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002010frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2011{
2012 struct window *window = data;
2013
2014 wl_callback_destroy(callback);
2015 window->redraw_scheduled = 0;
2016 if (window->redraw_needed)
2017 window_schedule_redraw(window);
2018}
2019
2020static const struct wl_callback_listener listener = {
2021 frame_callback
2022};
2023
2024static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002025idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002026{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002027 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002028 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002029
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002030 if (window->resize_needed)
2031 idle_resize(window);
2032
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002033 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002034 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002035 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002036 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002037 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002038
2039 callback = wl_surface_frame(window->surface);
2040 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002041}
2042
2043void
2044window_schedule_redraw(struct window *window)
2045{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002046 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002047 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002048 window->redraw_task.run = idle_redraw;
2049 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002050 window->redraw_scheduled = 1;
2051 }
2052}
2053
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002054void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002055window_set_custom(struct window *window)
2056{
2057 window->type = TYPE_CUSTOM;
2058}
2059
2060void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002061window_set_fullscreen(struct window *window, int fullscreen)
2062{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002063 if (!window->display->shell)
2064 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002065
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002066 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002067 return;
2068
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002069 if (fullscreen) {
2070 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002071 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002072 wl_shell_surface_set_fullscreen(window->shell_surface,
2073 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2074 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002075 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002076 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002077 wl_shell_surface_set_toplevel(window->shell_surface);
2078 window_schedule_resize(window,
2079 window->saved_allocation.width,
2080 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002081 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002082}
2083
2084void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002085window_set_maximized(struct window *window, int maximized)
2086{
2087 if (!window->display->shell)
2088 return;
2089
2090 if ((window->type == TYPE_MAXIMIZED) == maximized)
2091 return;
2092
2093 if (window->type == TYPE_TOPLEVEL) {
2094 window->saved_allocation = window->allocation;
2095 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2096 window->type = TYPE_MAXIMIZED;
2097 } else {
2098 wl_shell_surface_set_toplevel(window->shell_surface);
2099 window->type = TYPE_TOPLEVEL;
2100 window_schedule_resize(window,
2101 window->saved_allocation.width,
2102 window->saved_allocation.height);
2103 }
2104}
2105
2106void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002107window_set_user_data(struct window *window, void *data)
2108{
2109 window->user_data = data;
2110}
2111
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002112void *
2113window_get_user_data(struct window *window)
2114{
2115 return window->user_data;
2116}
2117
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002118void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002119window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002120 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002121{
2122 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002123}
2124
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002125void
2126window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002127 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002128{
2129 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002130}
2131
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002132void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002133window_set_data_handler(struct window *window, window_data_handler_t handler)
2134{
2135 window->data_handler = handler;
2136}
2137
2138void
2139window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2140{
2141 window->drop_handler = handler;
2142}
2143
2144void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002145window_set_close_handler(struct window *window,
2146 window_close_handler_t handler)
2147{
2148 window->close_handler = handler;
2149}
2150
2151void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002152window_set_title(struct window *window, const char *title)
2153{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002154 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002155 window->title = strdup(title);
2156}
2157
2158const char *
2159window_get_title(struct window *window)
2160{
2161 return window->title;
2162}
2163
2164void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002165window_damage(struct window *window, int32_t x, int32_t y,
2166 int32_t width, int32_t height)
2167{
2168 wl_surface_damage(window->surface, x, y, width, height);
2169}
2170
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002171static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002172window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002173{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002174 struct window *window;
2175
2176 window = malloc(sizeof *window);
2177 if (window == NULL)
2178 return NULL;
2179
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002180 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002181 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002182 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002183 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002184 if (display->shell) {
2185 window->shell_surface =
2186 wl_shell_get_shell_surface(display->shell,
2187 window->surface);
2188 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002189 window->allocation.x = 0;
2190 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002191 window->allocation.width = 0;
2192 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002193 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002194 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002195 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002196 window->input_region = NULL;
2197 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002198
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002199 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002200#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002201 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002202#else
2203 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2204#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002205 else
2206 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002207
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002208 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002209 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002210 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002211
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002212 if (window->shell_surface) {
2213 wl_shell_surface_set_user_data(window->shell_surface, window);
2214 wl_shell_surface_add_listener(window->shell_surface,
2215 &shell_surface_listener, window);
2216 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002217
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002218 return window;
2219}
2220
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002221struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002222window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002223{
2224 struct window *window;
2225
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002226 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002227 if (!window)
2228 return NULL;
2229
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002230 window->type = TYPE_TOPLEVEL;
2231 if (display->shell)
2232 wl_shell_surface_set_toplevel(window->shell_surface);
2233
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002234 return window;
2235}
2236
2237struct window *
2238window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002239 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002240{
2241 struct window *window;
2242
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002243 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002244 if (!window)
2245 return NULL;
2246
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002247 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002248 window->x = x;
2249 window->y = y;
2250
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002251 if (display->shell)
2252 wl_shell_surface_set_transient(window->shell_surface,
2253 window->parent->shell_surface,
2254 window->x, window->y, 0);
2255
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002256 return window;
2257}
2258
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002259static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002260menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002261{
2262 int next;
2263
2264 next = (sy - 8) / 20;
2265 if (menu->current != next) {
2266 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002267 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002268 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002269}
2270
2271static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002272menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002273 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002274 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002275{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002276 struct menu *menu = data;
2277
2278 if (widget == menu->widget)
2279 menu_set_item(data, y);
2280
2281 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002282}
2283
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002284static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002285menu_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002286 struct input *input, int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002287{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002288 struct menu *menu = data;
2289
2290 if (widget == menu->widget)
2291 menu_set_item(data, y);
2292
2293 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002294}
2295
2296static void
2297menu_leave_handler(struct widget *widget, struct input *input, void *data)
2298{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002299 struct menu *menu = data;
2300
2301 if (widget == menu->widget)
2302 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002303}
2304
2305static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002306menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002307 struct input *input, uint32_t time,
2308 int button, int state, void *data)
2309
2310{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002311 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002312
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002313 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002314 /* Either relase after press-drag-release or
2315 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002316 menu->func(menu->window->parent,
2317 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002318 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002319 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002320 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002321}
2322
2323static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002324menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002325{
2326 cairo_t *cr;
2327 const int32_t r = 3, margin = 3;
2328 struct menu *menu = data;
2329 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002330 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002331
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002332 cr = cairo_create(window->cairo_surface);
2333 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2334 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2335 cairo_paint(cr);
2336
2337 width = window->allocation.width;
2338 height = window->allocation.height;
2339 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002340
2341 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002342 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2343 cairo_fill(cr);
2344
2345 for (i = 0; i < menu->count; i++) {
2346 if (i == menu->current) {
2347 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2348 cairo_rectangle(cr, margin, i * 20 + margin,
2349 width - 2 * margin, 20);
2350 cairo_fill(cr);
2351 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2352 cairo_move_to(cr, 10, i * 20 + 16);
2353 cairo_show_text(cr, menu->entries[i]);
2354 } else {
2355 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2356 cairo_move_to(cr, 10, i * 20 + 16);
2357 cairo_show_text(cr, menu->entries[i]);
2358 }
2359 }
2360
2361 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002362}
2363
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002364void
2365window_show_menu(struct display *display,
2366 struct input *input, uint32_t time, struct window *parent,
2367 int32_t x, int32_t y,
2368 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002369{
2370 struct window *window;
2371 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002372 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002373
2374 menu = malloc(sizeof *menu);
2375 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002376 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002377
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002378 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002379 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002380 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002381
2382 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002383 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002384 menu->entries = entries;
2385 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002386 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002387 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002388 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002389 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002390 window->type = TYPE_MENU;
2391 window->x = x;
2392 window->y = y;
2393
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002394 wl_shell_surface_set_popup(window->shell_surface,
2395 input->input_device, time,
2396 window->parent->shell_surface,
2397 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002398
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002399 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002400 widget_set_enter_handler(menu->widget, menu_enter_handler);
2401 widget_set_leave_handler(menu->widget, menu_leave_handler);
2402 widget_set_motion_handler(menu->widget, menu_motion_handler);
2403 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002404
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002405 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002406 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002407}
2408
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002409void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002410window_set_buffer_type(struct window *window, enum window_buffer_type type)
2411{
2412 window->buffer_type = type;
2413}
2414
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002415
2416static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002417display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002418 struct wl_output *wl_output,
2419 int x, int y,
2420 int physical_width,
2421 int physical_height,
2422 int subpixel,
2423 const char *make,
2424 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002425{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002426 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002427
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002428 output->allocation.x = x;
2429 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002430}
2431
2432static void
2433display_handle_mode(void *data,
2434 struct wl_output *wl_output,
2435 uint32_t flags,
2436 int width,
2437 int height,
2438 int refresh)
2439{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002440 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002441 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002442
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002443 if (flags & WL_OUTPUT_MODE_CURRENT) {
2444 output->allocation.width = width;
2445 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002446 if (display->output_configure_handler)
2447 (*display->output_configure_handler)(
2448 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002449 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002450}
2451
2452static const struct wl_output_listener output_listener = {
2453 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002454 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002455};
2456
2457static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002458display_add_output(struct display *d, uint32_t id)
2459{
2460 struct output *output;
2461
2462 output = malloc(sizeof *output);
2463 if (output == NULL)
2464 return;
2465
2466 memset(output, 0, sizeof *output);
2467 output->display = d;
2468 output->output =
2469 wl_display_bind(d->display, id, &wl_output_interface);
2470 wl_list_insert(d->output_list.prev, &output->link);
2471
2472 wl_output_add_listener(output->output, &output_listener, output);
2473}
2474
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002475static void
2476output_destroy(struct output *output)
2477{
2478 if (output->destroy_handler)
2479 (*output->destroy_handler)(output, output->user_data);
2480
2481 wl_output_destroy(output->output);
2482 wl_list_remove(&output->link);
2483 free(output);
2484}
2485
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002486void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002487display_set_output_configure_handler(struct display *display,
2488 display_output_handler_t handler)
2489{
2490 struct output *output;
2491
2492 display->output_configure_handler = handler;
2493 if (!handler)
2494 return;
2495
2496 wl_list_for_each(output, &display->output_list, link)
2497 (*display->output_configure_handler)(output,
2498 display->user_data);
2499}
2500
2501void
2502output_set_user_data(struct output *output, void *data)
2503{
2504 output->user_data = data;
2505}
2506
2507void *
2508output_get_user_data(struct output *output)
2509{
2510 return output->user_data;
2511}
2512
2513void
2514output_set_destroy_handler(struct output *output,
2515 display_output_handler_t handler)
2516{
2517 output->destroy_handler = handler;
2518 /* FIXME: implement this, once we have way to remove outputs */
2519}
2520
2521void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002522output_get_allocation(struct output *output, struct rectangle *allocation)
2523{
2524 *allocation = output->allocation;
2525}
2526
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002527struct wl_output *
2528output_get_wl_output(struct output *output)
2529{
2530 return output->output;
2531}
2532
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002533static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002534display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002535{
2536 struct input *input;
2537
2538 input = malloc(sizeof *input);
2539 if (input == NULL)
2540 return;
2541
2542 memset(input, 0, sizeof *input);
2543 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002544 input->input_device =
2545 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002546 input->pointer_focus = NULL;
2547 input->keyboard_focus = NULL;
2548 wl_list_insert(d->input_list.prev, &input->link);
2549
2550 wl_input_device_add_listener(input->input_device,
2551 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002552 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002553
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002554 input->data_device =
2555 wl_data_device_manager_get_data_device(d->data_device_manager,
2556 input->input_device);
2557 wl_data_device_add_listener(input->data_device,
2558 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002559}
2560
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002561static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002562input_destroy(struct input *input)
2563{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002564 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002565 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002566
2567 if (input->drag_offer)
2568 data_offer_destroy(input->drag_offer);
2569
2570 if (input->selection_offer)
2571 data_offer_destroy(input->selection_offer);
2572
2573 wl_data_device_destroy(input->data_device);
2574 wl_list_remove(&input->link);
2575 wl_input_device_destroy(input->input_device);
2576 free(input);
2577}
2578
2579static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002580display_handle_global(struct wl_display *display, uint32_t id,
2581 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002582{
2583 struct display *d = data;
2584
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002585 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002586 d->compositor =
2587 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002588 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002589 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002590 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002591 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002592 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002593 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002594 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002595 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002596 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2597 d->data_device_manager =
2598 wl_display_bind(display, id,
2599 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002600 }
2601}
2602
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002603static void
2604display_render_frame(struct display *d)
2605{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002606 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002607 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002608
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002609 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002610 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2611 cr = cairo_create(d->shadow);
2612 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2613 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002614 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002615 cairo_fill(cr);
2616 cairo_destroy(cr);
2617 blur_surface(d->shadow, 64);
2618
2619 d->active_frame =
2620 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2621 cr = cairo_create(d->active_frame);
2622 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002623
2624 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2625 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2626 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2627 cairo_set_source(cr, pattern);
2628 cairo_pattern_destroy(pattern);
2629
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002630 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002631 cairo_fill(cr);
2632 cairo_destroy(cr);
2633
2634 d->inactive_frame =
2635 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2636 cr = cairo_create(d->inactive_frame);
2637 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002638 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002639 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002640 cairo_fill(cr);
2641 cairo_destroy(cr);
2642}
2643
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002644static void
2645init_xkb(struct display *d)
2646{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002647 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002648
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002649 names.rules = "evdev";
2650 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002651 names.layout = option_xkb_layout;
2652 names.variant = option_xkb_variant;
2653 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002654
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002655 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002656 if (!d->xkb) {
2657 fprintf(stderr, "Failed to compile keymap\n");
2658 exit(1);
2659 }
2660}
2661
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002662static void
2663fini_xkb(struct display *display)
2664{
2665 xkb_free_keymap(display->xkb);
2666}
2667
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002668#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002669static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002670init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002671{
2672 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002673 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002674
Rob Clark6396ed32012-03-11 19:48:41 -05002675#ifdef USE_CAIRO_GLESV2
2676# define GL_BIT EGL_OPENGL_ES2_BIT
2677#else
2678# define GL_BIT EGL_OPENGL_BIT
2679#endif
2680
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002681 static const EGLint argb_cfg_attribs[] = {
2682 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002683 EGL_RED_SIZE, 1,
2684 EGL_GREEN_SIZE, 1,
2685 EGL_BLUE_SIZE, 1,
2686 EGL_ALPHA_SIZE, 1,
2687 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002688 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002689 EGL_NONE
2690 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002691
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002692#ifdef USE_CAIRO_GLESV2
2693 static const EGLint context_attribs[] = {
2694 EGL_CONTEXT_CLIENT_VERSION, 2,
2695 EGL_NONE
2696 };
2697 EGLint api = EGL_OPENGL_ES_API;
2698#else
2699 EGLint *context_attribs = NULL;
2700 EGLint api = EGL_OPENGL_API;
2701#endif
2702
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002703 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002704 if (!eglInitialize(d->dpy, &major, &minor)) {
2705 fprintf(stderr, "failed to initialize display\n");
2706 return -1;
2707 }
2708
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002709 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002710 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2711 return -1;
2712 }
2713
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002714 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2715 &d->argb_config, 1, &n) || n != 1) {
2716 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002717 return -1;
2718 }
2719
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002720 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002721 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002722 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002723 fprintf(stderr, "failed to create context\n");
2724 return -1;
2725 }
2726
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002727 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002728 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002729 return -1;
2730 }
2731
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002732#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002733 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2734 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2735 fprintf(stderr, "failed to get cairo egl argb device\n");
2736 return -1;
2737 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002738#endif
2739
2740 return 0;
2741}
2742
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002743static void
2744fini_egl(struct display *display)
2745{
2746#ifdef HAVE_CAIRO_EGL
2747 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002748#endif
2749
2750 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2751 EGL_NO_CONTEXT);
2752
2753 eglTerminate(display->dpy);
2754 eglReleaseThread();
2755}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002756#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002757
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002758static int
2759event_mask_update(uint32_t mask, void *data)
2760{
2761 struct display *d = data;
2762
2763 d->mask = mask;
2764
2765 return 0;
2766}
2767
2768static void
2769handle_display_data(struct task *task, uint32_t events)
2770{
2771 struct display *display =
2772 container_of(task, struct display, display_task);
2773
2774 wl_display_iterate(display->display, display->mask);
2775}
2776
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002777struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002778display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002779{
2780 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002781
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002782 argc = parse_options(xkb_options,
2783 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002784
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002785 d = malloc(sizeof *d);
2786 if (d == NULL)
2787 return NULL;
2788
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002789 memset(d, 0, sizeof *d);
2790
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002791 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002792 if (d->display == NULL) {
2793 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002794 return NULL;
2795 }
2796
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002797 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2798 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2799 d->display_task.run = handle_display_data;
2800 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2801
2802 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002803 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002804 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002805
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002806 /* Set up listener so we'll catch all events. */
2807 wl_display_add_global_listener(d->display,
2808 display_handle_global, d);
2809
2810 /* Process connection events. */
2811 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002812#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002813 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002814 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002815#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002816
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002817 d->image_target_texture_2d =
2818 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2819 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2820 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2821
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002822 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002823
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002824 display_render_frame(d);
2825
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002826 wl_list_init(&d->window_list);
2827
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002828 init_xkb(d);
2829
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002830 return d;
2831}
2832
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002833static void
2834display_destroy_outputs(struct display *display)
2835{
2836 struct output *tmp;
2837 struct output *output;
2838
2839 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2840 output_destroy(output);
2841}
2842
Pekka Paalanene1207c72011-12-16 12:02:09 +02002843static void
2844display_destroy_inputs(struct display *display)
2845{
2846 struct input *tmp;
2847 struct input *input;
2848
2849 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2850 input_destroy(input);
2851}
2852
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002853void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002854display_destroy(struct display *display)
2855{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002856 if (!wl_list_empty(&display->window_list))
2857 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2858
2859 if (!wl_list_empty(&display->deferred_list))
2860 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2861
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002862 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002863 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002864
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002865 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002866
2867 cairo_surface_destroy(display->active_frame);
2868 cairo_surface_destroy(display->inactive_frame);
2869 cairo_surface_destroy(display->shadow);
2870 destroy_pointer_surfaces(display);
2871
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002872#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002873 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002874#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002875
Pekka Paalanenc2052982011-12-16 11:41:32 +02002876 if (display->shell)
2877 wl_shell_destroy(display->shell);
2878
2879 if (display->shm)
2880 wl_shm_destroy(display->shm);
2881
2882 if (display->data_device_manager)
2883 wl_data_device_manager_destroy(display->data_device_manager);
2884
2885 wl_compositor_destroy(display->compositor);
2886
2887 close(display->epoll_fd);
2888
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002889 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05002890 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002891 free(display);
2892}
2893
2894void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002895display_set_user_data(struct display *display, void *data)
2896{
2897 display->user_data = data;
2898}
2899
2900void *
2901display_get_user_data(struct display *display)
2902{
2903 return display->user_data;
2904}
2905
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002906struct wl_display *
2907display_get_display(struct display *display)
2908{
2909 return display->display;
2910}
2911
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002912struct output *
2913display_get_output(struct display *display)
2914{
2915 return container_of(display->output_list.next, struct output, link);
2916}
2917
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002918struct wl_compositor *
2919display_get_compositor(struct display *display)
2920{
2921 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002922}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002923
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002924uint32_t
2925display_get_serial(struct display *display)
2926{
2927 return display->serial;
2928}
2929
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002930EGLDisplay
2931display_get_egl_display(struct display *d)
2932{
2933 return d->dpy;
2934}
2935
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002936struct wl_data_source *
2937display_create_data_source(struct display *display)
2938{
2939 return wl_data_device_manager_create_data_source(display->data_device_manager);
2940}
2941
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002942EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002943display_get_argb_egl_config(struct display *d)
2944{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002945 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002946}
2947
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002948struct wl_shell *
2949display_get_shell(struct display *display)
2950{
2951 return display->shell;
2952}
2953
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002954int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002955display_acquire_window_surface(struct display *display,
2956 struct window *window,
2957 EGLContext ctx)
2958{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002959#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002960 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002961 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002962
2963 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002964 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002965 device = cairo_surface_get_device(window->cairo_surface);
2966 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002967 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002968
Benjamin Franzke0c991632011-09-27 21:57:31 +02002969 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002970 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02002971 ctx = display->argb_ctx;
2972 else
2973 assert(0);
2974 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002975
2976 data = cairo_surface_get_user_data(window->cairo_surface,
2977 &surface_data_key);
2978
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002979 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002980 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002981 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2982 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002983
2984 return 0;
2985#else
2986 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002987#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002988}
2989
2990void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002991display_release_window_surface(struct display *display,
2992 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002993{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002994#ifdef HAVE_CAIRO_EGL
2995 cairo_device_t *device;
2996
2997 device = cairo_surface_get_device(window->cairo_surface);
2998 if (!device)
2999 return;
3000
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003001 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003002 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003003 cairo_device_release(device);
3004#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003005}
3006
3007void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003008display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003009{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003010 wl_list_insert(&display->deferred_list, &task->link);
3011}
3012
3013void
3014display_watch_fd(struct display *display,
3015 int fd, uint32_t events, struct task *task)
3016{
3017 struct epoll_event ep;
3018
3019 ep.events = events;
3020 ep.data.ptr = task;
3021 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3022}
3023
3024void
3025display_run(struct display *display)
3026{
3027 struct task *task;
3028 struct epoll_event ep[16];
3029 int i, count;
3030
Pekka Paalanen826d7952011-12-15 10:14:07 +02003031 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003032 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003033 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003034
3035 while (!wl_list_empty(&display->deferred_list)) {
3036 task = container_of(display->deferred_list.next,
3037 struct task, link);
3038 wl_list_remove(&task->link);
3039 task->run(task, 0);
3040 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003041
3042 if (!display->running)
3043 break;
3044
3045 wl_display_flush(display->display);
3046
3047 count = epoll_wait(display->epoll_fd,
3048 ep, ARRAY_LENGTH(ep), -1);
3049 for (i = 0; i < count; i++) {
3050 task = ep[i].data.ptr;
3051 task->run(task, ep[i].events);
3052 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003053 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003054}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003055
3056void
3057display_exit(struct display *display)
3058{
3059 display->running = 0;
3060}