blob: 54f57a33192f293492437b9d5989b6a5f6a3ba55 [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øgsberg407ef642012-04-27 17:17:12 -0400106 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400107 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400108 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500109 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400110 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500111 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400112 TYPE_CUSTOM
113};
114
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500115struct window {
116 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500117 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500118 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200119 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500120 struct wl_region *input_region;
121 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500122 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500123 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500124 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500125 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400126 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400127 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400128 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400129 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400130 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400131 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400132 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400133 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400134 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500135
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400136 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500137
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400138 struct wl_shm_pool *pool;
139 size_t pool_size;
140 void *pool_data;
141
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500142 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500143 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400144 window_data_handler_t data_handler;
145 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500146 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400147
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200148 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500149 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500150
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500151 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400152 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500153};
154
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500155struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500156 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500157 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400158 struct wl_list link;
159 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500160 widget_resize_handler_t resize_handler;
161 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500162 widget_enter_handler_t enter_handler;
163 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500164 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500165 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400166 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500167 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400168};
169
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400170struct input {
171 struct display *display;
172 struct wl_input_device *input_device;
173 struct window *pointer_focus;
174 struct window *keyboard_focus;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400175 int current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400176 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400177 uint32_t pointer_enter_serial;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200178 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400179 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400180
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500181 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500182 struct widget *grab;
183 uint32_t grab_button;
184
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400185 struct wl_data_device *data_device;
186 struct data_offer *drag_offer;
187 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400188};
189
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500190struct output {
191 struct display *display;
192 struct wl_output *output;
193 struct rectangle allocation;
194 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200195
196 display_output_handler_t destroy_handler;
197 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500198};
199
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500200struct frame {
201 struct widget *widget;
202 struct widget *child;
203 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400204 int width;
205 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500206};
207
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500208struct menu {
209 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500210 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500211 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500212 const char **entries;
213 uint32_t time;
214 int current;
215 int count;
216 menu_func_t func;
217};
218
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400219enum {
220 POINTER_DEFAULT = 100,
221 POINTER_UNSET
222};
223
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500224enum window_location {
225 WINDOW_INTERIOR = 0,
226 WINDOW_RESIZING_TOP = 1,
227 WINDOW_RESIZING_BOTTOM = 2,
228 WINDOW_RESIZING_LEFT = 4,
229 WINDOW_RESIZING_TOP_LEFT = 5,
230 WINDOW_RESIZING_BOTTOM_LEFT = 6,
231 WINDOW_RESIZING_RIGHT = 8,
232 WINDOW_RESIZING_TOP_RIGHT = 9,
233 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
234 WINDOW_RESIZING_MASK = 15,
235 WINDOW_EXTERIOR = 16,
236 WINDOW_TITLEBAR = 17,
237 WINDOW_CLIENT_AREA = 18,
238};
239
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400240const char *option_xkb_layout = "us";
241const char *option_xkb_variant = "";
242const char *option_xkb_options = "";
243
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400244static const struct weston_option xkb_options[] = {
245 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
246 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
247 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400248};
249
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400250static const cairo_user_data_key_t surface_data_key;
251struct surface_data {
252 struct wl_buffer *buffer;
253};
254
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500255#define MULT(_d,c,a,t) \
256 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
257
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500258#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400259
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100260struct egl_window_surface_data {
261 struct display *display;
262 struct wl_surface *surface;
263 struct wl_egl_window *window;
264 EGLSurface surf;
265};
266
267static void
268egl_window_surface_data_destroy(void *p)
269{
270 struct egl_window_surface_data *data = p;
271 struct display *d = data->display;
272
273 eglDestroySurface(d->dpy, data->surf);
274 wl_egl_window_destroy(data->window);
275 data->surface = NULL;
276
277 free(p);
278}
279
280static cairo_surface_t *
281display_create_egl_window_surface(struct display *display,
282 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400283 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100284 struct rectangle *rectangle)
285{
286 cairo_surface_t *cairo_surface;
287 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400288 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200289 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100290
291 data = malloc(sizeof *data);
292 if (data == NULL)
293 return NULL;
294
295 data->display = display;
296 data->surface = surface;
297
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500298 config = display->argb_config;
299 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400300
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400301 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100302 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400303 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100304
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400305 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500306 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100307
Benjamin Franzke0c991632011-09-27 21:57:31 +0200308 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100309 data->surf,
310 rectangle->width,
311 rectangle->height);
312
313 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
314 data, egl_window_surface_data_destroy);
315
316 return cairo_surface;
317}
318
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319#endif
320
321struct wl_buffer *
322display_get_buffer_for_surface(struct display *display,
323 cairo_surface_t *surface)
324{
325 struct surface_data *data;
326
327 data = cairo_surface_get_user_data (surface, &surface_data_key);
328
329 return data->buffer;
330}
331
332struct shm_surface_data {
333 struct surface_data data;
334 void *map;
335 size_t length;
336};
337
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500338static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400339shm_surface_data_destroy(void *p)
340{
341 struct shm_surface_data *data = p;
342
343 wl_buffer_destroy(data->data.buffer);
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400344 if (data->map)
345 munmap(data->map, data->length);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400346}
347
Kristian Høgsberg16626282012-04-03 11:21:27 -0400348static struct wl_shm_pool *
349make_shm_pool(struct display *display, int size, void **data)
350{
351 char filename[] = "/tmp/wayland-shm-XXXXXX";
352 struct wl_shm_pool *pool;
353 int fd;
354
355 fd = mkstemp(filename);
356 if (fd < 0) {
357 fprintf(stderr, "open %s failed: %m\n", filename);
358 return NULL;
359 }
360 if (ftruncate(fd, size) < 0) {
361 fprintf(stderr, "ftruncate failed: %m\n");
362 close(fd);
363 return NULL;
364 }
365
366 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
367 unlink(filename);
368
369 if (*data == MAP_FAILED) {
370 fprintf(stderr, "mmap failed: %m\n");
371 close(fd);
372 return NULL;
373 }
374
375 pool = wl_shm_create_pool(display->shm, fd, size);
376
377 close(fd);
378
379 return pool;
380}
381
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500382static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400383display_create_shm_surface(struct display *display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400384 struct rectangle *rectangle, uint32_t flags,
385 struct window *window)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400386{
387 struct shm_surface_data *data;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400388 struct wl_shm_pool *pool = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400389 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400390 cairo_surface_t *surface;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400391 void *map;
Kristian Høgsberg16626282012-04-03 11:21:27 -0400392 int stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393
394 data = malloc(sizeof *data);
395 if (data == NULL)
396 return NULL;
397
398 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
399 rectangle->width);
400 data->length = stride * rectangle->height;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400401 if (window && window->pool && data->length < window->pool_size) {
402 pool = window->pool;
403 map = window->pool_data;
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400404 data->map = NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400405 } else {
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400406 pool = make_shm_pool(display, data->length, &map);
407 data->map = map;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400408 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400410 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411 CAIRO_FORMAT_ARGB32,
412 rectangle->width,
413 rectangle->height,
414 stride);
415
416 cairo_surface_set_user_data (surface, &surface_data_key,
417 data, shm_surface_data_destroy);
418
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400419 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500420 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400421 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500422 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400423
Kristian Høgsberg16626282012-04-03 11:21:27 -0400424 data->data.buffer = wl_shm_pool_create_buffer(pool, 0,
425 rectangle->width,
426 rectangle->height,
427 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428
Kristian Høgsberg24b5e232012-04-10 11:32:35 -0400429 if (data->map)
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400430 wl_shm_pool_destroy(pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431
432 return surface;
433}
434
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500435static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436display_create_shm_surface_from_file(struct display *display,
437 const char *filename,
438 struct rectangle *rect)
439{
440 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400441 pixman_image_t *image;
442 void *dest;
443 int size;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400444
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400445 image = load_image(filename);
Kristian Høgsberg610a3f22012-04-03 14:49:50 -0400446 if (image == NULL)
447 return NULL;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400448
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400449 surface = display_create_shm_surface(display, rect, 0, NULL);
nobled7b87cb02011-02-01 18:51:47 +0000450 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400451 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000452 return NULL;
453 }
454
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400455 size = pixman_image_get_stride(image) * pixman_image_get_height(image);
456 dest = cairo_image_surface_get_data(surface);
457 memcpy(dest, pixman_image_get_data(image), size);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400458
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400459 pixman_image_unref(image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400460
461 return surface;
462}
463
nobled7b87cb02011-02-01 18:51:47 +0000464static int
465check_size(struct rectangle *rect)
466{
467 if (rect->width && rect->height)
468 return 0;
469
470 fprintf(stderr, "tried to create surface of "
471 "width: %d, height: %d\n", rect->width, rect->height);
472 return -1;
473}
474
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400475cairo_surface_t *
476display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100477 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400478 struct rectangle *rectangle,
479 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400480{
nobled7b87cb02011-02-01 18:51:47 +0000481 if (check_size(rectangle) < 0)
482 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500483#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400484 if (display->dpy)
485 return display_create_egl_window_surface(display,
486 surface,
487 flags,
488 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400490 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400491}
492
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500493static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400494display_create_surface_from_file(struct display *display,
495 const char *filename,
496 struct rectangle *rectangle)
497{
nobled7b87cb02011-02-01 18:51:47 +0000498 if (check_size(rectangle) < 0)
499 return NULL;
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400500
Yuval Fledel45568f62010-12-06 09:18:12 -0500501 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400502}
Yuval Fledel45568f62010-12-06 09:18:12 -0500503 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400504 const char *filename;
505 int hotspot_x, hotspot_y;
506} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500507 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
508 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
509 { DATADIR "/weston/bottom_side.png", 16, 20 },
510 { DATADIR "/weston/grabbing.png", 20, 17 },
511 { DATADIR "/weston/left_ptr.png", 10, 5 },
512 { DATADIR "/weston/left_side.png", 10, 20 },
513 { DATADIR "/weston/right_side.png", 30, 19 },
514 { DATADIR "/weston/top_left_corner.png", 8, 8 },
515 { DATADIR "/weston/top_right_corner.png", 26, 8 },
516 { DATADIR "/weston/top_side.png", 18, 8 },
517 { DATADIR "/weston/xterm.png", 15, 15 },
518 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400519};
520
521static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400522create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400523{
524 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400525 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400526 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400527
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400528 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400529 display->pointer_surfaces =
530 malloc(count * sizeof *display->pointer_surfaces);
531 rect.width = width;
532 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400533 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400534 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400535 display_create_surface_from_file(display,
536 pointer_images[i].filename,
537 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100538 if (!display->pointer_surfaces[i]) {
539 fprintf(stderr, "Error loading pointer image: %s\n",
540 pointer_images[i].filename);
541 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400542 }
543
544}
545
Pekka Paalanen325bb602011-12-19 10:31:45 +0200546static void
547destroy_pointer_surfaces(struct display *display)
548{
549 int i, count;
550
551 count = ARRAY_LENGTH(pointer_images);
552 for (i = 0; i < count; ++i) {
553 if (display->pointer_surfaces[i])
554 cairo_surface_destroy(display->pointer_surfaces[i]);
555 }
556 free(display->pointer_surfaces);
557}
558
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400559cairo_surface_t *
560display_get_pointer_surface(struct display *display, int pointer,
561 int *width, int *height,
562 int *hotspot_x, int *hotspot_y)
563{
564 cairo_surface_t *surface;
565
566 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500567#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400568 *width = cairo_gl_surface_get_width(surface);
569 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000570#else
571 *width = cairo_image_surface_get_width(surface);
572 *height = cairo_image_surface_get_height(surface);
573#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400574 *hotspot_x = pointer_images[pointer].hotspot_x;
575 *hotspot_y = pointer_images[pointer].hotspot_y;
576
577 return cairo_surface_reference(surface);
578}
579
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400580static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200581window_get_resize_dx_dy(struct window *window, int *x, int *y)
582{
583 if (window->resize_edges & WINDOW_RESIZING_LEFT)
584 *x = window->server_allocation.width - window->allocation.width;
585 else
586 *x = 0;
587
588 if (window->resize_edges & WINDOW_RESIZING_TOP)
589 *y = window->server_allocation.height -
590 window->allocation.height;
591 else
592 *y = 0;
593
594 window->resize_edges = 0;
595}
596
597static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500598window_attach_surface(struct window *window)
599{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400600 struct display *display = window->display;
601 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000602#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100603 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000604#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500605 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100606
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400607 if (window->type == TYPE_NONE) {
608 window->type = TYPE_TOPLEVEL;
609 if (display->shell)
610 wl_shell_surface_set_toplevel(window->shell_surface);
611 }
612
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100613 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000614#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100615 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
616 data = cairo_surface_get_user_data(window->cairo_surface,
617 &surface_data_key);
618
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100619 cairo_gl_surface_swapbuffers(window->cairo_surface);
620 wl_egl_window_get_attached_size(data->window,
621 &window->server_allocation.width,
622 &window->server_allocation.height);
623 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000624#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100625 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100626 buffer =
627 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400628 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100629
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200630 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400632 wl_surface_damage(window->surface, 0, 0,
633 window->allocation.width,
634 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400636 cairo_surface_destroy(window->cairo_surface);
637 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000639 default:
640 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100641 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500642
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500643 if (window->input_region) {
644 wl_surface_set_input_region(window->surface,
645 window->input_region);
646 wl_region_destroy(window->input_region);
647 window->input_region = NULL;
648 }
649
650 if (window->opaque_region) {
651 wl_surface_set_opaque_region(window->surface,
652 window->opaque_region);
653 wl_region_destroy(window->opaque_region);
654 window->opaque_region = NULL;
655 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500656}
657
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500658void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400659window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500660{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400661 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100662 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500663}
664
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400665void
666window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400667{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500668 cairo_surface_reference(surface);
669
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400670 if (window->cairo_surface != NULL)
671 cairo_surface_destroy(window->cairo_surface);
672
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500673 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400674}
675
Benjamin Franzke22d54812011-07-16 19:50:32 +0000676#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100677static void
678window_resize_cairo_window_surface(struct window *window)
679{
680 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200681 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100682
683 data = cairo_surface_get_user_data(window->cairo_surface,
684 &surface_data_key);
685
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200686 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100687 wl_egl_window_resize(data->window,
688 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200689 window->allocation.height,
690 x,y);
691
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100692 cairo_gl_surface_set_size(window->cairo_surface,
693 window->allocation.width,
694 window->allocation.height);
695}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000696#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100697
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400698struct display *
699window_get_display(struct window *window)
700{
701 return window->display;
702}
703
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400704void
705window_create_surface(struct window *window)
706{
707 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400708 uint32_t flags = 0;
709
710 if (!window->transparent)
711 flags = SURFACE_OPAQUE;
712
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400713 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500714#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100715 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
716 if (window->cairo_surface) {
717 window_resize_cairo_window_surface(window);
718 return;
719 }
720 surface = display_create_surface(window->display,
721 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400722 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100723 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400724#endif
725 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400726 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400727 &window->allocation,
728 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400729 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800730 default:
731 surface = NULL;
732 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400733 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400734
735 window_set_surface(window, surface);
736 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400737}
738
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200739static void frame_destroy(struct frame *frame);
740
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400741void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500742window_destroy(struct window *window)
743{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200744 struct display *display = window->display;
745 struct input *input;
746
747 if (window->redraw_scheduled)
748 wl_list_remove(&window->redraw_task.link);
749
750 wl_list_for_each(input, &display->input_list, link) {
751 if (input->pointer_focus == window)
752 input->pointer_focus = NULL;
753 if (input->keyboard_focus == window)
754 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500755 if (input->focus_widget &&
756 input->focus_widget->window == window)
757 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200758 }
759
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500760 if (window->input_region)
761 wl_region_destroy(window->input_region);
762 if (window->opaque_region)
763 wl_region_destroy(window->opaque_region);
764
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200765 if (window->frame)
766 frame_destroy(window->frame);
767
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200768 if (window->shell_surface)
769 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500770 wl_surface_destroy(window->surface);
771 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200772
773 if (window->cairo_surface != NULL)
774 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200775
776 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500777 free(window);
778}
779
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500780static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500781widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400782{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500783 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400784
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500785 wl_list_for_each(child, &widget->child_list, link) {
786 target = widget_find_widget(child, x, y);
787 if (target)
788 return target;
789 }
790
791 if (widget->allocation.x <= x &&
792 x < widget->allocation.x + widget->allocation.width &&
793 widget->allocation.y <= y &&
794 y < widget->allocation.y + widget->allocation.height) {
795 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400796 }
797
798 return NULL;
799}
800
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500801static struct widget *
802widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400803{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500804 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400805
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500806 widget = malloc(sizeof *widget);
807 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500808 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500809 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500810 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500811 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500812 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500813
814 return widget;
815}
816
817struct widget *
818window_add_widget(struct window *window, void *data)
819{
820 window->widget = widget_create(window, data);
821 wl_list_init(&window->widget->link);
822
823 return window->widget;
824}
825
826struct widget *
827widget_add_widget(struct widget *parent, void *data)
828{
829 struct widget *widget;
830
831 widget = widget_create(parent->window, data);
832 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400833
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500834 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400835}
836
837void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500838widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400839{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200840 struct display *display = widget->window->display;
841 struct input *input;
842
843 wl_list_for_each(input, &display->input_list, link) {
844 if (input->focus_widget == widget)
845 input->focus_widget = NULL;
846 }
847
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500848 wl_list_remove(&widget->link);
849 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400850}
851
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400852void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500853widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400854{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500855 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400856}
857
858void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500859widget_set_size(struct widget *widget, int32_t width, int32_t height)
860{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500861 widget->allocation.width = width;
862 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500863}
864
865void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500866widget_set_allocation(struct widget *widget,
867 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400868{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500869 widget->allocation.x = x;
870 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200871 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400872}
873
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500874void
875widget_set_transparent(struct widget *widget, int transparent)
876{
877 widget->opaque = !transparent;
878}
879
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400880void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500881widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400882{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500883 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400884}
885
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500886void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500887widget_set_resize_handler(struct widget *widget,
888 widget_resize_handler_t handler)
889{
890 widget->resize_handler = handler;
891}
892
893void
894widget_set_redraw_handler(struct widget *widget,
895 widget_redraw_handler_t handler)
896{
897 widget->redraw_handler = handler;
898}
899
900void
Kristian Høgsbergee143232012-01-09 08:42:24 -0500901widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500902{
Kristian Høgsbergee143232012-01-09 08:42:24 -0500903 widget->enter_handler = handler;
904}
905
906void
907widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
908{
909 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500910}
911
912void
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500913widget_set_motion_handler(struct widget *widget,
914 widget_motion_handler_t handler)
915{
916 widget->motion_handler = handler;
917}
918
919void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500920widget_set_button_handler(struct widget *widget,
921 widget_button_handler_t handler)
922{
923 widget->button_handler = handler;
924}
925
926void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500927widget_schedule_redraw(struct widget *widget)
928{
929 window_schedule_redraw(widget->window);
930}
931
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400932cairo_surface_t *
933window_get_surface(struct window *window)
934{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400935 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400936}
937
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100938struct wl_surface *
939window_get_wl_surface(struct window *window)
940{
941 return window->surface;
942}
943
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200944struct wl_shell_surface *
945window_get_wl_shell_surface(struct window *window)
946{
947 return window->shell_surface;
948}
949
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500950static void
951frame_resize_handler(struct widget *widget,
952 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400953{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500954 struct frame *frame = data;
955 struct widget *child = frame->child;
956 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500957 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500958 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500959 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500960
Kristian Høgsbergb435e842012-03-05 20:38:08 -0500961 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400962 decoration_width = (frame->width + frame->margin) * 2;
963 decoration_height = frame->width +
964 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500965
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400966 allocation.x = frame->width + frame->margin;
967 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -0500968 allocation.width = width - decoration_width;
969 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500970
971 widget->window->input_region =
972 wl_compositor_create_region(display->compositor);
973 wl_region_add(widget->window->input_region,
974 frame->margin, frame->margin,
975 width - 2 * frame->margin,
976 height - 2 * frame->margin);
977
978 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -0500979 } else {
980 decoration_width = 0;
981 decoration_height = 0;
982
983 allocation.x = 0;
984 allocation.y = 0;
985 allocation.width = width;
986 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500987 opaque_margin = 0;
988 }
989
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500990 widget_set_allocation(child, allocation.x, allocation.y,
991 allocation.width, allocation.height);
992
993 if (child->resize_handler)
994 child->resize_handler(child,
995 allocation.width,
996 allocation.height,
997 child->user_data);
998
999 widget_set_allocation(widget, 0, 0,
1000 child->allocation.width + decoration_width,
1001 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001002
1003 if (child->opaque) {
1004 widget->window->opaque_region =
1005 wl_compositor_create_region(display->compositor);
1006 wl_region_add(widget->window->opaque_region,
1007 opaque_margin, opaque_margin,
1008 widget->allocation.width - 2 * opaque_margin,
1009 widget->allocation.height - 2 * opaque_margin);
1010 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001011}
1012
1013static void
1014frame_redraw_handler(struct widget *widget, void *data)
1015{
1016 struct frame *frame = data;
1017 cairo_t *cr;
1018 cairo_text_extents_t extents;
1019 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001020 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001021 struct window *window = widget->window;
1022
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001023 if (window->type == TYPE_FULLSCREEN)
1024 return;
1025
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001026 width = widget->allocation.width;
1027 height = widget->allocation.height;
1028
1029 cr = cairo_create(window->cairo_surface);
1030
1031 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1032 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1033 cairo_paint(cr);
1034
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001035 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001036 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001037 2, 2, width + 8, height + 8,
1038 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001039
1040 if (window->keyboard_device)
1041 source = window->display->active_frame;
1042 else
1043 source = window->display->inactive_frame;
1044
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001045 tile_source(cr, source,
1046 frame->margin, frame->margin,
1047 width - frame->margin * 2, height - frame->margin * 2,
1048 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001049
1050 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001051 cairo_select_font_face(cr, "sans",
1052 CAIRO_FONT_SLANT_NORMAL,
1053 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001054 cairo_set_font_size(cr, 14);
1055 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001056 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001057 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001058 if (window->keyboard_device) {
1059 cairo_move_to(cr, x + 1, y + 1);
1060 cairo_set_source_rgb(cr, 1, 1, 1);
1061 cairo_show_text(cr, window->title);
1062 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001063 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001064 cairo_show_text(cr, window->title);
1065 } else {
1066 cairo_move_to(cr, x, y);
1067 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1068 cairo_show_text(cr, window->title);
1069 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001070
1071 cairo_destroy(cr);
1072}
1073
1074static int
1075frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1076{
1077 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001078 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001079 const int grip_size = 8;
1080
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001081 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001082 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001083 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001084 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001085 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001086 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001087 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001088 hlocation = WINDOW_RESIZING_RIGHT;
1089 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001090 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001091
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001092 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001093 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001094 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001095 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001096 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001097 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001098 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001099 vlocation = WINDOW_RESIZING_BOTTOM;
1100 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001101 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001102
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001103 location = vlocation | hlocation;
1104 if (location & WINDOW_EXTERIOR)
1105 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001106 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001107 location = WINDOW_TITLEBAR;
1108 else if (location == WINDOW_INTERIOR)
1109 location = WINDOW_CLIENT_AREA;
1110
1111 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001112}
1113
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001114static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001115frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001116{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001117 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001118
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001119 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001120 switch (location) {
1121 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001122 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001123 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001124 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001125 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001126 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001127 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001128 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001129 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001130 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001131 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001132 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001133 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001134 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001135 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001136 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001137 case WINDOW_EXTERIOR:
1138 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001140 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001141 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001142}
1143
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001144static void
1145frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001146{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001147 switch (index) {
1148 case 0: /* close */
1149 if (window->close_handler)
1150 window->close_handler(window->parent,
1151 window->user_data);
1152 else
1153 display_exit(window->display);
1154 break;
1155 case 1: /* fullscreen */
1156 /* we don't have a way to get out of fullscreen for now */
1157 window_set_fullscreen(window, 1);
1158 break;
1159 case 2: /* rotate */
1160 case 3: /* scale */
1161 break;
1162 }
1163}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001164
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001165void
1166window_show_frame_menu(struct window *window,
1167 struct input *input, uint32_t time)
1168{
1169 int32_t x, y;
1170
1171 static const char *entries[] = {
1172 "Close", "Fullscreen", "Rotate", "Scale"
1173 };
1174
1175 input_get_position(input, &x, &y);
1176 window_show_menu(window->display, input, time, window,
1177 x - 10, y - 10, frame_menu_func, entries, 4);
1178}
1179
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001180static int
1181frame_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001182 struct input *input, int32_t x, int32_t y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001183{
1184 return frame_get_pointer_image_for_location(data, input);
1185}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001186
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001187static int
1188frame_motion_handler(struct widget *widget,
1189 struct input *input, uint32_t time,
1190 int32_t x, int32_t y, void *data)
1191{
1192 return frame_get_pointer_image_for_location(data, input);
1193}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001194
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001195static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001196frame_button_handler(struct widget *widget,
1197 struct input *input, uint32_t time,
1198 int button, int state, void *data)
1199
1200{
1201 struct frame *frame = data;
1202 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001203 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001204 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001205
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001206 location = frame_get_pointer_location(frame, input->sx, input->sy);
1207
1208 if (window->display->shell && button == BTN_LEFT && state == 1) {
1209 switch (location) {
1210 case WINDOW_TITLEBAR:
1211 if (!window->shell_surface)
1212 break;
1213 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001214 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001215 wl_shell_surface_move(window->shell_surface,
1216 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001217 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001218 break;
1219 case WINDOW_RESIZING_TOP:
1220 case WINDOW_RESIZING_BOTTOM:
1221 case WINDOW_RESIZING_LEFT:
1222 case WINDOW_RESIZING_RIGHT:
1223 case WINDOW_RESIZING_TOP_LEFT:
1224 case WINDOW_RESIZING_TOP_RIGHT:
1225 case WINDOW_RESIZING_BOTTOM_LEFT:
1226 case WINDOW_RESIZING_BOTTOM_RIGHT:
1227 if (!window->shell_surface)
1228 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001229 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001230
1231 if (!display->dpy) {
1232 /* If we're using shm, allocate a big
1233 pool to create buffers out of while
1234 we resize. We should probably base
1235 this number on the size of the output. */
1236 window->pool_size = 6 * 1024 * 1024;
1237 window->pool = make_shm_pool(display,
1238 window->pool_size,
1239 &window->pool_data);
1240 }
1241
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242 wl_shell_surface_resize(window->shell_surface,
1243 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001244 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245 break;
1246 }
1247 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001248 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249 }
1250}
1251
1252struct widget *
1253frame_create(struct window *window, void *data)
1254{
1255 struct frame *frame;
1256
1257 frame = malloc(sizeof *frame);
1258 memset(frame, 0, sizeof *frame);
1259
1260 frame->widget = window_add_widget(window, frame);
1261 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001262 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001263 frame->width = 4;
1264 frame->titlebar_height = 30
1265;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001266 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1267 widget_set_resize_handler(frame->widget, frame_resize_handler);
1268 widget_set_enter_handler(frame->widget, frame_enter_handler);
1269 widget_set_motion_handler(frame->widget, frame_motion_handler);
1270 widget_set_button_handler(frame->widget, frame_button_handler);
1271
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001272 window->frame = frame;
1273
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001274 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001275}
1276
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001277static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001278frame_destroy(struct frame *frame)
1279{
1280 /* frame->child must be destroyed by the application */
1281 widget_destroy(frame->widget);
1282 free(frame);
1283}
1284
1285static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001286input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001287 int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001288{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001289 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001290 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001291
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001292 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001293 return;
1294
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001295 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001296 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001297 widget = old;
1298 if (input->grab)
1299 widget = input->grab;
1300 if (widget->leave_handler)
1301 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001302 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001303 }
1304
1305 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001306 widget = focus;
1307 if (input->grab)
1308 widget = input->grab;
1309 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001310 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001311 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001312 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001313
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001314 input_set_pointer_image(input, input->pointer_enter_serial,
1315 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001316 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001317}
1318
1319static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001320input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001321 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001322{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001323 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001324 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001325 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001326 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001327
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001328 input->sx = sx;
1329 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001330
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001331 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001332 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001333 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001334 }
1335
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001336 if (input->grab)
1337 widget = input->grab;
1338 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001339 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001340 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001341 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001342 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001343 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001344
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001345 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001346}
1347
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001348void
1349input_grab(struct input *input, struct widget *widget, uint32_t button)
1350{
1351 input->grab = widget;
1352 input->grab_button = button;
1353}
1354
1355void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001356input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001357{
1358 struct widget *widget;
1359
1360 input->grab = NULL;
1361 if (input->pointer_focus) {
1362 widget = widget_find_widget(input->pointer_focus->widget,
1363 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001364 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001365 }
1366}
1367
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001368static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001369input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001370 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001371 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001372{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001373 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001374 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001375
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001376 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001377 if (input->focus_widget && input->grab == NULL && state)
1378 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001379
Neil Roberts6b28aad2012-01-23 19:11:18 +00001380 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001381 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001382 (*widget->button_handler)(widget,
1383 input, time,
1384 button, state,
1385 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001386
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001387 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001388 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001389}
1390
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001391static void
Scott Moreau210d0792012-03-22 10:47:01 -06001392input_handle_axis(void *data,
1393 struct wl_input_device *input_device,
1394 uint32_t time, uint32_t axis, int32_t value)
1395{
1396}
1397
1398static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001399input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001400 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001401{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001402 struct input *input = data;
1403 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001404 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001405 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001406
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001407 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001408 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001409 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001410 return;
1411
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001412 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001413 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001414 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1415 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001416
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001417 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1418
1419 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001420 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001421 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001422 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001423
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001424 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1425 if (state)
1426 window_set_maximized(window,
1427 window->type != TYPE_MAXIMIZED);
1428 } else if (window->key_handler) {
1429 (*window->key_handler)(window, input, time, key,
1430 sym, state, window->user_data);
1431 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001432}
1433
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001434static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001435input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001436{
1437 struct window *window = input->pointer_focus;
1438
1439 if (!window)
1440 return;
1441
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001442 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001443
Pekka Paalanene1207c72011-12-16 12:02:09 +02001444 input->pointer_focus = NULL;
1445 input->current_pointer_image = POINTER_UNSET;
1446}
1447
1448static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001449input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001450 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001451 uint32_t serial, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001452 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001453{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001454 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001455 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001456 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001457
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001458 input->display->serial = serial;
1459 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001460 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001461 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001462
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001463 if (window->pool) {
1464 wl_shm_pool_destroy(window->pool);
1465 munmap(window->pool_data, window->pool_size);
1466 window->pool = NULL;
1467 /* Schedule a redraw to free the pool */
1468 window_schedule_redraw(window);
1469 }
1470
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001471 input->sx = sx;
1472 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001473
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001474 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001475 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001476}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001477
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001478static void
1479input_handle_pointer_leave(void *data,
1480 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001481 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001482{
1483 struct input *input = data;
1484
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001485 input->display->serial = serial;
1486 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001487}
1488
1489static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001490input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001491{
1492 struct window *window = input->keyboard_focus;
1493
1494 if (!window)
1495 return;
1496
1497 window->keyboard_device = NULL;
1498 if (window->keyboard_focus_handler)
1499 (*window->keyboard_focus_handler)(window, NULL,
1500 window->user_data);
1501
1502 input->keyboard_focus = NULL;
1503}
1504
1505static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001506input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001507 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001508 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001509 struct wl_surface *surface,
1510 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001511{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001512 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001513 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001514 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001515 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001516
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001517 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001518 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001519
1520 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001521 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001522 for (k = keys->data; k < end; k++)
1523 input->modifiers |= d->xkb->map->modmap[*k];
1524
1525 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001526 window->keyboard_device = input;
1527 if (window->keyboard_focus_handler)
1528 (*window->keyboard_focus_handler)(window,
1529 window->keyboard_device,
1530 window->user_data);
1531}
1532
1533static void
1534input_handle_keyboard_leave(void *data,
1535 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001536 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001537 struct wl_surface *surface)
1538{
1539 struct input *input = data;
1540
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001541 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001542 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001543}
1544
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001545static void
1546input_handle_touch_down(void *data,
1547 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001548 uint32_t serial, uint32_t time,
1549 struct wl_surface *surface,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001550 int32_t id, int32_t x, int32_t y)
1551{
1552}
1553
1554static void
1555input_handle_touch_up(void *data,
1556 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001557 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001558{
1559}
1560
1561static void
1562input_handle_touch_motion(void *data,
1563 struct wl_input_device *wl_input_device,
1564 uint32_t time, int32_t id, int32_t x, int32_t y)
1565{
1566}
1567
1568static void
1569input_handle_touch_frame(void *data,
1570 struct wl_input_device *wl_input_device)
1571{
1572}
1573
1574static void
1575input_handle_touch_cancel(void *data,
1576 struct wl_input_device *wl_input_device)
1577{
1578}
1579
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001580static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001581 input_handle_motion,
1582 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001583 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001584 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001585 input_handle_pointer_enter,
1586 input_handle_pointer_leave,
1587 input_handle_keyboard_enter,
1588 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001589 input_handle_touch_down,
1590 input_handle_touch_up,
1591 input_handle_touch_motion,
1592 input_handle_touch_frame,
1593 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001594};
1595
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001596void
1597input_get_position(struct input *input, int32_t *x, int32_t *y)
1598{
1599 *x = input->sx;
1600 *y = input->sy;
1601}
1602
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001603struct wl_input_device *
1604input_get_input_device(struct input *input)
1605{
1606 return input->input_device;
1607}
1608
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001609uint32_t
1610input_get_modifiers(struct input *input)
1611{
1612 return input->modifiers;
1613}
1614
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001615struct widget *
1616input_get_focus_widget(struct input *input)
1617{
1618 return input->focus_widget;
1619}
1620
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001621struct data_offer {
1622 struct wl_data_offer *offer;
1623 struct input *input;
1624 struct wl_array types;
1625 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001626
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001627 struct task io_task;
1628 int fd;
1629 data_func_t func;
1630 int32_t x, y;
1631 void *user_data;
1632};
1633
1634static void
1635data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1636{
1637 struct data_offer *offer = data;
1638 char **p;
1639
1640 p = wl_array_add(&offer->types, sizeof *p);
1641 *p = strdup(type);
1642}
1643
1644static const struct wl_data_offer_listener data_offer_listener = {
1645 data_offer_offer,
1646};
1647
1648static void
1649data_offer_destroy(struct data_offer *offer)
1650{
1651 char **p;
1652
1653 offer->refcount--;
1654 if (offer->refcount == 0) {
1655 wl_data_offer_destroy(offer->offer);
1656 for (p = offer->types.data; *p; p++)
1657 free(*p);
1658 wl_array_release(&offer->types);
1659 free(offer);
1660 }
1661}
1662
1663static void
1664data_device_data_offer(void *data,
1665 struct wl_data_device *data_device, uint32_t id)
1666{
1667 struct data_offer *offer;
1668
1669 offer = malloc(sizeof *offer);
1670
1671 wl_array_init(&offer->types);
1672 offer->refcount = 1;
1673 offer->input = data;
1674
1675 /* FIXME: Generate typesafe wrappers for this */
1676 offer->offer = (struct wl_data_offer *)
1677 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1678 id, &wl_data_offer_interface);
1679
1680 wl_data_offer_add_listener(offer->offer,
1681 &data_offer_listener, offer);
1682}
1683
1684static void
1685data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001686 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001687 int32_t x, int32_t y, struct wl_data_offer *offer)
1688{
1689 struct input *input = data;
1690 struct window *window;
1691 char **p;
1692
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001693 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001694 input->drag_offer = wl_data_offer_get_user_data(offer);
1695 window = wl_surface_get_user_data(surface);
1696 input->pointer_focus = window;
1697
1698 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1699 *p = NULL;
1700
1701 window = input->pointer_focus;
1702 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001703 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001704 input->drag_offer->types.data,
1705 window->user_data);
1706}
1707
1708static void
1709data_device_leave(void *data, struct wl_data_device *data_device)
1710{
1711 struct input *input = data;
1712
1713 data_offer_destroy(input->drag_offer);
1714 input->drag_offer = NULL;
1715}
1716
1717static void
1718data_device_motion(void *data, struct wl_data_device *data_device,
1719 uint32_t time, int32_t x, int32_t y)
1720{
1721 struct input *input = data;
1722 struct window *window = input->pointer_focus;
1723
1724 input->sx = x;
1725 input->sy = y;
1726
1727 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001728 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001729 input->drag_offer->types.data,
1730 window->user_data);
1731}
1732
1733static void
1734data_device_drop(void *data, struct wl_data_device *data_device)
1735{
1736 struct input *input = data;
1737 struct window *window = input->pointer_focus;
1738
1739 if (window->drop_handler)
1740 window->drop_handler(window, input,
1741 input->sx, input->sy, window->user_data);
1742}
1743
1744static void
1745data_device_selection(void *data,
1746 struct wl_data_device *wl_data_device,
1747 struct wl_data_offer *offer)
1748{
1749 struct input *input = data;
1750 char **p;
1751
1752 if (input->selection_offer)
1753 data_offer_destroy(input->selection_offer);
1754
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001755 if (offer) {
1756 input->selection_offer = wl_data_offer_get_user_data(offer);
1757 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1758 *p = NULL;
1759 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001760}
1761
1762static const struct wl_data_device_listener data_device_listener = {
1763 data_device_data_offer,
1764 data_device_enter,
1765 data_device_leave,
1766 data_device_motion,
1767 data_device_drop,
1768 data_device_selection
1769};
1770
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001771void
1772input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1773{
1774 struct display *display = input->display;
1775 struct wl_buffer *buffer;
1776 cairo_surface_t *surface;
1777
1778 if (pointer == input->current_pointer_image)
1779 return;
1780
1781 input->current_pointer_image = pointer;
1782 surface = display->pointer_surfaces[pointer];
1783
1784 if (!surface)
1785 return;
1786
1787 buffer = display_get_buffer_for_surface(display, surface);
1788 wl_input_device_attach(input->input_device, time, buffer,
1789 pointer_images[pointer].hotspot_x,
1790 pointer_images[pointer].hotspot_y);
1791}
1792
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001793struct wl_data_device *
1794input_get_data_device(struct input *input)
1795{
1796 return input->data_device;
1797}
1798
1799void
1800input_set_selection(struct input *input,
1801 struct wl_data_source *source, uint32_t time)
1802{
1803 wl_data_device_set_selection(input->data_device, source, time);
1804}
1805
1806void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001807input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001808{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001809 wl_data_offer_accept(input->drag_offer->offer,
1810 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001811}
1812
1813static void
1814offer_io_func(struct task *task, uint32_t events)
1815{
1816 struct data_offer *offer =
1817 container_of(task, struct data_offer, io_task);
1818 unsigned int len;
1819 char buffer[4096];
1820
1821 len = read(offer->fd, buffer, sizeof buffer);
1822 offer->func(buffer, len,
1823 offer->x, offer->y, offer->user_data);
1824
1825 if (len == 0) {
1826 close(offer->fd);
1827 data_offer_destroy(offer);
1828 }
1829}
1830
1831static void
1832data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1833 data_func_t func, void *user_data)
1834{
1835 int p[2];
1836
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02001837 if (pipe2(p, O_CLOEXEC) == -1)
1838 return;
1839
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001840 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1841 close(p[1]);
1842
1843 offer->io_task.run = offer_io_func;
1844 offer->fd = p[0];
1845 offer->func = func;
1846 offer->refcount++;
1847 offer->user_data = user_data;
1848
1849 display_watch_fd(offer->input->display,
1850 offer->fd, EPOLLIN, &offer->io_task);
1851}
1852
1853void
1854input_receive_drag_data(struct input *input, const char *mime_type,
1855 data_func_t func, void *data)
1856{
1857 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1858 input->drag_offer->x = input->sx;
1859 input->drag_offer->y = input->sy;
1860}
1861
1862int
1863input_receive_selection_data(struct input *input, const char *mime_type,
1864 data_func_t func, void *data)
1865{
1866 char **p;
1867
1868 if (input->selection_offer == NULL)
1869 return -1;
1870
1871 for (p = input->selection_offer->types.data; *p; p++)
1872 if (strcmp(mime_type, *p) == 0)
1873 break;
1874
1875 if (*p == NULL)
1876 return -1;
1877
1878 data_offer_receive_data(input->selection_offer,
1879 mime_type, func, data);
1880 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001881}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001882
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001883int
1884input_receive_selection_data_to_fd(struct input *input,
1885 const char *mime_type, int fd)
1886{
1887 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1888
1889 return 0;
1890}
1891
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001892void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001893window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001894{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001895 if (!window->shell_surface)
1896 return;
1897
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001898 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001899 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001900}
1901
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001902static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001903idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001904{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001905 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001906
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001907 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001908 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001909 widget_set_allocation(widget,
1910 window->pending_allocation.x,
1911 window->pending_allocation.y,
1912 window->pending_allocation.width,
1913 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001914
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001915 if (window->input_region) {
1916 wl_region_destroy(window->input_region);
1917 window->input_region = NULL;
1918 }
1919
1920 if (window->opaque_region) {
1921 wl_region_destroy(window->opaque_region);
1922 window->opaque_region = NULL;
1923 }
1924
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001925 if (widget->resize_handler)
1926 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001927 widget->allocation.width,
1928 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001929 widget->user_data);
1930
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05001931 if (window->allocation.width != widget->allocation.width ||
1932 window->allocation.height != widget->allocation.height) {
1933 window->allocation = widget->allocation;
1934 window_schedule_redraw(window);
1935 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001936}
1937
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001938void
1939window_schedule_resize(struct window *window, int width, int height)
1940{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001941 window->pending_allocation.x = 0;
1942 window->pending_allocation.y = 0;
1943 window->pending_allocation.width = width;
1944 window->pending_allocation.height = height;
1945
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04001946 window->resize_needed = 1;
1947 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001948}
1949
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001950void
1951widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
1952{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001953 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001954}
1955
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001956static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001957handle_ping(void *data, struct wl_shell_surface *shell_surface,
1958 uint32_t serial)
1959{
1960 wl_shell_surface_pong(shell_surface, serial);
1961}
1962
1963static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001964handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001965 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001966{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001967 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001968
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001969 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001970 return;
1971
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001972 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05001973 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001974}
1975
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001976static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02001977menu_destroy(struct menu *menu)
1978{
1979 widget_destroy(menu->widget);
1980 window_destroy(menu->window);
1981 free(menu);
1982}
1983
1984static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001985handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1986{
1987 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001988 struct menu *menu = window->widget->user_data;
1989
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001990 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001991 * device. Or just use wl_callback. And this really needs to
1992 * be a window vfunc that the menu can set. And we need the
1993 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001994
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001995 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001996 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02001997 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001998}
1999
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002000static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002001 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002002 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002003 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002004};
2005
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002006void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002007window_get_allocation(struct window *window,
2008 struct rectangle *allocation)
2009{
2010 *allocation = window->allocation;
2011}
2012
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002013static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002014widget_redraw(struct widget *widget)
2015{
2016 struct widget *child;
2017
2018 if (widget->redraw_handler)
2019 widget->redraw_handler(widget, widget->user_data);
2020 wl_list_for_each(child, &widget->child_list, link)
2021 widget_redraw(child);
2022}
2023
2024static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002025frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2026{
2027 struct window *window = data;
2028
2029 wl_callback_destroy(callback);
2030 window->redraw_scheduled = 0;
2031 if (window->redraw_needed)
2032 window_schedule_redraw(window);
2033}
2034
2035static const struct wl_callback_listener listener = {
2036 frame_callback
2037};
2038
2039static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002040idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002041{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002042 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002043 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002044
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002045 if (window->resize_needed)
2046 idle_resize(window);
2047
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002048 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002049 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002050 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002051 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002052 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002053
2054 callback = wl_surface_frame(window->surface);
2055 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002056}
2057
2058void
2059window_schedule_redraw(struct window *window)
2060{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002061 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002062 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002063 window->redraw_task.run = idle_redraw;
2064 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002065 window->redraw_scheduled = 1;
2066 }
2067}
2068
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002069void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002070window_set_custom(struct window *window)
2071{
2072 window->type = TYPE_CUSTOM;
2073}
2074
2075void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002076window_set_fullscreen(struct window *window, int fullscreen)
2077{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002078 if (!window->display->shell)
2079 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002080
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002081 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002082 return;
2083
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002084 if (fullscreen) {
2085 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002086 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002087 wl_shell_surface_set_fullscreen(window->shell_surface,
2088 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2089 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002090 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002091 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002092 wl_shell_surface_set_toplevel(window->shell_surface);
2093 window_schedule_resize(window,
2094 window->saved_allocation.width,
2095 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002096 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002097}
2098
2099void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002100window_set_maximized(struct window *window, int maximized)
2101{
2102 if (!window->display->shell)
2103 return;
2104
2105 if ((window->type == TYPE_MAXIMIZED) == maximized)
2106 return;
2107
2108 if (window->type == TYPE_TOPLEVEL) {
2109 window->saved_allocation = window->allocation;
2110 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2111 window->type = TYPE_MAXIMIZED;
2112 } else {
2113 wl_shell_surface_set_toplevel(window->shell_surface);
2114 window->type = TYPE_TOPLEVEL;
2115 window_schedule_resize(window,
2116 window->saved_allocation.width,
2117 window->saved_allocation.height);
2118 }
2119}
2120
2121void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002122window_set_user_data(struct window *window, void *data)
2123{
2124 window->user_data = data;
2125}
2126
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002127void *
2128window_get_user_data(struct window *window)
2129{
2130 return window->user_data;
2131}
2132
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002133void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002134window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002135 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002136{
2137 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002138}
2139
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002140void
2141window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002142 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002143{
2144 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002145}
2146
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002147void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002148window_set_data_handler(struct window *window, window_data_handler_t handler)
2149{
2150 window->data_handler = handler;
2151}
2152
2153void
2154window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2155{
2156 window->drop_handler = handler;
2157}
2158
2159void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002160window_set_close_handler(struct window *window,
2161 window_close_handler_t handler)
2162{
2163 window->close_handler = handler;
2164}
2165
2166void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002167window_set_title(struct window *window, const char *title)
2168{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002169 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002170 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002171 if (window->shell_surface)
2172 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002173}
2174
2175const char *
2176window_get_title(struct window *window)
2177{
2178 return window->title;
2179}
2180
2181void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002182window_damage(struct window *window, int32_t x, int32_t y,
2183 int32_t width, int32_t height)
2184{
2185 wl_surface_damage(window->surface, x, y, width, height);
2186}
2187
Casey Dahlin9074db52012-04-19 22:50:09 -04002188static void
2189surface_enter(void *data,
2190 struct wl_surface *wl_surface, struct wl_output *output)
2191{
2192}
2193
2194static void
2195surface_leave(void *data,
2196 struct wl_surface *wl_surface, struct wl_output *output)
2197{
2198}
2199
2200static const struct wl_surface_listener surface_listener = {
2201 surface_enter,
2202 surface_leave
2203};
2204
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002205static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002206window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002207{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002208 struct window *window;
2209
2210 window = malloc(sizeof *window);
2211 if (window == NULL)
2212 return NULL;
2213
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002214 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002215 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002216 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002217 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002218 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002219 if (display->shell) {
2220 window->shell_surface =
2221 wl_shell_get_shell_surface(display->shell,
2222 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002223 if (window->title)
2224 wl_shell_surface_set_title(window->shell_surface,
2225 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002226 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002227 window->allocation.x = 0;
2228 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002229 window->allocation.width = 0;
2230 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002231 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002232 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002233 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002234 window->input_region = NULL;
2235 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002236
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002237 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002238#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002239 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002240#else
2241 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2242#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002243 else
2244 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002245
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002246 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002247 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002248 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002249
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002250 if (window->shell_surface) {
2251 wl_shell_surface_set_user_data(window->shell_surface, window);
2252 wl_shell_surface_add_listener(window->shell_surface,
2253 &shell_surface_listener, window);
2254 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002255
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002256 return window;
2257}
2258
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002259struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002260window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002261{
2262 struct window *window;
2263
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002264 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002265 if (!window)
2266 return NULL;
2267
2268 return window;
2269}
2270
2271struct window *
2272window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002273 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002274{
2275 struct window *window;
2276
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002277 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002278 if (!window)
2279 return NULL;
2280
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002281 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002282 window->x = x;
2283 window->y = y;
2284
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002285 if (display->shell)
2286 wl_shell_surface_set_transient(window->shell_surface,
2287 window->parent->shell_surface,
2288 window->x, window->y, 0);
2289
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002290 return window;
2291}
2292
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002293static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002294menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002295{
2296 int next;
2297
2298 next = (sy - 8) / 20;
2299 if (menu->current != next) {
2300 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002301 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002302 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002303}
2304
2305static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002306menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002307 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002308 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002309{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002310 struct menu *menu = data;
2311
2312 if (widget == menu->widget)
2313 menu_set_item(data, y);
2314
2315 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002316}
2317
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002318static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002319menu_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002320 struct input *input, int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002321{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002322 struct menu *menu = data;
2323
2324 if (widget == menu->widget)
2325 menu_set_item(data, y);
2326
2327 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002328}
2329
2330static void
2331menu_leave_handler(struct widget *widget, struct input *input, void *data)
2332{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002333 struct menu *menu = data;
2334
2335 if (widget == menu->widget)
2336 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002337}
2338
2339static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002340menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002341 struct input *input, uint32_t time,
2342 int button, int state, void *data)
2343
2344{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002345 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002346
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002347 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002348 /* Either relase after press-drag-release or
2349 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002350 menu->func(menu->window->parent,
2351 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002352 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002353 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002354 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002355}
2356
2357static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002358menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002359{
2360 cairo_t *cr;
2361 const int32_t r = 3, margin = 3;
2362 struct menu *menu = data;
2363 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002364 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002365
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002366 cr = cairo_create(window->cairo_surface);
2367 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2368 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2369 cairo_paint(cr);
2370
2371 width = window->allocation.width;
2372 height = window->allocation.height;
2373 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002374
2375 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002376 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2377 cairo_fill(cr);
2378
2379 for (i = 0; i < menu->count; i++) {
2380 if (i == menu->current) {
2381 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2382 cairo_rectangle(cr, margin, i * 20 + margin,
2383 width - 2 * margin, 20);
2384 cairo_fill(cr);
2385 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2386 cairo_move_to(cr, 10, i * 20 + 16);
2387 cairo_show_text(cr, menu->entries[i]);
2388 } else {
2389 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2390 cairo_move_to(cr, 10, i * 20 + 16);
2391 cairo_show_text(cr, menu->entries[i]);
2392 }
2393 }
2394
2395 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002396}
2397
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002398void
2399window_show_menu(struct display *display,
2400 struct input *input, uint32_t time, struct window *parent,
2401 int32_t x, int32_t y,
2402 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002403{
2404 struct window *window;
2405 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002406 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002407
2408 menu = malloc(sizeof *menu);
2409 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002410 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002411
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002412 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002413 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002414 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002415
2416 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002417 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002418 menu->entries = entries;
2419 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002420 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002421 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002422 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002423 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002424 window->type = TYPE_MENU;
2425 window->x = x;
2426 window->y = y;
2427
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002428 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002429 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2430 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002431 window->parent->shell_surface,
2432 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002433
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002434 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002435 widget_set_enter_handler(menu->widget, menu_enter_handler);
2436 widget_set_leave_handler(menu->widget, menu_leave_handler);
2437 widget_set_motion_handler(menu->widget, menu_motion_handler);
2438 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002439
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002440 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002441 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002442}
2443
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002444void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002445window_set_buffer_type(struct window *window, enum window_buffer_type type)
2446{
2447 window->buffer_type = type;
2448}
2449
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002450
2451static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002452display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002453 struct wl_output *wl_output,
2454 int x, int y,
2455 int physical_width,
2456 int physical_height,
2457 int subpixel,
2458 const char *make,
2459 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002460{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002461 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002462
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002463 output->allocation.x = x;
2464 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002465}
2466
2467static void
2468display_handle_mode(void *data,
2469 struct wl_output *wl_output,
2470 uint32_t flags,
2471 int width,
2472 int height,
2473 int refresh)
2474{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002475 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002476 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002477
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002478 if (flags & WL_OUTPUT_MODE_CURRENT) {
2479 output->allocation.width = width;
2480 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002481 if (display->output_configure_handler)
2482 (*display->output_configure_handler)(
2483 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002484 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002485}
2486
2487static const struct wl_output_listener output_listener = {
2488 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002489 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002490};
2491
2492static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002493display_add_output(struct display *d, uint32_t id)
2494{
2495 struct output *output;
2496
2497 output = malloc(sizeof *output);
2498 if (output == NULL)
2499 return;
2500
2501 memset(output, 0, sizeof *output);
2502 output->display = d;
2503 output->output =
2504 wl_display_bind(d->display, id, &wl_output_interface);
2505 wl_list_insert(d->output_list.prev, &output->link);
2506
2507 wl_output_add_listener(output->output, &output_listener, output);
2508}
2509
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002510static void
2511output_destroy(struct output *output)
2512{
2513 if (output->destroy_handler)
2514 (*output->destroy_handler)(output, output->user_data);
2515
2516 wl_output_destroy(output->output);
2517 wl_list_remove(&output->link);
2518 free(output);
2519}
2520
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002521void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002522display_set_output_configure_handler(struct display *display,
2523 display_output_handler_t handler)
2524{
2525 struct output *output;
2526
2527 display->output_configure_handler = handler;
2528 if (!handler)
2529 return;
2530
2531 wl_list_for_each(output, &display->output_list, link)
2532 (*display->output_configure_handler)(output,
2533 display->user_data);
2534}
2535
2536void
2537output_set_user_data(struct output *output, void *data)
2538{
2539 output->user_data = data;
2540}
2541
2542void *
2543output_get_user_data(struct output *output)
2544{
2545 return output->user_data;
2546}
2547
2548void
2549output_set_destroy_handler(struct output *output,
2550 display_output_handler_t handler)
2551{
2552 output->destroy_handler = handler;
2553 /* FIXME: implement this, once we have way to remove outputs */
2554}
2555
2556void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002557output_get_allocation(struct output *output, struct rectangle *allocation)
2558{
2559 *allocation = output->allocation;
2560}
2561
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002562struct wl_output *
2563output_get_wl_output(struct output *output)
2564{
2565 return output->output;
2566}
2567
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002568static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002569display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002570{
2571 struct input *input;
2572
2573 input = malloc(sizeof *input);
2574 if (input == NULL)
2575 return;
2576
2577 memset(input, 0, sizeof *input);
2578 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002579 input->input_device =
2580 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002581 input->pointer_focus = NULL;
2582 input->keyboard_focus = NULL;
2583 wl_list_insert(d->input_list.prev, &input->link);
2584
2585 wl_input_device_add_listener(input->input_device,
2586 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002587 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002588
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002589 input->data_device =
2590 wl_data_device_manager_get_data_device(d->data_device_manager,
2591 input->input_device);
2592 wl_data_device_add_listener(input->data_device,
2593 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002594}
2595
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002596static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002597input_destroy(struct input *input)
2598{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002599 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002600 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002601
2602 if (input->drag_offer)
2603 data_offer_destroy(input->drag_offer);
2604
2605 if (input->selection_offer)
2606 data_offer_destroy(input->selection_offer);
2607
2608 wl_data_device_destroy(input->data_device);
2609 wl_list_remove(&input->link);
2610 wl_input_device_destroy(input->input_device);
2611 free(input);
2612}
2613
2614static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002615display_handle_global(struct wl_display *display, uint32_t id,
2616 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002617{
2618 struct display *d = data;
2619
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002620 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002621 d->compositor =
2622 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002623 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002624 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002625 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002626 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002627 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002628 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002629 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002630 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002631 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2632 d->data_device_manager =
2633 wl_display_bind(display, id,
2634 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002635 }
2636}
2637
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002638static void
2639display_render_frame(struct display *d)
2640{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002641 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002642 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002643
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002644 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002645 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2646 cr = cairo_create(d->shadow);
2647 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2648 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002649 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002650 cairo_fill(cr);
2651 cairo_destroy(cr);
2652 blur_surface(d->shadow, 64);
2653
2654 d->active_frame =
2655 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2656 cr = cairo_create(d->active_frame);
2657 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002658
2659 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2660 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2661 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2662 cairo_set_source(cr, pattern);
2663 cairo_pattern_destroy(pattern);
2664
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002665 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002666 cairo_fill(cr);
2667 cairo_destroy(cr);
2668
2669 d->inactive_frame =
2670 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2671 cr = cairo_create(d->inactive_frame);
2672 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002673 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002674 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002675 cairo_fill(cr);
2676 cairo_destroy(cr);
2677}
2678
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002679static void
2680init_xkb(struct display *d)
2681{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002682 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002683
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002684 names.rules = "evdev";
2685 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002686 names.layout = option_xkb_layout;
2687 names.variant = option_xkb_variant;
2688 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002689
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002690 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002691 if (!d->xkb) {
2692 fprintf(stderr, "Failed to compile keymap\n");
2693 exit(1);
2694 }
2695}
2696
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002697static void
2698fini_xkb(struct display *display)
2699{
2700 xkb_free_keymap(display->xkb);
2701}
2702
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002703#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002704static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002705init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002706{
2707 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002708 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002709
Rob Clark6396ed32012-03-11 19:48:41 -05002710#ifdef USE_CAIRO_GLESV2
2711# define GL_BIT EGL_OPENGL_ES2_BIT
2712#else
2713# define GL_BIT EGL_OPENGL_BIT
2714#endif
2715
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002716 static const EGLint argb_cfg_attribs[] = {
2717 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002718 EGL_RED_SIZE, 1,
2719 EGL_GREEN_SIZE, 1,
2720 EGL_BLUE_SIZE, 1,
2721 EGL_ALPHA_SIZE, 1,
2722 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002723 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002724 EGL_NONE
2725 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002726
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002727#ifdef USE_CAIRO_GLESV2
2728 static const EGLint context_attribs[] = {
2729 EGL_CONTEXT_CLIENT_VERSION, 2,
2730 EGL_NONE
2731 };
2732 EGLint api = EGL_OPENGL_ES_API;
2733#else
2734 EGLint *context_attribs = NULL;
2735 EGLint api = EGL_OPENGL_API;
2736#endif
2737
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002738 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002739 if (!eglInitialize(d->dpy, &major, &minor)) {
2740 fprintf(stderr, "failed to initialize display\n");
2741 return -1;
2742 }
2743
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002744 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002745 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2746 return -1;
2747 }
2748
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002749 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2750 &d->argb_config, 1, &n) || n != 1) {
2751 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002752 return -1;
2753 }
2754
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002755 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002756 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002757 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002758 fprintf(stderr, "failed to create context\n");
2759 return -1;
2760 }
2761
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002762 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002763 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002764 return -1;
2765 }
2766
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002767#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002768 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2769 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2770 fprintf(stderr, "failed to get cairo egl argb device\n");
2771 return -1;
2772 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002773#endif
2774
2775 return 0;
2776}
2777
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002778static void
2779fini_egl(struct display *display)
2780{
2781#ifdef HAVE_CAIRO_EGL
2782 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002783#endif
2784
2785 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2786 EGL_NO_CONTEXT);
2787
2788 eglTerminate(display->dpy);
2789 eglReleaseThread();
2790}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002791#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002792
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002793static int
2794event_mask_update(uint32_t mask, void *data)
2795{
2796 struct display *d = data;
2797
2798 d->mask = mask;
2799
2800 return 0;
2801}
2802
2803static void
2804handle_display_data(struct task *task, uint32_t events)
2805{
2806 struct display *display =
2807 container_of(task, struct display, display_task);
2808
2809 wl_display_iterate(display->display, display->mask);
2810}
2811
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002812struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002813display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002814{
2815 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002816
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002817 argc = parse_options(xkb_options,
2818 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002819
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002820 d = malloc(sizeof *d);
2821 if (d == NULL)
2822 return NULL;
2823
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002824 memset(d, 0, sizeof *d);
2825
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002826 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002827 if (d->display == NULL) {
2828 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002829 return NULL;
2830 }
2831
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002832 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2833 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2834 d->display_task.run = handle_display_data;
2835 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2836
2837 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002838 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002839 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002840
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002841 /* Set up listener so we'll catch all events. */
2842 wl_display_add_global_listener(d->display,
2843 display_handle_global, d);
2844
2845 /* Process connection events. */
2846 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002847#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002848 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002849 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002850#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002851
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002852 d->image_target_texture_2d =
2853 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2854 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2855 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2856
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002857 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002858
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002859 display_render_frame(d);
2860
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002861 wl_list_init(&d->window_list);
2862
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002863 init_xkb(d);
2864
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002865 return d;
2866}
2867
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002868static void
2869display_destroy_outputs(struct display *display)
2870{
2871 struct output *tmp;
2872 struct output *output;
2873
2874 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2875 output_destroy(output);
2876}
2877
Pekka Paalanene1207c72011-12-16 12:02:09 +02002878static void
2879display_destroy_inputs(struct display *display)
2880{
2881 struct input *tmp;
2882 struct input *input;
2883
2884 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2885 input_destroy(input);
2886}
2887
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002888void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002889display_destroy(struct display *display)
2890{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002891 if (!wl_list_empty(&display->window_list))
2892 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2893
2894 if (!wl_list_empty(&display->deferred_list))
2895 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2896
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002897 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002898 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002899
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002900 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002901
2902 cairo_surface_destroy(display->active_frame);
2903 cairo_surface_destroy(display->inactive_frame);
2904 cairo_surface_destroy(display->shadow);
2905 destroy_pointer_surfaces(display);
2906
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002907#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002908 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002909#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002910
Pekka Paalanenc2052982011-12-16 11:41:32 +02002911 if (display->shell)
2912 wl_shell_destroy(display->shell);
2913
2914 if (display->shm)
2915 wl_shm_destroy(display->shm);
2916
2917 if (display->data_device_manager)
2918 wl_data_device_manager_destroy(display->data_device_manager);
2919
2920 wl_compositor_destroy(display->compositor);
2921
2922 close(display->epoll_fd);
2923
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002924 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05002925 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002926 free(display);
2927}
2928
2929void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002930display_set_user_data(struct display *display, void *data)
2931{
2932 display->user_data = data;
2933}
2934
2935void *
2936display_get_user_data(struct display *display)
2937{
2938 return display->user_data;
2939}
2940
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002941struct wl_display *
2942display_get_display(struct display *display)
2943{
2944 return display->display;
2945}
2946
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002947struct output *
2948display_get_output(struct display *display)
2949{
2950 return container_of(display->output_list.next, struct output, link);
2951}
2952
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002953struct wl_compositor *
2954display_get_compositor(struct display *display)
2955{
2956 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002957}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002958
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002959uint32_t
2960display_get_serial(struct display *display)
2961{
2962 return display->serial;
2963}
2964
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002965EGLDisplay
2966display_get_egl_display(struct display *d)
2967{
2968 return d->dpy;
2969}
2970
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002971struct wl_data_source *
2972display_create_data_source(struct display *display)
2973{
2974 return wl_data_device_manager_create_data_source(display->data_device_manager);
2975}
2976
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002977EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002978display_get_argb_egl_config(struct display *d)
2979{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002980 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002981}
2982
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002983struct wl_shell *
2984display_get_shell(struct display *display)
2985{
2986 return display->shell;
2987}
2988
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002989int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002990display_acquire_window_surface(struct display *display,
2991 struct window *window,
2992 EGLContext ctx)
2993{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002994#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002995 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002996 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002997
2998 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002999 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003000 device = cairo_surface_get_device(window->cairo_surface);
3001 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003002 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003003
Benjamin Franzke0c991632011-09-27 21:57:31 +02003004 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003005 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003006 ctx = display->argb_ctx;
3007 else
3008 assert(0);
3009 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003010
3011 data = cairo_surface_get_user_data(window->cairo_surface,
3012 &surface_data_key);
3013
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003014 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003015 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003016 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3017 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003018
3019 return 0;
3020#else
3021 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003022#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003023}
3024
3025void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003026display_release_window_surface(struct display *display,
3027 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003028{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003029#ifdef HAVE_CAIRO_EGL
3030 cairo_device_t *device;
3031
3032 device = cairo_surface_get_device(window->cairo_surface);
3033 if (!device)
3034 return;
3035
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003036 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003037 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003038 cairo_device_release(device);
3039#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003040}
3041
3042void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003043display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003044{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003045 wl_list_insert(&display->deferred_list, &task->link);
3046}
3047
3048void
3049display_watch_fd(struct display *display,
3050 int fd, uint32_t events, struct task *task)
3051{
3052 struct epoll_event ep;
3053
3054 ep.events = events;
3055 ep.data.ptr = task;
3056 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3057}
3058
3059void
3060display_run(struct display *display)
3061{
3062 struct task *task;
3063 struct epoll_event ep[16];
3064 int i, count;
3065
Pekka Paalanen826d7952011-12-15 10:14:07 +02003066 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003067 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003068 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003069
3070 while (!wl_list_empty(&display->deferred_list)) {
3071 task = container_of(display->deferred_list.next,
3072 struct task, link);
3073 wl_list_remove(&task->link);
3074 task->run(task, 0);
3075 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003076
3077 if (!display->running)
3078 break;
3079
3080 wl_display_flush(display->display);
3081
3082 count = epoll_wait(display->epoll_fd,
3083 ep, ARRAY_LENGTH(ep), -1);
3084 for (i = 0; i < count; i++) {
3085 task = ep[i].data.ptr;
3086 task->run(task, ep[i].events);
3087 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003088 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003089}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003090
3091void
3092display_exit(struct display *display)
3093{
3094 display->running = 0;
3095}