blob: 9e92adb9fb08b2167f10e83fc96702ff7fc6611b [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øgsberg6a1b2012009-12-16 14:43:37 -0500724window_attach_surface(struct window *window)
725{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400726 struct display *display = window->display;
727 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400728 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000729#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100730 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000731#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500732 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733
734 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000735#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
737 data = cairo_surface_get_user_data(window->cairo_surface,
738 &surface_data_key);
739
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 cairo_gl_surface_swapbuffers(window->cairo_surface);
741 wl_egl_window_get_attached_size(data->window,
742 &window->server_allocation.width,
743 &window->server_allocation.height);
744 break;
745 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000746#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100747 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200748 window_get_resize_dx_dy(window, &x, &y);
749
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100750 if (window->pending_surface != NULL)
751 return;
752
753 window->pending_surface = window->cairo_surface;
754 window->cairo_surface = NULL;
755
756 buffer =
757 display_get_buffer_for_surface(display,
758 window->pending_surface);
759
760 wl_surface_attach(window->surface, buffer, x, y);
761 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400762 cb = wl_display_sync(display->display);
763 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100764 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000765 default:
766 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100767 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500768
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400769 switch (window->type) {
770 case TYPE_FULLSCREEN:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400771 wl_shell_set_fullscreen(display->shell, window->surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400772 break;
773 case TYPE_TOPLEVEL:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400774 wl_shell_set_toplevel(display->shell, window->surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400775 break;
776 case TYPE_TRANSIENT:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400777 wl_shell_set_transient(display->shell, window->surface,
778 window->parent->surface,
779 window->x, window->y, 0);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400780 break;
781 case TYPE_CUSTOM:
782 break;
783 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500784
785 wl_surface_damage(window->surface, 0, 0,
786 window->allocation.width,
787 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500788}
789
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500790void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400791window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500792{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100793 if (window->cairo_surface) {
794 switch (window->buffer_type) {
795 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
796 case WINDOW_BUFFER_TYPE_SHM:
797 display_surface_damage(window->display,
798 window->cairo_surface,
799 0, 0,
800 window->allocation.width,
801 window->allocation.height);
802 break;
803 default:
804 break;
805 }
806 window_attach_surface(window);
807 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500808}
809
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400810void
811window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400812{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500813 cairo_surface_reference(surface);
814
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400815 if (window->cairo_surface != NULL)
816 cairo_surface_destroy(window->cairo_surface);
817
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500818 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400819}
820
Benjamin Franzke22d54812011-07-16 19:50:32 +0000821#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822static void
823window_resize_cairo_window_surface(struct window *window)
824{
825 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200826 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827
828 data = cairo_surface_get_user_data(window->cairo_surface,
829 &surface_data_key);
830
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200831 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100832 wl_egl_window_resize(data->window,
833 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200834 window->allocation.height,
835 x,y);
836
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 cairo_gl_surface_set_size(window->cairo_surface,
838 window->allocation.width,
839 window->allocation.height);
840}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000841#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100842
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400843void
844window_create_surface(struct window *window)
845{
846 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400847 uint32_t flags = 0;
848
849 if (!window->transparent)
850 flags = SURFACE_OPAQUE;
851
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400852 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500853#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
855 if (window->cairo_surface) {
856 window_resize_cairo_window_surface(window);
857 return;
858 }
859 surface = display_create_surface(window->display,
860 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400861 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100862 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500863 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400864 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100865 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400866 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400867 break;
868#endif
869 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400870 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400871 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400872 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800873 default:
874 surface = NULL;
875 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400876 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400877
878 window_set_surface(window, surface);
879 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400880}
881
882static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500883window_draw_menu(struct window *window)
884{
885 cairo_t *cr;
886 int width, height, r = 5;
887
888 window_create_surface(window);
889
890 cr = cairo_create(window->cairo_surface);
891 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
892 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
893 cairo_paint(cr);
894
895 width = window->allocation.width;
896 height = window->allocation.height;
897 rounded_rect(cr, r, r, width - r, height - r, r);
898 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
899 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
900 cairo_fill(cr);
901 cairo_destroy(cr);
902}
903
904static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500905window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500906{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500907 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500908 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400909 cairo_surface_t *frame;
910 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500911
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400912 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400913
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400914 width = window->allocation.width;
915 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500916
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500917 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500918
Kristian Høgsberg09531622010-06-14 23:22:15 -0400919 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400920 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400921 cairo_paint(cr);
922
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400923 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400924 tile_mask(cr, window->display->shadow,
925 shadow_dx, shadow_dy, width, height,
926 window->margin + 10 - shadow_dx,
927 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500928
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400929 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400930 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400931 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400932 frame = window->display->inactive_frame;
933
934 tile_source(cr, frame, 0, 0, width, height,
935 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500936
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500937 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
938 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500939 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400940 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400941 if (window->keyboard_device)
942 cairo_set_source_rgb(cr, 0, 0, 0);
943 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400944 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400945 cairo_show_text(cr, window->title);
946
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500947 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400948
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100949 /* FIXME: this breakes gears, fix cairo? */
950#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400951 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100952#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500953}
954
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400955void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500956window_destroy(struct window *window)
957{
958 wl_surface_destroy(window->surface);
959 wl_list_remove(&window->link);
960 free(window);
961}
962
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400963static struct item *
964window_find_item(struct window *window, int32_t x, int32_t y)
965{
966 struct item *item;
967
968 wl_list_for_each(item, &window->item_list, link) {
969 if (item->allocation.x <= x &&
970 x < item->allocation.x + item->allocation.width &&
971 item->allocation.y <= y &&
972 y < item->allocation.y + item->allocation.height) {
973 return item;
974 }
975 }
976
977 return NULL;
978}
979
980struct item *
981window_add_item(struct window *window, void *data)
982{
983 struct item *item;
984
985 item = malloc(sizeof *item);
986 memset(item, 0, sizeof *item);
987 item->user_data = data;
988 wl_list_insert(window->item_list.prev, &item->link);
989
990 return item;
991}
992
993void
994window_for_each_item(struct window *window, item_func_t func, void *data)
995{
996 struct item *item;
997
998 wl_list_for_each(item, &window->item_list, link)
999 func(item, data);
1000}
1001
1002struct item *
1003window_get_focus_item(struct window *window)
1004{
1005 return window->focus_item;
1006}
1007
1008void
1009item_get_allocation(struct item *item, struct rectangle *allocation)
1010{
1011 *allocation = item->allocation;
1012}
1013
1014void
1015item_set_allocation(struct item *item,
1016 int32_t x, int32_t y, int32_t width, int32_t height)
1017{
1018 item->allocation.x = x;
1019 item->allocation.y = y;
1020 item->allocation.width = width;
1021 item->allocation.height = height;
1022}
1023
1024void *
1025item_get_user_data(struct item *item)
1026{
1027 return item->user_data;
1028}
1029
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001030void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001031display_flush_cairo_device(struct display *display)
1032{
1033 cairo_device_flush (display->device);
1034}
1035
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001036void
1037window_draw(struct window *window)
1038{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001039 if (window->parent)
1040 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001041 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001042 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001043 else
1044 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001045}
1046
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001047cairo_surface_t *
1048window_get_surface(struct window *window)
1049{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001050 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001051}
1052
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001053struct wl_surface *
1054window_get_wl_surface(struct window *window)
1055{
1056 return window->surface;
1057}
1058
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001059static int
1060get_pointer_location(struct window *window, int32_t x, int32_t y)
1061{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001062 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001063 const int grip_size = 8;
1064
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001065 if (!window->decoration)
1066 return WINDOW_CLIENT_AREA;
1067
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001068 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001069 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001070 else if (window->margin <= x && x < window->margin + grip_size)
1071 hlocation = WINDOW_RESIZING_LEFT;
1072 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001073 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001074 else if (x < window->allocation.width - window->margin)
1075 hlocation = WINDOW_RESIZING_RIGHT;
1076 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001077 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001078
1079 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001080 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001081 else if (window->margin <= y && y < window->margin + grip_size)
1082 vlocation = WINDOW_RESIZING_TOP;
1083 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001084 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001085 else if (y < window->allocation.height - window->margin)
1086 vlocation = WINDOW_RESIZING_BOTTOM;
1087 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001088 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001089
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001090 location = vlocation | hlocation;
1091 if (location & WINDOW_EXTERIOR)
1092 location = WINDOW_EXTERIOR;
1093 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1094 location = WINDOW_TITLEBAR;
1095 else if (location == WINDOW_INTERIOR)
1096 location = WINDOW_CLIENT_AREA;
1097
1098 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001099}
1100
1101static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001102set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001103{
1104 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001105 struct wl_buffer *buffer;
1106 cairo_surface_t *surface;
1107 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001108
1109 location = get_pointer_location(input->pointer_focus,
1110 input->sx, input->sy);
1111 switch (location) {
1112 case WINDOW_RESIZING_TOP:
1113 pointer = POINTER_TOP;
1114 break;
1115 case WINDOW_RESIZING_BOTTOM:
1116 pointer = POINTER_BOTTOM;
1117 break;
1118 case WINDOW_RESIZING_LEFT:
1119 pointer = POINTER_LEFT;
1120 break;
1121 case WINDOW_RESIZING_RIGHT:
1122 pointer = POINTER_RIGHT;
1123 break;
1124 case WINDOW_RESIZING_TOP_LEFT:
1125 pointer = POINTER_TOP_LEFT;
1126 break;
1127 case WINDOW_RESIZING_TOP_RIGHT:
1128 pointer = POINTER_TOP_RIGHT;
1129 break;
1130 case WINDOW_RESIZING_BOTTOM_LEFT:
1131 pointer = POINTER_BOTTOM_LEFT;
1132 break;
1133 case WINDOW_RESIZING_BOTTOM_RIGHT:
1134 pointer = POINTER_BOTTOM_RIGHT;
1135 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001136 case WINDOW_EXTERIOR:
1137 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001138 if (input->current_pointer_image == POINTER_DEFAULT)
1139 return;
1140
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001141 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001142 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001143 return;
1144 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001145 break;
1146 }
1147
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001148 if (pointer == input->current_pointer_image)
1149 return;
1150
1151 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001152 surface = display->pointer_surfaces[pointer];
1153 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001154 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001155 pointer_images[pointer].hotspot_x,
1156 pointer_images[pointer].hotspot_y);
1157}
1158
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001159static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001160window_set_focus_item(struct window *window, struct item *focus)
1161{
1162 void *data;
1163
1164 if (focus == window->focus_item)
1165 return;
1166
1167 window->focus_item = focus;
1168 data = focus ? focus->user_data : NULL;
1169 if (window->item_focus_handler)
1170 window->item_focus_handler(window, focus, data);
1171}
1172
1173static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001174window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001175 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001176 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001177{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001178 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001179 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001180 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001181 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001182
1183 input->x = x;
1184 input->y = y;
1185 input->sx = sx;
1186 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001187
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001188 if (!window->focus_item || !window->item_grab_button) {
1189 item = window_find_item(window, sx, sy);
1190 window_set_focus_item(window, item);
1191 }
1192
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001193 if (window->motion_handler)
1194 pointer = (*window->motion_handler)(window, input, time,
1195 x, y, sx, sy,
1196 window->user_data);
1197
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001198 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001199}
1200
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001201static void
1202window_handle_button(void *data,
1203 struct wl_input_device *input_device,
1204 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001205{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001206 struct input *input = data;
1207 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001208 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001210
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001211 if (window->focus_item && window->item_grab_button == 0 && state)
1212 window->item_grab_button = button;
1213
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001214 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001215
1216 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001217 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001218 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001219 wl_shell_move(window->display->shell,
1220 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001221 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001222 case WINDOW_RESIZING_TOP:
1223 case WINDOW_RESIZING_BOTTOM:
1224 case WINDOW_RESIZING_LEFT:
1225 case WINDOW_RESIZING_RIGHT:
1226 case WINDOW_RESIZING_TOP_LEFT:
1227 case WINDOW_RESIZING_TOP_RIGHT:
1228 case WINDOW_RESIZING_BOTTOM_LEFT:
1229 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001230 wl_shell_resize(window->display->shell,
1231 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001232 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001233 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001234 case WINDOW_CLIENT_AREA:
1235 if (window->button_handler)
1236 (*window->button_handler)(window,
1237 input, time,
1238 button, state,
1239 window->user_data);
1240 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001241 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001242 } else {
1243 if (window->button_handler)
1244 (*window->button_handler)(window,
1245 input, time,
1246 button, state,
1247 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001248 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001249
1250 if (window->focus_item &&
1251 window->item_grab_button == button && !state) {
1252 window->item_grab_button = 0;
1253 item = window_find_item(window, input->sx, input->sy);
1254 window_set_focus_item(window, item);
1255 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001256}
1257
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001258static void
1259window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001260 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001261{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001262 struct input *input = data;
1263 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001264 struct display *d = window->display;
1265 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001266
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001267 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001268 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001269 return;
1270
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001271 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001272 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001273 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1274 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001275
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001276 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1277
1278 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001279 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001280 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001281 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001282
1283 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001284 (*window->key_handler)(window, input, time, key, sym, state,
1285 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001286}
1287
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001288static void
1289window_handle_pointer_focus(void *data,
1290 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001291 uint32_t time, struct wl_surface *surface,
1292 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001293{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001294 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001295 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001296 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001297 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001298
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001299 window = input->pointer_focus;
1300 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001301 window_set_focus_item(window, NULL);
1302
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001303 if (window->leave_handler)
1304 window->leave_handler(window, input,
1305 time, window->user_data);
1306 input->pointer_focus = NULL;
1307 input->current_pointer_image = POINTER_UNSET;
1308 }
1309
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001310 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001311 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001312 window = input->pointer_focus;
1313
Kristian Høgsberg59826582011-01-20 11:56:57 -05001314 input->x = x;
1315 input->y = y;
1316 input->sx = sx;
1317 input->sy = sy;
1318
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001319 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001320 if (window->enter_handler)
1321 pointer = window->enter_handler(window, input,
1322 time, sx, sy,
1323 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001324
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001325 item = window_find_item(window, x, y);
1326 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001327
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001328 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001329 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001330}
1331
1332static void
1333window_handle_keyboard_focus(void *data,
1334 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001335 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001336 struct wl_surface *surface,
1337 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001338{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001339 struct input *input = data;
1340 struct window *window = input->keyboard_focus;
1341 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001342 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001343
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001344 window = input->keyboard_focus;
1345 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001346 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001347 if (window->keyboard_focus_handler)
1348 (*window->keyboard_focus_handler)(window, NULL,
1349 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001350 }
1351
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001352 if (surface)
1353 input->keyboard_focus = wl_surface_get_user_data(surface);
1354 else
1355 input->keyboard_focus = NULL;
1356
1357 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001358 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001359 for (k = keys->data; k < end; k++)
1360 input->modifiers |= d->xkb->map->modmap[*k];
1361
1362 window = input->keyboard_focus;
1363 if (window) {
1364 window->keyboard_device = input;
1365 if (window->keyboard_focus_handler)
1366 (*window->keyboard_focus_handler)(window,
1367 window->keyboard_device,
1368 window->user_data);
1369 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001370}
1371
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001372static const struct wl_input_device_listener input_device_listener = {
1373 window_handle_motion,
1374 window_handle_button,
1375 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001376 window_handle_pointer_focus,
1377 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001378};
1379
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001380void
1381input_get_position(struct input *input, int32_t *x, int32_t *y)
1382{
1383 *x = input->sx;
1384 *y = input->sy;
1385}
1386
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001387struct wl_input_device *
1388input_get_input_device(struct input *input)
1389{
1390 return input->input_device;
1391}
1392
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001393uint32_t
1394input_get_modifiers(struct input *input)
1395{
1396 return input->modifiers;
1397}
1398
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001399struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001400window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001401{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001402 cairo_device_flush (window->display->device);
1403
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001404 return wl_shell_create_drag(window->display->shell);
1405}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001406
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001407void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001408window_move(struct window *window, struct input *input, uint32_t time)
1409{
1410 wl_shell_move(window->display->shell,
1411 window->surface, input->input_device, time);
1412}
1413
1414void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001415window_activate_drag(struct wl_drag *drag, struct window *window,
1416 struct input *input, uint32_t time)
1417{
1418 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001419}
1420
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001421static void
1422handle_configure(void *data, struct wl_shell *shell,
1423 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001424 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001425{
1426 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001427 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001428
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001429 /* FIXME: this is probably the wrong place to check for width
1430 * or height <= 0, but it prevents the compositor from crashing
1431 */
1432 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001433 return;
1434
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001435 window->resize_edges = edges;
1436
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001437 if (window->resize_handler) {
1438 child_width = width - 20 - window->margin * 2;
1439 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001440
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001441 (*window->resize_handler)(window,
1442 child_width, child_height,
1443 window->user_data);
1444 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001445 window->allocation.width = width;
1446 window->allocation.height = height;
1447
1448 if (window->redraw_handler)
1449 window_schedule_redraw(window);
1450 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001451}
1452
1453static const struct wl_shell_listener shell_listener = {
1454 handle_configure,
1455};
1456
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001457void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001458window_get_allocation(struct window *window,
1459 struct rectangle *allocation)
1460{
1461 *allocation = window->allocation;
1462}
1463
1464void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001465window_get_child_allocation(struct window *window,
1466 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001467{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001468 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001469 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001470 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001471 allocation->x = window->margin + 10;
1472 allocation->y = window->margin + 50;
1473 allocation->width =
1474 window->allocation.width - 20 - window->margin * 2;
1475 allocation->height =
1476 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001477 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001478}
1479
1480void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001481window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001482{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001483 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001484 window->allocation.x = 20 + window->margin;
1485 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001486 window->allocation.width = width + 20 + window->margin * 2;
1487 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001488 } else {
1489 window->allocation.x = 0;
1490 window->allocation.y = 0;
1491 window->allocation.width = width;
1492 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001493 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001494}
1495
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001496static void
1497idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001498{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001499 struct window *window =
1500 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001501
1502 window->redraw_handler(window, window->user_data);
1503 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001504}
1505
1506void
1507window_schedule_redraw(struct window *window)
1508{
1509 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001510 window->redraw_task.run = idle_redraw;
1511 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001512 window->redraw_scheduled = 1;
1513 }
1514}
1515
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001516void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001517window_set_custom(struct window *window)
1518{
1519 window->type = TYPE_CUSTOM;
1520}
1521
1522void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001523window_set_fullscreen(struct window *window, int fullscreen)
1524{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001525 int32_t width, height;
1526
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001527 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001528 return;
1529
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001530 if (fullscreen) {
1531 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001532 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001533 width = window->display->screen_allocation.width;
1534 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001535 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001536 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001537 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001538 width = window->saved_allocation.width - 20 - window->margin * 2;
1539 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001540 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001541 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001542
1543 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001544}
1545
1546void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001547window_set_decoration(struct window *window, int decoration)
1548{
1549 window->decoration = decoration;
1550}
1551
1552void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001553window_set_user_data(struct window *window, void *data)
1554{
1555 window->user_data = data;
1556}
1557
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001558void *
1559window_get_user_data(struct window *window)
1560{
1561 return window->user_data;
1562}
1563
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001564void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001565window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001566 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001567{
1568 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001569}
1570
1571void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001572window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001573 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001574{
1575 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001576}
1577
1578void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001579window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001580 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001581{
1582 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001583}
1584
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001585void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001586window_set_button_handler(struct window *window,
1587 window_button_handler_t handler)
1588{
1589 window->button_handler = handler;
1590}
1591
1592void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001593window_set_motion_handler(struct window *window,
1594 window_motion_handler_t handler)
1595{
1596 window->motion_handler = handler;
1597}
1598
1599void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001600window_set_enter_handler(struct window *window,
1601 window_enter_handler_t handler)
1602{
1603 window->enter_handler = handler;
1604}
1605
1606void
1607window_set_leave_handler(struct window *window,
1608 window_leave_handler_t handler)
1609{
1610 window->leave_handler = handler;
1611}
1612
1613void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001614window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001615 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001616{
1617 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001618}
1619
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001620void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001621window_set_item_focus_handler(struct window *window,
1622 window_item_focus_handler_t handler)
1623{
1624 window->item_focus_handler = handler;
1625}
1626
1627void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001628window_set_transparent(struct window *window, int transparent)
1629{
1630 window->transparent = transparent;
1631}
1632
1633void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001634window_set_title(struct window *window, const char *title)
1635{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001636 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001637 window->title = strdup(title);
1638}
1639
1640const char *
1641window_get_title(struct window *window)
1642{
1643 return window->title;
1644}
1645
1646void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001647display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1648 int32_t x, int32_t y, int32_t width, int32_t height)
1649{
1650 struct wl_buffer *buffer;
1651
1652 buffer = display_get_buffer_for_surface(display, cairo_surface);
1653
1654 wl_buffer_damage(buffer, x, y, width, height);
1655}
1656
1657void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001658window_damage(struct window *window, int32_t x, int32_t y,
1659 int32_t width, int32_t height)
1660{
1661 wl_surface_damage(window->surface, x, y, width, height);
1662}
1663
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001664static struct window *
1665window_create_internal(struct display *display, struct window *parent,
1666 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001667{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001668 struct window *window;
1669
1670 window = malloc(sizeof *window);
1671 if (window == NULL)
1672 return NULL;
1673
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001674 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001675 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001676 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001677 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001678 window->allocation.x = 0;
1679 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001680 window->allocation.width = width;
1681 window->allocation.height = height;
1682 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001683 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001684 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001685 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001687
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001688 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001689#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001690 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1691 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001692#else
1693 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1694#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001695 else
1696 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001697
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001698 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001699 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001700
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001701 return window;
1702}
1703
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001704struct window *
1705window_create(struct display *display, int32_t width, int32_t height)
1706{
1707 struct window *window;
1708
1709 window = window_create_internal(display, NULL, width, height);
1710 if (!window)
1711 return NULL;
1712
1713 return window;
1714}
1715
1716struct window *
1717window_create_transient(struct display *display, struct window *parent,
1718 int32_t x, int32_t y, int32_t width, int32_t height)
1719{
1720 struct window *window;
1721
1722 window = window_create_internal(parent->display,
1723 parent, width, height);
1724 if (!window)
1725 return NULL;
1726
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001727 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001728 window->x = x;
1729 window->y = y;
1730
1731 return window;
1732}
1733
1734void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001735window_set_buffer_type(struct window *window, enum window_buffer_type type)
1736{
1737 window->buffer_type = type;
1738}
1739
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001740
1741static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001742display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001743 struct wl_output *wl_output,
1744 int x, int y,
1745 int physical_width,
1746 int physical_height,
1747 int subpixel,
1748 const char *make,
1749 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001750{
1751 struct display *display = data;
1752
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001753 display->screen_allocation.x = x;
1754 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001755}
1756
1757static void
1758display_handle_mode(void *data,
1759 struct wl_output *wl_output,
1760 uint32_t flags,
1761 int width,
1762 int height,
1763 int refresh)
1764{
1765 struct display *display = data;
1766
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001767 display->screen_allocation.width = width;
1768 display->screen_allocation.height = height;
1769}
1770
1771static const struct wl_output_listener output_listener = {
1772 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001773 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001774};
1775
1776static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001777display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001778{
1779 struct input *input;
1780
1781 input = malloc(sizeof *input);
1782 if (input == NULL)
1783 return;
1784
1785 memset(input, 0, sizeof *input);
1786 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001787 input->input_device =
1788 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001789 input->pointer_focus = NULL;
1790 input->keyboard_focus = NULL;
1791 wl_list_insert(d->input_list.prev, &input->link);
1792
1793 wl_input_device_add_listener(input->input_device,
1794 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001795 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001796}
1797
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001798struct selection_offer {
1799 struct display *display;
1800 struct wl_selection_offer *offer;
1801 struct wl_array types;
1802 struct input *input;
1803};
1804
1805int
1806input_offers_mime_type(struct input *input, const char *type)
1807{
1808 struct selection_offer *offer = input->offer;
1809 char **p, **end;
1810
1811 if (offer == NULL)
1812 return 0;
1813
1814 end = offer->types.data + offer->types.size;
1815 for (p = offer->types.data; p < end; p++)
1816 if (strcmp(*p, type) == 0)
1817 return 1;
1818
1819 return 0;
1820}
1821
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001822void
1823input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001824{
1825 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001826
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001827 /* FIXME: A number of things can go wrong here: the object may
1828 * not be the current selection offer any more (which could
1829 * still work, but the source may have gone away or just
1830 * destroyed its wl_selection) or the offer may not have the
1831 * requested type after all (programmer/client error,
1832 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001833 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001834}
1835
1836static void
1837selection_offer_offer(void *data,
1838 struct wl_selection_offer *selection_offer,
1839 const char *type)
1840{
1841 struct selection_offer *offer = data;
1842
1843 char **p;
1844
1845 p = wl_array_add(&offer->types, sizeof *p);
1846 if (p)
1847 *p = strdup(type);
1848};
1849
1850static void
1851selection_offer_keyboard_focus(void *data,
1852 struct wl_selection_offer *selection_offer,
1853 struct wl_input_device *input_device)
1854{
1855 struct selection_offer *offer = data;
1856 struct input *input;
1857 char **p, **end;
1858
1859 if (input_device == NULL) {
1860 printf("selection offer retracted %p\n", selection_offer);
1861 input = offer->input;
1862 input->offer = NULL;
1863 wl_selection_offer_destroy(selection_offer);
1864 wl_array_release(&offer->types);
1865 free(offer);
1866 return;
1867 }
1868
1869 input = wl_input_device_get_user_data(input_device);
1870 printf("new selection offer %p:", selection_offer);
1871
1872 offer->input = input;
1873 input->offer = offer;
1874 end = offer->types.data + offer->types.size;
1875 for (p = offer->types.data; p < end; p++)
1876 printf(" %s", *p);
1877
1878 printf("\n");
1879}
1880
1881struct wl_selection_offer_listener selection_offer_listener = {
1882 selection_offer_offer,
1883 selection_offer_keyboard_focus
1884};
1885
1886static void
1887add_selection_offer(struct display *d, uint32_t id)
1888{
1889 struct selection_offer *offer;
1890
1891 offer = malloc(sizeof *offer);
1892 if (offer == NULL)
1893 return;
1894
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001895 offer->offer =
1896 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001897 offer->display = d;
1898 wl_array_init(&offer->types);
1899 offer->input = NULL;
1900
1901 wl_selection_offer_add_listener(offer->offer,
1902 &selection_offer_listener, offer);
1903}
1904
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001905static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001906display_handle_global(struct wl_display *display, uint32_t id,
1907 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001908{
1909 struct display *d = data;
1910
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001911 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001912 d->compositor =
1913 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001914 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001915 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001916 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001917 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001918 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001919 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001920 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001921 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001922 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001923 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001924 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001925 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001926 }
1927}
1928
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001929static void
1930display_render_frame(struct display *d)
1931{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001932 int radius = 8;
1933 cairo_t *cr;
1934
1935 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1936 cr = cairo_create(d->shadow);
1937 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1938 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1939 rounded_rect(cr, 16, 16, 112, 112, radius);
1940 cairo_fill(cr);
1941 cairo_destroy(cr);
1942 blur_surface(d->shadow, 64);
1943
1944 d->active_frame =
1945 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1946 cr = cairo_create(d->active_frame);
1947 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1948 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1949 rounded_rect(cr, 16, 16, 112, 112, radius);
1950 cairo_fill(cr);
1951 cairo_destroy(cr);
1952
1953 d->inactive_frame =
1954 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1955 cr = cairo_create(d->inactive_frame);
1956 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1957 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1958 rounded_rect(cr, 16, 16, 112, 112, radius);
1959 cairo_fill(cr);
1960 cairo_destroy(cr);
1961}
1962
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001963static void
1964init_xkb(struct display *d)
1965{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001966 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001967
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001968 names.rules = "evdev";
1969 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001970 names.layout = option_xkb_layout;
1971 names.variant = option_xkb_variant;
1972 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001973
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001974 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001975 if (!d->xkb) {
1976 fprintf(stderr, "Failed to compile keymap\n");
1977 exit(1);
1978 }
1979}
1980
Yuval Fledel45568f62010-12-06 09:18:12 -05001981static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001982init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001983{
1984 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001985 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001986
1987 static const EGLint premul_argb_cfg_attribs[] = {
1988 EGL_SURFACE_TYPE,
1989 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
1990 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001991 EGL_RED_SIZE, 1,
1992 EGL_GREEN_SIZE, 1,
1993 EGL_BLUE_SIZE, 1,
1994 EGL_ALPHA_SIZE, 1,
1995 EGL_DEPTH_SIZE, 1,
1996 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1997 EGL_NONE
1998 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001999
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002000 static const EGLint rgb_cfg_attribs[] = {
2001 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2002 EGL_RED_SIZE, 1,
2003 EGL_GREEN_SIZE, 1,
2004 EGL_BLUE_SIZE, 1,
2005 EGL_ALPHA_SIZE, 0,
2006 EGL_DEPTH_SIZE, 1,
2007 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2008 EGL_NONE
2009 };
2010
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002011 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002012 if (!eglInitialize(d->dpy, &major, &minor)) {
2013 fprintf(stderr, "failed to initialize display\n");
2014 return -1;
2015 }
2016
2017 if (!eglBindAPI(EGL_OPENGL_API)) {
2018 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2019 return -1;
2020 }
2021
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002022 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002023 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002024 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002025 return -1;
2026 }
2027
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002028 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2029 &d->rgb_config, 1, &n) || n != 1) {
2030 fprintf(stderr, "failed to choose rgb config\n");
2031 return -1;
2032 }
2033
2034 d->ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05002035 if (d->ctx == NULL) {
2036 fprintf(stderr, "failed to create context\n");
2037 return -1;
2038 }
2039
2040 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002041 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002042 return -1;
2043 }
2044
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002045#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002046 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04002047 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002048 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002049 return -1;
2050 }
2051#endif
2052
2053 return 0;
2054}
2055
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002056static int
2057event_mask_update(uint32_t mask, void *data)
2058{
2059 struct display *d = data;
2060
2061 d->mask = mask;
2062
2063 return 0;
2064}
2065
2066static void
2067handle_display_data(struct task *task, uint32_t events)
2068{
2069 struct display *display =
2070 container_of(task, struct display, display_task);
2071
2072 wl_display_iterate(display->display, display->mask);
2073}
2074
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002075struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002076display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002077{
2078 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002079 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002080 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002081 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002082
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002083 g_type_init();
2084
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002085 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002086 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002087 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002088
2089 xkb_option_group = g_option_group_new("xkb",
2090 "Keyboard options",
2091 "Show all XKB options",
2092 NULL, NULL);
2093 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2094 g_option_context_add_group (context, xkb_option_group);
2095
2096 if (!g_option_context_parse(context, argc, argv, &error)) {
2097 fprintf(stderr, "option parsing failed: %s\n", error->message);
2098 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002099 }
2100
Tim Wiederhake748f6722011-01-23 23:25:25 +01002101 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002102
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002103 d = malloc(sizeof *d);
2104 if (d == NULL)
2105 return NULL;
2106
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002107 memset(d, 0, sizeof *d);
2108
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002109 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002110 if (d->display == NULL) {
2111 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002112 return NULL;
2113 }
2114
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002115 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2116 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2117 d->display_task.run = handle_display_data;
2118 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2119
2120 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002121 wl_list_init(&d->input_list);
2122
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002123 /* Set up listener so we'll catch all events. */
2124 wl_display_add_global_listener(d->display,
2125 display_handle_global, d);
2126
2127 /* Process connection events. */
2128 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002129 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002130 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002131
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002132 d->image_target_texture_2d =
2133 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2134 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2135 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2136
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002137 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002138
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002139 display_render_frame(d);
2140
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002141 wl_list_init(&d->window_list);
2142
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002143 init_xkb(d);
2144
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002145 return d;
2146}
2147
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002148struct wl_display *
2149display_get_display(struct display *display)
2150{
2151 return display->display;
2152}
2153
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002154struct wl_compositor *
2155display_get_compositor(struct display *display)
2156{
2157 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002158}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002159
2160EGLDisplay
2161display_get_egl_display(struct display *d)
2162{
2163 return d->dpy;
2164}
2165
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002166EGLConfig
2167display_get_egl_config(struct display *d)
2168{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002169 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002170}
2171
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002172struct wl_shell *
2173display_get_shell(struct display *display)
2174{
2175 return display->shell;
2176}
2177
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002178void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002179display_acquire_window_surface(struct display *display,
2180 struct window *window,
2181 EGLContext ctx)
2182{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002183#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002184 struct egl_window_surface_data *data;
2185
2186 if (!window->cairo_surface)
2187 return;
2188
2189 if (!ctx)
2190 ctx = display->ctx;
2191
2192 data = cairo_surface_get_user_data(window->cairo_surface,
2193 &surface_data_key);
2194
2195 cairo_device_acquire(display->device);
2196 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2197 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke22d54812011-07-16 19:50:32 +00002198#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002199}
2200
2201void
2202display_release(struct display *display)
2203{
2204 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2205 fprintf(stderr, "failed to make context current\n");
2206 cairo_device_release(display->device);
2207}
2208
2209void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002210display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002211{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002212 wl_list_insert(&display->deferred_list, &task->link);
2213}
2214
2215void
2216display_watch_fd(struct display *display,
2217 int fd, uint32_t events, struct task *task)
2218{
2219 struct epoll_event ep;
2220
2221 ep.events = events;
2222 ep.data.ptr = task;
2223 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2224}
2225
2226void
2227display_run(struct display *display)
2228{
2229 struct task *task;
2230 struct epoll_event ep[16];
2231 int i, count;
2232
2233 while (1) {
2234 while (display->mask & WL_DISPLAY_WRITABLE)
2235 wl_display_iterate(display->display,
2236 WL_DISPLAY_WRITABLE);
2237
2238 count = epoll_wait(display->epoll_fd,
2239 ep, ARRAY_LENGTH(ep), -1);
2240 for (i = 0; i < count; i++) {
2241 task = ep[i].data.ptr;
2242 task->run(task, ep[i].events);
2243 }
2244
2245 while (!wl_list_empty(&display->deferred_list)) {
2246 task = container_of(display->deferred_list.next,
2247 struct task, link);
2248 wl_list_remove(&task->link);
2249 task->run(task, 0);
2250 }
2251 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002252}