blob: 54adde64954fe7c81f0dc4c18acbee477b372488 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040023#include "../config.h"
24
Kristian Høgsberg61017b12008-11-02 18:51:48 -050025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050029#include <fcntl.h>
30#include <unistd.h>
31#include <math.h>
32#include <time.h>
33#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050034#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040035#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040036#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040037#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040038
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050039#include <wayland-egl.h>
40
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041#include <GL/gl.h>
42#include <EGL/egl.h>
43#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040044
Kristian Høgsberg8def2642011-01-14 17:41:33 -050045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040046#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050048
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040049#include <X11/extensions/XKBcommon.h>
50
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050052#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050053#include "wayland-client.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040054#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050055
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050057
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050058struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050059 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050060 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040061 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040062 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050063 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040065 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040066 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020067 EGLConfig premultiplied_argb_config;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040068 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010069 cairo_device_t *device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040070
71 int display_fd;
72 uint32_t mask;
73 struct task display_task;
74
75 int epoll_fd;
76 struct wl_list deferred_list;
77
Kristian Høgsberg478d9262010-06-08 20:34:11 -040078 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040079 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040080 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040081 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040082 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040083 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040084
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050085 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
86 PFNEGLCREATEIMAGEKHRPROC create_image;
87 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050088};
89
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040090enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040091 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -040092 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040093 TYPE_TRANSIENT,
94 TYPE_CUSTOM
95};
96
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097struct window {
98 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050099 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500100 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500101 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500102 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500103 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400104 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400105 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400106 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500107 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500108 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400109 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400110 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400111 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400112 struct input *grab_device;
113 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500114 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400115 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500116
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400117 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500118 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500119
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500120 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400121 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500122 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400123 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500124 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400125 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400126 window_enter_handler_t enter_handler;
127 window_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400128 window_item_focus_handler_t item_focus_handler;
129
130 struct wl_list item_list;
131 struct item *focus_item;
132 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400133
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500134 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400135 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500136};
137
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400138struct item {
139 struct wl_list link;
140 struct rectangle allocation;
141 void *user_data;
142};
143
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400144struct input {
145 struct display *display;
146 struct wl_input_device *input_device;
147 struct window *pointer_focus;
148 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500149 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400150 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400151 uint32_t modifiers;
152 int32_t x, y, sx, sy;
153 struct wl_list link;
154};
155
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400156enum {
157 POINTER_DEFAULT = 100,
158 POINTER_UNSET
159};
160
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500161enum window_location {
162 WINDOW_INTERIOR = 0,
163 WINDOW_RESIZING_TOP = 1,
164 WINDOW_RESIZING_BOTTOM = 2,
165 WINDOW_RESIZING_LEFT = 4,
166 WINDOW_RESIZING_TOP_LEFT = 5,
167 WINDOW_RESIZING_BOTTOM_LEFT = 6,
168 WINDOW_RESIZING_RIGHT = 8,
169 WINDOW_RESIZING_TOP_RIGHT = 9,
170 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
171 WINDOW_RESIZING_MASK = 15,
172 WINDOW_EXTERIOR = 16,
173 WINDOW_TITLEBAR = 17,
174 WINDOW_CLIENT_AREA = 18,
175};
176
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400177const char *option_xkb_layout = "us";
178const char *option_xkb_variant = "";
179const char *option_xkb_options = "";
180
181static const GOptionEntry xkb_option_entries[] = {
182 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
183 &option_xkb_layout, "XKB Layout" },
184 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
185 &option_xkb_variant, "XKB Variant" },
186 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
187 &option_xkb_options, "XKB Options" },
188 { NULL }
189};
190
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400191static const cairo_user_data_key_t surface_data_key;
192struct surface_data {
193 struct wl_buffer *buffer;
194};
195
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500196#define MULT(_d,c,a,t) \
197 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
198
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500199#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400200
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100201struct egl_window_surface_data {
202 struct display *display;
203 struct wl_surface *surface;
204 struct wl_egl_window *window;
205 EGLSurface surf;
206};
207
208static void
209egl_window_surface_data_destroy(void *p)
210{
211 struct egl_window_surface_data *data = p;
212 struct display *d = data->display;
213
214 eglDestroySurface(d->dpy, data->surf);
215 wl_egl_window_destroy(data->window);
216 data->surface = NULL;
217
218 free(p);
219}
220
221static cairo_surface_t *
222display_create_egl_window_surface(struct display *display,
223 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400224 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100225 struct rectangle *rectangle)
226{
227 cairo_surface_t *cairo_surface;
228 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400229 EGLConfig config;
230 const EGLint *attribs;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100231
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400232 static const EGLint premul_attribs[] = {
233 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
234 EGL_NONE
235 };
236
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100237 data = malloc(sizeof *data);
238 if (data == NULL)
239 return NULL;
240
241 data->display = display;
242 data->surface = surface;
243
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400244 if (flags & SURFACE_OPAQUE) {
245 config = display->rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200246 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400247 } else {
248 config = display->premultiplied_argb_config;
249 attribs = premul_attribs;
250 }
251
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400252 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100253 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400254 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100255
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400256 data->surf = eglCreateWindowSurface(display->dpy, config,
257 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100258
259 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
260 data->surf,
261 rectangle->width,
262 rectangle->height);
263
264 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
265 data, egl_window_surface_data_destroy);
266
267 return cairo_surface;
268}
269
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500270struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400271 struct surface_data data;
272 EGLImageKHR image;
273 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800274 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500275 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400276};
277
278static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500279egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400280{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500281 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800282 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400283
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800284 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400285 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800286 cairo_device_release(d->device);
287
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500288 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400289 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500290 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500291 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400292}
293
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500294EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500295display_get_image_for_egl_image_surface(struct display *display,
296 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500297{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500298 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500299
300 data = cairo_surface_get_user_data (surface, &surface_data_key);
301
302 return data->image;
303}
304
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500305static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500306display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400307 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500308 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400309{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500310 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400311 EGLDisplay dpy = display->dpy;
312 cairo_surface_t *surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400313 EGLConfig config;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314
315 data = malloc(sizeof *data);
316 if (data == NULL)
317 return NULL;
318
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800319 data->display = display;
320
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400321 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400322 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000324 free(data);
325 return NULL;
326 }
327
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400328 if (flags & SURFACE_OPAQUE)
329 config = display->rgb_config;
330 else
331 config = display->premultiplied_argb_config;
332
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500333 data->image = display->create_image(dpy, NULL,
334 EGL_NATIVE_PIXMAP_KHR,
335 (EGLClientBuffer) data->pixmap,
336 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500337 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500338 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339 free(data);
340 return NULL;
341 }
342
343 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400344 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500345
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800346 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400347 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400348 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500349 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800350 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400351
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352 surface = cairo_gl_surface_create_for_texture(display->device,
353 CAIRO_CONTENT_COLOR_ALPHA,
354 data->texture,
355 rectangle->width,
356 rectangle->height);
357
358 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500359 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400360
361 return surface;
362}
363
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500364static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500365display_create_egl_image_surface_from_file(struct display *display,
366 const char *filename,
367 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400368{
369 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400370 GdkPixbuf *pixbuf;
371 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400372 int stride, i;
373 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500374 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400375
376 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400377 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400378 FALSE, &error);
379 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400380 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400381
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400382 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
383 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500384 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400385 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400386 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400387
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400388
389 stride = gdk_pixbuf_get_rowstride(pixbuf);
390 pixels = gdk_pixbuf_get_pixels(pixbuf);
391
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400393 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400395 while (p < end) {
396 unsigned int t;
397
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400398 MULT(p[0], p[0], p[3], t);
399 MULT(p[1], p[1], p[3], t);
400 MULT(p[2], p[2], p[3], t);
401 p += 4;
402
403 }
404 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400405
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200406 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000407 if (surface == NULL) {
408 g_object_unref(pixbuf);
409 return NULL;
410 }
411
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800412 data = cairo_surface_get_user_data(surface, &surface_data_key);
413
414 cairo_device_acquire(display->device);
415 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800416 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
417 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800418 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400419
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500420 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422 return surface;
423}
424
425#endif
426
427struct wl_buffer *
428display_get_buffer_for_surface(struct display *display,
429 cairo_surface_t *surface)
430{
431 struct surface_data *data;
432
433 data = cairo_surface_get_user_data (surface, &surface_data_key);
434
435 return data->buffer;
436}
437
438struct shm_surface_data {
439 struct surface_data data;
440 void *map;
441 size_t length;
442};
443
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500444static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445shm_surface_data_destroy(void *p)
446{
447 struct shm_surface_data *data = p;
448
449 wl_buffer_destroy(data->data.buffer);
450 munmap(data->map, data->length);
451}
452
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500453static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400455 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400456{
457 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400458 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400459 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800460 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400461 char filename[] = "/tmp/wayland-shm-XXXXXX";
462
463 data = malloc(sizeof *data);
464 if (data == NULL)
465 return NULL;
466
467 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
468 rectangle->width);
469 data->length = stride * rectangle->height;
470 fd = mkstemp(filename);
471 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000472 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400473 return NULL;
474 }
475 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000476 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400477 close(fd);
478 return NULL;
479 }
480
481 data->map = mmap(NULL, data->length,
482 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
483 unlink(filename);
484
485 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000486 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400487 close(fd);
488 return NULL;
489 }
490
491 surface = cairo_image_surface_create_for_data (data->map,
492 CAIRO_FORMAT_ARGB32,
493 rectangle->width,
494 rectangle->height,
495 stride);
496
497 cairo_surface_set_user_data (surface, &surface_data_key,
498 data, shm_surface_data_destroy);
499
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400500 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400501 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400502 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400503 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400504
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505 data->data.buffer = wl_shm_create_buffer(display->shm,
506 fd,
507 rectangle->width,
508 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400509 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400510
511 close(fd);
512
513 return surface;
514}
515
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500516static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517display_create_shm_surface_from_file(struct display *display,
518 const char *filename,
519 struct rectangle *rect)
520{
521 cairo_surface_t *surface;
522 GdkPixbuf *pixbuf;
523 GError *error = NULL;
524 int stride, i;
525 unsigned char *pixels, *p, *end, *dest_data;
526 int dest_stride;
527 uint32_t *d;
528
529 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
530 rect->width, rect->height,
531 FALSE, &error);
532 if (error != NULL)
533 return NULL;
534
535 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
536 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500537 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538 return NULL;
539 }
540
541 stride = gdk_pixbuf_get_rowstride(pixbuf);
542 pixels = gdk_pixbuf_get_pixels(pixbuf);
543
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400544 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000545 if (surface == NULL) {
546 g_object_unref(pixbuf);
547 return NULL;
548 }
549
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550 dest_data = cairo_image_surface_get_data (surface);
551 dest_stride = cairo_image_surface_get_stride (surface);
552
553 for (i = 0; i < rect->height; i++) {
554 d = (uint32_t *) (dest_data + i * dest_stride);
555 p = pixels + i * stride;
556 end = p + rect->width * 4;
557 while (p < end) {
558 unsigned int t;
559 unsigned char a, r, g, b;
560
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561 a = p[3];
562 MULT(r, p[0], a, t);
563 MULT(g, p[1], a, t);
564 MULT(b, p[2], a, t);
565 p += 4;
566 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
567 }
568 }
569
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500570 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400571
572 return surface;
573}
574
nobled7b87cb02011-02-01 18:51:47 +0000575static int
576check_size(struct rectangle *rect)
577{
578 if (rect->width && rect->height)
579 return 0;
580
581 fprintf(stderr, "tried to create surface of "
582 "width: %d, height: %d\n", rect->width, rect->height);
583 return -1;
584}
585
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400586cairo_surface_t *
587display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100588 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400589 struct rectangle *rectangle,
590 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400591{
nobled7b87cb02011-02-01 18:51:47 +0000592 if (check_size(rectangle) < 0)
593 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500594#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500595 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100596 if (surface)
597 return display_create_egl_window_surface(display,
598 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400599 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100600 rectangle);
601 else
602 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400603 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100604 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500605 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400607 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400608}
609
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500610static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400611display_create_surface_from_file(struct display *display,
612 const char *filename,
613 struct rectangle *rectangle)
614{
nobled7b87cb02011-02-01 18:51:47 +0000615 if (check_size(rectangle) < 0)
616 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500617#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500618 if (display->dpy) {
619 return display_create_egl_image_surface_from_file(display,
620 filename,
621 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500622 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400623#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500624 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400625}
Yuval Fledel45568f62010-12-06 09:18:12 -0500626 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400627 const char *filename;
628 int hotspot_x, hotspot_y;
629} pointer_images[] = {
630 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
631 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
632 { DATADIR "/wayland/bottom_side.png", 16, 20 },
633 { DATADIR "/wayland/grabbing.png", 20, 17 },
634 { DATADIR "/wayland/left_ptr.png", 10, 5 },
635 { DATADIR "/wayland/left_side.png", 10, 20 },
636 { DATADIR "/wayland/right_side.png", 30, 19 },
637 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
638 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
639 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400640 { DATADIR "/wayland/xterm.png", 15, 15 },
641 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400642};
643
644static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400645create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400646{
647 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400648 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400649 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400650
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400651 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400652 display->pointer_surfaces =
653 malloc(count * sizeof *display->pointer_surfaces);
654 rect.width = width;
655 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400656 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400657 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658 display_create_surface_from_file(display,
659 pointer_images[i].filename,
660 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400661 }
662
663}
664
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400665cairo_surface_t *
666display_get_pointer_surface(struct display *display, int pointer,
667 int *width, int *height,
668 int *hotspot_x, int *hotspot_y)
669{
670 cairo_surface_t *surface;
671
672 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500673#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400674 *width = cairo_gl_surface_get_width(surface);
675 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000676#else
677 *width = cairo_image_surface_get_width(surface);
678 *height = cairo_image_surface_get_height(surface);
679#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400680 *hotspot_x = pointer_images[pointer].hotspot_x;
681 *hotspot_y = pointer_images[pointer].hotspot_y;
682
683 return cairo_surface_reference(surface);
684}
685
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400686
687static void
688window_attach_surface(struct window *window);
689
690static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400691free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400692{
693 struct window *window = data;
694
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400695 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400696 cairo_surface_destroy(window->pending_surface);
697 window->pending_surface = NULL;
698 if (window->cairo_surface)
699 window_attach_surface(window);
700}
701
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400702static const struct wl_callback_listener free_surface_listener = {
703 free_surface
704};
705
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500706static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200707window_get_resize_dx_dy(struct window *window, int *x, int *y)
708{
709 if (window->resize_edges & WINDOW_RESIZING_LEFT)
710 *x = window->server_allocation.width - window->allocation.width;
711 else
712 *x = 0;
713
714 if (window->resize_edges & WINDOW_RESIZING_TOP)
715 *y = window->server_allocation.height -
716 window->allocation.height;
717 else
718 *y = 0;
719
720 window->resize_edges = 0;
721}
722
723static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400724window_set_type(struct window *window)
725{
726 struct display *display = window->display;
727
728 switch (window->type) {
729 case TYPE_FULLSCREEN:
730 wl_shell_set_fullscreen(display->shell, window->surface);
731 break;
732 case TYPE_TOPLEVEL:
733 wl_shell_set_toplevel(display->shell, window->surface);
734 break;
735 case TYPE_TRANSIENT:
736 wl_shell_set_transient(display->shell, window->surface,
737 window->parent->surface,
738 window->x, window->y, 0);
739 break;
740 case TYPE_CUSTOM:
741 break;
742 }
743}
744
745static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500746window_attach_surface(struct window *window)
747{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400748 struct display *display = window->display;
749 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400750 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000751#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100752 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000753#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500754 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100755
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400756 if (display->shell)
757 window_set_type(window);
758
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100759 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000760#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100761 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
762 data = cairo_surface_get_user_data(window->cairo_surface,
763 &surface_data_key);
764
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100765 cairo_gl_surface_swapbuffers(window->cairo_surface);
766 wl_egl_window_get_attached_size(data->window,
767 &window->server_allocation.width,
768 &window->server_allocation.height);
769 break;
770 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000771#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100772 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200773 window_get_resize_dx_dy(window, &x, &y);
774
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100775 if (window->pending_surface != NULL)
776 return;
777
778 window->pending_surface = window->cairo_surface;
779 window->cairo_surface = NULL;
780
781 buffer =
782 display_get_buffer_for_surface(display,
783 window->pending_surface);
784
785 wl_surface_attach(window->surface, buffer, x, y);
786 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400787 cb = wl_display_sync(display->display);
788 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000790 default:
791 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500793
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500794 wl_surface_damage(window->surface, 0, 0,
795 window->allocation.width,
796 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500797}
798
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500799void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400800window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500801{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100802 if (window->cairo_surface) {
803 switch (window->buffer_type) {
804 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
805 case WINDOW_BUFFER_TYPE_SHM:
806 display_surface_damage(window->display,
807 window->cairo_surface,
808 0, 0,
809 window->allocation.width,
810 window->allocation.height);
811 break;
812 default:
813 break;
814 }
815 window_attach_surface(window);
816 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500817}
818
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400819void
820window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500822 cairo_surface_reference(surface);
823
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400824 if (window->cairo_surface != NULL)
825 cairo_surface_destroy(window->cairo_surface);
826
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500827 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400828}
829
Benjamin Franzke22d54812011-07-16 19:50:32 +0000830#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831static void
832window_resize_cairo_window_surface(struct window *window)
833{
834 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200835 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100836
837 data = cairo_surface_get_user_data(window->cairo_surface,
838 &surface_data_key);
839
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200840 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100841 wl_egl_window_resize(data->window,
842 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200843 window->allocation.height,
844 x,y);
845
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 cairo_gl_surface_set_size(window->cairo_surface,
847 window->allocation.width,
848 window->allocation.height);
849}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000850#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400852void
853window_create_surface(struct window *window)
854{
855 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400856 uint32_t flags = 0;
857
858 if (!window->transparent)
859 flags = SURFACE_OPAQUE;
860
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400861 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500862#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100863 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
864 if (window->cairo_surface) {
865 window_resize_cairo_window_surface(window);
866 return;
867 }
868 surface = display_create_surface(window->display,
869 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400870 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100871 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500872 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400873 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100874 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400875 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400876 break;
877#endif
878 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400879 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400880 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400881 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800882 default:
883 surface = NULL;
884 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400885 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400886
887 window_set_surface(window, surface);
888 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889}
890
891static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500892window_draw_menu(struct window *window)
893{
894 cairo_t *cr;
895 int width, height, r = 5;
896
897 window_create_surface(window);
898
899 cr = cairo_create(window->cairo_surface);
900 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
901 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
902 cairo_paint(cr);
903
904 width = window->allocation.width;
905 height = window->allocation.height;
906 rounded_rect(cr, r, r, width - r, height - r, r);
907 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
908 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
909 cairo_fill(cr);
910 cairo_destroy(cr);
911}
912
913static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500914window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500915{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500916 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500917 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400918 cairo_surface_t *frame;
919 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500920
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400921 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400922
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400923 width = window->allocation.width;
924 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500925
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500926 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500927
Kristian Høgsberg09531622010-06-14 23:22:15 -0400928 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400929 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400930 cairo_paint(cr);
931
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400932 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400933 tile_mask(cr, window->display->shadow,
934 shadow_dx, shadow_dy, width, height,
935 window->margin + 10 - shadow_dx,
936 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500937
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400938 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400939 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400940 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400941 frame = window->display->inactive_frame;
942
943 tile_source(cr, frame, 0, 0, width, height,
944 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500945
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500946 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
947 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500948 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400949 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400950 if (window->keyboard_device)
951 cairo_set_source_rgb(cr, 0, 0, 0);
952 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400953 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400954 cairo_show_text(cr, window->title);
955
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500956 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400957
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100958 /* FIXME: this breakes gears, fix cairo? */
959#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400960 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100961#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500962}
963
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400964void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500965window_destroy(struct window *window)
966{
967 wl_surface_destroy(window->surface);
968 wl_list_remove(&window->link);
969 free(window);
970}
971
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400972static struct item *
973window_find_item(struct window *window, int32_t x, int32_t y)
974{
975 struct item *item;
976
977 wl_list_for_each(item, &window->item_list, link) {
978 if (item->allocation.x <= x &&
979 x < item->allocation.x + item->allocation.width &&
980 item->allocation.y <= y &&
981 y < item->allocation.y + item->allocation.height) {
982 return item;
983 }
984 }
985
986 return NULL;
987}
988
989struct item *
990window_add_item(struct window *window, void *data)
991{
992 struct item *item;
993
994 item = malloc(sizeof *item);
995 memset(item, 0, sizeof *item);
996 item->user_data = data;
997 wl_list_insert(window->item_list.prev, &item->link);
998
999 return item;
1000}
1001
1002void
1003window_for_each_item(struct window *window, item_func_t func, void *data)
1004{
1005 struct item *item;
1006
1007 wl_list_for_each(item, &window->item_list, link)
1008 func(item, data);
1009}
1010
1011struct item *
1012window_get_focus_item(struct window *window)
1013{
1014 return window->focus_item;
1015}
1016
1017void
1018item_get_allocation(struct item *item, struct rectangle *allocation)
1019{
1020 *allocation = item->allocation;
1021}
1022
1023void
1024item_set_allocation(struct item *item,
1025 int32_t x, int32_t y, int32_t width, int32_t height)
1026{
1027 item->allocation.x = x;
1028 item->allocation.y = y;
1029 item->allocation.width = width;
1030 item->allocation.height = height;
1031}
1032
1033void *
1034item_get_user_data(struct item *item)
1035{
1036 return item->user_data;
1037}
1038
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001039void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001040display_flush_cairo_device(struct display *display)
1041{
1042 cairo_device_flush (display->device);
1043}
1044
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001045void
1046window_draw(struct window *window)
1047{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001048 if (window->parent)
1049 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001050 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001051 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001052 else
1053 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001054}
1055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001056cairo_surface_t *
1057window_get_surface(struct window *window)
1058{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001059 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001060}
1061
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001062struct wl_surface *
1063window_get_wl_surface(struct window *window)
1064{
1065 return window->surface;
1066}
1067
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001068static int
1069get_pointer_location(struct window *window, int32_t x, int32_t y)
1070{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001071 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001072 const int grip_size = 8;
1073
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001074 if (!window->decoration)
1075 return WINDOW_CLIENT_AREA;
1076
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001077 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001078 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001079 else if (window->margin <= x && x < window->margin + grip_size)
1080 hlocation = WINDOW_RESIZING_LEFT;
1081 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001082 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001083 else if (x < window->allocation.width - window->margin)
1084 hlocation = WINDOW_RESIZING_RIGHT;
1085 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001086 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001087
1088 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001089 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001090 else if (window->margin <= y && y < window->margin + grip_size)
1091 vlocation = WINDOW_RESIZING_TOP;
1092 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001093 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001094 else if (y < window->allocation.height - window->margin)
1095 vlocation = WINDOW_RESIZING_BOTTOM;
1096 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001097 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001098
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001099 location = vlocation | hlocation;
1100 if (location & WINDOW_EXTERIOR)
1101 location = WINDOW_EXTERIOR;
1102 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1103 location = WINDOW_TITLEBAR;
1104 else if (location == WINDOW_INTERIOR)
1105 location = WINDOW_CLIENT_AREA;
1106
1107 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001108}
1109
1110static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001111set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001112{
1113 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001114 struct wl_buffer *buffer;
1115 cairo_surface_t *surface;
1116 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001117
1118 location = get_pointer_location(input->pointer_focus,
1119 input->sx, input->sy);
1120 switch (location) {
1121 case WINDOW_RESIZING_TOP:
1122 pointer = POINTER_TOP;
1123 break;
1124 case WINDOW_RESIZING_BOTTOM:
1125 pointer = POINTER_BOTTOM;
1126 break;
1127 case WINDOW_RESIZING_LEFT:
1128 pointer = POINTER_LEFT;
1129 break;
1130 case WINDOW_RESIZING_RIGHT:
1131 pointer = POINTER_RIGHT;
1132 break;
1133 case WINDOW_RESIZING_TOP_LEFT:
1134 pointer = POINTER_TOP_LEFT;
1135 break;
1136 case WINDOW_RESIZING_TOP_RIGHT:
1137 pointer = POINTER_TOP_RIGHT;
1138 break;
1139 case WINDOW_RESIZING_BOTTOM_LEFT:
1140 pointer = POINTER_BOTTOM_LEFT;
1141 break;
1142 case WINDOW_RESIZING_BOTTOM_RIGHT:
1143 pointer = POINTER_BOTTOM_RIGHT;
1144 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001145 case WINDOW_EXTERIOR:
1146 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001147 if (input->current_pointer_image == POINTER_DEFAULT)
1148 return;
1149
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001150 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001151 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001152 return;
1153 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001154 break;
1155 }
1156
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001157 if (pointer == input->current_pointer_image)
1158 return;
1159
1160 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001161 surface = display->pointer_surfaces[pointer];
1162 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001163 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001164 pointer_images[pointer].hotspot_x,
1165 pointer_images[pointer].hotspot_y);
1166}
1167
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001168static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001169window_set_focus_item(struct window *window, struct item *focus)
1170{
1171 void *data;
1172
1173 if (focus == window->focus_item)
1174 return;
1175
1176 window->focus_item = focus;
1177 data = focus ? focus->user_data : NULL;
1178 if (window->item_focus_handler)
1179 window->item_focus_handler(window, focus, data);
1180}
1181
1182static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001183window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001184 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001185 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001186{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001187 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001188 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001189 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001190 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001191
1192 input->x = x;
1193 input->y = y;
1194 input->sx = sx;
1195 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001196
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001197 if (!window->focus_item || !window->item_grab_button) {
1198 item = window_find_item(window, sx, sy);
1199 window_set_focus_item(window, item);
1200 }
1201
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001202 if (window->motion_handler)
1203 pointer = (*window->motion_handler)(window, input, time,
1204 x, y, sx, sy,
1205 window->user_data);
1206
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001207 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001208}
1209
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001210static void
1211window_handle_button(void *data,
1212 struct wl_input_device *input_device,
1213 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001214{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001215 struct input *input = data;
1216 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001217 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001218 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001219
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001220 if (window->focus_item && window->item_grab_button == 0 && state)
1221 window->item_grab_button = button;
1222
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001223 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001224
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001225 if (window->display->shell &&
1226 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001227 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001228 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001229 wl_shell_move(window->display->shell,
1230 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001231 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001232 case WINDOW_RESIZING_TOP:
1233 case WINDOW_RESIZING_BOTTOM:
1234 case WINDOW_RESIZING_LEFT:
1235 case WINDOW_RESIZING_RIGHT:
1236 case WINDOW_RESIZING_TOP_LEFT:
1237 case WINDOW_RESIZING_TOP_RIGHT:
1238 case WINDOW_RESIZING_BOTTOM_LEFT:
1239 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001240 wl_shell_resize(window->display->shell,
1241 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001242 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001243 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001244 case WINDOW_CLIENT_AREA:
1245 if (window->button_handler)
1246 (*window->button_handler)(window,
1247 input, time,
1248 button, state,
1249 window->user_data);
1250 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001251 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001252 } else {
1253 if (window->button_handler)
1254 (*window->button_handler)(window,
1255 input, time,
1256 button, state,
1257 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001258 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001259
1260 if (window->focus_item &&
1261 window->item_grab_button == button && !state) {
1262 window->item_grab_button = 0;
1263 item = window_find_item(window, input->sx, input->sy);
1264 window_set_focus_item(window, item);
1265 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001266}
1267
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001268static void
1269window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001270 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001271{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001272 struct input *input = data;
1273 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001274 struct display *d = window->display;
1275 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001276
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001277 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001278 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001279 return;
1280
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001281 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001282 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001283 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1284 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001285
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001286 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1287
1288 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001289 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001290 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001291 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001292
1293 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001294 (*window->key_handler)(window, input, time, key, sym, state,
1295 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001296}
1297
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001298static void
1299window_handle_pointer_focus(void *data,
1300 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001301 uint32_t time, struct wl_surface *surface,
1302 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001303{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001304 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001305 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001306 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001307 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001308
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001309 window = input->pointer_focus;
1310 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001311 window_set_focus_item(window, NULL);
1312
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001313 if (window->leave_handler)
1314 window->leave_handler(window, input,
1315 time, window->user_data);
1316 input->pointer_focus = NULL;
1317 input->current_pointer_image = POINTER_UNSET;
1318 }
1319
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001320 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001321 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001322 window = input->pointer_focus;
1323
Kristian Høgsberg59826582011-01-20 11:56:57 -05001324 input->x = x;
1325 input->y = y;
1326 input->sx = sx;
1327 input->sy = sy;
1328
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001329 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001330 if (window->enter_handler)
1331 pointer = window->enter_handler(window, input,
1332 time, sx, sy,
1333 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001334
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001335 item = window_find_item(window, x, y);
1336 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001337
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001338 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001339 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001340}
1341
1342static void
1343window_handle_keyboard_focus(void *data,
1344 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001345 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001346 struct wl_surface *surface,
1347 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001348{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001349 struct input *input = data;
1350 struct window *window = input->keyboard_focus;
1351 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001352 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001353
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001354 window = input->keyboard_focus;
1355 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001356 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001357 if (window->keyboard_focus_handler)
1358 (*window->keyboard_focus_handler)(window, NULL,
1359 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001360 }
1361
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001362 if (surface)
1363 input->keyboard_focus = wl_surface_get_user_data(surface);
1364 else
1365 input->keyboard_focus = NULL;
1366
1367 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001368 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001369 for (k = keys->data; k < end; k++)
1370 input->modifiers |= d->xkb->map->modmap[*k];
1371
1372 window = input->keyboard_focus;
1373 if (window) {
1374 window->keyboard_device = input;
1375 if (window->keyboard_focus_handler)
1376 (*window->keyboard_focus_handler)(window,
1377 window->keyboard_device,
1378 window->user_data);
1379 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001380}
1381
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001382static const struct wl_input_device_listener input_device_listener = {
1383 window_handle_motion,
1384 window_handle_button,
1385 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001386 window_handle_pointer_focus,
1387 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001388};
1389
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001390void
1391input_get_position(struct input *input, int32_t *x, int32_t *y)
1392{
1393 *x = input->sx;
1394 *y = input->sy;
1395}
1396
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001397struct wl_input_device *
1398input_get_input_device(struct input *input)
1399{
1400 return input->input_device;
1401}
1402
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001403uint32_t
1404input_get_modifiers(struct input *input)
1405{
1406 return input->modifiers;
1407}
1408
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001409struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001410window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001411{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001412 cairo_device_flush (window->display->device);
1413
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001414 return wl_shell_create_drag(window->display->shell);
1415}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001416
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001417void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001418window_move(struct window *window, struct input *input, uint32_t time)
1419{
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001420 if (window->display->shell)
1421 wl_shell_move(window->display->shell,
1422 window->surface, input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001423}
1424
1425void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001426window_activate_drag(struct wl_drag *drag, struct window *window,
1427 struct input *input, uint32_t time)
1428{
1429 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001430}
1431
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001432static void
1433handle_configure(void *data, struct wl_shell *shell,
1434 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001435 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001436{
1437 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001438 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001439
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001440 /* FIXME: this is probably the wrong place to check for width
1441 * or height <= 0, but it prevents the compositor from crashing
1442 */
1443 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001444 return;
1445
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001446 window->resize_edges = edges;
1447
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001448 if (window->resize_handler) {
1449 child_width = width - 20 - window->margin * 2;
1450 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001451
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001452 (*window->resize_handler)(window,
1453 child_width, child_height,
1454 window->user_data);
1455 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001456 window->allocation.width = width;
1457 window->allocation.height = height;
1458
1459 if (window->redraw_handler)
1460 window_schedule_redraw(window);
1461 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001462}
1463
1464static const struct wl_shell_listener shell_listener = {
1465 handle_configure,
1466};
1467
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001468void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001469window_get_allocation(struct window *window,
1470 struct rectangle *allocation)
1471{
1472 *allocation = window->allocation;
1473}
1474
1475void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001476window_get_child_allocation(struct window *window,
1477 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001478{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001479 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001480 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001481 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001482 allocation->x = window->margin + 10;
1483 allocation->y = window->margin + 50;
1484 allocation->width =
1485 window->allocation.width - 20 - window->margin * 2;
1486 allocation->height =
1487 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001488 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001489}
1490
1491void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001492window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001493{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001494 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001495 window->allocation.x = 20 + window->margin;
1496 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001497 window->allocation.width = width + 20 + window->margin * 2;
1498 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001499 } else {
1500 window->allocation.x = 0;
1501 window->allocation.y = 0;
1502 window->allocation.width = width;
1503 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001504 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001505}
1506
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001507static void
1508idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001509{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001510 struct window *window =
1511 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001512
1513 window->redraw_handler(window, window->user_data);
1514 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001515}
1516
1517void
1518window_schedule_redraw(struct window *window)
1519{
1520 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001521 window->redraw_task.run = idle_redraw;
1522 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001523 window->redraw_scheduled = 1;
1524 }
1525}
1526
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001527void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001528window_set_custom(struct window *window)
1529{
1530 window->type = TYPE_CUSTOM;
1531}
1532
1533void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001534window_set_fullscreen(struct window *window, int fullscreen)
1535{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001536 int32_t width, height;
1537
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001538 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001539 return;
1540
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001541 if (fullscreen) {
1542 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001543 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001544 width = window->display->screen_allocation.width;
1545 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001546 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001547 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001548 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001549 width = window->saved_allocation.width - 20 - window->margin * 2;
1550 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001551 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001552 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001553
1554 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001555}
1556
1557void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001558window_set_decoration(struct window *window, int decoration)
1559{
1560 window->decoration = decoration;
1561}
1562
1563void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001564window_set_user_data(struct window *window, void *data)
1565{
1566 window->user_data = data;
1567}
1568
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001569void *
1570window_get_user_data(struct window *window)
1571{
1572 return window->user_data;
1573}
1574
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001575void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001576window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001577 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001578{
1579 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001580}
1581
1582void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001583window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001584 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001585{
1586 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001587}
1588
1589void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001590window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001591 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001592{
1593 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001594}
1595
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001596void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001597window_set_button_handler(struct window *window,
1598 window_button_handler_t handler)
1599{
1600 window->button_handler = handler;
1601}
1602
1603void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001604window_set_motion_handler(struct window *window,
1605 window_motion_handler_t handler)
1606{
1607 window->motion_handler = handler;
1608}
1609
1610void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001611window_set_enter_handler(struct window *window,
1612 window_enter_handler_t handler)
1613{
1614 window->enter_handler = handler;
1615}
1616
1617void
1618window_set_leave_handler(struct window *window,
1619 window_leave_handler_t handler)
1620{
1621 window->leave_handler = handler;
1622}
1623
1624void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001625window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001626 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001627{
1628 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001629}
1630
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001631void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001632window_set_item_focus_handler(struct window *window,
1633 window_item_focus_handler_t handler)
1634{
1635 window->item_focus_handler = handler;
1636}
1637
1638void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001639window_set_transparent(struct window *window, int transparent)
1640{
1641 window->transparent = transparent;
1642}
1643
1644void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001645window_set_title(struct window *window, const char *title)
1646{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001647 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001648 window->title = strdup(title);
1649}
1650
1651const char *
1652window_get_title(struct window *window)
1653{
1654 return window->title;
1655}
1656
1657void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001658display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1659 int32_t x, int32_t y, int32_t width, int32_t height)
1660{
1661 struct wl_buffer *buffer;
1662
1663 buffer = display_get_buffer_for_surface(display, cairo_surface);
1664
1665 wl_buffer_damage(buffer, x, y, width, height);
1666}
1667
1668void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001669window_damage(struct window *window, int32_t x, int32_t y,
1670 int32_t width, int32_t height)
1671{
1672 wl_surface_damage(window->surface, x, y, width, height);
1673}
1674
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001675static struct window *
1676window_create_internal(struct display *display, struct window *parent,
1677 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001678{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001679 struct window *window;
1680
1681 window = malloc(sizeof *window);
1682 if (window == NULL)
1683 return NULL;
1684
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001685 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001686 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001687 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001688 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001689 window->allocation.x = 0;
1690 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001691 window->allocation.width = width;
1692 window->allocation.height = height;
1693 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001694 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001695 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001696 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001697 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001698
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001699 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001700#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001701 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1702 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001703#else
1704 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1705#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001706 else
1707 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001708
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001709 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001710 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001711
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001712 return window;
1713}
1714
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001715struct window *
1716window_create(struct display *display, int32_t width, int32_t height)
1717{
1718 struct window *window;
1719
1720 window = window_create_internal(display, NULL, width, height);
1721 if (!window)
1722 return NULL;
1723
1724 return window;
1725}
1726
1727struct window *
1728window_create_transient(struct display *display, struct window *parent,
1729 int32_t x, int32_t y, int32_t width, int32_t height)
1730{
1731 struct window *window;
1732
1733 window = window_create_internal(parent->display,
1734 parent, width, height);
1735 if (!window)
1736 return NULL;
1737
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001738 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001739 window->x = x;
1740 window->y = y;
1741
1742 return window;
1743}
1744
1745void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001746window_set_buffer_type(struct window *window, enum window_buffer_type type)
1747{
1748 window->buffer_type = type;
1749}
1750
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001751
1752static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001753display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001754 struct wl_output *wl_output,
1755 int x, int y,
1756 int physical_width,
1757 int physical_height,
1758 int subpixel,
1759 const char *make,
1760 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001761{
1762 struct display *display = data;
1763
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001764 display->screen_allocation.x = x;
1765 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001766}
1767
1768static void
1769display_handle_mode(void *data,
1770 struct wl_output *wl_output,
1771 uint32_t flags,
1772 int width,
1773 int height,
1774 int refresh)
1775{
1776 struct display *display = data;
1777
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001778 display->screen_allocation.width = width;
1779 display->screen_allocation.height = height;
1780}
1781
1782static const struct wl_output_listener output_listener = {
1783 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001784 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001785};
1786
1787static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001788display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001789{
1790 struct input *input;
1791
1792 input = malloc(sizeof *input);
1793 if (input == NULL)
1794 return;
1795
1796 memset(input, 0, sizeof *input);
1797 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001798 input->input_device =
1799 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001800 input->pointer_focus = NULL;
1801 input->keyboard_focus = NULL;
1802 wl_list_insert(d->input_list.prev, &input->link);
1803
1804 wl_input_device_add_listener(input->input_device,
1805 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001806 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001807}
1808
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001809struct selection_offer {
1810 struct display *display;
1811 struct wl_selection_offer *offer;
1812 struct wl_array types;
1813 struct input *input;
1814};
1815
1816int
1817input_offers_mime_type(struct input *input, const char *type)
1818{
1819 struct selection_offer *offer = input->offer;
1820 char **p, **end;
1821
1822 if (offer == NULL)
1823 return 0;
1824
1825 end = offer->types.data + offer->types.size;
1826 for (p = offer->types.data; p < end; p++)
1827 if (strcmp(*p, type) == 0)
1828 return 1;
1829
1830 return 0;
1831}
1832
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001833void
1834input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001835{
1836 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001837
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001838 /* FIXME: A number of things can go wrong here: the object may
1839 * not be the current selection offer any more (which could
1840 * still work, but the source may have gone away or just
1841 * destroyed its wl_selection) or the offer may not have the
1842 * requested type after all (programmer/client error,
1843 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001844 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001845}
1846
1847static void
1848selection_offer_offer(void *data,
1849 struct wl_selection_offer *selection_offer,
1850 const char *type)
1851{
1852 struct selection_offer *offer = data;
1853
1854 char **p;
1855
1856 p = wl_array_add(&offer->types, sizeof *p);
1857 if (p)
1858 *p = strdup(type);
1859};
1860
1861static void
1862selection_offer_keyboard_focus(void *data,
1863 struct wl_selection_offer *selection_offer,
1864 struct wl_input_device *input_device)
1865{
1866 struct selection_offer *offer = data;
1867 struct input *input;
1868 char **p, **end;
1869
1870 if (input_device == NULL) {
1871 printf("selection offer retracted %p\n", selection_offer);
1872 input = offer->input;
1873 input->offer = NULL;
1874 wl_selection_offer_destroy(selection_offer);
1875 wl_array_release(&offer->types);
1876 free(offer);
1877 return;
1878 }
1879
1880 input = wl_input_device_get_user_data(input_device);
1881 printf("new selection offer %p:", selection_offer);
1882
1883 offer->input = input;
1884 input->offer = offer;
1885 end = offer->types.data + offer->types.size;
1886 for (p = offer->types.data; p < end; p++)
1887 printf(" %s", *p);
1888
1889 printf("\n");
1890}
1891
1892struct wl_selection_offer_listener selection_offer_listener = {
1893 selection_offer_offer,
1894 selection_offer_keyboard_focus
1895};
1896
1897static void
1898add_selection_offer(struct display *d, uint32_t id)
1899{
1900 struct selection_offer *offer;
1901
1902 offer = malloc(sizeof *offer);
1903 if (offer == NULL)
1904 return;
1905
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001906 offer->offer =
1907 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001908 offer->display = d;
1909 wl_array_init(&offer->types);
1910 offer->input = NULL;
1911
1912 wl_selection_offer_add_listener(offer->offer,
1913 &selection_offer_listener, offer);
1914}
1915
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001916static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001917display_handle_global(struct wl_display *display, uint32_t id,
1918 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001919{
1920 struct display *d = data;
1921
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001922 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001923 d->compositor =
1924 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001925 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001926 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001927 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001928 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001929 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001930 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001931 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001932 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001933 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001934 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001935 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001936 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001937 }
1938}
1939
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001940static void
1941display_render_frame(struct display *d)
1942{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001943 int radius = 8;
1944 cairo_t *cr;
1945
1946 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1947 cr = cairo_create(d->shadow);
1948 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1949 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1950 rounded_rect(cr, 16, 16, 112, 112, radius);
1951 cairo_fill(cr);
1952 cairo_destroy(cr);
1953 blur_surface(d->shadow, 64);
1954
1955 d->active_frame =
1956 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1957 cr = cairo_create(d->active_frame);
1958 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1959 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1960 rounded_rect(cr, 16, 16, 112, 112, radius);
1961 cairo_fill(cr);
1962 cairo_destroy(cr);
1963
1964 d->inactive_frame =
1965 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1966 cr = cairo_create(d->inactive_frame);
1967 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1968 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1969 rounded_rect(cr, 16, 16, 112, 112, radius);
1970 cairo_fill(cr);
1971 cairo_destroy(cr);
1972}
1973
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001974static void
1975init_xkb(struct display *d)
1976{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001977 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001978
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001979 names.rules = "evdev";
1980 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001981 names.layout = option_xkb_layout;
1982 names.variant = option_xkb_variant;
1983 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001984
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001985 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001986 if (!d->xkb) {
1987 fprintf(stderr, "Failed to compile keymap\n");
1988 exit(1);
1989 }
1990}
1991
Yuval Fledel45568f62010-12-06 09:18:12 -05001992static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001993init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001994{
1995 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001996 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001997
1998 static const EGLint premul_argb_cfg_attribs[] = {
1999 EGL_SURFACE_TYPE,
2000 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2001 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002002 EGL_RED_SIZE, 1,
2003 EGL_GREEN_SIZE, 1,
2004 EGL_BLUE_SIZE, 1,
2005 EGL_ALPHA_SIZE, 1,
2006 EGL_DEPTH_SIZE, 1,
2007 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2008 EGL_NONE
2009 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002010
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002011 static const EGLint rgb_cfg_attribs[] = {
2012 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2013 EGL_RED_SIZE, 1,
2014 EGL_GREEN_SIZE, 1,
2015 EGL_BLUE_SIZE, 1,
2016 EGL_ALPHA_SIZE, 0,
2017 EGL_DEPTH_SIZE, 1,
2018 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2019 EGL_NONE
2020 };
2021
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002022 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002023 if (!eglInitialize(d->dpy, &major, &minor)) {
2024 fprintf(stderr, "failed to initialize display\n");
2025 return -1;
2026 }
2027
2028 if (!eglBindAPI(EGL_OPENGL_API)) {
2029 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2030 return -1;
2031 }
2032
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002033 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002034 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002035 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002036 return -1;
2037 }
2038
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002039 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2040 &d->rgb_config, 1, &n) || n != 1) {
2041 fprintf(stderr, "failed to choose rgb config\n");
2042 return -1;
2043 }
2044
2045 d->ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05002046 if (d->ctx == NULL) {
2047 fprintf(stderr, "failed to create context\n");
2048 return -1;
2049 }
2050
2051 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002052 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002053 return -1;
2054 }
2055
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002056#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002057 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04002058 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002059 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002060 return -1;
2061 }
2062#endif
2063
2064 return 0;
2065}
2066
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002067static int
2068event_mask_update(uint32_t mask, void *data)
2069{
2070 struct display *d = data;
2071
2072 d->mask = mask;
2073
2074 return 0;
2075}
2076
2077static void
2078handle_display_data(struct task *task, uint32_t events)
2079{
2080 struct display *display =
2081 container_of(task, struct display, display_task);
2082
2083 wl_display_iterate(display->display, display->mask);
2084}
2085
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002086struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002087display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002088{
2089 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002090 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002091 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002092 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002093
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002094 g_type_init();
2095
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002096 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002097 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002098 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002099
2100 xkb_option_group = g_option_group_new("xkb",
2101 "Keyboard options",
2102 "Show all XKB options",
2103 NULL, NULL);
2104 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2105 g_option_context_add_group (context, xkb_option_group);
2106
2107 if (!g_option_context_parse(context, argc, argv, &error)) {
2108 fprintf(stderr, "option parsing failed: %s\n", error->message);
2109 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002110 }
2111
Tim Wiederhake748f6722011-01-23 23:25:25 +01002112 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002113
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002114 d = malloc(sizeof *d);
2115 if (d == NULL)
2116 return NULL;
2117
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002118 memset(d, 0, sizeof *d);
2119
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002120 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002121 if (d->display == NULL) {
2122 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002123 return NULL;
2124 }
2125
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002126 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2127 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2128 d->display_task.run = handle_display_data;
2129 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2130
2131 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002132 wl_list_init(&d->input_list);
2133
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002134 /* Set up listener so we'll catch all events. */
2135 wl_display_add_global_listener(d->display,
2136 display_handle_global, d);
2137
2138 /* Process connection events. */
2139 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002140 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002141 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002142
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002143 d->image_target_texture_2d =
2144 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2145 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2146 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2147
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002148 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002149
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002150 display_render_frame(d);
2151
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002152 wl_list_init(&d->window_list);
2153
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002154 init_xkb(d);
2155
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002156 return d;
2157}
2158
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002159struct wl_display *
2160display_get_display(struct display *display)
2161{
2162 return display->display;
2163}
2164
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002165struct wl_compositor *
2166display_get_compositor(struct display *display)
2167{
2168 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002169}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002170
2171EGLDisplay
2172display_get_egl_display(struct display *d)
2173{
2174 return d->dpy;
2175}
2176
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002177EGLConfig
2178display_get_egl_config(struct display *d)
2179{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002180 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002181}
2182
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002183struct wl_shell *
2184display_get_shell(struct display *display)
2185{
2186 return display->shell;
2187}
2188
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002189void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002190display_acquire_window_surface(struct display *display,
2191 struct window *window,
2192 EGLContext ctx)
2193{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002194#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002195 struct egl_window_surface_data *data;
2196
2197 if (!window->cairo_surface)
2198 return;
2199
2200 if (!ctx)
2201 ctx = display->ctx;
2202
2203 data = cairo_surface_get_user_data(window->cairo_surface,
2204 &surface_data_key);
2205
2206 cairo_device_acquire(display->device);
2207 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2208 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke22d54812011-07-16 19:50:32 +00002209#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002210}
2211
2212void
2213display_release(struct display *display)
2214{
2215 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2216 fprintf(stderr, "failed to make context current\n");
2217 cairo_device_release(display->device);
2218}
2219
2220void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002221display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002222{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002223 wl_list_insert(&display->deferred_list, &task->link);
2224}
2225
2226void
2227display_watch_fd(struct display *display,
2228 int fd, uint32_t events, struct task *task)
2229{
2230 struct epoll_event ep;
2231
2232 ep.events = events;
2233 ep.data.ptr = task;
2234 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2235}
2236
2237void
2238display_run(struct display *display)
2239{
2240 struct task *task;
2241 struct epoll_event ep[16];
2242 int i, count;
2243
2244 while (1) {
2245 while (display->mask & WL_DISPLAY_WRITABLE)
2246 wl_display_iterate(display->display,
2247 WL_DISPLAY_WRITABLE);
2248
2249 count = epoll_wait(display->epoll_fd,
2250 ep, ARRAY_LENGTH(ep), -1);
2251 for (i = 0; i < count; i++) {
2252 task = ep[i].data.ptr;
2253 task->run(task, ep[i].events);
2254 }
2255
2256 while (!wl_list_empty(&display->deferred_list)) {
2257 task = container_of(display->deferred_list.next,
2258 struct task, link);
2259 wl_list_remove(&task->link);
2260 task->run(task, 0);
2261 }
2262 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002263}