blob: e9e8a2742c93c1b77525b3f9a769eb5c7f3eea68 [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øgsbergd0c3b9d2010-10-25 11:40:03 -040023#include "../config.h"
24
Kristian Høgsberg61017b12008-11-02 18:51:48 -050025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050029#include <fcntl.h>
30#include <unistd.h>
31#include <math.h>
32#include <time.h>
33#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050034#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040035#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040036#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040037#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040038
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050039#include <wayland-egl.h>
40
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041#include <GL/gl.h>
42#include <EGL/egl.h>
43#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040044
Kristian Høgsberg8def2642011-01-14 17:41:33 -050045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040046#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050048
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040049#include <X11/extensions/XKBcommon.h>
50
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050052#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050053#include "wayland-client.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040054#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050055
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050057
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050058struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050059 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050060 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040061 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040062 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050063 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040065 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040066 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020067 EGLConfig premultiplied_argb_config;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040068 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010069 cairo_device_t *device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040070
71 int display_fd;
72 uint32_t mask;
73 struct task display_task;
74
75 int epoll_fd;
76 struct wl_list deferred_list;
77
Kristian Høgsberg478d9262010-06-08 20:34:11 -040078 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040079 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040080 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040081 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040082 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040083 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040084
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050085 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
86 PFNEGLCREATEIMAGEKHRPROC create_image;
87 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050088};
89
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040090enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040091 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -040092 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040093 TYPE_TRANSIENT,
94 TYPE_CUSTOM
95};
96
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097struct window {
98 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050099 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500100 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500101 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500102 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500103 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400104 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400105 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400106 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500107 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500108 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400109 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400110 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400111 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400112 struct input *grab_device;
113 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500114 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400115 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500116
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400117 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500118 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500119
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500120 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400121 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500122 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400123 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500124 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400125 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400126 window_enter_handler_t enter_handler;
127 window_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400128 window_item_focus_handler_t item_focus_handler;
129
130 struct wl_list item_list;
131 struct item *focus_item;
132 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400133
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500134 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400135 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500136};
137
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400138struct item {
139 struct wl_list link;
140 struct rectangle allocation;
141 void *user_data;
142};
143
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400144struct input {
145 struct display *display;
146 struct wl_input_device *input_device;
147 struct window *pointer_focus;
148 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500149 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400150 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400151 uint32_t modifiers;
152 int32_t x, y, sx, sy;
153 struct wl_list link;
154};
155
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400156enum {
157 POINTER_DEFAULT = 100,
158 POINTER_UNSET
159};
160
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500161enum window_location {
162 WINDOW_INTERIOR = 0,
163 WINDOW_RESIZING_TOP = 1,
164 WINDOW_RESIZING_BOTTOM = 2,
165 WINDOW_RESIZING_LEFT = 4,
166 WINDOW_RESIZING_TOP_LEFT = 5,
167 WINDOW_RESIZING_BOTTOM_LEFT = 6,
168 WINDOW_RESIZING_RIGHT = 8,
169 WINDOW_RESIZING_TOP_RIGHT = 9,
170 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
171 WINDOW_RESIZING_MASK = 15,
172 WINDOW_EXTERIOR = 16,
173 WINDOW_TITLEBAR = 17,
174 WINDOW_CLIENT_AREA = 18,
175};
176
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400177const char *option_xkb_layout = "us";
178const char *option_xkb_variant = "";
179const char *option_xkb_options = "";
180
181static const GOptionEntry xkb_option_entries[] = {
182 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
183 &option_xkb_layout, "XKB Layout" },
184 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
185 &option_xkb_variant, "XKB Variant" },
186 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
187 &option_xkb_options, "XKB Options" },
188 { NULL }
189};
190
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400191static const cairo_user_data_key_t surface_data_key;
192struct surface_data {
193 struct wl_buffer *buffer;
194};
195
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500196#define MULT(_d,c,a,t) \
197 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
198
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500199#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400200
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100201struct egl_window_surface_data {
202 struct display *display;
203 struct wl_surface *surface;
204 struct wl_egl_window *window;
205 EGLSurface surf;
206};
207
208static void
209egl_window_surface_data_destroy(void *p)
210{
211 struct egl_window_surface_data *data = p;
212 struct display *d = data->display;
213
214 eglDestroySurface(d->dpy, data->surf);
215 wl_egl_window_destroy(data->window);
216 data->surface = NULL;
217
218 free(p);
219}
220
221static cairo_surface_t *
222display_create_egl_window_surface(struct display *display,
223 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400224 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100225 struct rectangle *rectangle)
226{
227 cairo_surface_t *cairo_surface;
228 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400229 EGLConfig config;
230 const EGLint *attribs;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100231
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400232 static const EGLint premul_attribs[] = {
233 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
234 EGL_NONE
235 };
236
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100237 data = malloc(sizeof *data);
238 if (data == NULL)
239 return NULL;
240
241 data->display = display;
242 data->surface = surface;
243
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400244 if (flags & SURFACE_OPAQUE) {
245 config = display->rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200246 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400247 } else {
248 config = display->premultiplied_argb_config;
249 attribs = premul_attribs;
250 }
251
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400252 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100253 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400254 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100255
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400256 data->surf = eglCreateWindowSurface(display->dpy, config,
257 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100258
259 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
260 data->surf,
261 rectangle->width,
262 rectangle->height);
263
264 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
265 data, egl_window_surface_data_destroy);
266
267 return cairo_surface;
268}
269
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500270struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400271 struct surface_data data;
272 EGLImageKHR image;
273 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800274 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500275 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400276};
277
278static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500279egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400280{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500281 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800282 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400283
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800284 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400285 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800286 cairo_device_release(d->device);
287
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500288 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400289 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500290 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500291 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400292}
293
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500294EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500295display_get_image_for_egl_image_surface(struct display *display,
296 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500297{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500298 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500299
300 data = cairo_surface_get_user_data (surface, &surface_data_key);
301
302 return data->image;
303}
304
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500305static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500306display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400307 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500308 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400309{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500310 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400311 EGLDisplay dpy = display->dpy;
312 cairo_surface_t *surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400313 EGLConfig config;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314
315 data = malloc(sizeof *data);
316 if (data == NULL)
317 return NULL;
318
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800319 data->display = display;
320
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400321 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400322 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000324 free(data);
325 return NULL;
326 }
327
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400328 if (flags & SURFACE_OPAQUE)
329 config = display->rgb_config;
330 else
331 config = display->premultiplied_argb_config;
332
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500333 data->image = display->create_image(dpy, NULL,
334 EGL_NATIVE_PIXMAP_KHR,
335 (EGLClientBuffer) data->pixmap,
336 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500337 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500338 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339 free(data);
340 return NULL;
341 }
342
343 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400344 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500345
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800346 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400347 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400348 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500349 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800350 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400351
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352 surface = cairo_gl_surface_create_for_texture(display->device,
353 CAIRO_CONTENT_COLOR_ALPHA,
354 data->texture,
355 rectangle->width,
356 rectangle->height);
357
358 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500359 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400360
361 return surface;
362}
363
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500364static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500365display_create_egl_image_surface_from_file(struct display *display,
366 const char *filename,
367 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400368{
369 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400370 GdkPixbuf *pixbuf;
371 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400372 int stride, i;
373 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500374 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400375
376 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400377 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400378 FALSE, &error);
379 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400380 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400381
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400382 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
383 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500384 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400385 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400386 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400387
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400388
389 stride = gdk_pixbuf_get_rowstride(pixbuf);
390 pixels = gdk_pixbuf_get_pixels(pixbuf);
391
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400393 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400395 while (p < end) {
396 unsigned int t;
397
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400398 MULT(p[0], p[0], p[3], t);
399 MULT(p[1], p[1], p[3], t);
400 MULT(p[2], p[2], p[3], t);
401 p += 4;
402
403 }
404 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400405
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200406 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000407 if (surface == NULL) {
408 g_object_unref(pixbuf);
409 return NULL;
410 }
411
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800412 data = cairo_surface_get_user_data(surface, &surface_data_key);
413
414 cairo_device_acquire(display->device);
415 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800416 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
417 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800418 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400419
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500420 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422 return surface;
423}
424
425#endif
426
427struct wl_buffer *
428display_get_buffer_for_surface(struct display *display,
429 cairo_surface_t *surface)
430{
431 struct surface_data *data;
432
433 data = cairo_surface_get_user_data (surface, &surface_data_key);
434
435 return data->buffer;
436}
437
438struct shm_surface_data {
439 struct surface_data data;
440 void *map;
441 size_t length;
442};
443
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500444static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445shm_surface_data_destroy(void *p)
446{
447 struct shm_surface_data *data = p;
448
449 wl_buffer_destroy(data->data.buffer);
450 munmap(data->map, data->length);
451}
452
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500453static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400455 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400456{
457 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400458 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400459 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800460 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400461 char filename[] = "/tmp/wayland-shm-XXXXXX";
462
463 data = malloc(sizeof *data);
464 if (data == NULL)
465 return NULL;
466
467 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
468 rectangle->width);
469 data->length = stride * rectangle->height;
470 fd = mkstemp(filename);
471 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000472 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400473 return NULL;
474 }
475 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000476 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400477 close(fd);
478 return NULL;
479 }
480
481 data->map = mmap(NULL, data->length,
482 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
483 unlink(filename);
484
485 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000486 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400487 close(fd);
488 return NULL;
489 }
490
491 surface = cairo_image_surface_create_for_data (data->map,
492 CAIRO_FORMAT_ARGB32,
493 rectangle->width,
494 rectangle->height,
495 stride);
496
497 cairo_surface_set_user_data (surface, &surface_data_key,
498 data, shm_surface_data_destroy);
499
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400500 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400501 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400502 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400503 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400504
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505 data->data.buffer = wl_shm_create_buffer(display->shm,
506 fd,
507 rectangle->width,
508 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400509 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400510
511 close(fd);
512
513 return surface;
514}
515
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500516static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517display_create_shm_surface_from_file(struct display *display,
518 const char *filename,
519 struct rectangle *rect)
520{
521 cairo_surface_t *surface;
522 GdkPixbuf *pixbuf;
523 GError *error = NULL;
524 int stride, i;
525 unsigned char *pixels, *p, *end, *dest_data;
526 int dest_stride;
527 uint32_t *d;
528
529 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
530 rect->width, rect->height,
531 FALSE, &error);
532 if (error != NULL)
533 return NULL;
534
535 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
536 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500537 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538 return NULL;
539 }
540
541 stride = gdk_pixbuf_get_rowstride(pixbuf);
542 pixels = gdk_pixbuf_get_pixels(pixbuf);
543
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400544 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000545 if (surface == NULL) {
546 g_object_unref(pixbuf);
547 return NULL;
548 }
549
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550 dest_data = cairo_image_surface_get_data (surface);
551 dest_stride = cairo_image_surface_get_stride (surface);
552
553 for (i = 0; i < rect->height; i++) {
554 d = (uint32_t *) (dest_data + i * dest_stride);
555 p = pixels + i * stride;
556 end = p + rect->width * 4;
557 while (p < end) {
558 unsigned int t;
559 unsigned char a, r, g, b;
560
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561 a = p[3];
562 MULT(r, p[0], a, t);
563 MULT(g, p[1], a, t);
564 MULT(b, p[2], a, t);
565 p += 4;
566 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
567 }
568 }
569
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500570 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400571
572 return surface;
573}
574
nobled7b87cb02011-02-01 18:51:47 +0000575static int
576check_size(struct rectangle *rect)
577{
578 if (rect->width && rect->height)
579 return 0;
580
581 fprintf(stderr, "tried to create surface of "
582 "width: %d, height: %d\n", rect->width, rect->height);
583 return -1;
584}
585
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400586cairo_surface_t *
587display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100588 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400589 struct rectangle *rectangle,
590 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400591{
nobled7b87cb02011-02-01 18:51:47 +0000592 if (check_size(rectangle) < 0)
593 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500594#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500595 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100596 if (surface)
597 return display_create_egl_window_surface(display,
598 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400599 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100600 rectangle);
601 else
602 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400603 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100604 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500605 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400607 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400608}
609
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500610static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400611display_create_surface_from_file(struct display *display,
612 const char *filename,
613 struct rectangle *rectangle)
614{
nobled7b87cb02011-02-01 18:51:47 +0000615 if (check_size(rectangle) < 0)
616 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500617#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500618 if (display->dpy) {
619 return display_create_egl_image_surface_from_file(display,
620 filename,
621 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500622 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400623#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500624 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400625}
Yuval Fledel45568f62010-12-06 09:18:12 -0500626 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400627 const char *filename;
628 int hotspot_x, hotspot_y;
629} pointer_images[] = {
630 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
631 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
632 { DATADIR "/wayland/bottom_side.png", 16, 20 },
633 { DATADIR "/wayland/grabbing.png", 20, 17 },
634 { DATADIR "/wayland/left_ptr.png", 10, 5 },
635 { DATADIR "/wayland/left_side.png", 10, 20 },
636 { DATADIR "/wayland/right_side.png", 30, 19 },
637 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
638 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
639 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400640 { DATADIR "/wayland/xterm.png", 15, 15 },
641 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400642};
643
644static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400645create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400646{
647 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400648 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400649 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400650
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400651 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400652 display->pointer_surfaces =
653 malloc(count * sizeof *display->pointer_surfaces);
654 rect.width = width;
655 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400656 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400657 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658 display_create_surface_from_file(display,
659 pointer_images[i].filename,
660 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400661 }
662
663}
664
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400665cairo_surface_t *
666display_get_pointer_surface(struct display *display, int pointer,
667 int *width, int *height,
668 int *hotspot_x, int *hotspot_y)
669{
670 cairo_surface_t *surface;
671
672 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500673#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400674 *width = cairo_gl_surface_get_width(surface);
675 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000676#else
677 *width = cairo_image_surface_get_width(surface);
678 *height = cairo_image_surface_get_height(surface);
679#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400680 *hotspot_x = pointer_images[pointer].hotspot_x;
681 *hotspot_y = pointer_images[pointer].hotspot_y;
682
683 return cairo_surface_reference(surface);
684}
685
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400686
687static void
688window_attach_surface(struct window *window);
689
690static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400691free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400692{
693 struct window *window = data;
694
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400695 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400696 cairo_surface_destroy(window->pending_surface);
697 window->pending_surface = NULL;
698 if (window->cairo_surface)
699 window_attach_surface(window);
700}
701
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400702static const struct wl_callback_listener free_surface_listener = {
703 free_surface
704};
705
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500706static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200707window_get_resize_dx_dy(struct window *window, int *x, int *y)
708{
709 if (window->resize_edges & WINDOW_RESIZING_LEFT)
710 *x = window->server_allocation.width - window->allocation.width;
711 else
712 *x = 0;
713
714 if (window->resize_edges & WINDOW_RESIZING_TOP)
715 *y = window->server_allocation.height -
716 window->allocation.height;
717 else
718 *y = 0;
719
720 window->resize_edges = 0;
721}
722
723static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400724window_set_type(struct window *window)
725{
726 struct display *display = window->display;
727
728 switch (window->type) {
729 case TYPE_FULLSCREEN:
730 wl_shell_set_fullscreen(display->shell, window->surface);
731 break;
732 case TYPE_TOPLEVEL:
733 wl_shell_set_toplevel(display->shell, window->surface);
734 break;
735 case TYPE_TRANSIENT:
736 wl_shell_set_transient(display->shell, window->surface,
737 window->parent->surface,
738 window->x, window->y, 0);
739 break;
740 case TYPE_CUSTOM:
741 break;
742 }
743}
744
745static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500746window_attach_surface(struct window *window)
747{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400748 struct display *display = window->display;
749 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400750 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000751#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100752 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000753#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500754 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100755
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400756 if (display->shell)
757 window_set_type(window);
758
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100759 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000760#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100761 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
762 data = cairo_surface_get_user_data(window->cairo_surface,
763 &surface_data_key);
764
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100765 cairo_gl_surface_swapbuffers(window->cairo_surface);
766 wl_egl_window_get_attached_size(data->window,
767 &window->server_allocation.width,
768 &window->server_allocation.height);
769 break;
770 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000771#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100772 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200773 window_get_resize_dx_dy(window, &x, &y);
774
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100775 if (window->pending_surface != NULL)
776 return;
777
778 window->pending_surface = window->cairo_surface;
779 window->cairo_surface = NULL;
780
781 buffer =
782 display_get_buffer_for_surface(display,
783 window->pending_surface);
784
785 wl_surface_attach(window->surface, buffer, x, y);
786 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400787 cb = wl_display_sync(display->display);
788 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000790 default:
791 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500793
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500794 wl_surface_damage(window->surface, 0, 0,
795 window->allocation.width,
796 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500797}
798
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500799void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400800window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500801{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100802 if (window->cairo_surface) {
803 switch (window->buffer_type) {
804 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
805 case WINDOW_BUFFER_TYPE_SHM:
806 display_surface_damage(window->display,
807 window->cairo_surface,
808 0, 0,
809 window->allocation.width,
810 window->allocation.height);
811 break;
812 default:
813 break;
814 }
815 window_attach_surface(window);
816 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500817}
818
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400819void
820window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500822 cairo_surface_reference(surface);
823
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400824 if (window->cairo_surface != NULL)
825 cairo_surface_destroy(window->cairo_surface);
826
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500827 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400828}
829
Benjamin Franzke22d54812011-07-16 19:50:32 +0000830#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831static void
832window_resize_cairo_window_surface(struct window *window)
833{
834 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200835 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100836
837 data = cairo_surface_get_user_data(window->cairo_surface,
838 &surface_data_key);
839
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200840 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100841 wl_egl_window_resize(data->window,
842 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200843 window->allocation.height,
844 x,y);
845
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 cairo_gl_surface_set_size(window->cairo_surface,
847 window->allocation.width,
848 window->allocation.height);
849}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000850#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400852void
853window_create_surface(struct window *window)
854{
855 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400856 uint32_t flags = 0;
857
858 if (!window->transparent)
859 flags = SURFACE_OPAQUE;
860
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400861 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500862#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100863 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
864 if (window->cairo_surface) {
865 window_resize_cairo_window_surface(window);
866 return;
867 }
868 surface = display_create_surface(window->display,
869 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400870 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100871 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500872 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400873 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100874 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400875 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400876 break;
877#endif
878 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400879 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400880 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400881 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800882 default:
883 surface = NULL;
884 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400885 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400886
887 window_set_surface(window, surface);
888 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889}
890
891static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500892window_draw_menu(struct window *window)
893{
894 cairo_t *cr;
895 int width, height, r = 5;
896
897 window_create_surface(window);
898
899 cr = cairo_create(window->cairo_surface);
900 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
901 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
902 cairo_paint(cr);
903
904 width = window->allocation.width;
905 height = window->allocation.height;
906 rounded_rect(cr, r, r, width - r, height - r, r);
907 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
908 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
909 cairo_fill(cr);
910 cairo_destroy(cr);
911}
912
913static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500914window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500915{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500916 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500917 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400918 cairo_surface_t *frame;
919 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500920
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400921 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400922
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400923 width = window->allocation.width;
924 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500925
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500926 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500927
Kristian Høgsberg09531622010-06-14 23:22:15 -0400928 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400929 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400930 cairo_paint(cr);
931
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400932 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400933 tile_mask(cr, window->display->shadow,
934 shadow_dx, shadow_dy, width, height,
935 window->margin + 10 - shadow_dx,
936 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500937
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400938 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400939 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400940 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400941 frame = window->display->inactive_frame;
942
943 tile_source(cr, frame, 0, 0, width, height,
944 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500945
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500946 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
947 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500948 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400949 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400950 if (window->keyboard_device)
951 cairo_set_source_rgb(cr, 0, 0, 0);
952 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400953 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400954 cairo_show_text(cr, window->title);
955
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500956 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400957
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100958 /* FIXME: this breakes gears, fix cairo? */
959#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400960 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100961#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500962}
963
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400964void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500965window_destroy(struct window *window)
966{
967 wl_surface_destroy(window->surface);
968 wl_list_remove(&window->link);
969 free(window);
970}
971
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400972static struct item *
973window_find_item(struct window *window, int32_t x, int32_t y)
974{
975 struct item *item;
976
977 wl_list_for_each(item, &window->item_list, link) {
978 if (item->allocation.x <= x &&
979 x < item->allocation.x + item->allocation.width &&
980 item->allocation.y <= y &&
981 y < item->allocation.y + item->allocation.height) {
982 return item;
983 }
984 }
985
986 return NULL;
987}
988
989struct item *
990window_add_item(struct window *window, void *data)
991{
992 struct item *item;
993
994 item = malloc(sizeof *item);
995 memset(item, 0, sizeof *item);
996 item->user_data = data;
997 wl_list_insert(window->item_list.prev, &item->link);
998
999 return item;
1000}
1001
1002void
1003window_for_each_item(struct window *window, item_func_t func, void *data)
1004{
1005 struct item *item;
1006
1007 wl_list_for_each(item, &window->item_list, link)
1008 func(item, data);
1009}
1010
1011struct item *
1012window_get_focus_item(struct window *window)
1013{
1014 return window->focus_item;
1015}
1016
1017void
1018item_get_allocation(struct item *item, struct rectangle *allocation)
1019{
1020 *allocation = item->allocation;
1021}
1022
1023void
1024item_set_allocation(struct item *item,
1025 int32_t x, int32_t y, int32_t width, int32_t height)
1026{
1027 item->allocation.x = x;
1028 item->allocation.y = y;
1029 item->allocation.width = width;
1030 item->allocation.height = height;
1031}
1032
1033void *
1034item_get_user_data(struct item *item)
1035{
1036 return item->user_data;
1037}
1038
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001039void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001040window_draw(struct window *window)
1041{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001042 if (window->parent)
1043 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001044 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001045 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001046 else
1047 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001048}
1049
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001050cairo_surface_t *
1051window_get_surface(struct window *window)
1052{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001053 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001054}
1055
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001056struct wl_surface *
1057window_get_wl_surface(struct window *window)
1058{
1059 return window->surface;
1060}
1061
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001062static int
1063get_pointer_location(struct window *window, int32_t x, int32_t y)
1064{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001065 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001066 const int grip_size = 8;
1067
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001068 if (!window->decoration)
1069 return WINDOW_CLIENT_AREA;
1070
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001071 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001072 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001073 else if (window->margin <= x && x < window->margin + grip_size)
1074 hlocation = WINDOW_RESIZING_LEFT;
1075 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001076 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001077 else if (x < window->allocation.width - window->margin)
1078 hlocation = WINDOW_RESIZING_RIGHT;
1079 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001080 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001081
1082 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001083 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001084 else if (window->margin <= y && y < window->margin + grip_size)
1085 vlocation = WINDOW_RESIZING_TOP;
1086 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001087 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001088 else if (y < window->allocation.height - window->margin)
1089 vlocation = WINDOW_RESIZING_BOTTOM;
1090 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001091 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001092
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001093 location = vlocation | hlocation;
1094 if (location & WINDOW_EXTERIOR)
1095 location = WINDOW_EXTERIOR;
1096 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1097 location = WINDOW_TITLEBAR;
1098 else if (location == WINDOW_INTERIOR)
1099 location = WINDOW_CLIENT_AREA;
1100
1101 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001102}
1103
1104static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001105set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001106{
1107 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001108 struct wl_buffer *buffer;
1109 cairo_surface_t *surface;
1110 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001111
1112 location = get_pointer_location(input->pointer_focus,
1113 input->sx, input->sy);
1114 switch (location) {
1115 case WINDOW_RESIZING_TOP:
1116 pointer = POINTER_TOP;
1117 break;
1118 case WINDOW_RESIZING_BOTTOM:
1119 pointer = POINTER_BOTTOM;
1120 break;
1121 case WINDOW_RESIZING_LEFT:
1122 pointer = POINTER_LEFT;
1123 break;
1124 case WINDOW_RESIZING_RIGHT:
1125 pointer = POINTER_RIGHT;
1126 break;
1127 case WINDOW_RESIZING_TOP_LEFT:
1128 pointer = POINTER_TOP_LEFT;
1129 break;
1130 case WINDOW_RESIZING_TOP_RIGHT:
1131 pointer = POINTER_TOP_RIGHT;
1132 break;
1133 case WINDOW_RESIZING_BOTTOM_LEFT:
1134 pointer = POINTER_BOTTOM_LEFT;
1135 break;
1136 case WINDOW_RESIZING_BOTTOM_RIGHT:
1137 pointer = POINTER_BOTTOM_RIGHT;
1138 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001139 case WINDOW_EXTERIOR:
1140 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001141 if (input->current_pointer_image == POINTER_DEFAULT)
1142 return;
1143
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001144 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001145 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001146 return;
1147 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001148 break;
1149 }
1150
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001151 if (pointer == input->current_pointer_image)
1152 return;
1153
1154 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001155 surface = display->pointer_surfaces[pointer];
1156 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001157 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001158 pointer_images[pointer].hotspot_x,
1159 pointer_images[pointer].hotspot_y);
1160}
1161
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001162static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001163window_set_focus_item(struct window *window, struct item *focus)
1164{
1165 void *data;
1166
1167 if (focus == window->focus_item)
1168 return;
1169
1170 window->focus_item = focus;
1171 data = focus ? focus->user_data : NULL;
1172 if (window->item_focus_handler)
1173 window->item_focus_handler(window, focus, data);
1174}
1175
1176static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001177window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001178 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001179 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001180{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001181 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001182 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001183 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001184 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001185
1186 input->x = x;
1187 input->y = y;
1188 input->sx = sx;
1189 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001190
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001191 if (!window->focus_item || !window->item_grab_button) {
1192 item = window_find_item(window, sx, sy);
1193 window_set_focus_item(window, item);
1194 }
1195
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001196 if (window->motion_handler)
1197 pointer = (*window->motion_handler)(window, input, time,
1198 x, y, sx, sy,
1199 window->user_data);
1200
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001201 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001202}
1203
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001204static void
1205window_handle_button(void *data,
1206 struct wl_input_device *input_device,
1207 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001208{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001209 struct input *input = data;
1210 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001211 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001212 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001213
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001214 if (window->focus_item && window->item_grab_button == 0 && state)
1215 window->item_grab_button = button;
1216
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001217 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001218
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001219 if (window->display->shell &&
1220 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001221 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001222 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001223 wl_shell_move(window->display->shell,
1224 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001225 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001226 case WINDOW_RESIZING_TOP:
1227 case WINDOW_RESIZING_BOTTOM:
1228 case WINDOW_RESIZING_LEFT:
1229 case WINDOW_RESIZING_RIGHT:
1230 case WINDOW_RESIZING_TOP_LEFT:
1231 case WINDOW_RESIZING_TOP_RIGHT:
1232 case WINDOW_RESIZING_BOTTOM_LEFT:
1233 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001234 wl_shell_resize(window->display->shell,
1235 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001236 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001237 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001238 case WINDOW_CLIENT_AREA:
1239 if (window->button_handler)
1240 (*window->button_handler)(window,
1241 input, time,
1242 button, state,
1243 window->user_data);
1244 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001245 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001246 } else {
1247 if (window->button_handler)
1248 (*window->button_handler)(window,
1249 input, time,
1250 button, state,
1251 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001252 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001253
1254 if (window->focus_item &&
1255 window->item_grab_button == button && !state) {
1256 window->item_grab_button = 0;
1257 item = window_find_item(window, input->sx, input->sy);
1258 window_set_focus_item(window, item);
1259 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001260}
1261
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001262static void
1263window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001264 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001265{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001266 struct input *input = data;
1267 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001268 struct display *d = window->display;
1269 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001270
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001271 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001272 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001273 return;
1274
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001275 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001276 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001277 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1278 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001279
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001280 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1281
1282 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001283 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001284 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001285 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001286
1287 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001288 (*window->key_handler)(window, input, time, key, sym, state,
1289 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001290}
1291
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001292static void
1293window_handle_pointer_focus(void *data,
1294 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001295 uint32_t time, struct wl_surface *surface,
1296 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001297{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001298 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001299 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001300 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001301 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001302
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001303 window = input->pointer_focus;
1304 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001305 window_set_focus_item(window, NULL);
1306
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001307 if (window->leave_handler)
1308 window->leave_handler(window, input,
1309 time, window->user_data);
1310 input->pointer_focus = NULL;
1311 input->current_pointer_image = POINTER_UNSET;
1312 }
1313
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001314 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001315 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001316 window = input->pointer_focus;
1317
Kristian Høgsberg59826582011-01-20 11:56:57 -05001318 input->x = x;
1319 input->y = y;
1320 input->sx = sx;
1321 input->sy = sy;
1322
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001323 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001324 if (window->enter_handler)
1325 pointer = window->enter_handler(window, input,
1326 time, sx, sy,
1327 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001328
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001329 item = window_find_item(window, x, y);
1330 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001331
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001332 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001333 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001334}
1335
1336static void
1337window_handle_keyboard_focus(void *data,
1338 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001339 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001340 struct wl_surface *surface,
1341 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001342{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001343 struct input *input = data;
1344 struct window *window = input->keyboard_focus;
1345 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001346 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001347
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001348 window = input->keyboard_focus;
1349 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001350 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001351 if (window->keyboard_focus_handler)
1352 (*window->keyboard_focus_handler)(window, NULL,
1353 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001354 }
1355
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001356 if (surface)
1357 input->keyboard_focus = wl_surface_get_user_data(surface);
1358 else
1359 input->keyboard_focus = NULL;
1360
1361 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001362 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001363 for (k = keys->data; k < end; k++)
1364 input->modifiers |= d->xkb->map->modmap[*k];
1365
1366 window = input->keyboard_focus;
1367 if (window) {
1368 window->keyboard_device = input;
1369 if (window->keyboard_focus_handler)
1370 (*window->keyboard_focus_handler)(window,
1371 window->keyboard_device,
1372 window->user_data);
1373 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001374}
1375
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001376static const struct wl_input_device_listener input_device_listener = {
1377 window_handle_motion,
1378 window_handle_button,
1379 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001380 window_handle_pointer_focus,
1381 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001382};
1383
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001384void
1385input_get_position(struct input *input, int32_t *x, int32_t *y)
1386{
1387 *x = input->sx;
1388 *y = input->sy;
1389}
1390
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001391struct wl_input_device *
1392input_get_input_device(struct input *input)
1393{
1394 return input->input_device;
1395}
1396
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001397uint32_t
1398input_get_modifiers(struct input *input)
1399{
1400 return input->modifiers;
1401}
1402
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001403struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001404window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001405{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001406 cairo_device_flush (window->display->device);
1407
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001408 return wl_shell_create_drag(window->display->shell);
1409}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001410
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001411void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001412window_move(struct window *window, struct input *input, uint32_t time)
1413{
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001414 if (window->display->shell)
1415 wl_shell_move(window->display->shell,
1416 window->surface, input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001417}
1418
1419void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001420window_activate_drag(struct wl_drag *drag, struct window *window,
1421 struct input *input, uint32_t time)
1422{
1423 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001424}
1425
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001426static void
1427handle_configure(void *data, struct wl_shell *shell,
1428 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001429 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001430{
1431 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001432 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001433
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001434 /* FIXME: this is probably the wrong place to check for width
1435 * or height <= 0, but it prevents the compositor from crashing
1436 */
1437 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001438 return;
1439
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001440 window->resize_edges = edges;
1441
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001442 if (window->resize_handler) {
1443 child_width = width - 20 - window->margin * 2;
1444 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001445
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001446 (*window->resize_handler)(window,
1447 child_width, child_height,
1448 window->user_data);
1449 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001450 window->allocation.width = width;
1451 window->allocation.height = height;
1452
1453 if (window->redraw_handler)
1454 window_schedule_redraw(window);
1455 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001456}
1457
1458static const struct wl_shell_listener shell_listener = {
1459 handle_configure,
1460};
1461
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001462void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001463window_get_allocation(struct window *window,
1464 struct rectangle *allocation)
1465{
1466 *allocation = window->allocation;
1467}
1468
1469void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001470window_get_child_allocation(struct window *window,
1471 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001472{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001473 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001474 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001475 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001476 allocation->x = window->margin + 10;
1477 allocation->y = window->margin + 50;
1478 allocation->width =
1479 window->allocation.width - 20 - window->margin * 2;
1480 allocation->height =
1481 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001482 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001483}
1484
1485void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001486window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001487{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001488 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001489 window->allocation.x = 20 + window->margin;
1490 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001491 window->allocation.width = width + 20 + window->margin * 2;
1492 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001493 } else {
1494 window->allocation.x = 0;
1495 window->allocation.y = 0;
1496 window->allocation.width = width;
1497 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001498 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001499}
1500
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001501static void
1502idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001503{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001504 struct window *window =
1505 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001506
1507 window->redraw_handler(window, window->user_data);
1508 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001509}
1510
1511void
1512window_schedule_redraw(struct window *window)
1513{
1514 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001515 window->redraw_task.run = idle_redraw;
1516 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001517 window->redraw_scheduled = 1;
1518 }
1519}
1520
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001521void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001522window_set_custom(struct window *window)
1523{
1524 window->type = TYPE_CUSTOM;
1525}
1526
1527void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001528window_set_fullscreen(struct window *window, int fullscreen)
1529{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001530 int32_t width, height;
1531
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001532 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001533 return;
1534
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001535 if (fullscreen) {
1536 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001537 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001538 width = window->display->screen_allocation.width;
1539 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001540 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001541 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001542 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001543 width = window->saved_allocation.width - 20 - window->margin * 2;
1544 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001545 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001546 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001547
1548 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001549}
1550
1551void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001552window_set_decoration(struct window *window, int decoration)
1553{
1554 window->decoration = decoration;
1555}
1556
1557void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001558window_set_user_data(struct window *window, void *data)
1559{
1560 window->user_data = data;
1561}
1562
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001563void *
1564window_get_user_data(struct window *window)
1565{
1566 return window->user_data;
1567}
1568
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001569void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001570window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001571 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001572{
1573 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001574}
1575
1576void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001577window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001578 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001579{
1580 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001581}
1582
1583void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001584window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001585 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001586{
1587 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001588}
1589
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001590void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001591window_set_button_handler(struct window *window,
1592 window_button_handler_t handler)
1593{
1594 window->button_handler = handler;
1595}
1596
1597void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001598window_set_motion_handler(struct window *window,
1599 window_motion_handler_t handler)
1600{
1601 window->motion_handler = handler;
1602}
1603
1604void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001605window_set_enter_handler(struct window *window,
1606 window_enter_handler_t handler)
1607{
1608 window->enter_handler = handler;
1609}
1610
1611void
1612window_set_leave_handler(struct window *window,
1613 window_leave_handler_t handler)
1614{
1615 window->leave_handler = handler;
1616}
1617
1618void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001619window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001620 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001621{
1622 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001623}
1624
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001625void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001626window_set_item_focus_handler(struct window *window,
1627 window_item_focus_handler_t handler)
1628{
1629 window->item_focus_handler = handler;
1630}
1631
1632void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001633window_set_transparent(struct window *window, int transparent)
1634{
1635 window->transparent = transparent;
1636}
1637
1638void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001639window_set_title(struct window *window, const char *title)
1640{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001641 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001642 window->title = strdup(title);
1643}
1644
1645const char *
1646window_get_title(struct window *window)
1647{
1648 return window->title;
1649}
1650
1651void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001652display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1653 int32_t x, int32_t y, int32_t width, int32_t height)
1654{
1655 struct wl_buffer *buffer;
1656
1657 buffer = display_get_buffer_for_surface(display, cairo_surface);
1658
1659 wl_buffer_damage(buffer, x, y, width, height);
1660}
1661
1662void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001663window_damage(struct window *window, int32_t x, int32_t y,
1664 int32_t width, int32_t height)
1665{
1666 wl_surface_damage(window->surface, x, y, width, height);
1667}
1668
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001669static struct window *
1670window_create_internal(struct display *display, struct window *parent,
1671 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001672{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001673 struct window *window;
1674
1675 window = malloc(sizeof *window);
1676 if (window == NULL)
1677 return NULL;
1678
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001679 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001680 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001681 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001682 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001683 window->allocation.x = 0;
1684 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001685 window->allocation.width = width;
1686 window->allocation.height = height;
1687 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001688 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001689 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001690 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001691 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001692
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001693 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001694#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001695 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1696 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001697#else
1698 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1699#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001700 else
1701 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001702
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001703 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001704 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001705
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001706 return window;
1707}
1708
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001709struct window *
1710window_create(struct display *display, int32_t width, int32_t height)
1711{
1712 struct window *window;
1713
1714 window = window_create_internal(display, NULL, width, height);
1715 if (!window)
1716 return NULL;
1717
1718 return window;
1719}
1720
1721struct window *
1722window_create_transient(struct display *display, struct window *parent,
1723 int32_t x, int32_t y, int32_t width, int32_t height)
1724{
1725 struct window *window;
1726
1727 window = window_create_internal(parent->display,
1728 parent, width, height);
1729 if (!window)
1730 return NULL;
1731
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001732 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001733 window->x = x;
1734 window->y = y;
1735
1736 return window;
1737}
1738
1739void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001740window_set_buffer_type(struct window *window, enum window_buffer_type type)
1741{
1742 window->buffer_type = type;
1743}
1744
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001745
1746static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001747display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001748 struct wl_output *wl_output,
1749 int x, int y,
1750 int physical_width,
1751 int physical_height,
1752 int subpixel,
1753 const char *make,
1754 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001755{
1756 struct display *display = data;
1757
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001758 display->screen_allocation.x = x;
1759 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001760}
1761
1762static void
1763display_handle_mode(void *data,
1764 struct wl_output *wl_output,
1765 uint32_t flags,
1766 int width,
1767 int height,
1768 int refresh)
1769{
1770 struct display *display = data;
1771
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001772 display->screen_allocation.width = width;
1773 display->screen_allocation.height = height;
1774}
1775
1776static const struct wl_output_listener output_listener = {
1777 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001778 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001779};
1780
1781static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001782display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001783{
1784 struct input *input;
1785
1786 input = malloc(sizeof *input);
1787 if (input == NULL)
1788 return;
1789
1790 memset(input, 0, sizeof *input);
1791 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001792 input->input_device =
1793 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001794 input->pointer_focus = NULL;
1795 input->keyboard_focus = NULL;
1796 wl_list_insert(d->input_list.prev, &input->link);
1797
1798 wl_input_device_add_listener(input->input_device,
1799 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001800 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001801}
1802
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001803struct selection_offer {
1804 struct display *display;
1805 struct wl_selection_offer *offer;
1806 struct wl_array types;
1807 struct input *input;
1808};
1809
1810int
1811input_offers_mime_type(struct input *input, const char *type)
1812{
1813 struct selection_offer *offer = input->offer;
1814 char **p, **end;
1815
1816 if (offer == NULL)
1817 return 0;
1818
1819 end = offer->types.data + offer->types.size;
1820 for (p = offer->types.data; p < end; p++)
1821 if (strcmp(*p, type) == 0)
1822 return 1;
1823
1824 return 0;
1825}
1826
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001827void
1828input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001829{
1830 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001831
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001832 /* FIXME: A number of things can go wrong here: the object may
1833 * not be the current selection offer any more (which could
1834 * still work, but the source may have gone away or just
1835 * destroyed its wl_selection) or the offer may not have the
1836 * requested type after all (programmer/client error,
1837 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001838 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001839}
1840
1841static void
1842selection_offer_offer(void *data,
1843 struct wl_selection_offer *selection_offer,
1844 const char *type)
1845{
1846 struct selection_offer *offer = data;
1847
1848 char **p;
1849
1850 p = wl_array_add(&offer->types, sizeof *p);
1851 if (p)
1852 *p = strdup(type);
1853};
1854
1855static void
1856selection_offer_keyboard_focus(void *data,
1857 struct wl_selection_offer *selection_offer,
1858 struct wl_input_device *input_device)
1859{
1860 struct selection_offer *offer = data;
1861 struct input *input;
1862 char **p, **end;
1863
1864 if (input_device == NULL) {
1865 printf("selection offer retracted %p\n", selection_offer);
1866 input = offer->input;
1867 input->offer = NULL;
1868 wl_selection_offer_destroy(selection_offer);
1869 wl_array_release(&offer->types);
1870 free(offer);
1871 return;
1872 }
1873
1874 input = wl_input_device_get_user_data(input_device);
1875 printf("new selection offer %p:", selection_offer);
1876
1877 offer->input = input;
1878 input->offer = offer;
1879 end = offer->types.data + offer->types.size;
1880 for (p = offer->types.data; p < end; p++)
1881 printf(" %s", *p);
1882
1883 printf("\n");
1884}
1885
1886struct wl_selection_offer_listener selection_offer_listener = {
1887 selection_offer_offer,
1888 selection_offer_keyboard_focus
1889};
1890
1891static void
1892add_selection_offer(struct display *d, uint32_t id)
1893{
1894 struct selection_offer *offer;
1895
1896 offer = malloc(sizeof *offer);
1897 if (offer == NULL)
1898 return;
1899
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001900 offer->offer =
1901 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001902 offer->display = d;
1903 wl_array_init(&offer->types);
1904 offer->input = NULL;
1905
1906 wl_selection_offer_add_listener(offer->offer,
1907 &selection_offer_listener, offer);
1908}
1909
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001910static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001911display_handle_global(struct wl_display *display, uint32_t id,
1912 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001913{
1914 struct display *d = data;
1915
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001916 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001917 d->compositor =
1918 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001919 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001920 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001921 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001922 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001923 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001924 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001925 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001926 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001927 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001928 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001929 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001930 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001931 }
1932}
1933
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001934static void
1935display_render_frame(struct display *d)
1936{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001937 int radius = 8;
1938 cairo_t *cr;
1939
1940 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1941 cr = cairo_create(d->shadow);
1942 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1943 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1944 rounded_rect(cr, 16, 16, 112, 112, radius);
1945 cairo_fill(cr);
1946 cairo_destroy(cr);
1947 blur_surface(d->shadow, 64);
1948
1949 d->active_frame =
1950 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1951 cr = cairo_create(d->active_frame);
1952 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1953 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1954 rounded_rect(cr, 16, 16, 112, 112, radius);
1955 cairo_fill(cr);
1956 cairo_destroy(cr);
1957
1958 d->inactive_frame =
1959 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1960 cr = cairo_create(d->inactive_frame);
1961 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1962 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1963 rounded_rect(cr, 16, 16, 112, 112, radius);
1964 cairo_fill(cr);
1965 cairo_destroy(cr);
1966}
1967
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001968static void
1969init_xkb(struct display *d)
1970{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001971 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001972
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001973 names.rules = "evdev";
1974 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001975 names.layout = option_xkb_layout;
1976 names.variant = option_xkb_variant;
1977 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001978
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001979 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001980 if (!d->xkb) {
1981 fprintf(stderr, "Failed to compile keymap\n");
1982 exit(1);
1983 }
1984}
1985
Yuval Fledel45568f62010-12-06 09:18:12 -05001986static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001987init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001988{
1989 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001990 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001991
1992 static const EGLint premul_argb_cfg_attribs[] = {
1993 EGL_SURFACE_TYPE,
1994 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
1995 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001996 EGL_RED_SIZE, 1,
1997 EGL_GREEN_SIZE, 1,
1998 EGL_BLUE_SIZE, 1,
1999 EGL_ALPHA_SIZE, 1,
2000 EGL_DEPTH_SIZE, 1,
2001 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2002 EGL_NONE
2003 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002004
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002005 static const EGLint rgb_cfg_attribs[] = {
2006 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2007 EGL_RED_SIZE, 1,
2008 EGL_GREEN_SIZE, 1,
2009 EGL_BLUE_SIZE, 1,
2010 EGL_ALPHA_SIZE, 0,
2011 EGL_DEPTH_SIZE, 1,
2012 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2013 EGL_NONE
2014 };
2015
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002016 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002017 if (!eglInitialize(d->dpy, &major, &minor)) {
2018 fprintf(stderr, "failed to initialize display\n");
2019 return -1;
2020 }
2021
2022 if (!eglBindAPI(EGL_OPENGL_API)) {
2023 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2024 return -1;
2025 }
2026
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002027 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002028 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002029 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002030 return -1;
2031 }
2032
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002033 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2034 &d->rgb_config, 1, &n) || n != 1) {
2035 fprintf(stderr, "failed to choose rgb config\n");
2036 return -1;
2037 }
2038
2039 d->ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05002040 if (d->ctx == NULL) {
2041 fprintf(stderr, "failed to create context\n");
2042 return -1;
2043 }
2044
2045 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002046 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002047 return -1;
2048 }
2049
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002050#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002051 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04002052 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002053 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002054 return -1;
2055 }
2056#endif
2057
2058 return 0;
2059}
2060
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002061static int
2062event_mask_update(uint32_t mask, void *data)
2063{
2064 struct display *d = data;
2065
2066 d->mask = mask;
2067
2068 return 0;
2069}
2070
2071static void
2072handle_display_data(struct task *task, uint32_t events)
2073{
2074 struct display *display =
2075 container_of(task, struct display, display_task);
2076
2077 wl_display_iterate(display->display, display->mask);
2078}
2079
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002080struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002081display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002082{
2083 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002084 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002085 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002086 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002087
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002088 g_type_init();
2089
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002090 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002091 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002092 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002093
2094 xkb_option_group = g_option_group_new("xkb",
2095 "Keyboard options",
2096 "Show all XKB options",
2097 NULL, NULL);
2098 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2099 g_option_context_add_group (context, xkb_option_group);
2100
2101 if (!g_option_context_parse(context, argc, argv, &error)) {
2102 fprintf(stderr, "option parsing failed: %s\n", error->message);
2103 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002104 }
2105
Tim Wiederhake748f6722011-01-23 23:25:25 +01002106 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002107
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002108 d = malloc(sizeof *d);
2109 if (d == NULL)
2110 return NULL;
2111
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002112 memset(d, 0, sizeof *d);
2113
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002114 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002115 if (d->display == NULL) {
2116 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002117 return NULL;
2118 }
2119
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002120 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2121 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2122 d->display_task.run = handle_display_data;
2123 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2124
2125 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002126 wl_list_init(&d->input_list);
2127
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002128 /* Set up listener so we'll catch all events. */
2129 wl_display_add_global_listener(d->display,
2130 display_handle_global, d);
2131
2132 /* Process connection events. */
2133 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002134 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002135 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002136
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002137 d->image_target_texture_2d =
2138 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2139 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2140 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2141
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002142 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002143
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002144 display_render_frame(d);
2145
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002146 wl_list_init(&d->window_list);
2147
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002148 init_xkb(d);
2149
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002150 return d;
2151}
2152
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002153struct wl_display *
2154display_get_display(struct display *display)
2155{
2156 return display->display;
2157}
2158
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002159struct wl_compositor *
2160display_get_compositor(struct display *display)
2161{
2162 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002163}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002164
2165EGLDisplay
2166display_get_egl_display(struct display *d)
2167{
2168 return d->dpy;
2169}
2170
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002171EGLConfig
2172display_get_egl_config(struct display *d)
2173{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002174 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002175}
2176
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002177struct wl_shell *
2178display_get_shell(struct display *display)
2179{
2180 return display->shell;
2181}
2182
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002183void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002184display_acquire_window_surface(struct display *display,
2185 struct window *window,
2186 EGLContext ctx)
2187{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002188#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002189 struct egl_window_surface_data *data;
2190
2191 if (!window->cairo_surface)
2192 return;
2193
2194 if (!ctx)
2195 ctx = display->ctx;
2196
2197 data = cairo_surface_get_user_data(window->cairo_surface,
2198 &surface_data_key);
2199
2200 cairo_device_acquire(display->device);
2201 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2202 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke22d54812011-07-16 19:50:32 +00002203#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002204}
2205
2206void
2207display_release(struct display *display)
2208{
2209 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2210 fprintf(stderr, "failed to make context current\n");
2211 cairo_device_release(display->device);
2212}
2213
2214void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002215display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002216{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002217 wl_list_insert(&display->deferred_list, &task->link);
2218}
2219
2220void
2221display_watch_fd(struct display *display,
2222 int fd, uint32_t events, struct task *task)
2223{
2224 struct epoll_event ep;
2225
2226 ep.events = events;
2227 ep.data.ptr = task;
2228 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2229}
2230
2231void
2232display_run(struct display *display)
2233{
2234 struct task *task;
2235 struct epoll_event ep[16];
2236 int i, count;
2237
2238 while (1) {
2239 while (display->mask & WL_DISPLAY_WRITABLE)
2240 wl_display_iterate(display->display,
2241 WL_DISPLAY_WRITABLE);
2242
2243 count = epoll_wait(display->epoll_fd,
2244 ep, ARRAY_LENGTH(ep), -1);
2245 for (i = 0; i < count; i++) {
2246 task = ep[i].data.ptr;
2247 task->run(task, ep[i].events);
2248 }
2249
2250 while (!wl_list_empty(&display->deferred_list)) {
2251 task = container_of(display->deferred_list.next,
2252 struct task, link);
2253 wl_list_remove(&task->link);
2254 task->run(task, 0);
2255 }
2256 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002257}