blob: 5465c2dc94515931f1b7bb9d2d19d13ad48f323d [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øgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Rob Clark6396ed32012-03-11 19:48:41 -050044#ifdef USE_CAIRO_GLESV2
45#include <GLES2/gl2.h>
46#include <GLES2/gl2ext.h>
47#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040048#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050049#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <EGL/egl.h>
51#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040052
Kristian Høgsberg8def2642011-01-14 17:41:33 -050053#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050056
Daniel Stone9d4f0302012-02-15 16:33:21 +000057#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050058#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040059
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020061#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040062#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050063
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050065
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050066struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050067 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050068 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040069 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040070 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040071 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040072 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050073 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020074 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020075 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040076
77 int display_fd;
78 uint32_t mask;
79 struct task display_task;
80
81 int epoll_fd;
82 struct wl_list deferred_list;
83
Pekka Paalanen826d7952011-12-15 10:14:07 +020084 int running;
85
Kristian Høgsberg478d9262010-06-08 20:34:11 -040086 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040087 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050088 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040089 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050090 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040091 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040092 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040093
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050094 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
95 PFNEGLCREATEIMAGEKHRPROC create_image;
96 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020097
98 display_output_handler_t output_configure_handler;
99
100 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500101};
102
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400103enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400105 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500106 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400107 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500108 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400109 TYPE_CUSTOM
110};
111
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500112struct window {
113 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500114 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500115 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200116 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500117 struct wl_region *input_region;
118 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500119 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500120 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500121 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500122 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400123 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400124 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400125 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500126 int resize_scheduled;
127 struct task resize_task;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400128 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400129 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400130 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400131 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500132
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500133 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500134
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500135 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500136 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400137 window_data_handler_t data_handler;
138 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500139 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400140
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200141 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500142 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500143
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500144 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400145 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500146};
147
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500148struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500149 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500150 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400151 struct wl_list link;
152 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500153 widget_resize_handler_t resize_handler;
154 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500155 widget_enter_handler_t enter_handler;
156 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500157 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500158 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400159 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500160 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161};
162
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400163struct input {
164 struct display *display;
165 struct wl_input_device *input_device;
166 struct window *pointer_focus;
167 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400168 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400169 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200170 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400171 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400172
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500173 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500174 struct widget *grab;
175 uint32_t grab_button;
176
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400177 struct wl_data_device *data_device;
178 struct data_offer *drag_offer;
179 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400180};
181
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500182struct output {
183 struct display *display;
184 struct wl_output *output;
185 struct rectangle allocation;
186 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200187
188 display_output_handler_t destroy_handler;
189 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500190};
191
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500192struct frame {
193 struct widget *widget;
194 struct widget *child;
195 int margin;
196};
197
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500198struct menu {
199 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500200 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500201 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500202 const char **entries;
203 uint32_t time;
204 int current;
205 int count;
206 menu_func_t func;
207};
208
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400209enum {
210 POINTER_DEFAULT = 100,
211 POINTER_UNSET
212};
213
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500214enum window_location {
215 WINDOW_INTERIOR = 0,
216 WINDOW_RESIZING_TOP = 1,
217 WINDOW_RESIZING_BOTTOM = 2,
218 WINDOW_RESIZING_LEFT = 4,
219 WINDOW_RESIZING_TOP_LEFT = 5,
220 WINDOW_RESIZING_BOTTOM_LEFT = 6,
221 WINDOW_RESIZING_RIGHT = 8,
222 WINDOW_RESIZING_TOP_RIGHT = 9,
223 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
224 WINDOW_RESIZING_MASK = 15,
225 WINDOW_EXTERIOR = 16,
226 WINDOW_TITLEBAR = 17,
227 WINDOW_CLIENT_AREA = 18,
228};
229
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400230const char *option_xkb_layout = "us";
231const char *option_xkb_variant = "";
232const char *option_xkb_options = "";
233
234static const GOptionEntry xkb_option_entries[] = {
235 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
236 &option_xkb_layout, "XKB Layout" },
237 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
238 &option_xkb_variant, "XKB Variant" },
239 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
240 &option_xkb_options, "XKB Options" },
241 { NULL }
242};
243
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400244static const cairo_user_data_key_t surface_data_key;
245struct surface_data {
246 struct wl_buffer *buffer;
247};
248
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500249#define MULT(_d,c,a,t) \
250 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
251
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500252#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400253
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100254struct egl_window_surface_data {
255 struct display *display;
256 struct wl_surface *surface;
257 struct wl_egl_window *window;
258 EGLSurface surf;
259};
260
261static void
262egl_window_surface_data_destroy(void *p)
263{
264 struct egl_window_surface_data *data = p;
265 struct display *d = data->display;
266
267 eglDestroySurface(d->dpy, data->surf);
268 wl_egl_window_destroy(data->window);
269 data->surface = NULL;
270
271 free(p);
272}
273
274static cairo_surface_t *
275display_create_egl_window_surface(struct display *display,
276 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400277 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100278 struct rectangle *rectangle)
279{
280 cairo_surface_t *cairo_surface;
281 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400282 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200283 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100284
285 data = malloc(sizeof *data);
286 if (data == NULL)
287 return NULL;
288
289 data->display = display;
290 data->surface = surface;
291
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500292 config = display->argb_config;
293 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400294
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400295 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100296 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100298
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400299 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500300 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100301
Benjamin Franzke0c991632011-09-27 21:57:31 +0200302 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100303 data->surf,
304 rectangle->width,
305 rectangle->height);
306
307 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
308 data, egl_window_surface_data_destroy);
309
310 return cairo_surface;
311}
312
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500313struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200315 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316 EGLImageKHR image;
317 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800318 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320};
321
322static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400324{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500325 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800326 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327
Benjamin Franzke0c991632011-09-27 21:57:31 +0200328 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200330 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800331
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500332 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500334 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336}
337
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500338EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339display_get_image_for_egl_image_surface(struct display *display,
340 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500341{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500342 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500343
344 data = cairo_surface_get_user_data (surface, &surface_data_key);
345
346 return data->image;
347}
348
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500349static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400351 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500352 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400353{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500354 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400355 EGLDisplay dpy = display->dpy;
356 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200357 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400358
359 data = malloc(sizeof *data);
360 if (data == NULL)
361 return NULL;
362
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800363 data->display = display;
364
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400365 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400366 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500367 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000368 free(data);
369 return NULL;
370 }
371
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500372 data->device = display->argb_device;
373 content = CAIRO_CONTENT_COLOR_ALPHA;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500375 data->image = display->create_image(dpy, NULL,
376 EGL_NATIVE_PIXMAP_KHR,
377 (EGLClientBuffer) data->pixmap,
378 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500379 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500380 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500381 free(data);
382 return NULL;
383 }
384
385 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400386 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500387
Benjamin Franzke0c991632011-09-27 21:57:31 +0200388 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400389 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500391 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200392 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393
Benjamin Franzke0c991632011-09-27 21:57:31 +0200394 surface = cairo_gl_surface_create_for_texture(data->device,
395 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396 data->texture,
397 rectangle->width,
398 rectangle->height);
399
400 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500401 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402
403 return surface;
404}
405
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500406static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500407display_create_egl_image_surface_from_file(struct display *display,
408 const char *filename,
409 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410{
411 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400412 GdkPixbuf *pixbuf;
413 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400414 int stride, i;
415 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500416 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400417
418 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400420 FALSE, &error);
421 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400423
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400424 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
425 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500426 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400428 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400429
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400430
431 stride = gdk_pixbuf_get_rowstride(pixbuf);
432 pixels = gdk_pixbuf_get_pixels(pixbuf);
433
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400434 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400435 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400437 while (p < end) {
438 unsigned int t;
439
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400440 MULT(p[0], p[0], p[3], t);
441 MULT(p[1], p[1], p[3], t);
442 MULT(p[2], p[2], p[3], t);
443 p += 4;
444
445 }
446 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400447
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200448 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000449 if (surface == NULL) {
450 g_object_unref(pixbuf);
451 return NULL;
452 }
453
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800454 data = cairo_surface_get_user_data(surface, &surface_data_key);
455
Benjamin Franzke0c991632011-09-27 21:57:31 +0200456 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800457 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800458 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
459 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200460 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400461
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500462 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400463
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400464 return surface;
465}
466
467#endif
468
469struct wl_buffer *
470display_get_buffer_for_surface(struct display *display,
471 cairo_surface_t *surface)
472{
473 struct surface_data *data;
474
475 data = cairo_surface_get_user_data (surface, &surface_data_key);
476
477 return data->buffer;
478}
479
480struct shm_surface_data {
481 struct surface_data data;
482 void *map;
483 size_t length;
484};
485
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500486static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400487shm_surface_data_destroy(void *p)
488{
489 struct shm_surface_data *data = p;
490
491 wl_buffer_destroy(data->data.buffer);
492 munmap(data->map, data->length);
493}
494
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500495static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400496display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400497 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400498{
499 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400500 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400501 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800502 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400503 char filename[] = "/tmp/wayland-shm-XXXXXX";
504
505 data = malloc(sizeof *data);
506 if (data == NULL)
507 return NULL;
508
509 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
510 rectangle->width);
511 data->length = stride * rectangle->height;
512 fd = mkstemp(filename);
513 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000514 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400515 return NULL;
516 }
517 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000518 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400519 close(fd);
520 return NULL;
521 }
522
523 data->map = mmap(NULL, data->length,
524 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
525 unlink(filename);
526
527 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000528 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400529 close(fd);
530 return NULL;
531 }
532
533 surface = cairo_image_surface_create_for_data (data->map,
534 CAIRO_FORMAT_ARGB32,
535 rectangle->width,
536 rectangle->height,
537 stride);
538
539 cairo_surface_set_user_data (surface, &surface_data_key,
540 data, shm_surface_data_destroy);
541
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400542 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500543 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400544 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500545 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400546
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400547 data->data.buffer = wl_shm_create_buffer(display->shm,
548 fd,
549 rectangle->width,
550 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400551 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400552
553 close(fd);
554
555 return surface;
556}
557
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500558static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559display_create_shm_surface_from_file(struct display *display,
560 const char *filename,
561 struct rectangle *rect)
562{
563 cairo_surface_t *surface;
564 GdkPixbuf *pixbuf;
565 GError *error = NULL;
566 int stride, i;
567 unsigned char *pixels, *p, *end, *dest_data;
568 int dest_stride;
569 uint32_t *d;
570
571 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
572 rect->width, rect->height,
573 FALSE, &error);
574 if (error != NULL)
575 return NULL;
576
577 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
578 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500579 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400580 return NULL;
581 }
582
583 stride = gdk_pixbuf_get_rowstride(pixbuf);
584 pixels = gdk_pixbuf_get_pixels(pixbuf);
585
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400586 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000587 if (surface == NULL) {
588 g_object_unref(pixbuf);
589 return NULL;
590 }
591
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400592 dest_data = cairo_image_surface_get_data (surface);
593 dest_stride = cairo_image_surface_get_stride (surface);
594
595 for (i = 0; i < rect->height; i++) {
596 d = (uint32_t *) (dest_data + i * dest_stride);
597 p = pixels + i * stride;
598 end = p + rect->width * 4;
599 while (p < end) {
600 unsigned int t;
601 unsigned char a, r, g, b;
602
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400603 a = p[3];
604 MULT(r, p[0], a, t);
605 MULT(g, p[1], a, t);
606 MULT(b, p[2], a, t);
607 p += 4;
608 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
609 }
610 }
611
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500612 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400613
614 return surface;
615}
616
nobled7b87cb02011-02-01 18:51:47 +0000617static int
618check_size(struct rectangle *rect)
619{
620 if (rect->width && rect->height)
621 return 0;
622
623 fprintf(stderr, "tried to create surface of "
624 "width: %d, height: %d\n", rect->width, rect->height);
625 return -1;
626}
627
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628cairo_surface_t *
629display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100630 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400631 struct rectangle *rectangle,
632 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633{
nobled7b87cb02011-02-01 18:51:47 +0000634 if (check_size(rectangle) < 0)
635 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500636#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500637 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638 if (surface)
639 return display_create_egl_window_surface(display,
640 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400641 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642 rectangle);
643 else
644 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400645 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100646 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500647 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400648#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400649 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650}
651
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500652static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400653display_create_surface_from_file(struct display *display,
654 const char *filename,
655 struct rectangle *rectangle)
656{
nobled7b87cb02011-02-01 18:51:47 +0000657 if (check_size(rectangle) < 0)
658 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500659#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500660 if (display->dpy) {
661 return display_create_egl_image_surface_from_file(display,
662 filename,
663 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500664 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400665#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500666 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400667}
Yuval Fledel45568f62010-12-06 09:18:12 -0500668 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400669 const char *filename;
670 int hotspot_x, hotspot_y;
671} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500672 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
673 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
674 { DATADIR "/weston/bottom_side.png", 16, 20 },
675 { DATADIR "/weston/grabbing.png", 20, 17 },
676 { DATADIR "/weston/left_ptr.png", 10, 5 },
677 { DATADIR "/weston/left_side.png", 10, 20 },
678 { DATADIR "/weston/right_side.png", 30, 19 },
679 { DATADIR "/weston/top_left_corner.png", 8, 8 },
680 { DATADIR "/weston/top_right_corner.png", 26, 8 },
681 { DATADIR "/weston/top_side.png", 18, 8 },
682 { DATADIR "/weston/xterm.png", 15, 15 },
683 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400684};
685
686static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400687create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400688{
689 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400690 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400691 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400692
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400694 display->pointer_surfaces =
695 malloc(count * sizeof *display->pointer_surfaces);
696 rect.width = width;
697 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400698 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400699 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400700 display_create_surface_from_file(display,
701 pointer_images[i].filename,
702 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100703 if (!display->pointer_surfaces[i]) {
704 fprintf(stderr, "Error loading pointer image: %s\n",
705 pointer_images[i].filename);
706 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400707 }
708
709}
710
Pekka Paalanen325bb602011-12-19 10:31:45 +0200711static void
712destroy_pointer_surfaces(struct display *display)
713{
714 int i, count;
715
716 count = ARRAY_LENGTH(pointer_images);
717 for (i = 0; i < count; ++i) {
718 if (display->pointer_surfaces[i])
719 cairo_surface_destroy(display->pointer_surfaces[i]);
720 }
721 free(display->pointer_surfaces);
722}
723
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400724cairo_surface_t *
725display_get_pointer_surface(struct display *display, int pointer,
726 int *width, int *height,
727 int *hotspot_x, int *hotspot_y)
728{
729 cairo_surface_t *surface;
730
731 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500732#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400733 *width = cairo_gl_surface_get_width(surface);
734 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000735#else
736 *width = cairo_image_surface_get_width(surface);
737 *height = cairo_image_surface_get_height(surface);
738#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400739 *hotspot_x = pointer_images[pointer].hotspot_x;
740 *hotspot_y = pointer_images[pointer].hotspot_y;
741
742 return cairo_surface_reference(surface);
743}
744
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400745static void
746window_attach_surface(struct window *window);
747
748static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400749free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400750{
751 struct window *window = data;
752
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400753 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400754 cairo_surface_destroy(window->pending_surface);
755 window->pending_surface = NULL;
756 if (window->cairo_surface)
757 window_attach_surface(window);
758}
759
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400760static const struct wl_callback_listener free_surface_listener = {
761 free_surface
762};
763
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500764static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200765window_get_resize_dx_dy(struct window *window, int *x, int *y)
766{
767 if (window->resize_edges & WINDOW_RESIZING_LEFT)
768 *x = window->server_allocation.width - window->allocation.width;
769 else
770 *x = 0;
771
772 if (window->resize_edges & WINDOW_RESIZING_TOP)
773 *y = window->server_allocation.height -
774 window->allocation.height;
775 else
776 *y = 0;
777
778 window->resize_edges = 0;
779}
780
781static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500782window_attach_surface(struct window *window)
783{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400784 struct display *display = window->display;
785 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400786 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000787#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000789#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500790 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100791
792 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000793#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
795 data = cairo_surface_get_user_data(window->cairo_surface,
796 &surface_data_key);
797
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798 cairo_gl_surface_swapbuffers(window->cairo_surface);
799 wl_egl_window_get_attached_size(data->window,
800 &window->server_allocation.width,
801 &window->server_allocation.height);
802 break;
803 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000804#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100805 case WINDOW_BUFFER_TYPE_SHM:
806 if (window->pending_surface != NULL)
807 return;
808
809 window->pending_surface = window->cairo_surface;
810 window->cairo_surface = NULL;
811
812 buffer =
813 display_get_buffer_for_surface(display,
814 window->pending_surface);
815
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200816 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 wl_surface_attach(window->surface, buffer, x, y);
818 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400819 cb = wl_display_sync(display->display);
820 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822 default:
823 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500825
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500826 if (window->input_region) {
827 wl_surface_set_input_region(window->surface,
828 window->input_region);
829 wl_region_destroy(window->input_region);
830 window->input_region = NULL;
831 }
832
833 if (window->opaque_region) {
834 wl_surface_set_opaque_region(window->surface,
835 window->opaque_region);
836 wl_region_destroy(window->opaque_region);
837 window->opaque_region = NULL;
838 }
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500839
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500840 wl_surface_damage(window->surface, 0, 0,
841 window->allocation.width,
842 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500843}
844
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500845void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400846window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500847{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100848 if (window->cairo_surface) {
849 switch (window->buffer_type) {
850 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
851 case WINDOW_BUFFER_TYPE_SHM:
852 display_surface_damage(window->display,
853 window->cairo_surface,
854 0, 0,
855 window->allocation.width,
856 window->allocation.height);
857 break;
858 default:
859 break;
860 }
861 window_attach_surface(window);
862 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500863}
864
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400865void
866window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400867{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500868 cairo_surface_reference(surface);
869
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400870 if (window->cairo_surface != NULL)
871 cairo_surface_destroy(window->cairo_surface);
872
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500873 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400874}
875
Benjamin Franzke22d54812011-07-16 19:50:32 +0000876#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100877static void
878window_resize_cairo_window_surface(struct window *window)
879{
880 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200881 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100882
883 data = cairo_surface_get_user_data(window->cairo_surface,
884 &surface_data_key);
885
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200886 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100887 wl_egl_window_resize(data->window,
888 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200889 window->allocation.height,
890 x,y);
891
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100892 cairo_gl_surface_set_size(window->cairo_surface,
893 window->allocation.width,
894 window->allocation.height);
895}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000896#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100897
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400898struct display *
899window_get_display(struct window *window)
900{
901 return window->display;
902}
903
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400904void
905window_create_surface(struct window *window)
906{
907 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400908 uint32_t flags = 0;
909
910 if (!window->transparent)
911 flags = SURFACE_OPAQUE;
912
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400913 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500914#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100915 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
916 if (window->cairo_surface) {
917 window_resize_cairo_window_surface(window);
918 return;
919 }
920 surface = display_create_surface(window->display,
921 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400922 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100923 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500924 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400925 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100926 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400927 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400928 break;
929#endif
930 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400931 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400932 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400933 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800934 default:
935 surface = NULL;
936 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400937 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400938
939 window_set_surface(window, surface);
940 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400941}
942
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200943static void frame_destroy(struct frame *frame);
944
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400945void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500946window_destroy(struct window *window)
947{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200948 struct display *display = window->display;
949 struct input *input;
950
951 if (window->redraw_scheduled)
952 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500953 if (window->resize_scheduled)
954 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200955
956 wl_list_for_each(input, &display->input_list, link) {
957 if (input->pointer_focus == window)
958 input->pointer_focus = NULL;
959 if (input->keyboard_focus == window)
960 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500961 if (input->focus_widget &&
962 input->focus_widget->window == window)
963 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200964 }
965
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500966 if (window->input_region)
967 wl_region_destroy(window->input_region);
968 if (window->opaque_region)
969 wl_region_destroy(window->opaque_region);
970
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200971 if (window->frame)
972 frame_destroy(window->frame);
973
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200974 if (window->shell_surface)
975 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500976 wl_surface_destroy(window->surface);
977 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200978
979 if (window->cairo_surface != NULL)
980 cairo_surface_destroy(window->cairo_surface);
981 if (window->pending_surface != NULL)
982 cairo_surface_destroy(window->pending_surface);
983
984 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500985 free(window);
986}
987
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500988static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500989widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400990{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500991 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400992
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500993 wl_list_for_each(child, &widget->child_list, link) {
994 target = widget_find_widget(child, x, y);
995 if (target)
996 return target;
997 }
998
999 if (widget->allocation.x <= x &&
1000 x < widget->allocation.x + widget->allocation.width &&
1001 widget->allocation.y <= y &&
1002 y < widget->allocation.y + widget->allocation.height) {
1003 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001004 }
1005
1006 return NULL;
1007}
1008
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001009static struct widget *
1010widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001011{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001012 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001013
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001014 widget = malloc(sizeof *widget);
1015 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001016 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001017 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001018 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001019 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001020 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001021
1022 return widget;
1023}
1024
1025struct widget *
1026window_add_widget(struct window *window, void *data)
1027{
1028 window->widget = widget_create(window, data);
1029 wl_list_init(&window->widget->link);
1030
1031 return window->widget;
1032}
1033
1034struct widget *
1035widget_add_widget(struct widget *parent, void *data)
1036{
1037 struct widget *widget;
1038
1039 widget = widget_create(parent->window, data);
1040 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001042 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001043}
1044
1045void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001046widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001047{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001048 struct display *display = widget->window->display;
1049 struct input *input;
1050
1051 wl_list_for_each(input, &display->input_list, link) {
1052 if (input->focus_widget == widget)
1053 input->focus_widget = NULL;
1054 }
1055
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001056 wl_list_remove(&widget->link);
1057 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001058}
1059
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001061widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001062{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001063 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064}
1065
1066void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001067widget_set_size(struct widget *widget, int32_t width, int32_t height)
1068{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001069 widget->allocation.width = width;
1070 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001071}
1072
1073void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001074widget_set_allocation(struct widget *widget,
1075 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001076{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001077 widget->allocation.x = x;
1078 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001079 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001080}
1081
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001082void
1083widget_set_transparent(struct widget *widget, int transparent)
1084{
1085 widget->opaque = !transparent;
1086}
1087
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001088void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001089widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001090{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001091 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001092}
1093
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001094void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001095widget_set_resize_handler(struct widget *widget,
1096 widget_resize_handler_t handler)
1097{
1098 widget->resize_handler = handler;
1099}
1100
1101void
1102widget_set_redraw_handler(struct widget *widget,
1103 widget_redraw_handler_t handler)
1104{
1105 widget->redraw_handler = handler;
1106}
1107
1108void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001109widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001110{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001111 widget->enter_handler = handler;
1112}
1113
1114void
1115widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1116{
1117 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001118}
1119
1120void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001121widget_set_motion_handler(struct widget *widget,
1122 widget_motion_handler_t handler)
1123{
1124 widget->motion_handler = handler;
1125}
1126
1127void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001128widget_set_button_handler(struct widget *widget,
1129 widget_button_handler_t handler)
1130{
1131 widget->button_handler = handler;
1132}
1133
1134void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001135widget_schedule_redraw(struct widget *widget)
1136{
1137 window_schedule_redraw(widget->window);
1138}
1139
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001140cairo_surface_t *
1141window_get_surface(struct window *window)
1142{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001143 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001144}
1145
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001146struct wl_surface *
1147window_get_wl_surface(struct window *window)
1148{
1149 return window->surface;
1150}
1151
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001152struct wl_shell_surface *
1153window_get_wl_shell_surface(struct window *window)
1154{
1155 return window->shell_surface;
1156}
1157
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001158static void
1159frame_resize_handler(struct widget *widget,
1160 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001161{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001162 struct frame *frame = data;
1163 struct widget *child = frame->child;
1164 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001165 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001166 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001167 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001168
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001169 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001170 decoration_width = 20 + frame->margin * 2;
1171 decoration_height = 60 + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001172
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001173 allocation.x = 10 + frame->margin;
1174 allocation.y = 50 + frame->margin;
1175 allocation.width = width - decoration_width;
1176 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001177
1178 widget->window->input_region =
1179 wl_compositor_create_region(display->compositor);
1180 wl_region_add(widget->window->input_region,
1181 frame->margin, frame->margin,
1182 width - 2 * frame->margin,
1183 height - 2 * frame->margin);
1184
1185 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001186 } else {
1187 decoration_width = 0;
1188 decoration_height = 0;
1189
1190 allocation.x = 0;
1191 allocation.y = 0;
1192 allocation.width = width;
1193 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001194 opaque_margin = 0;
1195 }
1196
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001197 widget_set_allocation(child, allocation.x, allocation.y,
1198 allocation.width, allocation.height);
1199
1200 if (child->resize_handler)
1201 child->resize_handler(child,
1202 allocation.width,
1203 allocation.height,
1204 child->user_data);
1205
1206 widget_set_allocation(widget, 0, 0,
1207 child->allocation.width + decoration_width,
1208 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001209
1210 if (child->opaque) {
1211 widget->window->opaque_region =
1212 wl_compositor_create_region(display->compositor);
1213 wl_region_add(widget->window->opaque_region,
1214 opaque_margin, opaque_margin,
1215 widget->allocation.width - 2 * opaque_margin,
1216 widget->allocation.height - 2 * opaque_margin);
1217 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001218}
1219
1220static void
1221frame_redraw_handler(struct widget *widget, void *data)
1222{
1223 struct frame *frame = data;
1224 cairo_t *cr;
1225 cairo_text_extents_t extents;
1226 cairo_surface_t *source;
1227 int width, height, shadow_dx = 3, shadow_dy = 3;
1228 struct window *window = widget->window;
1229
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001230 if (window->type == TYPE_FULLSCREEN)
1231 return;
1232
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001233 width = widget->allocation.width;
1234 height = widget->allocation.height;
1235
1236 cr = cairo_create(window->cairo_surface);
1237
1238 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1239 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1240 cairo_paint(cr);
1241
1242 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1243 tile_mask(cr, window->display->shadow,
1244 shadow_dx, shadow_dy, width, height,
1245 frame->margin + 10 - shadow_dx,
1246 frame->margin + 10 - shadow_dy);
1247
1248 if (window->keyboard_device)
1249 source = window->display->active_frame;
1250 else
1251 source = window->display->inactive_frame;
1252
1253 tile_source(cr, source, 0, 0, width, height,
1254 frame->margin + 10, frame->margin + 50);
1255
1256 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1257 cairo_set_font_size(cr, 14);
1258 cairo_text_extents(cr, window->title, &extents);
1259 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1260 if (window->keyboard_device)
1261 cairo_set_source_rgb(cr, 0, 0, 0);
1262 else
1263 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1264 cairo_show_text(cr, window->title);
1265
1266 cairo_destroy(cr);
1267}
1268
1269static int
1270frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1271{
1272 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001273 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274 const int grip_size = 8;
1275
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001276 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001277 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001278 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001279 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001280 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001281 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001282 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001283 hlocation = WINDOW_RESIZING_RIGHT;
1284 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001285 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001286
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001287 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001288 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001289 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001290 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001291 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001292 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001293 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001294 vlocation = WINDOW_RESIZING_BOTTOM;
1295 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001296 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001297
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001298 location = vlocation | hlocation;
1299 if (location & WINDOW_EXTERIOR)
1300 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001301 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001302 location = WINDOW_TITLEBAR;
1303 else if (location == WINDOW_INTERIOR)
1304 location = WINDOW_CLIENT_AREA;
1305
1306 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001307}
1308
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001309static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001310frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001311{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001312 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001313
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001314 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001315 switch (location) {
1316 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001317 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001318 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001319 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001320 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001321 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001322 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001323 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001324 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001325 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001326 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001327 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001328 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001329 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001330 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001331 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001332 case WINDOW_EXTERIOR:
1333 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001334 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001335 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001336 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001337}
1338
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001339static void
1340frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001341{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001342 switch (index) {
1343 case 0: /* close */
1344 if (window->close_handler)
1345 window->close_handler(window->parent,
1346 window->user_data);
1347 else
1348 display_exit(window->display);
1349 break;
1350 case 1: /* fullscreen */
1351 /* we don't have a way to get out of fullscreen for now */
1352 window_set_fullscreen(window, 1);
1353 break;
1354 case 2: /* rotate */
1355 case 3: /* scale */
1356 break;
1357 }
1358}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001359
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001360void
1361window_show_frame_menu(struct window *window,
1362 struct input *input, uint32_t time)
1363{
1364 int32_t x, y;
1365
1366 static const char *entries[] = {
1367 "Close", "Fullscreen", "Rotate", "Scale"
1368 };
1369
1370 input_get_position(input, &x, &y);
1371 window_show_menu(window->display, input, time, window,
1372 x - 10, y - 10, frame_menu_func, entries, 4);
1373}
1374
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001375static int
1376frame_enter_handler(struct widget *widget,
1377 struct input *input, uint32_t time,
1378 int32_t x, int32_t y, void *data)
1379{
1380 return frame_get_pointer_image_for_location(data, input);
1381}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001382
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001383static int
1384frame_motion_handler(struct widget *widget,
1385 struct input *input, uint32_t time,
1386 int32_t x, int32_t y, void *data)
1387{
1388 return frame_get_pointer_image_for_location(data, input);
1389}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001390
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001391static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001392frame_button_handler(struct widget *widget,
1393 struct input *input, uint32_t time,
1394 int button, int state, void *data)
1395
1396{
1397 struct frame *frame = data;
1398 struct window *window = widget->window;
1399 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001400 location = frame_get_pointer_location(frame, input->sx, input->sy);
1401
1402 if (window->display->shell && button == BTN_LEFT && state == 1) {
1403 switch (location) {
1404 case WINDOW_TITLEBAR:
1405 if (!window->shell_surface)
1406 break;
1407 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001408 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001409 wl_shell_surface_move(window->shell_surface,
1410 input_get_input_device(input),
1411 time);
1412 break;
1413 case WINDOW_RESIZING_TOP:
1414 case WINDOW_RESIZING_BOTTOM:
1415 case WINDOW_RESIZING_LEFT:
1416 case WINDOW_RESIZING_RIGHT:
1417 case WINDOW_RESIZING_TOP_LEFT:
1418 case WINDOW_RESIZING_TOP_RIGHT:
1419 case WINDOW_RESIZING_BOTTOM_LEFT:
1420 case WINDOW_RESIZING_BOTTOM_RIGHT:
1421 if (!window->shell_surface)
1422 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001423 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001424 wl_shell_surface_resize(window->shell_surface,
1425 input_get_input_device(input),
1426 time, location);
1427 break;
1428 }
1429 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001430 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001431 }
1432}
1433
1434struct widget *
1435frame_create(struct window *window, void *data)
1436{
1437 struct frame *frame;
1438
1439 frame = malloc(sizeof *frame);
1440 memset(frame, 0, sizeof *frame);
1441
1442 frame->widget = window_add_widget(window, frame);
1443 frame->child = widget_add_widget(frame->widget, data);
1444 frame->margin = 16;
1445
1446 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1447 widget_set_resize_handler(frame->widget, frame_resize_handler);
1448 widget_set_enter_handler(frame->widget, frame_enter_handler);
1449 widget_set_motion_handler(frame->widget, frame_motion_handler);
1450 widget_set_button_handler(frame->widget, frame_button_handler);
1451
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001452 window->frame = frame;
1453
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001454 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001455}
1456
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001457static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001458frame_destroy(struct frame *frame)
1459{
1460 /* frame->child must be destroyed by the application */
1461 widget_destroy(frame->widget);
1462 free(frame);
1463}
1464
1465static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001466input_set_focus_widget(struct input *input, struct widget *focus,
1467 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001468{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001469 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001470 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001471
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001472 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001473 return;
1474
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001475 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001476 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001477 widget = old;
1478 if (input->grab)
1479 widget = input->grab;
1480 if (widget->leave_handler)
1481 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001482 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001483 }
1484
1485 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001486 widget = focus;
1487 if (input->grab)
1488 widget = input->grab;
1489 if (widget->enter_handler)
1490 pointer = widget->enter_handler(focus, input, time,
1491 x, y,
1492 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001493 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001494
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001495 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001496 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001497}
1498
1499static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001500input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001501 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001502{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001503 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001504 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001505 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001506 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001507
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001508 input->sx = sx;
1509 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001510
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001511 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001512 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001513 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001514 }
1515
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001516 if (input->grab)
1517 widget = input->grab;
1518 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001519 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001520 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001521 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001522 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001523 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001524
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001525 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001526}
1527
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001528void
1529input_grab(struct input *input, struct widget *widget, uint32_t button)
1530{
1531 input->grab = widget;
1532 input->grab_button = button;
1533}
1534
1535void
1536input_ungrab(struct input *input, uint32_t time)
1537{
1538 struct widget *widget;
1539
1540 input->grab = NULL;
1541 if (input->pointer_focus) {
1542 widget = widget_find_widget(input->pointer_focus->widget,
1543 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001544 input_set_focus_widget(input, widget,
1545 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001546 }
1547}
1548
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001549static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001550input_handle_button(void *data,
1551 struct wl_input_device *input_device,
1552 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001553{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001554 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001555 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001556
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001557 if (input->focus_widget && input->grab == NULL && state)
1558 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001559
Neil Roberts6b28aad2012-01-23 19:11:18 +00001560 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001561 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001562 (*widget->button_handler)(widget,
1563 input, time,
1564 button, state,
1565 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001566
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001567 if (input->grab && input->grab_button == button && !state)
1568 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001569}
1570
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001571static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001572input_handle_key(void *data, struct wl_input_device *input_device,
1573 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001574{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001575 struct input *input = data;
1576 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001577 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001578 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001579
Daniel Stone0d5a5092012-02-16 12:48:00 +00001580 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001581 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001582 return;
1583
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001584 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001585 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001586 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1587 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001588
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001589 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1590
1591 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001592 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001593 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001594 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001595
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001596 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1597 if (state)
1598 window_set_maximized(window,
1599 window->type != TYPE_MAXIMIZED);
1600 } else if (window->key_handler) {
1601 (*window->key_handler)(window, input, time, key,
1602 sym, state, window->user_data);
1603 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001604}
1605
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001606static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001607input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001608{
1609 struct window *window = input->pointer_focus;
1610
1611 if (!window)
1612 return;
1613
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001614 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001615
Pekka Paalanene1207c72011-12-16 12:02:09 +02001616 input->pointer_focus = NULL;
1617 input->current_pointer_image = POINTER_UNSET;
1618}
1619
1620static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001621input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001622 struct wl_input_device *input_device,
1623 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001624 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001625{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001626 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001627 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001628 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001629
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001630 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001631 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001632
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001633 input->sx = sx;
1634 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001635
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001636 widget = widget_find_widget(window->widget, sx, sy);
1637 input_set_focus_widget(input, widget, time, sx, sy);
1638}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001639
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001640static void
1641input_handle_pointer_leave(void *data,
1642 struct wl_input_device *input_device,
1643 uint32_t time, struct wl_surface *surface)
1644{
1645 struct input *input = data;
1646
1647 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001648}
1649
1650static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001651input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001652{
1653 struct window *window = input->keyboard_focus;
1654
1655 if (!window)
1656 return;
1657
1658 window->keyboard_device = NULL;
1659 if (window->keyboard_focus_handler)
1660 (*window->keyboard_focus_handler)(window, NULL,
1661 window->user_data);
1662
1663 input->keyboard_focus = NULL;
1664}
1665
1666static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001667input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001668 struct wl_input_device *input_device,
1669 uint32_t time,
1670 struct wl_surface *surface,
1671 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001672{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001673 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001674 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001675 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001676 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001677
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001678 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001679
1680 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001681 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001682 for (k = keys->data; k < end; k++)
1683 input->modifiers |= d->xkb->map->modmap[*k];
1684
1685 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001686 window->keyboard_device = input;
1687 if (window->keyboard_focus_handler)
1688 (*window->keyboard_focus_handler)(window,
1689 window->keyboard_device,
1690 window->user_data);
1691}
1692
1693static void
1694input_handle_keyboard_leave(void *data,
1695 struct wl_input_device *input_device,
1696 uint32_t time,
1697 struct wl_surface *surface)
1698{
1699 struct input *input = data;
1700
1701 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001702}
1703
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001704static void
1705input_handle_touch_down(void *data,
1706 struct wl_input_device *wl_input_device,
1707 uint32_t time, struct wl_surface *surface,
1708 int32_t id, int32_t x, int32_t y)
1709{
1710}
1711
1712static void
1713input_handle_touch_up(void *data,
1714 struct wl_input_device *wl_input_device,
1715 uint32_t time, int32_t id)
1716{
1717}
1718
1719static void
1720input_handle_touch_motion(void *data,
1721 struct wl_input_device *wl_input_device,
1722 uint32_t time, int32_t id, int32_t x, int32_t y)
1723{
1724}
1725
1726static void
1727input_handle_touch_frame(void *data,
1728 struct wl_input_device *wl_input_device)
1729{
1730}
1731
1732static void
1733input_handle_touch_cancel(void *data,
1734 struct wl_input_device *wl_input_device)
1735{
1736}
1737
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001738static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001739 input_handle_motion,
1740 input_handle_button,
1741 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001742 input_handle_pointer_enter,
1743 input_handle_pointer_leave,
1744 input_handle_keyboard_enter,
1745 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001746 input_handle_touch_down,
1747 input_handle_touch_up,
1748 input_handle_touch_motion,
1749 input_handle_touch_frame,
1750 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001751};
1752
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001753void
1754input_get_position(struct input *input, int32_t *x, int32_t *y)
1755{
1756 *x = input->sx;
1757 *y = input->sy;
1758}
1759
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001760struct wl_input_device *
1761input_get_input_device(struct input *input)
1762{
1763 return input->input_device;
1764}
1765
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001766uint32_t
1767input_get_modifiers(struct input *input)
1768{
1769 return input->modifiers;
1770}
1771
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001772struct widget *
1773input_get_focus_widget(struct input *input)
1774{
1775 return input->focus_widget;
1776}
1777
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001778struct data_offer {
1779 struct wl_data_offer *offer;
1780 struct input *input;
1781 struct wl_array types;
1782 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001783
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001784 struct task io_task;
1785 int fd;
1786 data_func_t func;
1787 int32_t x, y;
1788 void *user_data;
1789};
1790
1791static void
1792data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1793{
1794 struct data_offer *offer = data;
1795 char **p;
1796
1797 p = wl_array_add(&offer->types, sizeof *p);
1798 *p = strdup(type);
1799}
1800
1801static const struct wl_data_offer_listener data_offer_listener = {
1802 data_offer_offer,
1803};
1804
1805static void
1806data_offer_destroy(struct data_offer *offer)
1807{
1808 char **p;
1809
1810 offer->refcount--;
1811 if (offer->refcount == 0) {
1812 wl_data_offer_destroy(offer->offer);
1813 for (p = offer->types.data; *p; p++)
1814 free(*p);
1815 wl_array_release(&offer->types);
1816 free(offer);
1817 }
1818}
1819
1820static void
1821data_device_data_offer(void *data,
1822 struct wl_data_device *data_device, uint32_t id)
1823{
1824 struct data_offer *offer;
1825
1826 offer = malloc(sizeof *offer);
1827
1828 wl_array_init(&offer->types);
1829 offer->refcount = 1;
1830 offer->input = data;
1831
1832 /* FIXME: Generate typesafe wrappers for this */
1833 offer->offer = (struct wl_data_offer *)
1834 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1835 id, &wl_data_offer_interface);
1836
1837 wl_data_offer_add_listener(offer->offer,
1838 &data_offer_listener, offer);
1839}
1840
1841static void
1842data_device_enter(void *data, struct wl_data_device *data_device,
1843 uint32_t time, struct wl_surface *surface,
1844 int32_t x, int32_t y, struct wl_data_offer *offer)
1845{
1846 struct input *input = data;
1847 struct window *window;
1848 char **p;
1849
1850 input->drag_offer = wl_data_offer_get_user_data(offer);
1851 window = wl_surface_get_user_data(surface);
1852 input->pointer_focus = window;
1853
1854 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1855 *p = NULL;
1856
1857 window = input->pointer_focus;
1858 if (window->data_handler)
1859 window->data_handler(window, input, time, x, y,
1860 input->drag_offer->types.data,
1861 window->user_data);
1862}
1863
1864static void
1865data_device_leave(void *data, struct wl_data_device *data_device)
1866{
1867 struct input *input = data;
1868
1869 data_offer_destroy(input->drag_offer);
1870 input->drag_offer = NULL;
1871}
1872
1873static void
1874data_device_motion(void *data, struct wl_data_device *data_device,
1875 uint32_t time, int32_t x, int32_t y)
1876{
1877 struct input *input = data;
1878 struct window *window = input->pointer_focus;
1879
1880 input->sx = x;
1881 input->sy = y;
1882
1883 if (window->data_handler)
1884 window->data_handler(window, input, time, x, y,
1885 input->drag_offer->types.data,
1886 window->user_data);
1887}
1888
1889static void
1890data_device_drop(void *data, struct wl_data_device *data_device)
1891{
1892 struct input *input = data;
1893 struct window *window = input->pointer_focus;
1894
1895 if (window->drop_handler)
1896 window->drop_handler(window, input,
1897 input->sx, input->sy, window->user_data);
1898}
1899
1900static void
1901data_device_selection(void *data,
1902 struct wl_data_device *wl_data_device,
1903 struct wl_data_offer *offer)
1904{
1905 struct input *input = data;
1906 char **p;
1907
1908 if (input->selection_offer)
1909 data_offer_destroy(input->selection_offer);
1910
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001911 if (offer) {
1912 input->selection_offer = wl_data_offer_get_user_data(offer);
1913 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1914 *p = NULL;
1915 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001916}
1917
1918static const struct wl_data_device_listener data_device_listener = {
1919 data_device_data_offer,
1920 data_device_enter,
1921 data_device_leave,
1922 data_device_motion,
1923 data_device_drop,
1924 data_device_selection
1925};
1926
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001927void
1928input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1929{
1930 struct display *display = input->display;
1931 struct wl_buffer *buffer;
1932 cairo_surface_t *surface;
1933
1934 if (pointer == input->current_pointer_image)
1935 return;
1936
1937 input->current_pointer_image = pointer;
1938 surface = display->pointer_surfaces[pointer];
1939
1940 if (!surface)
1941 return;
1942
1943 buffer = display_get_buffer_for_surface(display, surface);
1944 wl_input_device_attach(input->input_device, time, buffer,
1945 pointer_images[pointer].hotspot_x,
1946 pointer_images[pointer].hotspot_y);
1947}
1948
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001949struct wl_data_device *
1950input_get_data_device(struct input *input)
1951{
1952 return input->data_device;
1953}
1954
1955void
1956input_set_selection(struct input *input,
1957 struct wl_data_source *source, uint32_t time)
1958{
1959 wl_data_device_set_selection(input->data_device, source, time);
1960}
1961
1962void
1963input_accept(struct input *input, uint32_t time, const char *type)
1964{
1965 wl_data_offer_accept(input->drag_offer->offer, time, type);
1966}
1967
1968static void
1969offer_io_func(struct task *task, uint32_t events)
1970{
1971 struct data_offer *offer =
1972 container_of(task, struct data_offer, io_task);
1973 unsigned int len;
1974 char buffer[4096];
1975
1976 len = read(offer->fd, buffer, sizeof buffer);
1977 offer->func(buffer, len,
1978 offer->x, offer->y, offer->user_data);
1979
1980 if (len == 0) {
1981 close(offer->fd);
1982 data_offer_destroy(offer);
1983 }
1984}
1985
1986static void
1987data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1988 data_func_t func, void *user_data)
1989{
1990 int p[2];
1991
1992 pipe2(p, O_CLOEXEC);
1993 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1994 close(p[1]);
1995
1996 offer->io_task.run = offer_io_func;
1997 offer->fd = p[0];
1998 offer->func = func;
1999 offer->refcount++;
2000 offer->user_data = user_data;
2001
2002 display_watch_fd(offer->input->display,
2003 offer->fd, EPOLLIN, &offer->io_task);
2004}
2005
2006void
2007input_receive_drag_data(struct input *input, const char *mime_type,
2008 data_func_t func, void *data)
2009{
2010 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2011 input->drag_offer->x = input->sx;
2012 input->drag_offer->y = input->sy;
2013}
2014
2015int
2016input_receive_selection_data(struct input *input, const char *mime_type,
2017 data_func_t func, void *data)
2018{
2019 char **p;
2020
2021 if (input->selection_offer == NULL)
2022 return -1;
2023
2024 for (p = input->selection_offer->types.data; *p; p++)
2025 if (strcmp(mime_type, *p) == 0)
2026 break;
2027
2028 if (*p == NULL)
2029 return -1;
2030
2031 data_offer_receive_data(input->selection_offer,
2032 mime_type, func, data);
2033 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002034}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002035
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002036int
2037input_receive_selection_data_to_fd(struct input *input,
2038 const char *mime_type, int fd)
2039{
2040 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2041
2042 return 0;
2043}
2044
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002045void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002046window_move(struct window *window, struct input *input, uint32_t time)
2047{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002048 if (!window->shell_surface)
2049 return;
2050
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002051 wl_shell_surface_move(window->shell_surface,
2052 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002053}
2054
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002055static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002056idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002057{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002058 struct window *window =
2059 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002060 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002061
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002062 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002063 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002064 widget_set_allocation(widget,
2065 window->pending_allocation.x,
2066 window->pending_allocation.y,
2067 window->pending_allocation.width,
2068 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002069
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002070 if (window->input_region) {
2071 wl_region_destroy(window->input_region);
2072 window->input_region = NULL;
2073 }
2074
2075 if (window->opaque_region) {
2076 wl_region_destroy(window->opaque_region);
2077 window->opaque_region = NULL;
2078 }
2079
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002080 if (widget->resize_handler)
2081 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002082 widget->allocation.width,
2083 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002084 widget->user_data);
2085
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002086 if (window->allocation.width != widget->allocation.width ||
2087 window->allocation.height != widget->allocation.height) {
2088 window->allocation = widget->allocation;
2089 window_schedule_redraw(window);
2090 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002091}
2092
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002093void
2094window_schedule_resize(struct window *window, int width, int height)
2095{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002096 window->pending_allocation.x = 0;
2097 window->pending_allocation.y = 0;
2098 window->pending_allocation.width = width;
2099 window->pending_allocation.height = height;
2100
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002101 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002102 window->resize_task.run = idle_resize;
2103 display_defer(window->display, &window->resize_task);
2104 window->resize_scheduled = 1;
2105 }
2106}
2107
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002108void
2109widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2110{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002111 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002112}
2113
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002114static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002115handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002116 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002117 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002118{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002119 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002120
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002121 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002122 return;
2123
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002124 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002125 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002126}
2127
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002128static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002129menu_destroy(struct menu *menu)
2130{
2131 widget_destroy(menu->widget);
2132 window_destroy(menu->window);
2133 free(menu);
2134}
2135
2136static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002137handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2138{
2139 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002140 struct menu *menu = window->widget->user_data;
2141
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002142 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002143 * device. Or just use wl_callback. And this really needs to
2144 * be a window vfunc that the menu can set. And we need the
2145 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002146
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002147 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002148 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002149 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002150}
2151
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002152static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002153 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002154 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002155};
2156
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002157void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002158window_get_allocation(struct window *window,
2159 struct rectangle *allocation)
2160{
2161 *allocation = window->allocation;
2162}
2163
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002164static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002165widget_redraw(struct widget *widget)
2166{
2167 struct widget *child;
2168
2169 if (widget->redraw_handler)
2170 widget->redraw_handler(widget, widget->user_data);
2171 wl_list_for_each(child, &widget->child_list, link)
2172 widget_redraw(child);
2173}
2174
2175static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002176idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002177{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002178 struct window *window =
2179 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002180
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002181 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002182 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002183 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002184 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002185}
2186
2187void
2188window_schedule_redraw(struct window *window)
2189{
2190 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002191 window->redraw_task.run = idle_redraw;
2192 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002193 window->redraw_scheduled = 1;
2194 }
2195}
2196
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002197void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002198window_set_custom(struct window *window)
2199{
2200 window->type = TYPE_CUSTOM;
2201}
2202
2203void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002204window_set_fullscreen(struct window *window, int fullscreen)
2205{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002206 if (!window->display->shell)
2207 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002208
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002209 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002210 return;
2211
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002212 if (fullscreen) {
2213 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002214 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002215 wl_shell_surface_set_fullscreen(window->shell_surface,
2216 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2217 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002218 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002219 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002220 wl_shell_surface_set_toplevel(window->shell_surface);
2221 window_schedule_resize(window,
2222 window->saved_allocation.width,
2223 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002224 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002225}
2226
2227void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002228window_set_maximized(struct window *window, int maximized)
2229{
2230 if (!window->display->shell)
2231 return;
2232
2233 if ((window->type == TYPE_MAXIMIZED) == maximized)
2234 return;
2235
2236 if (window->type == TYPE_TOPLEVEL) {
2237 window->saved_allocation = window->allocation;
2238 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2239 window->type = TYPE_MAXIMIZED;
2240 } else {
2241 wl_shell_surface_set_toplevel(window->shell_surface);
2242 window->type = TYPE_TOPLEVEL;
2243 window_schedule_resize(window,
2244 window->saved_allocation.width,
2245 window->saved_allocation.height);
2246 }
2247}
2248
2249void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002250window_set_user_data(struct window *window, void *data)
2251{
2252 window->user_data = data;
2253}
2254
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002255void *
2256window_get_user_data(struct window *window)
2257{
2258 return window->user_data;
2259}
2260
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002261void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002262window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002263 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002264{
2265 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002266}
2267
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002268void
2269window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002270 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002271{
2272 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002273}
2274
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002275void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002276window_set_data_handler(struct window *window, window_data_handler_t handler)
2277{
2278 window->data_handler = handler;
2279}
2280
2281void
2282window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2283{
2284 window->drop_handler = handler;
2285}
2286
2287void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002288window_set_close_handler(struct window *window,
2289 window_close_handler_t handler)
2290{
2291 window->close_handler = handler;
2292}
2293
2294void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002295window_set_title(struct window *window, const char *title)
2296{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002297 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002298 window->title = strdup(title);
2299}
2300
2301const char *
2302window_get_title(struct window *window)
2303{
2304 return window->title;
2305}
2306
2307void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002308display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2309 int32_t x, int32_t y, int32_t width, int32_t height)
2310{
2311 struct wl_buffer *buffer;
2312
2313 buffer = display_get_buffer_for_surface(display, cairo_surface);
2314
2315 wl_buffer_damage(buffer, x, y, width, height);
2316}
2317
2318void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002319window_damage(struct window *window, int32_t x, int32_t y,
2320 int32_t width, int32_t height)
2321{
2322 wl_surface_damage(window->surface, x, y, width, height);
2323}
2324
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002325static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002326window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002327{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002328 struct window *window;
2329
2330 window = malloc(sizeof *window);
2331 if (window == NULL)
2332 return NULL;
2333
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002334 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002335 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002336 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002337 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002338 if (display->shell) {
2339 window->shell_surface =
2340 wl_shell_get_shell_surface(display->shell,
2341 window->surface);
2342 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002343 window->allocation.x = 0;
2344 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002345 window->allocation.width = 0;
2346 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002347 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002348 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002349 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002350 window->input_region = NULL;
2351 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002352
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002353 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002354#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002355 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2356 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002357#else
2358 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2359#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002360 else
2361 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002362
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002363 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002364 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002365
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002366 if (window->shell_surface) {
2367 wl_shell_surface_set_user_data(window->shell_surface, window);
2368 wl_shell_surface_add_listener(window->shell_surface,
2369 &shell_surface_listener, window);
2370 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002371
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002372 return window;
2373}
2374
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002375struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002376window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002377{
2378 struct window *window;
2379
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002380 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002381 if (!window)
2382 return NULL;
2383
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002384 window->type = TYPE_TOPLEVEL;
2385 if (display->shell)
2386 wl_shell_surface_set_toplevel(window->shell_surface);
2387
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002388 return window;
2389}
2390
2391struct window *
2392window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002393 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002394{
2395 struct window *window;
2396
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002397 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002398 if (!window)
2399 return NULL;
2400
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002401 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002402 window->x = x;
2403 window->y = y;
2404
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002405 if (display->shell)
2406 wl_shell_surface_set_transient(window->shell_surface,
2407 window->parent->shell_surface,
2408 window->x, window->y, 0);
2409
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002410 return window;
2411}
2412
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002413static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002414menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002415{
2416 int next;
2417
2418 next = (sy - 8) / 20;
2419 if (menu->current != next) {
2420 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002421 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002422 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002423}
2424
2425static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002426menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002427 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002428 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002429{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002430 struct menu *menu = data;
2431
2432 if (widget == menu->widget)
2433 menu_set_item(data, y);
2434
2435 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002436}
2437
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002438static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002439menu_enter_handler(struct widget *widget,
2440 struct input *input, uint32_t time,
2441 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002442{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002443 struct menu *menu = data;
2444
2445 if (widget == menu->widget)
2446 menu_set_item(data, y);
2447
2448 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002449}
2450
2451static void
2452menu_leave_handler(struct widget *widget, struct input *input, void *data)
2453{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002454 struct menu *menu = data;
2455
2456 if (widget == menu->widget)
2457 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002458}
2459
2460static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002461menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002462 struct input *input, uint32_t time,
2463 int button, int state, void *data)
2464
2465{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002466 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002467
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002468 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002469 /* Either relase after press-drag-release or
2470 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002471 menu->func(menu->window->parent,
2472 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002473 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002474 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002475 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002476}
2477
2478static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002479menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002480{
2481 cairo_t *cr;
2482 const int32_t r = 3, margin = 3;
2483 struct menu *menu = data;
2484 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002485 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002486
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002487 cr = cairo_create(window->cairo_surface);
2488 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2489 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2490 cairo_paint(cr);
2491
2492 width = window->allocation.width;
2493 height = window->allocation.height;
2494 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002495
2496 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002497 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2498 cairo_fill(cr);
2499
2500 for (i = 0; i < menu->count; i++) {
2501 if (i == menu->current) {
2502 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2503 cairo_rectangle(cr, margin, i * 20 + margin,
2504 width - 2 * margin, 20);
2505 cairo_fill(cr);
2506 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2507 cairo_move_to(cr, 10, i * 20 + 16);
2508 cairo_show_text(cr, menu->entries[i]);
2509 } else {
2510 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2511 cairo_move_to(cr, 10, i * 20 + 16);
2512 cairo_show_text(cr, menu->entries[i]);
2513 }
2514 }
2515
2516 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002517}
2518
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002519void
2520window_show_menu(struct display *display,
2521 struct input *input, uint32_t time, struct window *parent,
2522 int32_t x, int32_t y,
2523 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002524{
2525 struct window *window;
2526 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002527 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002528
2529 menu = malloc(sizeof *menu);
2530 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002531 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002532
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002533 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002534 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002535 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002536
2537 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002538 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002539 menu->entries = entries;
2540 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002541 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002542 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002543 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002544 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002545 window->type = TYPE_MENU;
2546 window->x = x;
2547 window->y = y;
2548
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002549 wl_shell_surface_set_popup(window->shell_surface,
2550 input->input_device, time,
2551 window->parent->shell_surface,
2552 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002553
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002554 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002555 widget_set_enter_handler(menu->widget, menu_enter_handler);
2556 widget_set_leave_handler(menu->widget, menu_leave_handler);
2557 widget_set_motion_handler(menu->widget, menu_motion_handler);
2558 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002559
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002560 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002561 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002562}
2563
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002564void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002565window_set_buffer_type(struct window *window, enum window_buffer_type type)
2566{
2567 window->buffer_type = type;
2568}
2569
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002570
2571static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002572display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002573 struct wl_output *wl_output,
2574 int x, int y,
2575 int physical_width,
2576 int physical_height,
2577 int subpixel,
2578 const char *make,
2579 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002580{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002581 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002582
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002583 output->allocation.x = x;
2584 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002585}
2586
2587static void
2588display_handle_mode(void *data,
2589 struct wl_output *wl_output,
2590 uint32_t flags,
2591 int width,
2592 int height,
2593 int refresh)
2594{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002595 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002596 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002597
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002598 if (flags & WL_OUTPUT_MODE_CURRENT) {
2599 output->allocation.width = width;
2600 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002601 if (display->output_configure_handler)
2602 (*display->output_configure_handler)(
2603 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002604 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002605}
2606
2607static const struct wl_output_listener output_listener = {
2608 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002609 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002610};
2611
2612static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002613display_add_output(struct display *d, uint32_t id)
2614{
2615 struct output *output;
2616
2617 output = malloc(sizeof *output);
2618 if (output == NULL)
2619 return;
2620
2621 memset(output, 0, sizeof *output);
2622 output->display = d;
2623 output->output =
2624 wl_display_bind(d->display, id, &wl_output_interface);
2625 wl_list_insert(d->output_list.prev, &output->link);
2626
2627 wl_output_add_listener(output->output, &output_listener, output);
2628}
2629
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002630static void
2631output_destroy(struct output *output)
2632{
2633 if (output->destroy_handler)
2634 (*output->destroy_handler)(output, output->user_data);
2635
2636 wl_output_destroy(output->output);
2637 wl_list_remove(&output->link);
2638 free(output);
2639}
2640
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002641void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002642display_set_output_configure_handler(struct display *display,
2643 display_output_handler_t handler)
2644{
2645 struct output *output;
2646
2647 display->output_configure_handler = handler;
2648 if (!handler)
2649 return;
2650
2651 wl_list_for_each(output, &display->output_list, link)
2652 (*display->output_configure_handler)(output,
2653 display->user_data);
2654}
2655
2656void
2657output_set_user_data(struct output *output, void *data)
2658{
2659 output->user_data = data;
2660}
2661
2662void *
2663output_get_user_data(struct output *output)
2664{
2665 return output->user_data;
2666}
2667
2668void
2669output_set_destroy_handler(struct output *output,
2670 display_output_handler_t handler)
2671{
2672 output->destroy_handler = handler;
2673 /* FIXME: implement this, once we have way to remove outputs */
2674}
2675
2676void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002677output_get_allocation(struct output *output, struct rectangle *allocation)
2678{
2679 *allocation = output->allocation;
2680}
2681
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002682struct wl_output *
2683output_get_wl_output(struct output *output)
2684{
2685 return output->output;
2686}
2687
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002688static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002689display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002690{
2691 struct input *input;
2692
2693 input = malloc(sizeof *input);
2694 if (input == NULL)
2695 return;
2696
2697 memset(input, 0, sizeof *input);
2698 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002699 input->input_device =
2700 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002701 input->pointer_focus = NULL;
2702 input->keyboard_focus = NULL;
2703 wl_list_insert(d->input_list.prev, &input->link);
2704
2705 wl_input_device_add_listener(input->input_device,
2706 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002707 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002708
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002709 input->data_device =
2710 wl_data_device_manager_get_data_device(d->data_device_manager,
2711 input->input_device);
2712 wl_data_device_add_listener(input->data_device,
2713 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002714}
2715
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002716static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002717input_destroy(struct input *input)
2718{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002719 input_remove_keyboard_focus(input);
2720 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002721
2722 if (input->drag_offer)
2723 data_offer_destroy(input->drag_offer);
2724
2725 if (input->selection_offer)
2726 data_offer_destroy(input->selection_offer);
2727
2728 wl_data_device_destroy(input->data_device);
2729 wl_list_remove(&input->link);
2730 wl_input_device_destroy(input->input_device);
2731 free(input);
2732}
2733
2734static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002735display_handle_global(struct wl_display *display, uint32_t id,
2736 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002737{
2738 struct display *d = data;
2739
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002740 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002741 d->compositor =
2742 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002743 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002744 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002745 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002746 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002747 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002748 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002749 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002750 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002751 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2752 d->data_device_manager =
2753 wl_display_bind(display, id,
2754 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002755 }
2756}
2757
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002758static void
2759display_render_frame(struct display *d)
2760{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002761 cairo_t *cr;
2762
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002763 d->frame_radius = 8;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002764 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2765 cr = cairo_create(d->shadow);
2766 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2767 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002768 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002769 cairo_fill(cr);
2770 cairo_destroy(cr);
2771 blur_surface(d->shadow, 64);
2772
2773 d->active_frame =
2774 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2775 cr = cairo_create(d->active_frame);
2776 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2777 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002778 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002779 cairo_fill(cr);
2780 cairo_destroy(cr);
2781
2782 d->inactive_frame =
2783 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2784 cr = cairo_create(d->inactive_frame);
2785 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2786 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002787 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002788 cairo_fill(cr);
2789 cairo_destroy(cr);
2790}
2791
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002792static void
2793init_xkb(struct display *d)
2794{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002795 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002796
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002797 names.rules = "evdev";
2798 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002799 names.layout = option_xkb_layout;
2800 names.variant = option_xkb_variant;
2801 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002802
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002803 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002804 if (!d->xkb) {
2805 fprintf(stderr, "Failed to compile keymap\n");
2806 exit(1);
2807 }
2808}
2809
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002810static void
2811fini_xkb(struct display *display)
2812{
2813 xkb_free_keymap(display->xkb);
2814}
2815
Yuval Fledel45568f62010-12-06 09:18:12 -05002816static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002817init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002818{
2819 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002820 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002821
Rob Clark6396ed32012-03-11 19:48:41 -05002822#ifdef USE_CAIRO_GLESV2
2823# define GL_BIT EGL_OPENGL_ES2_BIT
2824#else
2825# define GL_BIT EGL_OPENGL_BIT
2826#endif
2827
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002828 static const EGLint argb_cfg_attribs[] = {
2829 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002830 EGL_RED_SIZE, 1,
2831 EGL_GREEN_SIZE, 1,
2832 EGL_BLUE_SIZE, 1,
2833 EGL_ALPHA_SIZE, 1,
2834 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002835 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002836 EGL_NONE
2837 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002838
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002839#ifdef USE_CAIRO_GLESV2
2840 static const EGLint context_attribs[] = {
2841 EGL_CONTEXT_CLIENT_VERSION, 2,
2842 EGL_NONE
2843 };
2844 EGLint api = EGL_OPENGL_ES_API;
2845#else
2846 EGLint *context_attribs = NULL;
2847 EGLint api = EGL_OPENGL_API;
2848#endif
2849
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002850 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002851 if (!eglInitialize(d->dpy, &major, &minor)) {
2852 fprintf(stderr, "failed to initialize display\n");
2853 return -1;
2854 }
2855
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002856 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002857 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2858 return -1;
2859 }
2860
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002861 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2862 &d->argb_config, 1, &n) || n != 1) {
2863 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002864 return -1;
2865 }
2866
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002867 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002868 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002869 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002870 fprintf(stderr, "failed to create context\n");
2871 return -1;
2872 }
2873
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002874 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002875 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002876 return -1;
2877 }
2878
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002879#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002880 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2881 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2882 fprintf(stderr, "failed to get cairo egl argb device\n");
2883 return -1;
2884 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002885#endif
2886
2887 return 0;
2888}
2889
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002890static void
2891fini_egl(struct display *display)
2892{
2893#ifdef HAVE_CAIRO_EGL
2894 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002895#endif
2896
2897 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2898 EGL_NO_CONTEXT);
2899
2900 eglTerminate(display->dpy);
2901 eglReleaseThread();
2902}
2903
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002904static int
2905event_mask_update(uint32_t mask, void *data)
2906{
2907 struct display *d = data;
2908
2909 d->mask = mask;
2910
2911 return 0;
2912}
2913
2914static void
2915handle_display_data(struct task *task, uint32_t events)
2916{
2917 struct display *display =
2918 container_of(task, struct display, display_task);
2919
2920 wl_display_iterate(display->display, display->mask);
2921}
2922
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002923struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002924display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002925{
2926 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002927 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002928 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002929 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002930
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002931 g_type_init();
2932
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002933 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002934 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002935 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002936
2937 xkb_option_group = g_option_group_new("xkb",
2938 "Keyboard options",
2939 "Show all XKB options",
2940 NULL, NULL);
2941 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2942 g_option_context_add_group (context, xkb_option_group);
2943
2944 if (!g_option_context_parse(context, argc, argv, &error)) {
2945 fprintf(stderr, "option parsing failed: %s\n", error->message);
2946 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002947 }
2948
Tim Wiederhake748f6722011-01-23 23:25:25 +01002949 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002950
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002951 d = malloc(sizeof *d);
2952 if (d == NULL)
2953 return NULL;
2954
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002955 memset(d, 0, sizeof *d);
2956
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002957 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002958 if (d->display == NULL) {
2959 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002960 return NULL;
2961 }
2962
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002963 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2964 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2965 d->display_task.run = handle_display_data;
2966 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2967
2968 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002969 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002970 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002971
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002972 /* Set up listener so we'll catch all events. */
2973 wl_display_add_global_listener(d->display,
2974 display_handle_global, d);
2975
2976 /* Process connection events. */
2977 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002978 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002979 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002980
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002981 d->image_target_texture_2d =
2982 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2983 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2984 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2985
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002986 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002987
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002988 display_render_frame(d);
2989
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002990 wl_list_init(&d->window_list);
2991
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002992 init_xkb(d);
2993
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002994 return d;
2995}
2996
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002997static void
2998display_destroy_outputs(struct display *display)
2999{
3000 struct output *tmp;
3001 struct output *output;
3002
3003 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3004 output_destroy(output);
3005}
3006
Pekka Paalanene1207c72011-12-16 12:02:09 +02003007static void
3008display_destroy_inputs(struct display *display)
3009{
3010 struct input *tmp;
3011 struct input *input;
3012
3013 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3014 input_destroy(input);
3015}
3016
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003017void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003018display_destroy(struct display *display)
3019{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003020 if (!wl_list_empty(&display->window_list))
3021 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3022
3023 if (!wl_list_empty(&display->deferred_list))
3024 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3025
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003026 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003027 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003028
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003029 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003030
3031 cairo_surface_destroy(display->active_frame);
3032 cairo_surface_destroy(display->inactive_frame);
3033 cairo_surface_destroy(display->shadow);
3034 destroy_pointer_surfaces(display);
3035
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003036 fini_egl(display);
3037
Pekka Paalanenc2052982011-12-16 11:41:32 +02003038 if (display->shell)
3039 wl_shell_destroy(display->shell);
3040
3041 if (display->shm)
3042 wl_shm_destroy(display->shm);
3043
3044 if (display->data_device_manager)
3045 wl_data_device_manager_destroy(display->data_device_manager);
3046
3047 wl_compositor_destroy(display->compositor);
3048
3049 close(display->epoll_fd);
3050
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003051 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003052 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003053 free(display);
3054}
3055
3056void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003057display_set_user_data(struct display *display, void *data)
3058{
3059 display->user_data = data;
3060}
3061
3062void *
3063display_get_user_data(struct display *display)
3064{
3065 return display->user_data;
3066}
3067
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003068struct wl_display *
3069display_get_display(struct display *display)
3070{
3071 return display->display;
3072}
3073
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003074struct output *
3075display_get_output(struct display *display)
3076{
3077 return container_of(display->output_list.next, struct output, link);
3078}
3079
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003080struct wl_compositor *
3081display_get_compositor(struct display *display)
3082{
3083 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003084}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003085
3086EGLDisplay
3087display_get_egl_display(struct display *d)
3088{
3089 return d->dpy;
3090}
3091
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003092struct wl_data_source *
3093display_create_data_source(struct display *display)
3094{
3095 return wl_data_device_manager_create_data_source(display->data_device_manager);
3096}
3097
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003098EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003099display_get_argb_egl_config(struct display *d)
3100{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003101 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003102}
3103
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003104struct wl_shell *
3105display_get_shell(struct display *display)
3106{
3107 return display->shell;
3108}
3109
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003110int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003111display_acquire_window_surface(struct display *display,
3112 struct window *window,
3113 EGLContext ctx)
3114{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003115#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003116 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003117 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003118
3119 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003120 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003121 device = cairo_surface_get_device(window->cairo_surface);
3122 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003123 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003124
Benjamin Franzke0c991632011-09-27 21:57:31 +02003125 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003126 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003127 ctx = display->argb_ctx;
3128 else
3129 assert(0);
3130 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003131
3132 data = cairo_surface_get_user_data(window->cairo_surface,
3133 &surface_data_key);
3134
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003135 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003136 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003137 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3138 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003139
3140 return 0;
3141#else
3142 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003143#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003144}
3145
3146void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003147display_release_window_surface(struct display *display,
3148 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003149{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003150#ifdef HAVE_CAIRO_EGL
3151 cairo_device_t *device;
3152
3153 device = cairo_surface_get_device(window->cairo_surface);
3154 if (!device)
3155 return;
3156
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003157 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003158 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003159 cairo_device_release(device);
3160#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003161}
3162
3163void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003164display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003165{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003166 wl_list_insert(&display->deferred_list, &task->link);
3167}
3168
3169void
3170display_watch_fd(struct display *display,
3171 int fd, uint32_t events, struct task *task)
3172{
3173 struct epoll_event ep;
3174
3175 ep.events = events;
3176 ep.data.ptr = task;
3177 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3178}
3179
3180void
3181display_run(struct display *display)
3182{
3183 struct task *task;
3184 struct epoll_event ep[16];
3185 int i, count;
3186
Pekka Paalanen826d7952011-12-15 10:14:07 +02003187 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003188 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003189 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003190
3191 while (!wl_list_empty(&display->deferred_list)) {
3192 task = container_of(display->deferred_list.next,
3193 struct task, link);
3194 wl_list_remove(&task->link);
3195 task->run(task, 0);
3196 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003197
3198 if (!display->running)
3199 break;
3200
3201 wl_display_flush(display->display);
3202
3203 count = epoll_wait(display->epoll_fd,
3204 ep, ARRAY_LENGTH(ep), -1);
3205 for (i = 0; i < count; i++) {
3206 task = ep[i].data.ptr;
3207 task->run(task, ep[i].events);
3208 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003209 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003210}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003211
3212void
3213display_exit(struct display *display)
3214{
3215 display->running = 0;
3216}